-
Notifications
You must be signed in to change notification settings - Fork 279
/
HttpInListener.cs
165 lines (138 loc) · 6.31 KB
/
HttpInListener.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
using System.Diagnostics;
using System.Web;
using OpenTelemetry.Context.Propagation;
using OpenTelemetry.Internal;
using OpenTelemetry.Trace;
namespace OpenTelemetry.Instrumentation.AspNet.Implementation;
internal sealed class HttpInListener : IDisposable
{
private readonly HttpRequestRouteHelper routeHelper = new();
private readonly AspNetTraceInstrumentationOptions options;
private readonly RequestDataHelper requestDataHelper = new(configureByHttpKnownMethodsEnvironmentalVariable: true);
public HttpInListener(AspNetTraceInstrumentationOptions options)
{
Guard.ThrowIfNull(options);
this.options = options;
TelemetryHttpModule.Options.TextMapPropagator = Propagators.DefaultTextMapPropagator;
TelemetryHttpModule.Options.OnRequestStartedCallback += this.OnStartActivity;
TelemetryHttpModule.Options.OnRequestStoppedCallback += this.OnStopActivity;
TelemetryHttpModule.Options.OnExceptionCallback += this.OnException;
}
public void Dispose()
{
TelemetryHttpModule.Options.OnRequestStartedCallback -= this.OnStartActivity;
TelemetryHttpModule.Options.OnRequestStoppedCallback -= this.OnStopActivity;
TelemetryHttpModule.Options.OnExceptionCallback -= this.OnException;
}
private void OnStartActivity(Activity activity, HttpContext context)
{
if (activity.IsAllDataRequested)
{
try
{
// todo: Ideally we would also check
// Sdk.SuppressInstrumentation here to prevent tagging a
// span that will not be collected but we can't do that
// without an SDK reference. Need the spec to come around on
// this.
if (this.options.Filter?.Invoke(context) == false)
{
AspNetInstrumentationEventSource.Log.RequestIsFilteredOut(activity.OperationName);
activity.IsAllDataRequested = false;
activity.ActivityTraceFlags &= ~ActivityTraceFlags.Recorded;
return;
}
}
catch (Exception ex)
{
AspNetInstrumentationEventSource.Log.RequestFilterException(activity.OperationName, ex);
activity.IsAllDataRequested = false;
activity.ActivityTraceFlags &= ~ActivityTraceFlags.Recorded;
return;
}
var request = context.Request;
// see the spec https://github.com/open-telemetry/semantic-conventions/blob/v1.24.0/docs/http/http-spans.md
var originalHttpMethod = request.HttpMethod;
this.requestDataHelper.SetActivityDisplayName(activity, originalHttpMethod);
var url = request.Url;
activity.SetTag(SemanticConventions.AttributeServerAddress, url.Host);
activity.SetTag(SemanticConventions.AttributeServerPort, url.Port);
activity.SetTag(SemanticConventions.AttributeUrlScheme, url.Scheme);
this.requestDataHelper.SetHttpMethodTag(activity, originalHttpMethod);
var protocolVersion = RequestDataHelperExtensions.GetHttpProtocolVersion(request);
if (!string.IsNullOrEmpty(protocolVersion))
{
activity.SetTag(SemanticConventions.AttributeNetworkProtocolVersion, protocolVersion);
}
// TODO url.query should be sanitized
var query = url.Query;
if (!string.IsNullOrEmpty(query))
{
var queryString = query.StartsWith("?", StringComparison.InvariantCulture) ? query.Substring(1) : query;
activity.SetTag(SemanticConventions.AttributeUrlQuery, this.options.DisableUrlQueryRedaction ? queryString : RedactionHelper.GetRedactedQueryString(queryString));
}
var userAgent = request.UserAgent;
if (!string.IsNullOrEmpty(userAgent))
{
activity.SetTag(SemanticConventions.AttributeUserAgentOriginal, userAgent);
}
try
{
this.options.EnrichWithHttpRequest?.Invoke(activity, request);
}
catch (Exception ex)
{
AspNetInstrumentationEventSource.Log.EnrichmentException("OnStartActivity", ex);
}
}
}
private void OnStopActivity(Activity activity, HttpContext context)
{
if (activity.IsAllDataRequested)
{
var response = context.Response;
activity.SetTag(SemanticConventions.AttributeHttpResponseStatusCode, response.StatusCode);
if (activity.Status == ActivityStatusCode.Unset)
{
activity.SetStatus(SpanHelper.ResolveActivityStatusForHttpStatusCode(activity.Kind, response.StatusCode));
}
var template = this.routeHelper.GetRouteTemplate(context.Request);
if (!string.IsNullOrEmpty(template))
{
// Override the name that was previously set to the normalized HTTP method/HTTP
this.requestDataHelper.SetActivityDisplayName(activity, context.Request.HttpMethod, template);
activity.SetTag(SemanticConventions.AttributeHttpRoute, template);
}
try
{
this.options.EnrichWithHttpResponse?.Invoke(activity, response);
}
catch (Exception ex)
{
AspNetInstrumentationEventSource.Log.EnrichmentException("OnStopActivity", ex);
}
}
}
private void OnException(Activity activity, HttpContext context, Exception exception)
{
if (activity.IsAllDataRequested)
{
if (this.options.RecordException)
{
activity.RecordException(exception);
}
activity.SetStatus(ActivityStatusCode.Error, exception.Message);
activity.SetTag(SemanticConventions.AttributeErrorType, exception.GetType().FullName);
try
{
this.options.EnrichWithException?.Invoke(activity, exception);
}
catch (Exception ex)
{
AspNetInstrumentationEventSource.Log.EnrichmentException("OnException", ex);
}
}
}
}