Skip to content

Blazor - rendering metrics and tracing #61609

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

Open
wants to merge 28 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
cebb68e
rebase
pavelsavara Apr 23, 2025
6664645
Merge branch 'main' into blazor_metrics_feedback
pavelsavara Apr 23, 2025
0bcd459
more
pavelsavara Apr 24, 2025
2557f08
- remove FeatureSwitchDefinition for now
pavelsavara Apr 24, 2025
4c75495
whitespace
pavelsavara Apr 24, 2025
0f3d48a
navigation draft
pavelsavara Apr 24, 2025
cf15c8d
cleanup
pavelsavara Apr 24, 2025
550f633
cleanup
pavelsavara Apr 24, 2025
9ab8a84
more
pavelsavara Apr 25, 2025
0a4d488
IsAllDataRequested
pavelsavara Apr 25, 2025
cf2ab99
- ComponentsActivitySourceTest
pavelsavara Apr 28, 2025
44fa207
Update src/Components/Components/src/ComponentsMetrics.cs
pavelsavara Apr 30, 2025
105b02f
Update src/Components/Components/src/ComponentsMetrics.cs
pavelsavara Apr 30, 2025
63f8a69
Update src/Components/Components/src/ComponentsMetrics.cs
pavelsavara Apr 30, 2025
860931d
feedback
pavelsavara Apr 30, 2025
de22914
fix tests
pavelsavara Apr 30, 2025
5ca497d
update_parameters feedback
pavelsavara Apr 30, 2025
2ae95e0
Update src/Components/Components/src/ComponentsActivitySource.cs
pavelsavara May 2, 2025
b9331d9
Update src/Components/Components/src/ComponentsActivitySource.cs
pavelsavara May 2, 2025
96f1d6a
Update src/Components/Components/src/ComponentsActivitySource.cs
pavelsavara May 2, 2025
1c61f50
feedback
pavelsavara May 2, 2025
bda2aec
ActivityKind.Internal
pavelsavara May 6, 2025
84dbfd2
aspnetcore.components prefix for tags
pavelsavara May 6, 2025
3b475a4
- merge exception and duration metrics
pavelsavara May 7, 2025
2ea50aa
Event -> HandleEvent trace rename
pavelsavara May 7, 2025
971f484
remove aspnetcore.components.circuit.count
pavelsavara May 7, 2025
db97013
Improve descriptions
pavelsavara May 7, 2025
09986be
Merge branch 'main' into blazor_metrics_feedback
pavelsavara May 15, 2025
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
177 changes: 177 additions & 0 deletions src/Components/Components/src/ComponentsActivitySource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;

namespace Microsoft.AspNetCore.Components;

/// <summary>
/// This is instance scoped per renderer
/// </summary>
internal class ComponentsActivitySource
{
internal const string Name = "Microsoft.AspNetCore.Components";
internal const string OnCircuitName = $"{Name}.OnCircuit";
internal const string OnRouteName = $"{Name}.OnRoute";
internal const string OnEventName = $"{Name}.OnEvent";

private ActivityContext _httpContext;
private ActivityContext _circuitContext;
private string? _circuitId;
private ActivityContext _routeContext;

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

public static ActivityContext CaptureHttpContext()
{
var parentActivity = Activity.Current;
if (parentActivity is not null && parentActivity.OperationName == "Microsoft.AspNetCore.Hosting.HttpRequestIn" && parentActivity.Recorded)
{
return parentActivity.Context;
}
return default;
}
Comment on lines +25 to +33
Copy link
Member

Choose a reason for hiding this comment

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

Layering wise this shouldn't go here.

Microsoft.AspNetCore.Components shoudn't have a dependency on Microsoft.AspNetCore.Hosting

Copy link
Member Author

Choose a reason for hiding this comment

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

Please be more specific, what do you want to see instead ?

Copy link
Member

Choose a reason for hiding this comment

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

Are you trying to get the activity of the current request here? There is a feature in the features collection for doing that: https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.http.features.ihttpactivityfeature.activity?view=aspnetcore-9.0

Copy link
Member Author

Choose a reason for hiding this comment

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

Can I get that from DI without passing HttpContext thru all SignalR layers ? Or from some TLS ?

Copy link
Member

@JamesNK JamesNK May 15, 2025

Choose a reason for hiding this comment

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

It comes from HttpContext. It's in the features collection.


public Activity? StartCircuitActivity(string circuitId, ActivityContext httpContext)
{
_circuitId = circuitId;

var activity = ActivitySource.CreateActivity(OnCircuitName, ActivityKind.Server, parentId: null, null, null);
if (activity is not null)
{
if (activity.IsAllDataRequested)
{
if (_circuitId != null)
{
activity.SetTag("circuit.id", _circuitId);
}
if (httpContext != default)
{
activity.AddLink(new ActivityLink(httpContext));
}
}
activity.DisplayName = $"CIRCUIT {circuitId ?? ""}";
activity.Start();
_circuitContext = activity.Context;
}
return activity;
}

public void FailCircuitActivity(Activity? activity, Exception ex)
{
_circuitContext = default;
if (activity != null && !activity.IsStopped)
{
activity.SetTag("error.type", ex.GetType().FullName);
activity.SetStatus(ActivityStatusCode.Error);
activity.Stop();
}
}

public Activity? StartRouteActivity(string componentType, string route)
{
if (_httpContext == default)
{
_httpContext = CaptureHttpContext();
}

var activity = ActivitySource.CreateActivity(OnRouteName, ActivityKind.Server, parentId: null, null, null);
if (activity is not null)
{
if (activity.IsAllDataRequested)
{
if (_circuitId != null)
{
activity.SetTag("circuit.id", _circuitId);
}
if (componentType != null)
{
activity.SetTag("component.type", componentType);
}
if (route != null)
{
activity.SetTag("route", route);
}
if (_httpContext != default)
{
activity.AddLink(new ActivityLink(_httpContext));
}
if (_circuitContext != default)
{
activity.AddLink(new ActivityLink(_circuitContext));
}
}

activity.DisplayName = $"ROUTE {route ?? "unknown"} -> {componentType ?? "unknown"}";
activity.Start();
_routeContext = activity.Context;
}
return activity;
}

public Activity? StartEventActivity(string? componentType, string? methodName, string? attributeName)
{
var activity = ActivitySource.CreateActivity(OnEventName, ActivityKind.Server, parentId: null, null, null);
if (activity is not null)
{
if (activity.IsAllDataRequested)
{
if (_circuitId != null)
{
activity.SetTag("circuit.id", _circuitId);
}
if (componentType != null)
{
activity.SetTag("component.type", componentType);
}
if (methodName != null)
{
activity.SetTag("component.method", methodName);
}
if (attributeName != null)
{
activity.SetTag("attribute.name", attributeName);
}
if (_httpContext != default)
{
activity.AddLink(new ActivityLink(_httpContext));
}
if (_circuitContext != default)
{
activity.AddLink(new ActivityLink(_circuitContext));
}
if (_routeContext != default)
{
activity.AddLink(new ActivityLink(_routeContext));
}
}

activity.DisplayName = $"EVENT {attributeName ?? "unknown"} -> {componentType ?? "unknown"}.{methodName ?? "unknown"}";
activity.Start();
}
return activity;
}

public static void FailEventActivity(Activity? activity, Exception ex)
{
if (activity != null && !activity.IsStopped)
{
activity.SetTag("error.type", ex.GetType().FullName);
activity.SetStatus(ActivityStatusCode.Error);
activity.Stop();
}
}

public static async Task CaptureEventStopAsync(Task task, Activity? activity)
{
try
{
await task;
activity?.Stop();
}
catch (Exception ex)
{
FailEventActivity(activity, ex);
}
}
}
Loading
Loading