Skip to content

Commit 768834e

Browse files
committed
feedback
1 parent f030f48 commit 768834e

File tree

7 files changed

+53
-47
lines changed

7 files changed

+53
-47
lines changed

src/Components/Components/src/ComponentsActivitySource.cs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55

66
namespace Microsoft.AspNetCore.Components;
77

8-
internal struct ComponentsActivityWrapper
8+
/// <summary>
9+
/// Named tuple for restoring the previous activity after stopping the current one.
10+
/// </summary>
11+
internal struct ComponentsActivityHandle
912
{
1013
public Activity? Previous;
1114
public Activity? Activity;
@@ -27,7 +30,7 @@ internal class ComponentsActivitySource
2730

2831
private ActivitySource ActivitySource { get; } = new ActivitySource(Name);
2932

30-
public ComponentsActivityWrapper StartRouteActivity(string componentType, string route)
33+
public ComponentsActivityHandle StartRouteActivity(string componentType, string route)
3134
{
3235
var activity = ActivitySource.CreateActivity(OnRouteName, ActivityKind.Internal, parentId: null, null, null);
3336
if (activity is not null)
@@ -57,12 +60,12 @@ public ComponentsActivityWrapper StartRouteActivity(string componentType, string
5760
Activity.Current = null; // do not inherit the parent activity
5861
activity.Start();
5962
_routeContext = activity.Context;
60-
return new ComponentsActivityWrapper { Activity = activity, Previous = previousActivity };
63+
return new ComponentsActivityHandle { Activity = activity, Previous = previousActivity };
6164
}
6265
return default;
6366
}
6467

65-
public ComponentsActivityWrapper StartEventActivity(string? componentType, string? methodName, string? attributeName)
68+
public ComponentsActivityHandle StartEventActivity(string? componentType, string? methodName, string? attributeName)
6669
{
6770
var activity = ActivitySource.CreateActivity(OnEventName, ActivityKind.Internal, parentId: null, null, null);
6871
if (activity is not null)
@@ -91,14 +94,14 @@ public ComponentsActivityWrapper StartEventActivity(string? componentType, strin
9194
var previousActivity = Activity.Current;
9295
Activity.Current = null; // do not inherit the parent activity
9396
activity.Start();
94-
return new ComponentsActivityWrapper { Activity = activity, Previous = previousActivity };
97+
return new ComponentsActivityHandle { Activity = activity, Previous = previousActivity };
9598
}
9699
return default;
97100
}
98101

99-
public void StopComponentActivity(ComponentsActivityWrapper wrapper, Exception? ex)
102+
public void StopComponentActivity(ComponentsActivityHandle activityHandle, Exception? ex)
100103
{
101-
var activity = wrapper.Activity;
104+
var activity = activityHandle.Activity;
102105
if (activity != null && !activity.IsStopped)
103106
{
104107
if (activity.IsAllDataRequested)
@@ -127,23 +130,23 @@ public void StopComponentActivity(ComponentsActivityWrapper wrapper, Exception?
127130
}
128131
activity.Stop();
129132

130-
if (Activity.Current == null && wrapper.Previous != null && !wrapper.Previous.IsStopped)
133+
if (Activity.Current == null && activityHandle.Previous != null && !activityHandle.Previous.IsStopped)
131134
{
132-
Activity.Current = wrapper.Previous;
135+
Activity.Current = activityHandle.Previous;
133136
}
134137
}
135138
}
136139

137-
public async Task CaptureEventStopAsync(Task task, ComponentsActivityWrapper wrapper)
140+
public async Task CaptureEventStopAsync(Task task, ComponentsActivityHandle activityHandle)
138141
{
139142
try
140143
{
141144
await task;
142-
StopComponentActivity(wrapper, null);
145+
StopComponentActivity(activityHandle, null);
143146
}
144147
catch (Exception ex)
145148
{
146-
StopComponentActivity(wrapper, ex);
149+
StopComponentActivity(activityHandle, ex);
147150
}
148151
}
149152
}

src/Components/Components/src/RenderTree/Renderer.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -460,14 +460,14 @@ public virtual Task DispatchEventAsync(ulong eventHandlerId, EventFieldInfo? fie
460460
var (renderedByComponentId, callback, attributeName) = GetRequiredEventBindingEntry(eventHandlerId);
461461

462462
// collect trace
463-
ComponentsActivityWrapper wrapper = default;
463+
ComponentsActivityHandle activityHandle = default;
464464
string receiverName = null;
465465
string methodName = null;
466466
if (ComponentActivitySource != null)
467467
{
468468
receiverName ??= (callback.Receiver?.GetType() ?? callback.Delegate.Target?.GetType())?.FullName;
469469
methodName ??= callback.Delegate.Method?.Name;
470-
wrapper = ComponentActivitySource.StartEventActivity(receiverName, methodName, attributeName);
470+
activityHandle = ComponentActivitySource.StartEventActivity(receiverName, methodName, attributeName);
471471
}
472472

473473
var eventStartTimestamp = ComponentMetrics != null && ComponentMetrics.IsEventEnabled ? Stopwatch.GetTimestamp() : 0;
@@ -522,9 +522,9 @@ public virtual Task DispatchEventAsync(ulong eventHandlerId, EventFieldInfo? fie
522522
}
523523

524524
// stop activity/trace
525-
if (ComponentActivitySource != null && wrapper.Activity != null)
525+
if (ComponentActivitySource != null && activityHandle.Activity != null)
526526
{
527-
_ = ComponentActivitySource.CaptureEventStopAsync(task, wrapper);
527+
_ = ComponentActivitySource.CaptureEventStopAsync(task, activityHandle);
528528
}
529529
}
530530
catch (Exception e)
@@ -536,9 +536,9 @@ public virtual Task DispatchEventAsync(ulong eventHandlerId, EventFieldInfo? fie
536536
ComponentMetrics.FailEventSync(e, eventStartTimestamp, receiverName, methodName, attributeName);
537537
}
538538

539-
if (ComponentActivitySource != null && wrapper.Activity != null)
539+
if (ComponentActivitySource != null && activityHandle.Activity != null)
540540
{
541-
ComponentActivitySource.StopComponentActivity(wrapper, e);
541+
ComponentActivitySource.StopComponentActivity(activityHandle, e);
542542
}
543543

544544
HandleExceptionViaErrorBoundary(e, receiverComponentState);

src/Components/Components/src/Routing/Router.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -222,12 +222,12 @@ internal virtual void Refresh(bool isNavigationIntercepted)
222222
var relativePath = NavigationManager.ToBaseRelativePath(_locationAbsolute.AsSpan());
223223
var locationPathSpan = TrimQueryOrHash(relativePath);
224224
var locationPath = $"/{locationPathSpan}";
225-
ComponentsActivityWrapper activityWrapper;
225+
ComponentsActivityHandle activityHandle;
226226

227227
// In order to avoid routing twice we check for RouteData
228228
if (RoutingStateProvider?.RouteData is { } endpointRouteData)
229229
{
230-
activityWrapper = RecordDiagnostics(endpointRouteData.PageType.FullName, endpointRouteData.Template);
230+
activityHandle = RecordDiagnostics(endpointRouteData.PageType.FullName, endpointRouteData.Template);
231231

232232
// Other routers shouldn't provide RouteData, this is specific to our router component
233233
// and must abide by our syntax and behaviors.
@@ -240,7 +240,7 @@ internal virtual void Refresh(bool isNavigationIntercepted)
240240
endpointRouteData = RouteTable.ProcessParameters(endpointRouteData);
241241
_renderHandle.Render(Found(endpointRouteData));
242242

243-
_renderHandle.ComponentActivitySource?.StopComponentActivity(activityWrapper, null);
243+
_renderHandle.ComponentActivitySource?.StopComponentActivity(activityHandle, null);
244244
return;
245245
}
246246

@@ -257,7 +257,7 @@ internal virtual void Refresh(bool isNavigationIntercepted)
257257
$"does not implement {typeof(IComponent).FullName}.");
258258
}
259259

260-
activityWrapper = RecordDiagnostics(context.Handler.FullName, context.Entry.RoutePattern.RawText);
260+
activityHandle = RecordDiagnostics(context.Handler.FullName, context.Entry.RoutePattern.RawText);
261261

262262
Log.NavigatingToComponent(_logger, context.Handler, locationPath, _baseUri);
263263

@@ -278,7 +278,7 @@ internal virtual void Refresh(bool isNavigationIntercepted)
278278
{
279279
if (!isNavigationIntercepted)
280280
{
281-
activityWrapper = RecordDiagnostics("NotFound", "NotFound");
281+
activityHandle = RecordDiagnostics("NotFound", "NotFound");
282282

283283
Log.DisplayingNotFound(_logger, locationPath, _baseUri);
284284

@@ -289,18 +289,18 @@ internal virtual void Refresh(bool isNavigationIntercepted)
289289
}
290290
else
291291
{
292-
activityWrapper = RecordDiagnostics("External", "External");
292+
activityHandle = RecordDiagnostics("External", "External");
293293

294294
Log.NavigatingToExternalUri(_logger, _locationAbsolute, locationPath, _baseUri);
295295
NavigationManager.NavigateTo(_locationAbsolute, forceLoad: true);
296296
}
297297
}
298-
_renderHandle.ComponentActivitySource?.StopComponentActivity(activityWrapper, null);
298+
_renderHandle.ComponentActivitySource?.StopComponentActivity(activityHandle, null);
299299
}
300300

301-
private ComponentsActivityWrapper RecordDiagnostics(string componentType, string template)
301+
private ComponentsActivityHandle RecordDiagnostics(string componentType, string template)
302302
{
303-
ComponentsActivityWrapper activityWrapper = default;
303+
ComponentsActivityHandle activityWrapper = default;
304304
if (_renderHandle.ComponentActivitySource != null)
305305
{
306306
activityWrapper = _renderHandle.ComponentActivitySource.StartRouteActivity(componentType, template);

src/Components/Components/test/ComponentsActivitySourceTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,12 @@ public void FailEventActivity_SetsErrorStatusAndStopsActivity()
9595
{
9696
// Arrange
9797
var componentsActivitySource = new ComponentsActivitySource();
98-
var wrapper = componentsActivitySource.StartEventActivity("TestComponent", "OnClick", "onclick");
99-
var activity = wrapper.Activity;
98+
var activityHandle = componentsActivitySource.StartEventActivity("TestComponent", "OnClick", "onclick");
99+
var activity = activityHandle.Activity;
100100
var exception = new InvalidOperationException("Test exception");
101101

102102
// Act
103-
componentsActivitySource.StopComponentActivity(wrapper, exception);
103+
componentsActivitySource.StopComponentActivity(activityHandle, exception);
104104

105105
// Assert
106106
Assert.True(activity!.IsStopped);

src/Components/Server/src/Circuits/CircuitActivitySource.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
using System.Runtime.CompilerServices;
66
using Microsoft.AspNetCore.Components.RenderTree;
77

8-
internal struct CircuitActivityWrapper
8+
/// <summary>
9+
/// Named tuple for restoring the previous activity after stopping the current one.
10+
/// </summary>
11+
internal struct CircuitActivityHandle
912
{
1013
public Activity? Previous;
1114
public Activity? Activity;
@@ -18,7 +21,7 @@ internal class CircuitActivitySource
1821

1922
private ActivitySource ActivitySource { get; } = new ActivitySource(Name);
2023

21-
public CircuitActivityWrapper StartCircuitActivity(string circuitId, ActivityContext httpActivityContext, Renderer? renderer)
24+
public CircuitActivityHandle StartCircuitActivity(string circuitId, ActivityContext httpActivityContext, Renderer? renderer)
2225
{
2326
var activity = ActivitySource.CreateActivity(OnCircuitName, ActivityKind.Internal, parentId:null, null, null);
2427
if (activity is not null)
@@ -52,25 +55,25 @@ public CircuitActivityWrapper StartCircuitActivity(string circuitId, ActivityCon
5255
activity.AddLink(new ActivityLink(routeActivityContext));
5356
}
5457
}
55-
return new CircuitActivityWrapper { Previous = previousActivity, Activity = activity };
58+
return new CircuitActivityHandle { Previous = previousActivity, Activity = activity };
5659
}
5760
return default;
5861
}
5962

60-
public static void StopCircuitActivity(CircuitActivityWrapper wrapper, Exception? ex)
63+
public static void StopCircuitActivity(CircuitActivityHandle activityHandle, Exception? ex)
6164
{
62-
if (wrapper.Activity != null && !wrapper.Activity.IsStopped)
65+
if (activityHandle.Activity != null && !activityHandle.Activity.IsStopped)
6366
{
6467
if (ex != null)
6568
{
66-
wrapper.Activity.SetTag("error.type", ex.GetType().FullName);
67-
wrapper.Activity.SetStatus(ActivityStatusCode.Error);
69+
activityHandle.Activity.SetTag("error.type", ex.GetType().FullName);
70+
activityHandle.Activity.SetStatus(ActivityStatusCode.Error);
6871
}
69-
wrapper.Activity.Stop();
72+
activityHandle.Activity.Stop();
7073

71-
if (Activity.Current == null && wrapper.Previous != null && !wrapper.Previous.IsStopped)
74+
if (Activity.Current == null && activityHandle.Previous != null && !activityHandle.Previous.IsStopped)
7275
{
73-
Activity.Current = wrapper.Previous;
76+
Activity.Current = activityHandle.Previous;
7477
}
7578
}
7679
}

src/Components/Server/src/Circuits/CircuitHost.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,13 @@ public Task InitializeAsync(ProtectedPrerenderComponentApplicationStore store, A
119119
throw new InvalidOperationException("The circuit host is already initialized.");
120120
}
121121

122-
CircuitActivityWrapper circuitActivity = default;
122+
CircuitActivityHandle activityHandle = default;
123123

124124
try
125125
{
126126
_initialized = true; // We're ready to accept incoming JSInterop calls from here on
127127

128-
circuitActivity = _circuitActivitySource.StartCircuitActivity(CircuitId.Id, httpActivityContext, Renderer);
128+
activityHandle = _circuitActivitySource.StartCircuitActivity(CircuitId.Id, httpActivityContext, Renderer);
129129
_startTime = (_circuitMetrics != null && _circuitMetrics.IsDurationEnabled()) ? Stopwatch.GetTimestamp() : 0;
130130

131131
// We only run the handlers in case we are in a Blazor Server scenario, which renders
@@ -171,11 +171,11 @@ public Task InitializeAsync(ProtectedPrerenderComponentApplicationStore store, A
171171

172172
Log.InitializationSucceeded(_logger);
173173

174-
CircuitActivitySource.StopCircuitActivity(circuitActivity, null);
174+
CircuitActivitySource.StopCircuitActivity(activityHandle, null);
175175
}
176176
catch (Exception ex)
177177
{
178-
CircuitActivitySource.StopCircuitActivity(circuitActivity, ex);
178+
CircuitActivitySource.StopCircuitActivity(activityHandle, ex);
179179

180180
// Report errors asynchronously. InitializeAsync is designed not to throw.
181181
Log.InitializationFailed(_logger, ex);

src/Components/Server/test/Circuits/CircuitActivitySourceTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,12 @@ public void FailCircuitActivity_SetsErrorStatusAndStopsActivity()
6262
var circuitActivitySource = new CircuitActivitySource();
6363
var circuitId = "test-circuit-id";
6464
var httpContext = default(ActivityContext);
65-
var wrapper = circuitActivitySource.StartCircuitActivity(circuitId, httpContext, null);
66-
var activity = wrapper.Activity;
65+
var activityHandle = circuitActivitySource.StartCircuitActivity(circuitId, httpContext, null);
66+
var activity = activityHandle.Activity;
6767
var exception = new InvalidOperationException("Test exception");
6868

6969
// Act
70-
CircuitActivitySource.StopCircuitActivity(wrapper, exception);
70+
CircuitActivitySource.StopCircuitActivity(activityHandle, exception);
7171

7272
// Assert
7373
Assert.True(activity!.IsStopped);

0 commit comments

Comments
 (0)