Skip to content

Commit a7ca7c0

Browse files
Copilotjaviercn
andcommitted
Implement Activity linking from Renderer to Components
Co-authored-by: javiercn <6995051+javiercn@users.noreply.github.com>
1 parent 45bf8bc commit a7ca7c0

File tree

4 files changed

+21
-27
lines changed

4 files changed

+21
-27
lines changed

src/Components/Components/src/ComponentsActivitySource.cs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ internal class ComponentsActivitySource
1414
internal const string OnRouteName = $"{Name}.RouteChange";
1515
internal const string OnEventName = $"{Name}.HandleEvent";
1616

17-
private ActivityContext _httpContext;
1817
private ActivityContext _routeContext;
1918
private Activity? _capturedActivity;
2019

@@ -27,20 +26,6 @@ internal class ComponentsActivitySource
2726
public void Initialize(Activity? capturedActivity)
2827
{
2928
_capturedActivity = capturedActivity;
30-
_httpContext = CaptureHttpContext();
31-
}
32-
33-
/// <summary>
34-
/// Captures the current HTTP context activity.
35-
/// </summary>
36-
public static ActivityContext CaptureHttpContext()
37-
{
38-
var parentActivity = Activity.Current;
39-
if (parentActivity is not null && parentActivity.OperationName == "Microsoft.AspNetCore.Hosting.HttpRequestIn" && parentActivity.Recorded)
40-
{
41-
return parentActivity.Context;
42-
}
43-
return default;
4429
}
4530

4631
public Activity? StartRouteActivity(string componentType, string route)
@@ -64,10 +49,6 @@ public static ActivityContext CaptureHttpContext()
6449
{
6550
activity.SetTag("aspnetcore.components.route", route);
6651
}
67-
if (_httpContext != default)
68-
{
69-
activity.AddLink(new ActivityLink(_httpContext));
70-
}
7152
if (_capturedActivity != null)
7253
{
7354
activity.AddLink(new ActivityLink(_capturedActivity.Context));
@@ -106,10 +87,6 @@ public static ActivityContext CaptureHttpContext()
10687
{
10788
activity.SetTag("aspnetcore.components.attribute.name", attributeName);
10889
}
109-
if (_httpContext != default)
110-
{
111-
activity.AddLink(new ActivityLink(_httpContext));
112-
}
11390
if (_capturedActivity != null)
11491
{
11592
activity.AddLink(new ActivityLink(_capturedActivity.Context));

src/Components/Server/src/Circuits/CircuitFactory.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ public async ValueTask<CircuitHost> CreateCircuitHostAsync(
7070
}
7171
var componentsActivitySource = scope.ServiceProvider.GetService<ComponentsActivitySource>();
7272

73-
// Initialize the components activity source with the circuit activity if available
74-
componentsActivitySource?.Initialize(circuitActivity);
73+
// We don't need to explicitly initialize the ComponentsActivitySource here anymore
74+
// since the RemoteRenderer will capture Activity.Current and initialize it
7575

7676
var circuitActivitySource = scope.ServiceProvider.GetService<CircuitActivitySource>();
7777

src/Components/Server/src/Circuits/RemoteRenderer.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Collections.Concurrent;
5+
using System.Diagnostics;
56
using System.Diagnostics.CodeAnalysis;
67
using System.Linq;
78
using Microsoft.AspNetCore.Components.RenderTree;
@@ -28,6 +29,7 @@ internal partial class RemoteRenderer : WebRenderer
2829
internal readonly ConcurrentQueue<UnacknowledgedRenderBatch> _unacknowledgedRenderBatches = new ConcurrentQueue<UnacknowledgedRenderBatch>();
2930
private long _nextRenderId = 1;
3031
private bool _disposing;
32+
private readonly Activity? _capturedActivity;
3133

3234
/// <summary>
3335
/// Notifies when a rendering exception occurred.
@@ -54,6 +56,11 @@ public RemoteRenderer(
5456
_serverComponentDeserializer = serverComponentDeserializer;
5557
_logger = logger;
5658
_resourceCollection = resourceCollection;
59+
_capturedActivity = Activity.Current; // Capture the current activity
60+
61+
// Initialize ComponentsActivitySource with the captured activity
62+
var componentsActivitySource = serviceProvider.GetService<ComponentsActivitySource>();
63+
componentsActivitySource?.Initialize(_capturedActivity);
5764

5865
ElementReferenceContext = jsRuntime.ElementReferenceContext;
5966
}
@@ -369,7 +376,7 @@ private async Task CaptureAsyncExceptions(Task task)
369376
}
370377
}
371378

372-
private static new partial class Log
379+
private static partial class Log
373380
{
374381
[LoggerMessage(100, LogLevel.Warning, "Unhandled exception rendering component: {Message}", EventName = "ExceptionRenderingComponent")]
375382
private static partial void UnhandledExceptionRenderingComponent(ILogger logger, string message, Exception exception);

src/Components/Server/src/ComponentHub.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public ComponentHub(
6565
_circuitHandleRegistry = circuitHandleRegistry;
6666
_circuitActivitySource = circuitActivitySource;
6767
_logger = logger;
68-
_httpContext = CircuitActivitySource.CaptureHttpContext();
68+
_httpContext = CaptureHttpContext();
6969
}
7070

7171
/// <summary>
@@ -430,4 +430,14 @@ public static void InvalidCircuitId(ILogger logger, string circuitSecret)
430430
InvalidCircuitIdCore(logger, circuitSecret);
431431
}
432432
}
433+
434+
private static ActivityContext CaptureHttpContext()
435+
{
436+
var parentActivity = Activity.Current;
437+
if (parentActivity is not null && parentActivity.OperationName == "Microsoft.AspNetCore.Hosting.HttpRequestIn" && parentActivity.Recorded)
438+
{
439+
return parentActivity.Context;
440+
}
441+
return default;
442+
}
433443
}

0 commit comments

Comments
 (0)