Skip to content

fix: Stop double injecting headers with HttpClient on .NET Framework #1679

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ public AfterWrappedMethodDelegate BeforeWrappedMethod(InstrumentedMethodCall ins

var externalSegmentData = transactionExperimental.CreateExternalSegmentData(uri, method);
var segment = transactionExperimental.StartSegment(instrumentedMethodCall.MethodCall);
segment.GetExperimentalApi().SetSegmentData(externalSegmentData);
segment.GetExperimentalApi()
.SetSegmentData(externalSegmentData)
.MakeLeaf();

if (agent.Configuration.ForceSynchronousTimingCalculationHttpClient)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// SPDX-License-Identifier: Apache-2.0

using System;
using System.Linq;
using NewRelic.Agent.Api;
using NewRelic.Agent.Extensions.Providers.Wrapper;

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

// We should only inject headers with this instrumentation if HttpWebRequest created an external segment.
// Both HttpClient and RestSharp instrumentation have their own header injection support and do not rely
// on this instrumentation to inject the necessary headers. Other instrumentation rely on leaf segments
// to prevent this instrumentation from running.
if (!transaction.CurrentSegment.IsExternal)
{
transaction.LogFinest("Skipping HttpWebRequest header injection because the current segment was not created by HttpWebRequest.");
return Delegates.NoOp;
}

var setHeaders = new Action<System.Net.HttpWebRequest, string, string>((carrier, key, value) =>
{
carrier.Headers?.Set(key, value);
Expand Down