Skip to content

Commit 1f95c9c

Browse files
fix: Fix bug in distributed tracing when excludeNewrelicHeader is set to true (#2457)
* fix: Fix exception thrown when disabling newrelic header with DT * Exclude one supportability metric from assertions if NR header is excluded Also tweak names for understandability --------- Co-authored-by: chynesNR <[email protected]>
1 parent f097d37 commit 1f95c9c

File tree

4 files changed

+55
-8
lines changed

4 files changed

+55
-8
lines changed

src/Agent/NewRelic/Agent/Core/DistributedTracing/DistributedTracePayloadHandler.cs

+4
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ public void InsertDistributedTraceHeaders<T>(IInternalTransaction transaction, T
7474
setter(carrier, Constants.DistributedTracePayloadKeyAllLower, distributedTracePayload);
7575
}
7676
}
77+
else
78+
{
79+
transaction.SetSampled(_adaptiveSampler);
80+
}
7781

7882
var createOutboundTraceContextHeadersSuccess = false;
7983
try

tests/Agent/IntegrationTests/IntegrationTestHelpers/NewRelicConfigModifier.cs

+19
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,25 @@ public void SetOrDeleteDistributedTraceEnabled(bool? enabled)
243243
}
244244
}
245245

246+
/// <summary>
247+
/// Sets or deletes the excludeNewrelicHeader setting in the newrelic.config.
248+
/// </summary>
249+
/// <param name="enabled">If null, the setting will be deleted; otherwise, the setting will be set to the value of this parameter.</param>
250+
public void SetOrDeleteDistributedTraceExcludeNewRelicHeader(bool? exclude)
251+
{
252+
const string config = "configuration";
253+
const string distributedTracing = "distributedTracing";
254+
if (null == exclude)
255+
{
256+
CommonUtils.DeleteXmlNodeFromNewRelicConfig(_configFilePath, new[] { config }, distributedTracing);
257+
}
258+
else
259+
{
260+
CommonUtils.ModifyOrCreateXmlAttributeInNewRelicConfig(_configFilePath, new[] { config, distributedTracing },
261+
"excludeNewrelicHeader", exclude.Value ? "true" : "false");
262+
}
263+
}
264+
246265
public NewRelicConfigModifier SetAllowAllHeaders(bool? enabled)
247266
{
248267
const string config = "configuration";

tests/Agent/IntegrationTests/IntegrationTests/DistributedTracing/W3CInstrumentationTests/HttpClientW3CTestsNetCore.cs

+26-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace NewRelic.Agent.IntegrationTests.DistributedTracing.W3CInstrumentationT
1919
/// Instrumentations occur in this test are AspNetCore and HttpClient.
2020
/// </summary>
2121
[NetCoreTest]
22-
public class HttpClientW3CTestsNetCore : NewRelicIntegrationTest<AspNetCoreDistTraceRequestChainFixture>
22+
public abstract class HttpClientW3CTestsNetCore : NewRelicIntegrationTest<AspNetCoreDistTraceRequestChainFixture>
2323
{
2424
private readonly AspNetCoreDistTraceRequestChainFixture _fixture;
2525

@@ -30,11 +30,12 @@ public class HttpClientW3CTestsNetCore : NewRelicIntegrationTest<AspNetCoreDistT
3030
private const string TestTracingVendors = "rojo,congo";
3131
private const string TestOtherVendorEntries = "rojo=1,congo=2";
3232

33-
public HttpClientW3CTestsNetCore(AspNetCoreDistTraceRequestChainFixture fixture, ITestOutputHelper output)
33+
public HttpClientW3CTestsNetCore(AspNetCoreDistTraceRequestChainFixture fixture, ITestOutputHelper output, bool excludeNewRelicHeader)
3434
: base(fixture)
3535
{
3636
_fixture = fixture;
3737
_fixture.TestLogger = output;
38+
_fixture.ExcludeNewRelicHeader = excludeNewRelicHeader;
3839
_fixture.AddActions
3940
(
4041
exerciseApplication: () =>
@@ -46,7 +47,6 @@ public HttpClientW3CTestsNetCore(AspNetCoreDistTraceRequestChainFixture fixture,
4647
};
4748

4849
_fixture.ExecuteTraceRequestChain("CallNext", "CallNext", "CallEnd", headers);
49-
5050
_fixture.FirstCallAppAgentLog.WaitForLogLines(AgentLogBase.TransactionTransformCompletedLogLineRegex, TimeSpan.FromSeconds(15), ExpectedTransactionCount);
5151
_fixture.SecondCallAppAgentLog.WaitForLogLines(AgentLogBase.TransactionTransformCompletedLogLineRegex, TimeSpan.FromSeconds(15), ExpectedTransactionCount);
5252
_fixture.AgentLog.WaitForLogLines(AgentLogBase.TransactionTransformCompletedLogLineRegex, TimeSpan.FromSeconds(15), ExpectedTransactionCount);
@@ -120,13 +120,17 @@ public void Test()
120120

121121
var senderExpectedMetrics = new List<Assertions.ExpectedMetric>
122122
{
123-
new Assertions.ExpectedMetric { metricName = @"Supportability/DistributedTrace/CreatePayload/Success", callCount = 1 },
124123
new Assertions.ExpectedMetric { metricName = @"Supportability/SpanEvent/TotalEventsSeen", callCount = 4 },
125124
new Assertions.ExpectedMetric { metricName = @"Supportability/TraceContext/Accept/Success", callCount = 1 },
126125
new Assertions.ExpectedMetric { metricName = @"Supportability/TraceContext/Create/Success", callCount = 1 },
127126
new Assertions.ExpectedMetric { metricName = @"Supportability/TraceContext/TraceState/NoNrEntry", callCount = 1 }
128127
};
129128

129+
if (! _fixture.ExcludeNewRelicHeader)
130+
{
131+
senderExpectedMetrics.Add(new Assertions.ExpectedMetric { metricName = @"Supportability/DistributedTrace/CreatePayload/Success", callCount = 1 });
132+
}
133+
130134
var accountId = _fixture.SecondCallAppAgentLog.GetAccountId();
131135
var appId = _fixture.SecondCallAppAgentLog.GetApplicationId();
132136

@@ -157,4 +161,22 @@ public void Test()
157161
);
158162
}
159163
}
164+
165+
public class HttpClientW3CTestsNetCoreWithNRHeader : HttpClientW3CTestsNetCore
166+
{
167+
public HttpClientW3CTestsNetCoreWithNRHeader(AspNetCoreDistTraceRequestChainFixture fixture, ITestOutputHelper output)
168+
: base(fixture, output, excludeNewRelicHeader: false)
169+
{
170+
}
171+
172+
}
173+
174+
public class HttpClientW3CTestsNetCoreWithoutNRHeader : HttpClientW3CTestsNetCore
175+
{
176+
public HttpClientW3CTestsNetCoreWithoutNRHeader(AspNetCoreDistTraceRequestChainFixture fixture, ITestOutputHelper output)
177+
: base(fixture, output, excludeNewRelicHeader: true)
178+
{
179+
}
180+
}
181+
160182
}

tests/Agent/IntegrationTests/IntegrationTests/RemoteServiceFixtures/AspNetCoreDistTraceRequestChainFixture.cs

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright 2020 New Relic, Inc. All rights reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
54
using System;
65
using System.Collections.Generic;
76
using NewRelic.Agent.IntegrationTestHelpers;
@@ -18,6 +17,8 @@ public class AspNetCoreDistTraceRequestChainFixture : RemoteApplicationFixture
1817
public RemoteService FirstCallApplication { get; set; }
1918
public RemoteService SecondCallApplication { get; set; }
2019

20+
public bool ExcludeNewRelicHeader = false;
21+
2122
private AgentLogFile _firstCallAppAgentLog;
2223
private AgentLogFile _secondCallAppAgentLog;
2324

@@ -42,8 +43,8 @@ public AspNetCoreDistTraceRequestChainFixture()
4243
configModifier.SetLogLevel("all");
4344

4445
//Do during setup so TestLogger is set.
45-
FirstCallApplication = SetupDistributedTracingApplication();
46-
SecondCallApplication = SetupDistributedTracingApplication();
46+
FirstCallApplication = SetupDistributedTracingApplication(ExcludeNewRelicHeader);
47+
SecondCallApplication = SetupDistributedTracingApplication(ExcludeNewRelicHeader);
4748

4849
var environmentVariables = new Dictionary<string, string>();
4950

@@ -74,7 +75,7 @@ public void ExecuteTraceRequestChain(string firstAppAction, string secondAppActi
7475
}
7576
}
7677

77-
protected RemoteService SetupDistributedTracingApplication()
78+
protected RemoteService SetupDistributedTracingApplication(bool excludeNewRelicHeader)
7879
{
7980
var service = new RemoteService(
8081
ApplicationDirectoryName,
@@ -93,6 +94,7 @@ protected RemoteService SetupDistributedTracingApplication()
9394
var configModifier = new NewRelicConfigModifier(service.DestinationNewRelicConfigFilePath);
9495
configModifier.SetOrDeleteDistributedTraceEnabled(true);
9596
configModifier.SetOrDeleteSpanEventsEnabled(true);
97+
configModifier.SetOrDeleteDistributedTraceExcludeNewRelicHeader(excludeNewRelicHeader);
9698
configModifier.SetLogLevel("all");
9799

98100
return service;

0 commit comments

Comments
 (0)