Skip to content

Commit 26ee437

Browse files
authored
Fix SignalR server activity name (#57118)
1 parent 0e4ccd1 commit 26ee437

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

src/SignalR/server/Core/src/Internal/DefaultHubDispatcher.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -808,12 +808,12 @@ public override IReadOnlyList<Type> GetParameterTypes(string methodName)
808808
// Make sure to call Activity.Stop() once the Hub method completes, and consider calling SetActivityError on exception.
809809
private static Activity? StartActivity(HubConnectionContext connectionContext, IServiceProvider serviceProvider, string methodName)
810810
{
811-
if (serviceProvider.GetService<SignalRActivitySource>() is SignalRActivitySource signalRActivitySource
811+
if (serviceProvider.GetService<SignalRServerActivitySource>() is SignalRServerActivitySource signalRActivitySource
812812
&& signalRActivitySource.ActivitySource.HasListeners())
813813
{
814814
var requestContext = connectionContext.OriginalActivity?.Context;
815815

816-
return signalRActivitySource.ActivitySource.StartActivity($"{_fullHubName}/{methodName}", ActivityKind.Server, parentId: null,
816+
var activity = signalRActivitySource.ActivitySource.CreateActivity(SignalRServerActivitySource.InvocationIn, ActivityKind.Server, parentId: null,
817817
// https://github.com/open-telemetry/semantic-conventions/blob/main/docs/rpc/rpc-spans.md#server-attributes
818818
tags: [
819819
new("rpc.method", methodName),
@@ -824,6 +824,13 @@ public override IReadOnlyList<Type> GetParameterTypes(string methodName)
824824
//new("server.address", ...),
825825
],
826826
links: requestContext.HasValue ? [new ActivityLink(requestContext.Value)] : null);
827+
if (activity != null)
828+
{
829+
activity.DisplayName = $"{_fullHubName}/{methodName}";
830+
activity.Start();
831+
}
832+
833+
return activity;
827834
}
828835

829836
return null;

src/SignalR/server/Core/src/Internal/SignalRActivitySource.cs renamed to src/SignalR/server/Core/src/Internal/SignalRServerActivitySource.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ namespace Microsoft.AspNetCore.SignalR.Internal;
88
// Internal for now so we don't need API review.
99
// Just a wrapper for the ActivitySource
1010
// don't want to put ActivitySource directly in DI as hosting already does that and it could get overwritten.
11-
internal sealed class SignalRActivitySource
11+
internal sealed class SignalRServerActivitySource
1212
{
13-
public ActivitySource ActivitySource { get; } = new ActivitySource("Microsoft.AspNetCore.SignalR.Server");
13+
internal const string Name = "Microsoft.AspNetCore.SignalR.Server";
14+
internal const string InvocationIn = $"{Name}.InvocationIn";
15+
16+
public ActivitySource ActivitySource { get; } = new ActivitySource(Name);
1417
}

src/SignalR/server/Core/src/SignalRDependencyInjectionExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static ISignalRServerBuilder AddSignalRCore(this IServiceCollection servi
3232
services.TryAddScoped(typeof(IHubActivator<>), typeof(DefaultHubActivator<>));
3333
services.AddAuthorization();
3434

35-
services.TryAddSingleton(new SignalRActivitySource());
35+
services.TryAddSingleton(new SignalRServerActivitySource());
3636

3737
var builder = new SignalRServerBuilder(services);
3838
builder.AddJsonProtocol();

src/SignalR/server/SignalR/test/Microsoft.AspNetCore.SignalR.Tests/HubConnectionHandlerTests.Activity.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public async Task HubMethodInvokesCreateActivities()
2929
// Provided by hosting layer normally
3030
builder.AddSingleton(testSource);
3131
}, LoggerFactory);
32-
var signalrSource = serviceProvider.GetRequiredService<SignalRActivitySource>().ActivitySource;
32+
var signalrSource = serviceProvider.GetRequiredService<SignalRServerActivitySource>().ActivitySource;
3333

3434
using var listener = new ActivityListener
3535
{
@@ -100,7 +100,7 @@ public async Task StreamingHubMethodCreatesActivities()
100100
// Provided by hosting layer normally
101101
builder.AddSingleton(testSource);
102102
}, LoggerFactory);
103-
var signalrSource = serviceProvider.GetRequiredService<SignalRActivitySource>().ActivitySource;
103+
var signalrSource = serviceProvider.GetRequiredService<SignalRServerActivitySource>().ActivitySource;
104104

105105
using var listener = new ActivityListener
106106
{
@@ -165,7 +165,7 @@ bool ExpectedErrors(WriteContext writeContext)
165165
// Provided by hosting layer normally
166166
builder.AddSingleton(testSource);
167167
}, LoggerFactory);
168-
var signalrSource = serviceProvider.GetRequiredService<SignalRActivitySource>().ActivitySource;
168+
var signalrSource = serviceProvider.GetRequiredService<SignalRServerActivitySource>().ActivitySource;
169169

170170
using var listener = new ActivityListener
171171
{
@@ -212,7 +212,7 @@ bool ExpectedErrors(WriteContext writeContext)
212212
// Provided by hosting layer normally
213213
builder.AddSingleton(testSource);
214214
}, LoggerFactory);
215-
var signalrSource = serviceProvider.GetRequiredService<SignalRActivitySource>().ActivitySource;
215+
var signalrSource = serviceProvider.GetRequiredService<SignalRServerActivitySource>().ActivitySource;
216216

217217
using var listener = new ActivityListener
218218
{
@@ -263,7 +263,7 @@ bool ExpectedErrors(WriteContext writeContext)
263263
// Provided by hosting layer normally
264264
builder.AddSingleton(testSource);
265265
}, LoggerFactory);
266-
var signalrSource = serviceProvider.GetRequiredService<SignalRActivitySource>().ActivitySource;
266+
var signalrSource = serviceProvider.GetRequiredService<SignalRServerActivitySource>().ActivitySource;
267267

268268
using var listener = new ActivityListener
269269
{
@@ -313,7 +313,7 @@ bool ExpectedErrors(WriteContext writeContext)
313313
// Provided by hosting layer normally
314314
builder.AddSingleton(testSource);
315315
}, LoggerFactory);
316-
var signalrSource = serviceProvider.GetRequiredService<SignalRActivitySource>().ActivitySource;
316+
var signalrSource = serviceProvider.GetRequiredService<SignalRServerActivitySource>().ActivitySource;
317317

318318
using var listener = new ActivityListener
319319
{
@@ -348,7 +348,9 @@ private static void AssertHubMethodActivity<THub>(Activity activity, string meth
348348
{
349349
Assert.Null(activity.Parent);
350350
Assert.True(activity.IsStopped);
351-
Assert.Equal($"{typeof(THub).FullName}/{methodName}", activity.OperationName);
351+
Assert.Equal(SignalRServerActivitySource.Name, activity.Source.Name);
352+
Assert.Equal(SignalRServerActivitySource.InvocationIn, activity.OperationName);
353+
Assert.Equal($"{typeof(THub).FullName}/{methodName}", activity.DisplayName);
352354

353355
var tags = activity.Tags.ToArray();
354356
if (exceptionType is not null)

0 commit comments

Comments
 (0)