Skip to content

Commit e8bdc34

Browse files
authored
fix: Stop double injecting headers with HttpClient on .NET Framework (#1679)
1 parent 51080df commit e8bdc34

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/Agent/NewRelic/Agent/Extensions/Providers/Wrapper/HttpClient/SendAsync.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ public AfterWrappedMethodDelegate BeforeWrappedMethod(InstrumentedMethodCall ins
6161

6262
var externalSegmentData = transactionExperimental.CreateExternalSegmentData(uri, method);
6363
var segment = transactionExperimental.StartSegment(instrumentedMethodCall.MethodCall);
64-
segment.GetExperimentalApi().SetSegmentData(externalSegmentData);
64+
segment.GetExperimentalApi()
65+
.SetSegmentData(externalSegmentData)
66+
.MakeLeaf();
6567

6668
if (agent.Configuration.ForceSynchronousTimingCalculationHttpClient)
6769
{

src/Agent/NewRelic/Agent/Extensions/Providers/Wrapper/HttpWebRequest/SerializeHeadersWrapper.cs

+10-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
using System;
5-
using System.Linq;
65
using NewRelic.Agent.Api;
76
using NewRelic.Agent.Extensions.Providers.Wrapper;
87

@@ -33,6 +32,16 @@ public AfterWrappedMethodDelegate BeforeWrappedMethod(InstrumentedMethodCall ins
3332
throw new NullReferenceException("request.Headers");
3433
}
3534

35+
// We should only inject headers with this instrumentation if HttpWebRequest created an external segment.
36+
// Both HttpClient and RestSharp instrumentation have their own header injection support and do not rely
37+
// on this instrumentation to inject the necessary headers. Other instrumentation rely on leaf segments
38+
// to prevent this instrumentation from running.
39+
if (!transaction.CurrentSegment.IsExternal)
40+
{
41+
transaction.LogFinest("Skipping HttpWebRequest header injection because the current segment was not created by HttpWebRequest.");
42+
return Delegates.NoOp;
43+
}
44+
3645
var setHeaders = new Action<System.Net.HttpWebRequest, string, string>((carrier, key, value) =>
3746
{
3847
carrier.Headers?.Set(key, value);

0 commit comments

Comments
 (0)