-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathAspNetCoreHttpRequestHandler.cs
More file actions
495 lines (429 loc) · 23.3 KB
/
Copy pathAspNetCoreHttpRequestHandler.cs
File metadata and controls
495 lines (429 loc) · 23.3 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
// <copyright file="AspNetCoreHttpRequestHandler.cs" company="Datadog">
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
// </copyright>
#if !NETFRAMEWORK
using System;
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
using Datadog.Trace.Activity;
using Datadog.Trace.Activity.DuckTypes;
using Datadog.Trace.Activity.Helpers;
using Datadog.Trace.AppSec;
using Datadog.Trace.AppSec.Coordinator;
using Datadog.Trace.ClrProfiler.AutoInstrumentation.Proxy;
using Datadog.Trace.Configuration;
using Datadog.Trace.DataStreamsMonitoring;
using Datadog.Trace.DataStreamsMonitoring.TransactionTracking;
using Datadog.Trace.DiagnosticListeners;
using Datadog.Trace.DuckTyping;
using Datadog.Trace.Headers;
using Datadog.Trace.Logging;
using Datadog.Trace.Propagators;
using Datadog.Trace.Serverless;
using Datadog.Trace.Tagging;
using Datadog.Trace.Util;
using Datadog.Trace.Util.Http;
using Datadog.Trace.Vendors.Serilog.Events;
using Microsoft.AspNetCore.Http;
namespace Datadog.Trace.PlatformHelpers
{
internal sealed class AspNetCoreHttpRequestHandler
{
internal const string HttpContextTrackingKey = "__Datadog.AspNetCoreHttpRequestHandler.Tracking";
internal const string HttpContextActiveScopeKey = "__Datadog.AspNetCoreHttpRequestHandler.ActiveScope";
private readonly IDatadogLogger _log;
private readonly IntegrationId _integrationId;
private readonly string _requestInOperationName;
public AspNetCoreHttpRequestHandler(
IDatadogLogger log,
string requestInOperationName,
IntegrationId integrationInfo)
{
_log = log;
_integrationId = integrationInfo;
_requestInOperationName = requestInOperationName;
}
public string GetDefaultResourceName(HttpRequest request)
{
string httpMethod = request.Method?.ToUpperInvariant() ?? "UNKNOWN";
string absolutePath = request.PathBase.HasValue
? request.PathBase.ToUriComponent() + request.Path.ToUriComponent()
: request.Path.ToUriComponent();
string resourceUrl = UriHelpers.GetCleanUriPath(absolutePath)
.ToLowerInvariant();
return $"{httpMethod} {resourceUrl}";
}
private PropagationContext ExtractPropagatedContext(Tracer tracer, HttpRequest request)
{
try
{
// extract propagation details from http headers
if (request.Headers is { } headers)
{
return tracer.TracerManager.SpanContextPropagator.Extract(new HeadersCollectionAdapter(headers));
}
}
catch (Exception ex)
{
_log.Error(ex, "Error extracting propagated HTTP headers.");
}
return default;
}
private void AddHeaderTagsToSpan(ISpan span, HttpRequest request, Tracer tracer, ReadOnlyDictionary<string, string> headerTagsInternal)
{
try
{
// extract propagation details from http headers
if (request.Headers is { } requestHeaders)
{
tracer.TracerManager.SpanContextPropagator.AddHeadersToSpanAsTags(
span,
new HeadersCollectionAdapter(requestHeaders),
headerTagsInternal,
defaultTagPrefix: SpanContextPropagator.HttpRequestHeadersTagPrefix);
}
}
catch (Exception ex)
{
_log.Error(ex, "Error extracting propagated HTTP headers.");
}
}
public Scope StartAspNetCorePipelineScope(Tracer tracer, Security security, Iast.Iast iast, HttpContext httpContext, string resourceName)
{
var routeTemplateResourceNames = tracer.Settings.RouteTemplateResourceNamesEnabled;
var tags = routeTemplateResourceNames ? new AspNetCoreEndpointTags() : new AspNetCoreTags();
return StartAspNetCorePipelineScope(tracer, security, iast, httpContext, resourceName, tags, useSingleSpanRequestTracking: false);
}
#if NET6_0_OR_GREATER
public Scope StartAspNetCoreSingleSpanPipelineScope(Tracer tracer, Security security, Iast.Iast iast, HttpContext httpContext, string resourceName)
=> StartAspNetCorePipelineScope(tracer, security, iast, httpContext, resourceName, new AspNetCoreSingleSpanTags(), useSingleSpanRequestTracking: true);
#endif
private Scope StartAspNetCorePipelineScope(Tracer tracer, Security security, Iast.Iast iast, HttpContext httpContext, string resourceName, WebTags tags, bool useSingleSpanRequestTracking)
{
var request = httpContext.Request;
string host = request.Host.Value;
string httpMethod = request.Method?.ToUpperInvariant() ?? "UNKNOWN";
string url = request.GetUrlForSpan(tracer.TracerManager.QueryStringManager);
var userAgent = request.Headers[HttpHeaderNames.UserAgent];
resourceName ??= GetDefaultResourceName(request);
var extractedContext = ExtractPropagatedContext(tracer, request).MergeBaggageInto(Baggage.Current);
InferredProxyScopePropagationContext? proxyContext = null;
if (tracer.Settings.InferredProxySpansEnabled && request.Headers is { } headers)
{
proxyContext = InferredProxySpanHelper.ExtractAndCreateInferredProxyScope(tracer, new HeadersCollectionAdapter(headers), extractedContext);
if (proxyContext != null)
{
extractedContext = proxyContext.Value.Context;
}
}
var scope = tracer.StartActiveInternal(_requestInOperationName, extractedContext.SpanContext, tags: tags, links: extractedContext.Links);
scope.Span.DecorateWebServerSpan(resourceName, httpMethod, host, url, userAgent, tags);
var dataStreamsManager = tracer.TracerManager.DataStreamsManager;
if (dataStreamsManager.IsTransactionTrackingEnabled)
{
var extractors = dataStreamsManager.GetExtractorsByType(DataStreamsTransactionExtractor.ExtractorType.HttpInHeaders);
if (extractors != null)
{
foreach (var extractor in extractors)
{
if (request.Headers.TryGetValue(extractor.Value, out var headerValues))
{
foreach (var headerValue in headerValues)
{
scope.Span.TrackTransaction(dataStreamsManager, headerValue, extractor.Name);
}
}
}
}
}
var headerTagsInternal = tracer.CurrentTraceSettings.Settings.HeaderTags;
if (headerTagsInternal.Count != 0)
{
AddHeaderTagsToSpan(scope.Span, request, tracer, headerTagsInternal);
}
tracer.TracerManager.SpanContextPropagator.AddBaggageToSpanAsTags(scope.Span, extractedContext.Baggage, tracer.Settings.BaggageTagKeys);
var originalPath = request.PathBase.HasValue ? request.PathBase.Add(request.Path) : request.Path;
#if NET6_0_OR_GREATER
httpContext.Items[HttpContextTrackingKey] = useSingleSpanRequestTracking
? new SingleSpanRequestTrackingFeature(originalPath, scope, proxyContext?.Scope)
: new RequestTrackingFeature(originalPath, scope, proxyContext?.Scope);
#else
httpContext.Items[HttpContextTrackingKey] = new RequestTrackingFeature(originalPath, scope, proxyContext?.Scope);
#endif
if (AzureInfo.Instance.IsAzureFunction)
{
// Store scope in HttpContext.Items for Azure Functions middleware to retrieve
httpContext.Items[HttpContextActiveScopeKey] = scope;
if (_log.IsEnabled(LogEventLevel.Debug) && scope.Span.Context is { } spanContext)
{
_log.Debug(
"AspNetCore: Stored scope in HttpContext.Items, {TraceId}-{SpanId}, path: {Path}",
spanContext.RawTraceId,
spanContext.RawSpanId,
request.Path);
}
}
if (tracer.Settings.IpHeaderEnabled || security.AppsecEnabled)
{
var peerIp = new Headers.Ip.IpInfo(httpContext.Connection.RemoteIpAddress?.ToString(), httpContext.Connection.RemotePort);
string GetRequestHeaderFromKey(string key) => request.Headers.TryGetValue(key, out var value) ? value : string.Empty;
Headers.Ip.RequestIpExtractor.AddIpToTags(peerIp, request.IsHttps, GetRequestHeaderFromKey, tracer.Settings.IpHeader, tags);
}
if (iast.Settings.Enabled && iast.OverheadController.AcquireRequest())
{
// If the overheadController disables the vulnerability detection for this request, we do not initialize the iast context of TraceContext
scope.Span.Context?.TraceContext?.EnableIastInRequest();
}
tags.SetAnalyticsSampleRate(_integrationId, tracer.CurrentTraceSettings.Settings, enabledWithGlobalSetting: true);
tracer.TracerManager.Telemetry.IntegrationGeneratedSpan(_integrationId);
return scope;
}
public void StopAspNetCorePipelineScope(Tracer tracer, Security security, Scope rootScope, HttpContext httpContext)
=> StopAspNetCorePipelineScope(tracer, security, rootScope, httpContext, proxyScope: (httpContext.Items[HttpContextTrackingKey] as RequestTrackingFeature)?.ProxyScope);
public void StopAspNetCorePipelineScope(Tracer tracer, Security security, Scope rootScope, HttpContext httpContext, Scope proxyScope)
{
if (rootScope != null)
{
// We may need to update the resource name if none of the routing/mvc events updated it.
// If we had an unhandled exception, the status code will already be updated correctly,
// but if the span was manually marked as an error, we still need to record the status code
// WARNING: This code assumes that the rootSpan passed in is the aspnetcore.request
// root span. In "normal" operation, this will be the same span returned by
// Tracer.Instance.ActiveScope, but if a customer is not disposing a span somewhere,
// that will not necessarily be true, so make sure you use the RequestTrackingFeature.
var span = rootScope.Span;
CopyAspNetCoreActivityTagsIfRequired(span);
var isMissingHttpStatusCode = !span.HasHttpStatusCode();
var settings = tracer.CurrentTraceSettings.Settings;
if (string.IsNullOrEmpty(span.ResourceName) || isMissingHttpStatusCode)
{
if (string.IsNullOrEmpty(span.ResourceName))
{
span.ResourceName = GetDefaultResourceName(httpContext.Request);
}
if (isMissingHttpStatusCode)
{
span.SetHttpStatusCode(httpContext.Response.StatusCode, isServer: true, settings);
}
}
span.SetHeaderTags(new HeadersCollectionAdapter(httpContext.Response.Headers), settings.HeaderTags, defaultTagPrefix: SpanContextPropagator.HttpResponseHeadersTagPrefix);
if (proxyScope?.Span != null)
{
proxyScope.Span.SetHttpStatusCode(httpContext.Response.StatusCode, isServer: true, settings);
proxyScope.Span.SetHeaderTags(new HeadersCollectionAdapter(httpContext.Response.Headers), settings.HeaderTags, defaultTagPrefix: SpanContextPropagator.HttpResponseHeadersTagPrefix);
}
if (security.AppsecEnabled)
{
var securityCoordinator = SecurityCoordinator.Get(security, span, new SecurityCoordinator.HttpTransport(httpContext));
securityCoordinator.Reporter.AddResponseHeadersToSpan();
}
CoreHttpContextStore.Instance.Remove();
rootScope.Dispose();
proxyScope?.Dispose();
}
}
public void HandleAspNetCoreException(Tracer tracer, Security security, Span rootSpan, HttpContext httpContext, Exception exception)
=> HandleAspNetCoreException(tracer, security, rootSpan, httpContext, exception, proxyScope: (httpContext.Items[HttpContextTrackingKey] as RequestTrackingFeature)?.ProxyScope);
public void HandleAspNetCoreException(Tracer tracer, Security security, Span rootSpan, HttpContext httpContext, Exception exception, Scope proxyScope)
{
// WARNING: This code assumes that the rootSpan passed in is the aspnetcore.request
// root span. In "normal" operation, this will be the same span returned by
// Tracer.Instance.ActiveScope, but if a customer is not disposing a span somewhere,
// that will not necessarily be true, so make sure you use the RequestTrackingFeature.
if (rootSpan != null && httpContext is not null && exception is not null)
{
var statusCode = 500;
if (exception.TryDuckCast<AspNetCoreDiagnosticObserver.BadHttpRequestExceptionStruct>(out var badRequestException))
{
statusCode = badRequestException.StatusCode;
}
// Generic unhandled exceptions are converted to 500 errors by Kestrel
rootSpan.SetHttpStatusCode(statusCode: statusCode, isServer: true, tracer.CurrentTraceSettings.Settings);
if (proxyScope?.Span != null)
{
proxyScope.Span.SetHttpStatusCode(statusCode, isServer: true, tracer.CurrentTraceSettings.Settings);
}
if (BlockException.GetBlockException(exception) is null)
{
rootSpan.SetException(exception);
if (proxyScope?.Span != null)
{
proxyScope.Span.SetException(exception);
}
security.CheckAndBlock(httpContext, rootSpan);
}
}
}
public void CopyAspNetCoreActivityTagsIfRequired(Span span)
{
// Extract data from the Activity if there is one, and it's the one we expect
// We're using GetCurrentActivityObject rather than GetCurrentActivity because
// we don't actually need to duck cast as IActivity6 or IW3CActivity
// This will only be non-null if the activity listener is enabled by enabling
// the OTel integration
var rawActivity = ActivityListener.GetCurrentActivityObject();
if (rawActivity is null)
{
return;
}
AddActivityTags(span, rawActivity, _log);
// Extracted to method as not invoked in default config (only when otel enabled)
static void AddActivityTags(Span span, object rawActivity, IDatadogLogger log)
{
// AFAICT this has been static since at least .NET Core 2.1
// https://github.com/dotnet/aspnetcore/blob/v2.1.33/src/Hosting/Hosting/src/Internal/HostingApplicationDiagnostics.cs#L18C46-L18C88
// https://github.com/dotnet/aspnetcore/blob/v10.0.1/src/Hosting/Hosting/src/Internal/HostingApplicationDiagnostics.cs#L20
const string aspnetcoreActivityOperationName = "Microsoft.AspNetCore.Hosting.HttpRequestIn";
try
{
if (rawActivity.DuckAs<IActivity5>() is { } activity5
&& string.Equals(activity5.OperationName, aspnetcoreActivityOperationName, StringComparison.Ordinal)
&& activity5.HasTagObjects())
{
var state = new OtelTagsEnumerationState(span);
ActivityEnumerationHelper.EnumerateTagObjects(
activity5,
ref state,
static (ref s, kvp) =>
{
// We don't want to set know values to avoid conflicting scenarios
// with the status code, resource name, operation name etc that we set
// by default on aspnetcore spans when _not_ using activities
// We also don't want to override our standard aspnetcore/web tags.
if (!IsKnownWebTag(kvp.Key))
{
OtlpHelpers.SetTagObject(s.Span, kvp.Key, kvp.Value, setKnownValues: false);
}
return true;
});
}
else if (rawActivity.DuckAs<IActivity>() is { } activity
&& string.Equals(activity.OperationName, aspnetcoreActivityOperationName, StringComparison.Ordinal)
&& activity.HasTags())
{
var state = new OtelTagsEnumerationState(span);
ActivityEnumerationHelper.EnumerateTags(
activity,
ref state,
static (ref s, kvp) =>
{
// We don't want to set know values to avoid conflicting scenarios
// with the status code, resource name, operation name etc that we set
// by default on aspnetcore spans when _not_ using activities
// We also don't want to override our standard aspnetcore/web tags.
if (!IsKnownWebTag(kvp.Key))
{
OtlpHelpers.SetTagObject(s.Span, kvp.Key, kvp.Value, setKnownValues: false);
}
return true;
});
}
}
catch (Exception ex)
{
log.Error(ex, "Error extracting activity data.");
}
}
// Theoretically we should check
// for _all_ the tags we might set on aspnetcore root spans,
// but we only both to check tags that are likely to be set here
// (i.e. don't bother checking the aspnetcore. tags)
static bool IsKnownWebTag(string tagName) =>
tagName == Tags.HttpRoute
|| tagName == Tags.HttpUserAgent
|| tagName == Tags.HttpMethod
|| tagName == Tags.HttpUrl
|| tagName == Tags.HttpStatusCode
|| tagName == Tags.NetworkClientIp
|| tagName == Tags.HttpClientIp;
}
/// <summary>
/// Holds state that we want to pass between diagnostic source events
/// </summary>
internal sealed class RequestTrackingFeature
{
public RequestTrackingFeature(PathString originalPath, Scope rootAspNetCoreScope, Scope proxyScope)
{
OriginalPath = originalPath;
RootScope = rootAspNetCoreScope;
ProxyScope = proxyScope;
}
/// <summary>
/// Gets or sets a value indicating whether the pipeline using endpoint routing
/// </summary>
public bool IsUsingEndpointRouting { get; set; }
/// <summary>
/// Gets or sets a value indicating whether this is the first pipeline execution
/// </summary>
public bool IsFirstPipelineExecution { get; set; } = true;
/// <summary>
/// Gets or sets a value indicating the route as calculated by endpoint routing (if available)
/// </summary>
public string Route { get; set; }
/// <summary>
/// Gets or sets a value indicating the resource name as calculated by the endpoint routing(if available)
/// </summary>
public string ResourceName { get; set; }
/// <summary>
/// Gets a value indicating the original combined Path and PathBase
/// </summary>
public PathString OriginalPath { get; }
/// <summary>
/// Gets the root ASP.NET Core Scope
/// </summary>
public Scope RootScope { get; }
/// <summary>
/// Gets or sets the inferred ASP.NET Core Scope created from headers.
/// </summary>
public Scope ProxyScope { get; set; }
public bool MatchesOriginalPath(HttpRequest request)
{
if (!request.PathBase.HasValue)
{
return OriginalPath.Equals(request.Path, StringComparison.OrdinalIgnoreCase);
}
return OriginalPath.StartsWithSegments(
request.PathBase,
StringComparison.OrdinalIgnoreCase,
out var remaining)
&& remaining.Equals(request.Path, StringComparison.OrdinalIgnoreCase);
}
}
#if NET6_0_OR_GREATER
/// <summary>
/// Holds state that we want to pass between diagnostic source events
/// </summary>
internal sealed class SingleSpanRequestTrackingFeature(PathString originalPath, Scope rootScope, [MaybeNull] Scope proxyScope)
{
/// <summary>
/// Gets the root ASP.NET Core Scope
/// </summary>
[NotNull]
public Scope RootScope { get; } = rootScope;
/// <summary>
/// Gets the inferred ASP.NET Core Scope created from headers.
/// </summary>
[MaybeNull]
public Scope ProxyScope { get; } = proxyScope;
/// <summary>
/// Gets a value indicating the original combined Path and PathBase
/// </summary>
public PathString OriginalPath { get; } = originalPath;
public bool MatchesOriginalPath(HttpRequest request)
{
if (!request.PathBase.HasValue)
{
return OriginalPath.Equals(request.Path, StringComparison.OrdinalIgnoreCase);
}
return OriginalPath.StartsWithSegments(
request.PathBase,
StringComparison.OrdinalIgnoreCase)
&& OriginalPath.Value.AsSpan(request.PathBase.Value.Length)
.Equals(request.Path.Value, StringComparison.OrdinalIgnoreCase);
}
}
#endif
}
}
#endif