Skip to content

Fix SignalR server activity name #57118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/SignalR/server/Core/src/Internal/DefaultHubDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -808,12 +808,12 @@ public override IReadOnlyList<Type> 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<SignalRActivitySource>() is SignalRActivitySource signalRActivitySource
if (serviceProvider.GetService<SignalRServerActivitySource>() 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),
Expand All @@ -824,6 +824,13 @@ public override IReadOnlyList<Type> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Copy link
Member

@BrennanConroy BrennanConroy Aug 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a convention or just a name we've landed on?
Would we have an "InvocationOut" in the future, for example if the server calls a client method?

Edit: I just saw we have InvocationOut on the client side PR so I think my guess was right.


public ActivitySource ActivitySource { get; } = new ActivitySource(Name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public async Task HubMethodInvokesCreateActivities()
// Provided by hosting layer normally
builder.AddSingleton(testSource);
}, LoggerFactory);
var signalrSource = serviceProvider.GetRequiredService<SignalRActivitySource>().ActivitySource;
var signalrSource = serviceProvider.GetRequiredService<SignalRServerActivitySource>().ActivitySource;

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

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

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

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

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

using var listener = new ActivityListener
{
Expand Down Expand Up @@ -348,7 +348,9 @@ private static void AssertHubMethodActivity<THub>(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)
Expand Down
Loading