-
-
Notifications
You must be signed in to change notification settings - Fork 220
/
Copy pathScopeExtensions.cs
195 lines (170 loc) · 7.27 KB
/
ScopeExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Net.Http.Headers;
using Sentry.AspNetCore.Extensions;
using Sentry.Extensibility;
using Sentry.Protocol;
namespace Sentry.AspNetCore
{
/// <summary>
/// Scope Extensions
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static class ScopeExtensions
{
/// <summary>
/// Populates the scope with the HTTP data
/// </summary>
/// <remarks>
/// NOTE: The scope is applied to the event BEFORE running the event processors/exception processors.
/// The main Sentry SDK has processors which run right before any additional processors to the Event
/// </remarks>
public static void Populate(this Scope scope, HttpContext context, SentryAspNetCoreOptions options)
{
// Not to throw on code that ignores nullability warnings.
// ReSharper disable ConditionIsAlwaysTrueOrFalse
if (scope is null || context is null || options is null)
{
return;
}
// ReSharper restore ConditionIsAlwaysTrueOrFalse
// With the logger integration, a BeginScope call is made with RequestId. That ends up adding
// two tags with the same value: RequestId and TraceIdentifier
if (!scope.Tags.TryGetValue("RequestId", out var requestId) || requestId != context.TraceIdentifier)
{
scope.SetTag(nameof(context.TraceIdentifier), context.TraceIdentifier);
}
if (options.SendDefaultPii && !scope.HasUser())
{
var userFactory = context.RequestServices.GetService<IUserFactory>();
var user = userFactory?.Create(context);
if (user != null)
{
scope.User = user;
}
}
try
{
SetBody(scope, context, options);
}
catch (Exception e)
{
options.DiagnosticLogger?.LogError("Failed to extract body.", e);
}
SetEnv(scope, context, options);
// Extract the route data
try
{
var routeData = context.GetRouteData();
var controller = routeData.Values["controller"]?.ToString();
var action = routeData.Values["action"]?.ToString();
var area = routeData.Values["area"]?.ToString();
if (controller != null)
{
scope.SetTag("route.controller", controller);
}
if (action != null)
{
scope.SetTag("route.action", action);
}
if (area != null)
{
scope.SetTag("route.area", area);
}
scope.TransactionName = context.GetTransactionName();
}
catch(Exception e)
{
// Suppress the error here; we expect an ArgumentNullException if httpContext.Request.RouteValues is null from GetRouteData()
// TODO: Consider adding a bool to the Sentry options to make route data extraction optional in case they don't use a routing middleware?
options.DiagnosticLogger?.LogDebug("Failed to extract route data.", e);
}
// TODO: Get context stuff into scope
//context.Session
//context.Response
//context.Items
}
private static void SetEnv(Scope scope, HttpContext context, SentryAspNetCoreOptions options)
{
scope.Request.Method = context.Request.Method;
// Logging integration, if enabled, sets the following tag which ends up as duplicate
// to Request.Url. Prefer the interface value and remove tag.
var host = context.Request.Host.Host;
if (context.Request.Host.Port != null)
{
host += $":{context.Request.Host.Port}";
}
scope.Request.Url = $"{context.Request.Scheme}://{host}{context.Request.Path}";
scope.UnsetTag("RequestPath");
scope.Request.QueryString = context.Request.QueryString.ToString();
foreach (var requestHeader in context.Request.Headers)
{
if (!options.SendDefaultPii
// Don't add headers which might contain PII
&& (requestHeader.Key == HeaderNames.Cookie
|| requestHeader.Key == HeaderNames.Authorization))
{
continue;
}
scope.Request.Headers[requestHeader.Key] = requestHeader.Value;
}
// TODO: Hide these 'Env' behind some extension method as
// these might be reported in a non CGI, old-school way
if (options.SendDefaultPii
&& context.Connection.RemoteIpAddress?.ToString() is { } ipAddress)
{
scope.Request.Env["REMOTE_ADDR"] = ipAddress;
}
scope.Request.Env["SERVER_NAME"] = Environment.MachineName;
scope.Request.Env["SERVER_PORT"] = context.Connection.LocalPort.ToString();
if (context.Response.Headers.TryGetValue("Server", out var server))
{
scope.Request.Env["SERVER_SOFTWARE"] = server;
}
}
private static void SetBody(Scope scope, HttpContext context, SentryAspNetCoreOptions options)
{
var extractors = context.RequestServices.GetService<IEnumerable<IRequestPayloadExtractor>>();
if (extractors == null)
{
return;
}
var dispatcher = new RequestBodyExtractionDispatcher(extractors, options, () => options.MaxRequestBodySize);
var body = dispatcher.ExtractPayload(new HttpRequestAdapter(context.Request));
if (body != null)
{
scope.Request.Data = body;
}
}
/// <summary>
/// Populates the scope with the System.Diagnostics.Activity
/// </summary>
/// <param name="scope">The scope.</param>
/// <param name="activity">The activity.</param>
public static void Populate(this Scope scope, Activity activity)
{
// Not to throw on code that ignores nullability warnings.
// ReSharper disable ConditionIsAlwaysTrueOrFalse
if (scope is null || activity is null)
{
return;
}
// ReSharper restore ConditionIsAlwaysTrueOrFalse
//scope.ActivityId = activity.Id;
// TODO: enumerating Activity.Tags clears the collection and sets field to null?
scope.SetTags(activity.Tags
.Where(kv => !string.IsNullOrEmpty(kv.Value))
.Select(k => new KeyValuePair<string, string>(k.Key, k.Value!)));
}
internal static void SetWebRoot(this Scope scope, string webRoot)
{
scope.Request.Env["DOCUMENT_ROOT"] = webRoot;
}
}
}