diff --git a/src/SignalR/server/Core/src/Internal/DefaultHubDispatcher.cs b/src/SignalR/server/Core/src/Internal/DefaultHubDispatcher.cs index ece981d2c489..c511d5ac3765 100644 --- a/src/SignalR/server/Core/src/Internal/DefaultHubDispatcher.cs +++ b/src/SignalR/server/Core/src/Internal/DefaultHubDispatcher.cs @@ -808,12 +808,12 @@ public override IReadOnlyList GetParameterTypes(string methodName) // Make sure to call Activity.Stop() once the Hub method completes, and consider calling SetActivityError on exception. private static Activity? StartActivity(HubConnectionContext connectionContext, IServiceProvider serviceProvider, string methodName) { - if (serviceProvider.GetService() is SignalRActivitySource signalRActivitySource + if (serviceProvider.GetService() is SignalRServerActivitySource signalRActivitySource && signalRActivitySource.ActivitySource.HasListeners()) { var requestContext = connectionContext.OriginalActivity?.Context; - return signalRActivitySource.ActivitySource.StartActivity($"{_fullHubName}/{methodName}", ActivityKind.Server, parentId: null, + var activity = signalRActivitySource.ActivitySource.CreateActivity(SignalRServerActivitySource.InvocationIn, ActivityKind.Server, parentId: null, // https://github.com/open-telemetry/semantic-conventions/blob/main/docs/rpc/rpc-spans.md#server-attributes tags: [ new("rpc.method", methodName), @@ -824,6 +824,13 @@ public override IReadOnlyList GetParameterTypes(string methodName) //new("server.address", ...), ], links: requestContext.HasValue ? [new ActivityLink(requestContext.Value)] : null); + if (activity != null) + { + activity.DisplayName = $"{_fullHubName}/{methodName}"; + activity.Start(); + } + + return activity; } return null; diff --git a/src/SignalR/server/Core/src/Internal/SignalRActivitySource.cs b/src/SignalR/server/Core/src/Internal/SignalRServerActivitySource.cs similarity index 70% rename from src/SignalR/server/Core/src/Internal/SignalRActivitySource.cs rename to src/SignalR/server/Core/src/Internal/SignalRServerActivitySource.cs index d65522521e4a..606f36316911 100644 --- a/src/SignalR/server/Core/src/Internal/SignalRActivitySource.cs +++ b/src/SignalR/server/Core/src/Internal/SignalRServerActivitySource.cs @@ -8,7 +8,10 @@ namespace Microsoft.AspNetCore.SignalR.Internal; // Internal for now so we don't need API review. // Just a wrapper for the ActivitySource // don't want to put ActivitySource directly in DI as hosting already does that and it could get overwritten. -internal sealed class SignalRActivitySource +internal sealed class SignalRServerActivitySource { - public ActivitySource ActivitySource { get; } = new ActivitySource("Microsoft.AspNetCore.SignalR.Server"); + internal const string Name = "Microsoft.AspNetCore.SignalR.Server"; + internal const string InvocationIn = $"{Name}.InvocationIn"; + + public ActivitySource ActivitySource { get; } = new ActivitySource(Name); } diff --git a/src/SignalR/server/Core/src/SignalRDependencyInjectionExtensions.cs b/src/SignalR/server/Core/src/SignalRDependencyInjectionExtensions.cs index 51b5746fd96e..5740fa636224 100644 --- a/src/SignalR/server/Core/src/SignalRDependencyInjectionExtensions.cs +++ b/src/SignalR/server/Core/src/SignalRDependencyInjectionExtensions.cs @@ -32,7 +32,7 @@ public static ISignalRServerBuilder AddSignalRCore(this IServiceCollection servi services.TryAddScoped(typeof(IHubActivator<>), typeof(DefaultHubActivator<>)); services.AddAuthorization(); - services.TryAddSingleton(new SignalRActivitySource()); + services.TryAddSingleton(new SignalRServerActivitySource()); var builder = new SignalRServerBuilder(services); builder.AddJsonProtocol(); diff --git a/src/SignalR/server/SignalR/test/Microsoft.AspNetCore.SignalR.Tests/HubConnectionHandlerTests.Activity.cs b/src/SignalR/server/SignalR/test/Microsoft.AspNetCore.SignalR.Tests/HubConnectionHandlerTests.Activity.cs index d5d175d0f479..acd845ff1a24 100644 --- a/src/SignalR/server/SignalR/test/Microsoft.AspNetCore.SignalR.Tests/HubConnectionHandlerTests.Activity.cs +++ b/src/SignalR/server/SignalR/test/Microsoft.AspNetCore.SignalR.Tests/HubConnectionHandlerTests.Activity.cs @@ -29,7 +29,7 @@ public async Task HubMethodInvokesCreateActivities() // Provided by hosting layer normally builder.AddSingleton(testSource); }, LoggerFactory); - var signalrSource = serviceProvider.GetRequiredService().ActivitySource; + var signalrSource = serviceProvider.GetRequiredService().ActivitySource; using var listener = new ActivityListener { @@ -100,7 +100,7 @@ public async Task StreamingHubMethodCreatesActivities() // Provided by hosting layer normally builder.AddSingleton(testSource); }, LoggerFactory); - var signalrSource = serviceProvider.GetRequiredService().ActivitySource; + var signalrSource = serviceProvider.GetRequiredService().ActivitySource; using var listener = new ActivityListener { @@ -165,7 +165,7 @@ bool ExpectedErrors(WriteContext writeContext) // Provided by hosting layer normally builder.AddSingleton(testSource); }, LoggerFactory); - var signalrSource = serviceProvider.GetRequiredService().ActivitySource; + var signalrSource = serviceProvider.GetRequiredService().ActivitySource; using var listener = new ActivityListener { @@ -212,7 +212,7 @@ bool ExpectedErrors(WriteContext writeContext) // Provided by hosting layer normally builder.AddSingleton(testSource); }, LoggerFactory); - var signalrSource = serviceProvider.GetRequiredService().ActivitySource; + var signalrSource = serviceProvider.GetRequiredService().ActivitySource; using var listener = new ActivityListener { @@ -263,7 +263,7 @@ bool ExpectedErrors(WriteContext writeContext) // Provided by hosting layer normally builder.AddSingleton(testSource); }, LoggerFactory); - var signalrSource = serviceProvider.GetRequiredService().ActivitySource; + var signalrSource = serviceProvider.GetRequiredService().ActivitySource; using var listener = new ActivityListener { @@ -313,7 +313,7 @@ bool ExpectedErrors(WriteContext writeContext) // Provided by hosting layer normally builder.AddSingleton(testSource); }, LoggerFactory); - var signalrSource = serviceProvider.GetRequiredService().ActivitySource; + var signalrSource = serviceProvider.GetRequiredService().ActivitySource; using var listener = new ActivityListener { @@ -348,7 +348,9 @@ private static void AssertHubMethodActivity(Activity activity, string meth { Assert.Null(activity.Parent); Assert.True(activity.IsStopped); - Assert.Equal($"{typeof(THub).FullName}/{methodName}", activity.OperationName); + Assert.Equal(SignalRServerActivitySource.Name, activity.Source.Name); + Assert.Equal(SignalRServerActivitySource.InvocationIn, activity.OperationName); + Assert.Equal($"{typeof(THub).FullName}/{methodName}", activity.DisplayName); var tags = activity.Tags.ToArray(); if (exceptionType is not null)