Skip to content

Commit 10da6c4

Browse files
committed
chore: Add appSettings switch / env var to restore instrumentation of websockets in ASP.NET Core 6+
1 parent 7f2a5e9 commit 10da6c4

7 files changed

Lines changed: 31 additions & 12 deletions

File tree

src/Agent/NewRelic/Agent/Core/Configuration/DefaultConfiguration.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2132,6 +2132,11 @@ private void UpdateDiagnosticsAgentTimingSettings()
21322132
public bool EnableAspNetCore6PlusBrowserInjection =>
21332133
_enableAspNetCore6PlusBrowserInjection ??= EnvironmentOverrides(TryGetAppSettingAsBoolWithDefault("EnableAspNetCore6PlusBrowserInjection", true), "NEW_RELIC_ENABLE_ASPNETCORE6PLUS_BROWSER_INJECTION");
21342134

2135+
private bool? _instrumentAspNetCore6PlusWebsockets;
2136+
public bool InstrumentAspNetCore6PlusWebsockets =>
2137+
_instrumentAspNetCore6PlusWebsockets ??= EnvironmentOverrides(TryGetAppSettingAsBoolWithDefault("InstrumentAspNetCore6PlusWebsockets", false), "NEW_RELIC_INSTRUMENT_ASPNETCORE6PLUS_WEBSOCKETS");
2138+
2139+
21352140
private TimeSpan? _metricsHarvestCycleOverride = null;
21362141
public TimeSpan MetricsHarvestCycle
21372142
{

src/Agent/NewRelic/Agent/Core/Configuration/ReportedConfiguration.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,9 @@ public ReportedConfiguration(IConfiguration configuration)
620620
[JsonProperty("agent.enable_asp_net_core_6plus_browser_injection")]
621621
public bool EnableAspNetCore6PlusBrowserInjection => _configuration.EnableAspNetCore6PlusBrowserInjection;
622622

623+
[JsonProperty("agent.instrument_asp_net_core_6plus_websockets")]
624+
public bool InstrumentAspNetCore6PlusWebsockets => _configuration.InstrumentAspNetCore6PlusWebsockets;
625+
623626
[JsonProperty("agent.exclude_new_relic_header")]
624627
public bool ExcludeNewrelicHeader => _configuration.ExcludeNewrelicHeader;
625628

@@ -809,4 +812,4 @@ public IReadOnlyDictionary<string, string> GetAppSettings()
809812
public bool HybridHttpContextStorageEnabled => _configuration.HybridHttpContextStorageEnabled;
810813

811814
#endregion
812-
}
815+
}

src/Agent/NewRelic/Agent/Extensions/NewRelic.Agent.Extensions/Configuration/IConfiguration.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ public interface IConfiguration
191191
int DatabaseStatementCacheCapacity { get; }
192192
bool ForceSynchronousTimingCalculationHttpClient { get; }
193193
bool EnableAspNetCore6PlusBrowserInjection { get; }
194+
195+
bool InstrumentAspNetCore6PlusWebsockets { get; }
194196
bool ExcludeNewrelicHeader { get; }
195197

196198
SamplerType RootSamplerType { get; }
@@ -283,4 +285,4 @@ public interface IConfiguration
283285
#endregion
284286

285287
bool HybridHttpContextStorageEnabled { get; }
286-
}
288+
}

src/Agent/NewRelic/Agent/Extensions/Providers/Wrapper/AspNetCore6Plus/WrapPipelineMiddleware.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,19 @@ public async Task Invoke(HttpContext context)
4545
return;
4646
}
4747

48-
// if there's a "Sec-WebSocket-Key" header, this is a websocket handshake request and we should not create a transaction for it
49-
// as it is a long-running connection that would skew transaction timings.
50-
// See https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Sec-WebSocket-Key for more information on this header.
51-
if (context.Request.Headers.ContainsKey("Sec-WebSocket-Key"))
48+
// undocumented switch to restore previous behavior of instrumenting websocket handshake requests.
49+
if (!_agent.Configuration.InstrumentAspNetCore6PlusWebsockets)
5250
{
53-
_agent.Logger.Debug("WebSocket handshake request detected; not instrumenting this long-running request.");
54-
await _next(context);
55-
return;
51+
// if there's a "Sec-WebSocket-Key" header, this is a websocket handshake request and we should not create a transaction for it
52+
// as it is a long-running connection that would skew transaction timings.
53+
// See https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Sec-WebSocket-Key for more information on this header.
54+
if (context.Request.Headers.ContainsKey("Sec-WebSocket-Key"))
55+
{
56+
_agent.Logger.Debug(
57+
"WebSocket handshake request detected; not instrumenting this long-running request.");
58+
await _next(context);
59+
return;
60+
}
5661
}
5762

5863
ITransaction transaction = null;

tests/Agent/UnitTests/Core.UnitTest/DataTransport/AgentSettingsTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ public void serializes_correctly()
308308
"transaction_tracer.database_statement_cache_capacity": 1234,
309309
"agent.force_synchronous_timing_calculation_for_http_client": true,
310310
"agent.enable_asp_net_core_6plus_browser_injection": true,
311+
"agent.instrument_asp_net_core_6plus_websockets": false,
311312
"agent.exclude_new_relic_header": true,
312313
"application_logging.enabled": true,
313314
"application_logging.metrics.enabled": true,
@@ -386,4 +387,4 @@ public void serializes_correctly()
386387
Assert.That(agentSettings.AwsAccountId, Is.Empty);
387388
});
388389
}
389-
}
390+
}

tests/Agent/UnitTests/Core.UnitTest/DataTransport/ConnectModelTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ public void serializes_correctly()
380380
"transaction_tracer.database_statement_cache_capacity": 1234,
381381
"agent.force_synchronous_timing_calculation_for_http_client": true,
382382
"agent.enable_asp_net_core_6plus_browser_injection": true,
383+
"agent.instrument_asp_net_core_6plus_websockets": false,
383384
"agent.exclude_new_relic_header": true,
384385
"application_logging.enabled": true,
385386
"application_logging.metrics.enabled": true,
@@ -528,4 +529,4 @@ public void serializes_correctly()
528529
});
529530
}
530531
}
531-
}
532+
}

tests/Agent/UnitTests/Core.UnitTest/DataTransport/ExhaustiveTestConfiguration.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,8 @@ public bool TryGetApplicationNames(out IEnumerable<string> names)
427427

428428
public bool EnableAspNetCore6PlusBrowserInjection => true;
429429

430+
public bool InstrumentAspNetCore6PlusWebsockets => false;
431+
430432
public bool ExcludeNewrelicHeader => true;
431433

432434
public bool ApplicationLoggingEnabled => true;
@@ -537,4 +539,4 @@ public IReadOnlyDictionary<string, string> GetAppSettings()
537539
public int OpenTelemetryMetricsExportTimeoutMs => 10000;
538540

539541
public bool HybridHttpContextStorageEnabled => false;
540-
}
542+
}

0 commit comments

Comments
 (0)