-
Notifications
You must be signed in to change notification settings - Fork 10.3k
[Blazor] Remove InternalsVisibleTo from Components to Components.Server #62085
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
base: main
Are you sure you want to change the base?
Changes from all commits
a553b97
f0af6ba
4abc68c
45bf8bc
a7ca7c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,79 +11,36 @@ namespace Microsoft.AspNetCore.Components; | |
internal class ComponentsActivitySource | ||
{ | ||
internal const string Name = "Microsoft.AspNetCore.Components"; | ||
internal const string OnCircuitName = $"{Name}.CircuitStart"; | ||
internal const string OnRouteName = $"{Name}.RouteChange"; | ||
internal const string OnEventName = $"{Name}.HandleEvent"; | ||
|
||
private ActivityContext _httpContext; | ||
private ActivityContext _circuitContext; | ||
private string? _circuitId; | ||
private ActivityContext _routeContext; | ||
private Activity? _capturedActivity; | ||
|
||
private ActivitySource ActivitySource { get; } = new ActivitySource(Name); | ||
|
||
public static ActivityContext CaptureHttpContext() | ||
/// <summary> | ||
/// Initializes the ComponentsActivitySource with a captured activity for linking. | ||
/// </summary> | ||
/// <param name="capturedActivity">Activity to link with component activities.</param> | ||
public void Initialize(Activity? capturedActivity) | ||
{ | ||
var parentActivity = Activity.Current; | ||
if (parentActivity is not null && parentActivity.OperationName == "Microsoft.AspNetCore.Hosting.HttpRequestIn" && parentActivity.Recorded) | ||
{ | ||
return parentActivity.Context; | ||
} | ||
return default; | ||
} | ||
|
||
public Activity? StartCircuitActivity(string circuitId, ActivityContext httpContext) | ||
{ | ||
_circuitId = circuitId; | ||
|
||
var activity = ActivitySource.CreateActivity(OnCircuitName, ActivityKind.Internal, parentId: null, null, null); | ||
if (activity is not null) | ||
{ | ||
if (activity.IsAllDataRequested) | ||
{ | ||
if (_circuitId != null) | ||
{ | ||
activity.SetTag("aspnetcore.components.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(); | ||
} | ||
_capturedActivity = capturedActivity; | ||
} | ||
|
||
public Activity? StartRouteActivity(string componentType, string route) | ||
{ | ||
if (_httpContext == default) | ||
{ | ||
_httpContext = CaptureHttpContext(); | ||
} | ||
|
||
var activity = ActivitySource.CreateActivity(OnRouteName, ActivityKind.Internal, parentId: null, null, null); | ||
if (activity is not null) | ||
{ | ||
if (activity.IsAllDataRequested) | ||
{ | ||
if (_circuitId != null) | ||
// Copy any circuit ID from captured activity if present | ||
if (_capturedActivity != null && _capturedActivity.GetTagItem("aspnetcore.components.circuit.id") is string circuitId) | ||
{ | ||
activity.SetTag("aspnetcore.components.circuit.id", _circuitId); | ||
activity.SetTag("aspnetcore.components.circuit.id", circuitId); | ||
} | ||
|
||
if (componentType != null) | ||
{ | ||
activity.SetTag("aspnetcore.components.type", componentType); | ||
|
@@ -92,13 +49,9 @@ public void FailCircuitActivity(Activity? activity, Exception ex) | |
{ | ||
activity.SetTag("aspnetcore.components.route", route); | ||
} | ||
if (_httpContext != default) | ||
if (_capturedActivity != null) | ||
{ | ||
activity.AddLink(new ActivityLink(_httpContext)); | ||
} | ||
if (_circuitContext != default) | ||
{ | ||
activity.AddLink(new ActivityLink(_circuitContext)); | ||
activity.AddLink(new ActivityLink(_capturedActivity.Context)); | ||
} | ||
} | ||
|
||
|
@@ -116,10 +69,12 @@ public void FailCircuitActivity(Activity? activity, Exception ex) | |
{ | ||
if (activity.IsAllDataRequested) | ||
{ | ||
if (_circuitId != null) | ||
// Copy any circuit ID from captured activity if present | ||
if (_capturedActivity != null && _capturedActivity.GetTagItem("aspnetcore.components.circuit.id") is string circuitId) | ||
{ | ||
activity.SetTag("aspnetcore.components.circuit.id", _circuitId); | ||
activity.SetTag("aspnetcore.components.circuit.id", circuitId); | ||
} | ||
|
||
Comment on lines
+72
to
+77
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FailCircuitActivity should receive the CircuitId as a parameter, not search for it inside the captured tag |
||
if (componentType != null) | ||
{ | ||
activity.SetTag("aspnetcore.components.type", componentType); | ||
|
@@ -132,13 +87,9 @@ public void FailCircuitActivity(Activity? activity, Exception ex) | |
{ | ||
activity.SetTag("aspnetcore.components.attribute.name", attributeName); | ||
} | ||
if (_httpContext != default) | ||
{ | ||
activity.AddLink(new ActivityLink(_httpContext)); | ||
} | ||
if (_circuitContext != default) | ||
if (_capturedActivity != null) | ||
{ | ||
activity.AddLink(new ActivityLink(_circuitContext)); | ||
activity.AddLink(new ActivityLink(_capturedActivity.Context)); | ||
} | ||
if (_routeContext != default) | ||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// 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.Server.Circuits; | ||
|
||
/// <summary> | ||
/// Activity source for circuit-related activities. | ||
/// </summary> | ||
public class CircuitActivitySource | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be internal |
||
{ | ||
internal const string Name = "Microsoft.AspNetCore.Components.Server"; | ||
internal const string OnCircuitName = $"{Name}.CircuitStart"; | ||
|
||
private ActivitySource ActivitySource { get; } = new ActivitySource(Name); | ||
|
||
/// <summary> | ||
/// Creates and starts a new activity for circuit initialization. | ||
/// </summary> | ||
/// <param name="circuitId">The ID of the circuit being initialized.</param> | ||
/// <param name="httpContext">The HTTP context associated with the request that created the circuit.</param> | ||
/// <returns>The created activity.</returns> | ||
public Activity? StartCircuitActivity(string circuitId, ActivityContext httpContext) | ||
{ | ||
var activity = ActivitySource.CreateActivity(OnCircuitName, ActivityKind.Internal, parentId: null, null, null); | ||
if (activity is not null) | ||
{ | ||
if (activity.IsAllDataRequested) | ||
{ | ||
if (circuitId != null) | ||
{ | ||
activity.SetTag("aspnetcore.components.circuit.id", circuitId); | ||
} | ||
Comment on lines
+31
to
+34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. circuitId is never null |
||
if (httpContext != default) | ||
{ | ||
activity.AddLink(new ActivityLink(httpContext)); | ||
} | ||
} | ||
activity.DisplayName = $"Circuit {circuitId ?? ""}"; | ||
activity.Start(); | ||
} | ||
return activity; | ||
} | ||
|
||
/// <summary> | ||
/// Stops a circuit activity that was previously started. | ||
/// </summary> | ||
/// <param name="activity">The activity to stop.</param> | ||
public void StopCircuitActivity(Activity? activity) | ||
{ | ||
if (activity != null && !activity.IsStopped) | ||
{ | ||
activity.Stop(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Marks a circuit activity as failed and stops it. | ||
/// </summary> | ||
/// <param name="activity">The activity to mark as failed.</param> | ||
/// <param name="ex">The exception that caused the failure.</param> | ||
public void FailCircuitActivity(Activity? activity, Exception ex) | ||
{ | ||
if (activity != null && !activity.IsStopped) | ||
{ | ||
activity.SetTag("error.type", ex.GetType().FullName); | ||
activity.SetStatus(ActivityStatusCode.Error); | ||
activity.Stop(); | ||
} | ||
} | ||
Comment on lines
+63
to
+71
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should receive the circuitId and add it as a tag |
||
|
||
/// <summary> | ||
/// Captures the current HTTP context activity. | ||
/// </summary> | ||
/// <returns>The captured HTTP context activity.</returns> | ||
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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.AspNetCore.Components.Server.Circuits; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
|
||
namespace Microsoft.Extensions.DependencyInjection; | ||
|
||
/// <summary> | ||
/// Extension methods for adding <see cref="CircuitActivitySource"/> to the service collection. | ||
/// </summary> | ||
internal static class CircuitActivitySourceServiceCollectionExtensions | ||
{ | ||
/// <summary> | ||
/// Adds <see cref="CircuitActivitySource"/> to the service collection. | ||
/// </summary> | ||
/// <param name="services">The <see cref="IServiceCollection"/>.</param> | ||
/// <returns>The <see cref="IServiceCollection"/>.</returns> | ||
public static IServiceCollection AddCircuitActivitySource(this IServiceCollection services) | ||
{ | ||
services.TryAddSingleton<CircuitActivitySource>(); | ||
return services; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copy all the tags or don't copy any. but don't look for the specific circuit id tag here