-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathHttpMessageHandlerCommon.cs
114 lines (97 loc) · 4.11 KB
/
HttpMessageHandlerCommon.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
// <copyright file="HttpMessageHandlerCommon.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>
using System;
using System.Linq;
using System.Threading;
using Datadog.Trace.ClrProfiler.CallTarget;
using Datadog.Trace.Configuration;
using Datadog.Trace.Propagators;
namespace Datadog.Trace.ClrProfiler.AutoInstrumentation.Http.HttpClient
{
internal static class HttpMessageHandlerCommon
{
public static CallTargetState OnMethodBegin<TTarget, TRequest>(TTarget instance, TRequest requestMessage, CancellationToken cancellationToken, IntegrationId integrationId, IntegrationId? implementationIntegrationId)
where TRequest : IHttpRequestMessage
{
if (requestMessage.Instance is null)
{
return CallTargetState.GetDefault();
}
var tracer = Tracer.Instance;
var headers = requestMessage.Headers;
Scope scope = null;
if (IsTracingEnabled(headers, implementationIntegrationId))
{
scope = ScopeFactory.CreateOutboundHttpScope(
tracer,
requestMessage.Method.Method,
requestMessage.RequestUri,
integrationId,
out var tags);
if (scope is not null)
{
tags.HttpClientHandlerType = instance.GetType().FullName;
tracer.TracerManager.Telemetry.IntegrationGeneratedSpan(implementationIntegrationId ?? integrationId);
}
}
// add propagation headers to the HTTP request
var context = new PropagationContext(scope?.Span.Context, Baggage.Current);
tracer.TracerManager.SpanContextPropagator.Inject(context, new HttpHeadersCollection(headers));
return scope is null ? CallTargetState.GetDefault() : new CallTargetState(scope);
}
public static TResponse OnMethodEnd<TTarget, TResponse>(TTarget instance, TResponse responseMessage, Exception exception, in CallTargetState state)
where TResponse : IHttpResponseMessage
{
Scope scope = state.Scope;
if (scope is null)
{
return responseMessage;
}
try
{
if (responseMessage.Instance is not null)
{
scope.Span.SetHttpStatusCode(responseMessage.StatusCode, false, Tracer.Instance.Settings);
}
if (exception != null)
{
scope.Span.SetException(exception);
}
}
finally
{
scope.Dispose();
}
return responseMessage;
}
private static bool IsTracingEnabled(IRequestHeaders headers, IntegrationId? implementationIntegrationId)
{
if (implementationIntegrationId != null && !Tracer.Instance.Settings.IsIntegrationEnabled(implementationIntegrationId.Value))
{
return false;
}
if (headers.TryGetValues(HttpHeaderNames.TracingEnabled, out var headerValues))
{
if (headerValues is string[] arrayValues)
{
for (var i = 0; i < arrayValues.Length; i++)
{
if (string.Equals(arrayValues[i], "false", StringComparison.OrdinalIgnoreCase))
{
return false;
}
}
return true;
}
if (headerValues != null && headerValues.Any(s => string.Equals(s, "false", StringComparison.OrdinalIgnoreCase)))
{
// tracing is disabled for this request via http header
return false;
}
}
return true;
}
}
}