Skip to content

Commit f22a5c3

Browse files
Add long running activity tag for SignalR connections (#32084)
1 parent b69660a commit f22a5c3

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

src/SignalR/common/Http.Connections/src/Internal/HttpConnectionDispatcher.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ public async Task ExecuteNegotiateAsync(HttpContext context, HttpConnectionDispa
115115

116116
private async Task ExecuteAsync(HttpContext context, ConnectionDelegate connectionDelegate, HttpConnectionDispatcherOptions options, ConnectionLogScope logScope)
117117
{
118+
// set a tag to allow Application Performance Management tools to differentiate long running requests for reporting purposes
119+
context.Features.Get<IHttpActivityFeature>()?.Activity.AddTag("http.long_running", "true");
120+
118121
var supportedTransports = options.Transports;
119122

120123
// Server sent events transport

src/SignalR/common/Http.Connections/test/HttpConnectionDispatcherTests.cs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Buffers;
66
using System.Collections.Generic;
7+
using System.Diagnostics;
78
using System.IO;
89
using System.IO.Pipelines;
910
using System.Linq;
@@ -2078,7 +2079,7 @@ public async Task WriteThatIsDisposedBeforeCompleteReturns404()
20782079
TransportMaxBufferSize = 13,
20792080
ApplicationMaxBufferSize = 13
20802081
};
2081-
2082+
20822083
var connection = manager.CreateConnection(options);
20832084
connection.TransportType = HttpTransportType.LongPolling;
20842085

@@ -2594,6 +2595,53 @@ public async Task DisposeLongPollingConnectionDisposesServiceScope()
25942595
}
25952596
}
25962597

2598+
private class TestActivityFeature : IHttpActivityFeature
2599+
{
2600+
public TestActivityFeature(Activity activity)
2601+
{
2602+
Activity = activity;
2603+
}
2604+
2605+
public Activity Activity { get; set; }
2606+
}
2607+
2608+
[Fact]
2609+
public async Task LongRunningActivityTagSetOnExecuteAsync()
2610+
{
2611+
using (StartVerifiableLog())
2612+
{
2613+
var manager = CreateConnectionManager(LoggerFactory);
2614+
var connection = manager.CreateConnection();
2615+
connection.TransportType = HttpTransportType.ServerSentEvents;
2616+
2617+
var dispatcher = new HttpConnectionDispatcher(manager, LoggerFactory);
2618+
var services = new ServiceCollection();
2619+
services.AddSingleton<NeverEndingConnectionHandler>();
2620+
var context = MakeRequest("/foo", connection, services);
2621+
var cts = new CancellationTokenSource();
2622+
context.RequestAborted = cts.Token;
2623+
SetTransport(context, HttpTransportType.ServerSentEvents);
2624+
2625+
var builder = new ConnectionBuilder(services.BuildServiceProvider());
2626+
builder.UseConnectionHandler<NeverEndingConnectionHandler>();
2627+
var app = builder.Build();
2628+
2629+
var activityFeature = new TestActivityFeature(new Activity("name"));
2630+
activityFeature.Activity.Start();
2631+
context.Features.Set<IHttpActivityFeature>(activityFeature);
2632+
2633+
_ = dispatcher.ExecuteAsync(context, new HttpConnectionDispatcherOptions(), app);
2634+
2635+
Assert.Equal("true", Activity.Current.GetTagItem("http.long_running"));
2636+
2637+
connection.Transport.Output.Complete();
2638+
2639+
await connection.ConnectionClosed.WaitForCancellationAsync().DefaultTimeout();
2640+
2641+
activityFeature.Activity.Dispose();
2642+
}
2643+
}
2644+
25972645
private static async Task CheckTransportSupported(HttpTransportType supportedTransports, HttpTransportType transportType, int status, ILoggerFactory loggerFactory)
25982646
{
25992647
var manager = CreateConnectionManager(loggerFactory);

0 commit comments

Comments
 (0)