Skip to content

Commit 7538cbb

Browse files
Adds client-request-id guidance. Closes #209 (#223)
1 parent 516e3a0 commit 7538cbb

File tree

5 files changed

+72
-3
lines changed

5 files changed

+72
-3
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using Microsoft.Extensions.Configuration;
5+
using Microsoft.Graph.DeveloperProxy.Abstractions;
6+
using System.Text.RegularExpressions;
7+
using Titanium.Web.Proxy.Http;
8+
9+
namespace Microsoft.Graph.DeveloperProxy.Plugins.Guidance;
10+
11+
public class GraphClientRequestIdGuidancePlugin : BaseProxyPlugin
12+
{
13+
public override string Name => nameof(GraphClientRequestIdGuidancePlugin);
14+
15+
public override void Register(IPluginEvents pluginEvents,
16+
IProxyContext context,
17+
ISet<Regex> urlsToWatch,
18+
IConfigurationSection? configSection = null)
19+
{
20+
base.Register(pluginEvents, context, urlsToWatch, configSection);
21+
22+
pluginEvents.BeforeRequest += BeforeRequest;
23+
}
24+
25+
private void BeforeRequest(object? sender, ProxyRequestArgs e)
26+
{
27+
Request request = e.Session.HttpClient.Request;
28+
if (_urlsToWatch is not null && e.HasRequestUrlMatch(_urlsToWatch) && WarnNoClientRequestId(request))
29+
{
30+
_logger?.LogRequest(BuildAddClientRequestIdMessage(request), MessageType.Warning, new LoggingContext(e.Session));
31+
32+
if (!ProxyUtils.IsSdkRequest(request))
33+
{
34+
_logger?.LogRequest(MessageUtils.BuildUseSdkMessage(request), MessageType.Tip, new LoggingContext(e.Session));
35+
}
36+
}
37+
}
38+
39+
private static bool WarnNoClientRequestId(Request request) =>
40+
ProxyUtils.IsGraphRequest(request) &&
41+
!request.Headers.HeaderExists("client-request-id");
42+
43+
private static string GetClientRequestIdGuidanceUrl() => "https://aka.ms/graph/proxy/guidance/client-request-id";
44+
private static string[] BuildAddClientRequestIdMessage(Request r) => new[] {
45+
$"To help Microsoft investigate errors, to each request to Microsoft Graph",
46+
"add the client-request-id header with a unique GUID.",
47+
$"More info at {GetClientRequestIdGuidanceUrl()}" };
48+
}

msgraph-developer-proxy-plugins/Guidance/GraphSdkGuidancePlugin.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ private void OnAfterResponse(object? sender, ProxyResponseArgs e) {
2727
&& _urlsToWatch is not null
2828
&& e.HasRequestUrlMatch(_urlsToWatch)
2929
&& WarnNoSdk(request)) {
30-
_logger?.LogRequest(MessageUtils.BuildUseSdkMessage(request), MessageType.Tip, new LoggingContext(e.Session));
30+
_logger?.LogRequest(MessageUtils.BuildUseSdkForErrorsMessage(request), MessageType.Tip, new LoggingContext(e.Session));
3131
}
3232
}
3333

msgraph-developer-proxy-plugins/MessageUtils.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@
66
namespace Microsoft.Graph.DeveloperProxy.Plugins;
77

88
internal class MessageUtils {
9-
public static string[] BuildUseSdkMessage(Request r) => new[] { "To handle API errors more easily, use the Graph SDK.", $"More info at {GetMoveToSdkUrl(r)}" };
9+
public static string[] BuildUseSdkForErrorsMessage(Request r) => new[] { "To handle API errors more easily, use the Microsoft Graph SDK.", $"More info at {GetMoveToSdkUrl(r)}" };
10+
11+
public static string[] BuildUseSdkMessage(Request r) => new[] {
12+
"To more easily follow best practices for working with Microsoft Graph, ",
13+
"use the Microsoft Graph SDK.",
14+
$"More info at {GetMoveToSdkUrl(r)}"
15+
};
1016

1117
public static string GetMoveToSdkUrl(Request request) {
1218
// TODO: return language-specific guidance links based on the language detected from the User-Agent

msgraph-developer-proxy-plugins/RandomErrors/GraphRandomErrorPlugin.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ private void UpdateProxyResponse(ProxyRequestArgs ev, HttpStatusCode errorStatus
165165
_logger?.LogRequest(new[] { $"{(int)errorStatus} {errorStatus.ToString()}" }, MessageType.Chaos, new LoggingContext(ev.Session));
166166
session.GenericResponse(body ?? string.Empty, errorStatus, headers);
167167
}
168-
private static string BuildApiErrorMessage(Request r) => $"Some error was generated by the proxy. {(ProxyUtils.IsGraphRequest(r) ? ProxyUtils.IsSdkRequest(r) ? "" : String.Join(' ', MessageUtils.BuildUseSdkMessage(r)) : "")}";
168+
private static string BuildApiErrorMessage(Request r) => $"Some error was generated by the proxy. {(ProxyUtils.IsGraphRequest(r) ? ProxyUtils.IsSdkRequest(r) ? "" : String.Join(' ', MessageUtils.BuildUseSdkForErrorsMessage(r)) : "")}";
169169

170170
private string BuildThrottleKey(Request r) => $"{r.Method}-{r.Url}";
171171

msgraph-developer-proxy/appsettings.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,21 @@
4141
"https://microsoftgraph.chinacloudapi.cn/beta/*"
4242
]
4343
},
44+
{
45+
"name": "GraphClientRequestIdGuidancePlugin",
46+
"enabled": true,
47+
"pluginPath": "GraphProxyPlugins\\msgraph-developer-proxy-plugins.dll",
48+
"urlsToWatch": [
49+
"https://graph.microsoft.com/v1.0/*",
50+
"https://graph.microsoft.com/beta/*",
51+
"https://graph.microsoft.us/v1.0/*",
52+
"https://graph.microsoft.us/beta/*",
53+
"https://dod-graph.microsoft.us/v1.0/*",
54+
"https://dod-graph.microsoft.us/beta/*",
55+
"https://microsoftgraph.chinacloudapi.cn/v1.0/*",
56+
"https://microsoftgraph.chinacloudapi.cn/beta/*"
57+
]
58+
},
4459
{
4560
"name": "MockResponsePlugin",
4661
"enabled": true,

0 commit comments

Comments
 (0)