-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathHttpWebRequest_BeginGetResponse_Integration.cs
76 lines (70 loc) · 3.63 KB
/
HttpWebRequest_BeginGetResponse_Integration.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
// <copyright file="HttpWebRequest_BeginGetResponse_Integration.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.ComponentModel;
using System.Net;
using Datadog.Trace.ClrProfiler.CallTarget;
using Datadog.Trace.Propagators;
namespace Datadog.Trace.ClrProfiler.AutoInstrumentation.Http.WebRequest
{
/// <summary>
/// CallTarget integration for HttpWebRequest.BeginGetResponse
/// We only instrument .NET Framework - .NET Core uses an HttpClient
/// internally, which is already instrumented
/// </summary>
[InstrumentMethod(
AssemblyName = WebRequestCommon.NetFrameworkAssembly,
TypeName = WebRequestCommon.HttpWebRequestTypeName,
MethodName = MethodName,
ReturnTypeName = ClrNames.IAsyncResult,
ParameterTypeNames = new[] { ClrNames.AsyncCallback, ClrNames.Object },
MinimumVersion = WebRequestCommon.Major4,
MaximumVersion = WebRequestCommon.Major4,
IntegrationName = WebRequestCommon.IntegrationName)]
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
public class HttpWebRequest_BeginGetResponse_Integration
{
private const string MethodName = "BeginGetResponse";
/// <summary>
/// OnMethodBegin callback
/// </summary>
/// <typeparam name="TTarget">Type of the target</typeparam>
/// <param name="instance">Instance value, aka `this` of the instrumented method.</param>
/// <param name="callback">The AsyncCallback delegate</param>
/// <param name="state">An object containing state information for this asynchronous request</param>
/// <returns>Calltarget state value</returns>
internal static CallTargetState OnMethodBegin<TTarget>(TTarget instance, AsyncCallback callback, object state)
{
if (instance is HttpWebRequest request && WebRequestCommon.IsTracingEnabled(request))
{
var tracer = Tracer.Instance;
// We may have already set headers
if (request.Headers.Get(HttpHeaderNames.TraceId) is null)
{
var span = ScopeFactory.CreateInactiveOutboundHttpSpan(
tracer,
request.Method,
request.RequestUri,
WebRequestCommon.IntegrationId,
out _,
traceId: TraceId.Zero,
spanId: 0,
startTime: null,
addToTraceContext: false);
// Add distributed tracing headers to the HTTP request.
// We don't want to set an active scope now, because it's possible that EndGetResponse will never be called.
// Instead, we generate a spancontext and inject it in the headers. EndGetResponse will fetch them and create an active scope with the right id.
// Additionally, add the request headers to a cache to indicate that distributed tracing headers were
// added by us, not the application
var context = new PropagationContext(span?.Context, Baggage.Current);
tracer.TracerManager.SpanContextPropagator.Inject(context, request.Headers.Wrap());
HeadersInjectedCache.SetInjectedHeaders(request.Headers);
}
}
return CallTargetState.GetDefault();
}
}
}