Skip to content

Commit 67031ad

Browse files
committed
Clean up
1 parent 2117875 commit 67031ad

File tree

3 files changed

+47
-48
lines changed

3 files changed

+47
-48
lines changed

src/Hosting/Hosting/src/Internal/HostingApplicationDiagnostics.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ private void RecordRequestStartMetrics(HttpContext httpContext)
390390
hasDiagnosticListener = false;
391391

392392
var headers = httpContext.Request.Headers;
393-
var activity = ActivityCreator.CreateActivity(
393+
var activity = ActivityCreator.CreateFromRemoteActivity(
394394
_activitySource,
395395
_propagator,
396396
headers,

src/Shared/Diagnostics/ActivityCreator.cs

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,21 @@ internal static class ActivityCreator
1010
/// <summary>
1111
/// Create an activity with details received from a remote source.
1212
/// </summary>
13-
public static Activity? CreateActivity(
13+
public static Activity? CreateFromRemoteActivity(
1414
ActivitySource activitySource,
1515
DistributedContextPropagator propagator,
16-
object? distributedContextCarrier,
16+
object distributedContextCarrier,
1717
DistributedContextPropagator.PropagatorGetterCallback propagatorGetter,
1818
string activityName,
1919
IEnumerable<KeyValuePair<string, object?>>? tags,
2020
IEnumerable<ActivityLink>? links,
2121
bool diagnosticsOrLoggingEnabled)
2222
{
23-
string? requestId = null;
24-
string? traceState = null;
25-
if (distributedContextCarrier != null)
26-
{
27-
propagator.ExtractTraceIdAndState(
28-
distributedContextCarrier,
29-
propagatorGetter,
30-
out requestId,
31-
out traceState);
32-
}
23+
propagator.ExtractTraceIdAndState(
24+
distributedContextCarrier,
25+
propagatorGetter,
26+
out var requestId,
27+
out var traceState);
3328

3429
Activity? activity = null;
3530
if (activitySource.HasListeners())
@@ -93,19 +88,16 @@ internal static class ActivityCreator
9388

9489
// Baggage can be used regardless of whether a distributed trace id was present on the inbound request.
9590
// https://www.w3.org/TR/baggage/#abstract
96-
if (distributedContextCarrier != null)
97-
{
98-
var baggage = propagator.ExtractBaggage(distributedContextCarrier, propagatorGetter);
91+
var baggage = propagator.ExtractBaggage(distributedContextCarrier, propagatorGetter);
9992

100-
// AddBaggage adds items at the beginning of the list, so we need to add them in reverse to keep the same order as the client
101-
// By contract, the propagator has already reversed the order of items so we need not reverse it again
102-
// Order could be important if baggage has two items with the same key (that is allowed by the contract)
103-
if (baggage is not null)
93+
// AddBaggage adds items at the beginning of the list, so we need to add them in reverse to keep the same order as the client
94+
// By contract, the propagator has already reversed the order of items so we need not reverse it again
95+
// Order could be important if baggage has two items with the same key (that is allowed by the contract)
96+
if (baggage is not null)
97+
{
98+
foreach (var baggageItem in baggage)
10499
{
105-
foreach (var baggageItem in baggage)
106-
{
107-
activity.AddBaggage(baggageItem.Key, baggageItem.Value);
108-
}
100+
activity.AddBaggage(baggageItem.Key, baggageItem.Value);
109101
}
110102
}
111103

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

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -848,33 +848,40 @@ public override IReadOnlyList<Type> GetParameterTypes(string methodName)
848848
IEnumerable<KeyValuePair<string, object?>> tags =
849849
[
850850
new("rpc.method", methodName),
851-
new("rpc.system", "signalr"),
852-
new("rpc.service", _fullHubName),
853-
// See https://github.com/dotnet/aspnetcore/blob/027c60168383421750f01e427e4f749d0684bc02/src/Servers/Kestrel/Core/src/Internal/Infrastructure/KestrelMetrics.cs#L308
854-
// And https://github.com/dotnet/aspnetcore/issues/43786
855-
//new("server.address", ...),
856-
];
851+
new("rpc.system", "signalr"),
852+
new("rpc.service", _fullHubName),
853+
// See https://github.com/dotnet/aspnetcore/blob/027c60168383421750f01e427e4f749d0684bc02/src/Servers/Kestrel/Core/src/Internal/Infrastructure/KestrelMetrics.cs#L308
854+
// And https://github.com/dotnet/aspnetcore/issues/43786
855+
//new("server.address", ...),
856+
];
857857
IEnumerable<ActivityLink>? links = (linkedActivity is not null) ? [new ActivityLink(linkedActivity.Context)] : null;
858858

859-
var propagator = serviceProvider.GetService<DistributedContextPropagator>() ?? DistributedContextPropagator.Current;
859+
var propagator = serviceProvider.GetService<DistributedContextPropagator>() ?? DistributedContextPropagator.Current;
860860

861-
var activity = ActivityCreator.CreateActivity(
862-
activitySource,
863-
propagator,
864-
headers,
865-
static (object? carrier, string fieldName, out string? fieldValue, out IEnumerable<string>? fieldValues) =>
866-
{
867-
fieldValues = default;
868-
var headers = (IDictionary<string, string>)carrier!;
869-
headers.TryGetValue(fieldName, out fieldValue);
870-
},
871-
name,
872-
tags,
873-
links,
874-
loggingEnabled);
875-
876-
Debug.Assert(activity != null, "Activity should always be created.");
877-
activity.Start();
861+
Activity? activity;
862+
if (headers != null)
863+
{
864+
activity = ActivityCreator.CreateFromRemoteActivity(
865+
activitySource,
866+
propagator,
867+
headers,
868+
static (object? carrier, string fieldName, out string? fieldValue, out IEnumerable<string>? fieldValues) =>
869+
{
870+
fieldValues = default;
871+
var headers = (IDictionary<string, string>)carrier!;
872+
headers.TryGetValue(fieldName, out fieldValue);
873+
},
874+
name,
875+
tags,
876+
links,
877+
loggingEnabled);
878+
}
879+
else
880+
{
881+
activity = activitySource.CreateActivity(name, ActivityKind.Server, parentId: null, tags: tags, links: links);
882+
}
883+
884+
activity?.Start();
878885

879886
return activity;
880887
}

0 commit comments

Comments
 (0)