Skip to content

Commit efd2aee

Browse files
committed
more
1 parent b3f3857 commit efd2aee

File tree

6 files changed

+61
-43
lines changed

6 files changed

+61
-43
lines changed

src/Components/Components/src/ComponentsActivitySource.cs

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ internal class ComponentsActivitySource
2020
internal const string OnRouteName = $"{Name}.RouteChange";
2121
internal const string OnEventName = $"{Name}.HandleEvent";
2222

23-
internal ActivityContext _httpContext;
24-
internal ActivityContext _circuitContext;
23+
internal ActivityContext _httpActivityContext;
24+
internal ActivityContext _routeContext;
25+
internal ActivityContext _circuitActivityContext;
2526
internal string? _circuitId;
2627

27-
private ActivityContext _routeContext;
2828

2929
private ActivitySource ActivitySource { get; } = new ActivitySource(Name);
3030

@@ -47,14 +47,6 @@ public ComponentsActivityWrapper StartRouteActivity(string componentType, string
4747
{
4848
activity.SetTag("aspnetcore.components.route", route);
4949
}
50-
if (_httpContext != default)
51-
{
52-
activity.AddLink(new ActivityLink(_httpContext));
53-
}
54-
if (_circuitContext != default)
55-
{
56-
activity.AddLink(new ActivityLink(_circuitContext));
57-
}
5850
}
5951

6052
activity.DisplayName = $"Route {route ?? "[unknown path]"} -> {componentType ?? "[unknown component]"}";
@@ -90,18 +82,6 @@ public ComponentsActivityWrapper StartEventActivity(string? componentType, strin
9082
{
9183
activity.SetTag("aspnetcore.components.attribute.name", attributeName);
9284
}
93-
if (_httpContext != default)
94-
{
95-
activity.AddLink(new ActivityLink(_httpContext));
96-
}
97-
if (_circuitContext != default)
98-
{
99-
activity.AddLink(new ActivityLink(_circuitContext));
100-
}
101-
if (_routeContext != default)
102-
{
103-
activity.AddLink(new ActivityLink(_routeContext));
104-
}
10585
}
10686

10787
activity.DisplayName = $"Event {attributeName ?? "[unknown attribute]"} -> {componentType ?? "[unknown component]"}.{methodName ?? "[unknown method]"}";
@@ -113,16 +93,36 @@ public ComponentsActivityWrapper StartEventActivity(string? componentType, strin
11393
return default;
11494
}
11595

116-
public static void StopComponentActivity(ComponentsActivityWrapper wrapper, Exception? ex)
96+
public void StopComponentActivity(ComponentsActivityWrapper wrapper, Exception? ex)
11797
{
118-
if (wrapper.Activity != null && !wrapper.Activity.IsStopped)
98+
var activity = wrapper.Activity;
99+
if (activity != null && !activity.IsStopped)
119100
{
101+
if (activity.IsAllDataRequested)
102+
{
103+
if (_circuitId != null)
104+
{
105+
activity.SetTag("aspnetcore.components.circuit.id", _circuitId);
106+
}
107+
if (_httpActivityContext != default)
108+
{
109+
activity.AddLink(new ActivityLink(_httpActivityContext));
110+
}
111+
if (_circuitActivityContext != default)
112+
{
113+
activity.AddLink(new ActivityLink(_circuitActivityContext));
114+
}
115+
if (_routeContext != default && activity.Context != _routeContext)
116+
{
117+
activity.AddLink(new ActivityLink(_routeContext));
118+
}
119+
}
120120
if (ex != null)
121121
{
122-
wrapper.Activity.SetTag("error.type", ex.GetType().FullName);
123-
wrapper.Activity.SetStatus(ActivityStatusCode.Error);
122+
activity.SetTag("error.type", ex.GetType().FullName);
123+
activity.SetStatus(ActivityStatusCode.Error);
124124
}
125-
wrapper.Activity.Stop();
125+
activity.Stop();
126126

127127
if (Activity.Current == null && wrapper.Previous != null && !wrapper.Previous.IsStopped)
128128
{
@@ -131,7 +131,7 @@ public static void StopComponentActivity(ComponentsActivityWrapper wrapper, Exce
131131
}
132132
}
133133

134-
public static async Task CaptureEventStopAsync(Task task, ComponentsActivityWrapper wrapper)
134+
public async Task CaptureEventStopAsync(Task task, ComponentsActivityWrapper wrapper)
135135
{
136136
try
137137
{

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ public Renderer(IServiceProvider serviceProvider, ILoggerFactory loggerFactory,
9696
_componentFactory = new ComponentFactory(componentActivator, this);
9797
_componentsMetrics = serviceProvider.GetService<ComponentsMetrics>();
9898
_componentsActivitySource = serviceProvider.GetService<ComponentsActivitySource>();
99+
99100
ServiceProviderCascadingValueSuppliers = serviceProvider.GetService<ICascadingValueSupplier>() is null
100101
? Array.Empty<ICascadingValueSupplier>()
101102
: serviceProvider.GetServices<ICascadingValueSupplier>().ToArray();
@@ -114,14 +115,16 @@ private static IComponentActivator GetComponentActivatorOrDefault(IServiceProvid
114115
?? new DefaultComponentActivator(serviceProvider);
115116
}
116117

117-
internal void SetCircuitActivityContext(ActivityContext httpContext, ActivityContext circuitContext, string circuitId)
118+
internal ActivityContext LinkActivityContexts(ActivityContext httpActivityContext, ActivityContext circuitActivityContext, string? circuitId)
118119
{
119120
if (ComponentActivitySource != null)
120121
{
121-
ComponentActivitySource._httpContext = httpContext;
122-
ComponentActivitySource._circuitContext = circuitContext;
122+
ComponentActivitySource._httpActivityContext = httpActivityContext;
123+
ComponentActivitySource._circuitActivityContext = circuitActivityContext;
123124
ComponentActivitySource._circuitId = circuitId;
125+
return ComponentActivitySource._routeContext;
124126
}
127+
return default;
125128
}
126129

127130
/// <summary>
@@ -521,7 +524,7 @@ public virtual Task DispatchEventAsync(ulong eventHandlerId, EventFieldInfo? fie
521524
// stop activity/trace
522525
if (ComponentActivitySource != null && wrapper.Activity != null)
523526
{
524-
_ = ComponentsActivitySource.CaptureEventStopAsync(task, wrapper);
527+
_ = ComponentActivitySource.CaptureEventStopAsync(task, wrapper);
525528
}
526529
}
527530
catch (Exception e)
@@ -535,7 +538,7 @@ public virtual Task DispatchEventAsync(ulong eventHandlerId, EventFieldInfo? fie
535538

536539
if (ComponentActivitySource != null && wrapper.Activity != null)
537540
{
538-
ComponentsActivitySource.StopComponentActivity(wrapper, e);
541+
ComponentActivitySource.StopComponentActivity(wrapper, e);
539542
}
540543

541544
HandleExceptionViaErrorBoundary(e, receiverComponentState);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ internal virtual void Refresh(bool isNavigationIntercepted)
240240
endpointRouteData = RouteTable.ProcessParameters(endpointRouteData);
241241
_renderHandle.Render(Found(endpointRouteData));
242242

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

@@ -295,7 +295,7 @@ internal virtual void Refresh(bool isNavigationIntercepted)
295295
NavigationManager.NavigateTo(_locationAbsolute, forceLoad: true);
296296
}
297297
}
298-
ComponentsActivitySource.StopComponentActivity(activityWrapper, null);
298+
_renderHandle.ComponentActivitySource?.StopComponentActivity(activityWrapper, null);
299299
}
300300

301301
private ComponentsActivityWrapper RecordDiagnostics(string componentType, string template)

src/Components/Components/test/ComponentsActivitySourceTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public void FailEventActivity_SetsErrorStatusAndStopsActivity()
100100
var exception = new InvalidOperationException("Test exception");
101101

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

105105
// Assert
106106
Assert.True(activity!.IsStopped);
@@ -118,7 +118,7 @@ public async Task CaptureEventStopAsync_StopsActivityOnSuccessfulTask()
118118
var task = Task.CompletedTask;
119119

120120
// Act
121-
await ComponentsActivitySource.CaptureEventStopAsync(task, wrapper);
121+
await componentsActivitySource.CaptureEventStopAsync(task, wrapper);
122122

123123
// Assert
124124
Assert.True(activity!.IsStopped);
@@ -136,7 +136,7 @@ public async Task CaptureEventStopAsync_FailsActivityOnException()
136136
var task = Task.FromException(exception);
137137

138138
// Act
139-
await ComponentsActivitySource.CaptureEventStopAsync(task, wrapper);
139+
await componentsActivitySource.CaptureEventStopAsync(task, wrapper);
140140

141141
// Assert
142142
Assert.True(activity!.IsStopped);

src/Components/Endpoints/src/RazorComponentEndpointInvoker.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33

44
using System.Buffers;
55
using System.Diagnostics;
6+
using System.Runtime.CompilerServices;
67
using System.Text;
78
using System.Text.Encodings.Web;
89
using Microsoft.AspNetCore.Antiforgery;
910
using Microsoft.AspNetCore.Components.Endpoints.Rendering;
11+
using Microsoft.AspNetCore.Components.RenderTree;
1012
using Microsoft.AspNetCore.Diagnostics;
1113
using Microsoft.AspNetCore.Http;
1214
using Microsoft.AspNetCore.Http.Features;
@@ -41,6 +43,7 @@ private async Task RenderComponentCore(HttpContext context)
4143
var isErrorHandler = context.Features.Get<IExceptionHandlerFeature>() is not null;
4244
var hasStatusCodePage = context.Features.Get<IStatusCodePagesFeature>() is not null;
4345
var isReExecuted = context.Features.Get<IStatusCodeReExecuteFeature>() is not null;
46+
var httpActivityContext = context.Features.Get<IHttpActivityFeature>()?.Activity.Context ?? default;
4447
if (isErrorHandler)
4548
{
4649
Log.InteractivityDisabledForErrorHandling(_logger);
@@ -80,6 +83,11 @@ private async Task RenderComponentCore(HttpContext context)
8083
return Task.CompletedTask;
8184
});
8285

86+
if (httpActivityContext != default)
87+
{
88+
LinkActivityContexts(_renderer, default, httpActivityContext, null);
89+
}
90+
8391
await _renderer.InitializeStandardComponentServicesAsync(
8492
context,
8593
componentType: pageComponent,
@@ -273,6 +281,9 @@ private async Task<RequestValidationState> ValidateRequestAsync(HttpContext cont
273281
return null;
274282
}
275283

284+
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "LinkActivityContexts")]
285+
static extern ActivityContext LinkActivityContexts(Renderer type, ActivityContext httpContext, ActivityContext circuitContext, string? circuitId);
286+
276287
[DebuggerDisplay($"{{{nameof(GetDebuggerDisplay)}(),nq}}")]
277288
private readonly struct RequestValidationState(bool isValid, bool isPost, string? handlerName)
278289
{

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ internal struct CircuitActivityWrapper
1313

1414
internal class CircuitActivitySource
1515
{
16-
internal const string Name = "Microsoft.AspNetCore.Components";
16+
internal const string Name = "Microsoft.AspNetCore.Components.Server.Circuits";
1717
internal const string OnCircuitName = $"{Name}.CircuitStart";
1818

1919
private ActivitySource ActivitySource { get; } = new ActivitySource(Name);
@@ -41,7 +41,11 @@ public CircuitActivityWrapper StartCircuitActivity(string circuitId, ActivityCon
4141

4242
if (renderer != null)
4343
{
44-
SetCircuitActivityContext(renderer, httpActivityContext, activity.Context, circuitId);
44+
var routeActivityContext = LinkActivityContexts(renderer, httpActivityContext, activity.Context, circuitId);
45+
if (routeActivityContext != default)
46+
{
47+
activity.AddLink(new ActivityLink(routeActivityContext));
48+
}
4549
}
4650
return new CircuitActivityWrapper { Previous = previousActivity, Activity = activity };
4751
}
@@ -66,6 +70,6 @@ public static void StopCircuitActivity(CircuitActivityWrapper wrapper, Exception
6670
}
6771
}
6872

69-
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "SetCircuitActivityContext")]
70-
static extern void SetCircuitActivityContext(Renderer type, ActivityContext httpContext, ActivityContext circuitContext, string circuitId);
73+
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "LinkActivityContexts")]
74+
static extern ActivityContext LinkActivityContexts(Renderer type, ActivityContext httpContext, ActivityContext circuitContext, string? circuitId);
7175
}

0 commit comments

Comments
 (0)