Skip to content

Commit 9ab8a84

Browse files
committed
more
1 parent 550f633 commit 9ab8a84

File tree

13 files changed

+170
-270
lines changed

13 files changed

+170
-270
lines changed

src/Components/Components/src/ComponentsActivitySource.cs

Lines changed: 88 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,60 +12,127 @@ internal class ComponentsActivitySource
1212
{
1313
internal const string Name = "Microsoft.AspNetCore.Components";
1414
internal const string OnEventName = $"{Name}.OnEvent";
15-
internal const string OnNavigationName = $"{Name}.OnNavigation";
15+
internal const string OnRouteName = $"{Name}.OnRoute";
1616

17-
public static ActivitySource ActivitySource { get; } = new ActivitySource(Name);
17+
private ActivityContext _httpContext;
18+
private ActivityContext _circuitContext;
19+
private string? _circuitId;
20+
private ActivityContext _routeContext;
1821

19-
private Activity? _routeActivity;
22+
private ActivitySource ActivitySource { get; } = new ActivitySource(Name);
2023

21-
public void StartRouteActivity(string componentType, string route)
24+
public static ActivityContext CaptureHttpContext()
2225
{
23-
StopRouteActivity();
26+
var parentActivity = Activity.Current;
27+
if (parentActivity is not null && parentActivity.OperationName == "Microsoft.AspNetCore.Hosting.HttpRequestIn")
28+
{
29+
return parentActivity.Context;
30+
}
31+
return default;
32+
}
33+
34+
public Activity? StartCircuitActivity(string circuitId, ActivityContext httpContext)
35+
{
36+
_circuitId = circuitId;
37+
IEnumerable<KeyValuePair<string, object?>> tags =
38+
[
39+
new("circuit.id", _circuitId ?? "unknown"),
40+
];
41+
42+
var links = new List<ActivityLink>();
43+
if (httpContext != default)
44+
{
45+
_httpContext = httpContext;
46+
links.Add(new ActivityLink(httpContext));
47+
}
48+
49+
var activity = ActivitySource.CreateActivity(OnRouteName, ActivityKind.Server, parentId:null, tags, links);
50+
if (activity is not null)
51+
{
52+
activity.DisplayName = $"CIRCUIT {circuitId ?? "unknown"}";
53+
activity.Start();
54+
_circuitContext = activity.Context;
55+
56+
Console.WriteLine($"StartCircuitActivity: {circuitId}");
57+
Console.WriteLine($"circuitContext: {_circuitContext.TraceId} {_circuitContext.SpanId} {_circuitContext.TraceState}");
58+
Console.WriteLine($"httpContext: {httpContext.TraceId} {httpContext.SpanId} {httpContext.TraceState}");
59+
}
60+
return activity;
61+
}
62+
63+
public void FailCircuitActivity(Activity activity, Exception ex)
64+
{
65+
_circuitContext = default;
66+
if (!activity.IsStopped)
67+
{
68+
activity.SetTag("error.type", ex.GetType().FullName);
69+
activity.SetStatus(ActivityStatusCode.Error);
70+
activity.Stop();
71+
}
72+
}
2473

74+
public Activity? StartRouteActivity(string componentType, string route)
75+
{
2576
IEnumerable<KeyValuePair<string, object?>> tags =
2677
[
78+
new("circuit.id", _circuitId ?? "unknown"),
2779
new("component.type", componentType ?? "unknown"),
2880
new("route", route ?? "unknown"),
2981
];
30-
var parentActivity = Activity.Current;
31-
IEnumerable<ActivityLink>? links = parentActivity is not null ? [new ActivityLink(parentActivity.Context)] : null;
82+
var links = new List<ActivityLink>();
83+
if (_httpContext == default)
84+
{
85+
_httpContext = CaptureHttpContext();
86+
}
87+
if (_httpContext != default)
88+
{
89+
links.Add(new ActivityLink(_httpContext));
90+
}
91+
if (_circuitContext != default)
92+
{
93+
links.Add(new ActivityLink(_circuitContext));
94+
}
3295

33-
var activity = ActivitySource.CreateActivity(OnEventName, ActivityKind.Server, parentId: null, tags, links);
96+
var activity = ActivitySource.CreateActivity(OnRouteName, ActivityKind.Server, parentId: null, tags, links);
3497
if (activity is not null)
3598
{
36-
activity.DisplayName = $"NAVIGATE {route ?? "unknown"} -> {componentType ?? "unknown"}";
99+
_routeContext = activity.Context;
100+
activity.DisplayName = $"ROUTE {route ?? "unknown"} -> {componentType ?? "unknown"}";
37101
activity.Start();
38-
_routeActivity = activity;
39102
}
103+
return activity;
40104
}
41105

42-
public void StopRouteActivity()
106+
public void StopRouteActivity(Activity activity)
43107
{
44-
if (_routeActivity != null)
108+
_routeContext = default;
109+
if (!activity.IsStopped)
45110
{
46-
_routeActivity.Stop();
47-
_routeActivity = null;
48-
return;
111+
activity.Stop();
49112
}
50113
}
51114

52115
public Activity? StartEventActivity(string? componentType, string? methodName, string? attributeName)
53116
{
54117
IEnumerable<KeyValuePair<string, object?>> tags =
55118
[
119+
new("circuit.id", _circuitId ?? "unknown"),
56120
new("component.type", componentType ?? "unknown"),
57121
new("component.method", methodName ?? "unknown"),
58122
new("attribute.name", attributeName ?? "unknown"),
59123
];
60-
List<ActivityLink>? links = new List<ActivityLink>();
61-
var parentActivity = Activity.Current;
62-
if (parentActivity is not null)
124+
var links = new List<ActivityLink>();
125+
if (_httpContext != default)
126+
{
127+
links.Add(new ActivityLink(_httpContext));
128+
}
129+
if (_circuitContext != default)
63130
{
64-
links.Add(new ActivityLink(parentActivity.Context));
131+
links.Add(new ActivityLink(_circuitContext));
65132
}
66-
if (_routeActivity is not null)
133+
if (_routeContext != default)
67134
{
68-
links.Add(new ActivityLink(_routeActivity.Context));
135+
links.Add(new ActivityLink(_routeContext));
69136
}
70137

71138
var activity = ActivitySource.CreateActivity(OnEventName, ActivityKind.Server, parentId: null, tags, links);

src/Components/Components/src/ComponentsMetrics.cs

Lines changed: 13 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,22 @@ internal sealed class ComponentsMetrics : IDisposable
1515

1616
private readonly Counter<long> _navigationCount;
1717

18-
private readonly Histogram<double> _eventSyncDuration;
19-
private readonly Histogram<double> _eventAsyncDuration;
18+
private readonly Histogram<double> _eventDuration;
2019
private readonly Counter<long> _eventException;
2120

22-
private readonly Histogram<double> _parametersSyncDuration;
23-
private readonly Histogram<double> _parametersAsyncDuration;
21+
private readonly Histogram<double> _parametersDuration;
2422
private readonly Counter<long> _parametersException;
2523

26-
private readonly Histogram<double> _diffDuration;
27-
2824
private readonly Histogram<double> _batchDuration;
2925
private readonly Counter<long> _batchException;
3026

3127
public bool IsNavigationEnabled => _navigationCount.Enabled;
3228

33-
public bool IsEventDurationEnabled => _eventSyncDuration.Enabled || _eventAsyncDuration.Enabled;
29+
public bool IsEventDurationEnabled => _eventDuration.Enabled;
3430
public bool IsEventExceptionEnabled => _eventException.Enabled;
3531

36-
public bool IsStateDurationEnabled => _parametersSyncDuration.Enabled || _parametersAsyncDuration.Enabled;
37-
public bool IsStateExceptionEnabled => _parametersException.Enabled;
38-
39-
public bool IsDiffDurationEnabled => _diffDuration.Enabled;
32+
public bool IsParametersDurationEnabled => _parametersDuration.Enabled;
33+
public bool IsParametersExceptionEnabled => _parametersException.Enabled;
4034

4135
public bool IsBatchDurationEnabled => _batchDuration.Enabled;
4236
public bool IsBatchExceptionEnabled => _batchException.Enabled;
@@ -52,46 +46,28 @@ public ComponentsMetrics(IMeterFactory meterFactory)
5246
unit: "{exceptions}",
5347
description: "Total number of route changes.");
5448

55-
_eventSyncDuration = _meter.CreateHistogram(
56-
"aspnetcore.components.event.synchronous.duration",
57-
unit: "s",
58-
description: "Duration of processing browser event synchronously.",
59-
advice: new InstrumentAdvice<double> { HistogramBucketBoundaries = MetricsConstants.ShortSecondsBucketBoundaries });
60-
61-
_eventAsyncDuration = _meter.CreateHistogram(
62-
"aspnetcore.components.event.asynchronous.duration",
49+
_eventDuration = _meter.CreateHistogram(
50+
"aspnetcore.components.event.duration",
6351
unit: "s",
64-
description: "Duration of processing browser event asynchronously.",
52+
description: "Duration of processing browser event.",
6553
advice: new InstrumentAdvice<double> { HistogramBucketBoundaries = MetricsConstants.ShortSecondsBucketBoundaries });
6654

6755
_eventException = _meter.CreateCounter<long>(
6856
"aspnetcore.components.event.exception",
6957
unit: "{exceptions}",
7058
description: "Total number of exceptions during browser event processing.");
7159

72-
_parametersSyncDuration = _meter.CreateHistogram(
73-
"aspnetcore.components.parameters.synchronous.duration",
60+
_parametersDuration = _meter.CreateHistogram(
61+
"aspnetcore.components.parameters.duration",
7462
unit: "s",
75-
description: "Duration of processing component parameters synchronously.",
76-
advice: new InstrumentAdvice<double> { HistogramBucketBoundaries = MetricsConstants.ShortSecondsBucketBoundaries });
77-
78-
_parametersAsyncDuration = _meter.CreateHistogram(
79-
"aspnetcore.components.parameters.asynchronous.duration",
80-
unit: "s",
81-
description: "Duration of processing component parameters asynchronously.",
63+
description: "Duration of processing component parameters.",
8264
advice: new InstrumentAdvice<double> { HistogramBucketBoundaries = MetricsConstants.ShortSecondsBucketBoundaries });
8365

8466
_parametersException = _meter.CreateCounter<long>(
8567
"aspnetcore.components.parameters.exception",
8668
unit: "{exceptions}",
8769
description: "Total number of exceptions during processing component parameters.");
8870

89-
_diffDuration = _meter.CreateHistogram(
90-
"aspnetcore.components.rendering.diff.duration",
91-
unit: "s",
92-
description: "Duration of rendering component HTML diff.",
93-
advice: new InstrumentAdvice<double> { HistogramBucketBoundaries = MetricsConstants.ShortSecondsBucketBoundaries });
94-
9571
_batchDuration = _meter.CreateHistogram(
9672
"aspnetcore.components.rendering.batch.duration",
9773
unit: "s",
@@ -115,19 +91,6 @@ public void Navigation(string componentType, string route)
11591
_navigationCount.Add(1, tags);
11692
}
11793

118-
public void EventDurationSync(long startTimestamp, string? componentType, string? methodName, string? attributeName)
119-
{
120-
var tags = new TagList
121-
{
122-
{ "component.type", componentType ?? "unknown" },
123-
{ "component.method", methodName ?? "unknown" },
124-
{ "attribute.name", attributeName ?? "unknown"}
125-
};
126-
127-
var duration = Stopwatch.GetElapsedTime(startTimestamp);
128-
_eventSyncDuration.Record(duration.TotalSeconds, tags);
129-
}
130-
13194
public async Task CaptureEventDurationAsync(Task task, long startTimestamp, string? componentType, string? methodName, string? attributeName)
13295
{
13396
try
@@ -142,25 +105,14 @@ public async Task CaptureEventDurationAsync(Task task, long startTimestamp, stri
142105
};
143106

144107
var duration = Stopwatch.GetElapsedTime(startTimestamp);
145-
_eventAsyncDuration.Record(duration.TotalSeconds, tags);
108+
_eventDuration.Record(duration.TotalSeconds, tags);
146109
}
147110
catch
148111
{
149112
// none
150113
}
151114
}
152115

153-
public void ParametersDurationSync(long startTimestamp, string? componentType)
154-
{
155-
var tags = new TagList
156-
{
157-
{ "component.type", componentType ?? "unknown" },
158-
};
159-
160-
var duration = Stopwatch.GetElapsedTime(startTimestamp);
161-
_parametersSyncDuration.Record(duration.TotalSeconds, tags);
162-
}
163-
164116
public async Task CaptureParametersDurationAsync(Task task, long startTimestamp, string? componentType)
165117
{
166118
try
@@ -173,26 +125,14 @@ public async Task CaptureParametersDurationAsync(Task task, long startTimestamp,
173125
};
174126

175127
var duration = Stopwatch.GetElapsedTime(startTimestamp);
176-
_parametersAsyncDuration.Record(duration.TotalSeconds, tags);
128+
_parametersDuration.Record(duration.TotalSeconds, tags);
177129
}
178130
catch
179131
{
180132
// none
181133
}
182134
}
183135

184-
public void DiffDuration(long startTimestamp, string? componentType, int diffLength)
185-
{
186-
var tags = new TagList
187-
{
188-
{ "component.type", componentType ?? "unknown" },
189-
{ "diff.length.bucket", BucketEditLength(diffLength) }
190-
};
191-
192-
var duration = Stopwatch.GetElapsedTime(startTimestamp);
193-
_diffDuration.Record(duration.TotalSeconds, tags);
194-
}
195-
196136
public void BatchDuration(long startTimestamp, int diffLength)
197137
{
198138
var tags = new TagList

src/Components/Components/src/Microsoft.AspNetCore.Components.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979

8080
<ItemGroup>
8181
<InternalsVisibleTo Include="Microsoft.AspNetCore.Components.Web" />
82+
<InternalsVisibleTo Include="Microsoft.AspNetCore.Components.Server" />
8283
<InternalsVisibleTo Include="Microsoft.AspNetCore.Blazor.Build.Tests" />
8384
<InternalsVisibleTo Include="Microsoft.AspNetCore.Components.Authorization.Tests" />
8485
<InternalsVisibleTo Include="Microsoft.AspNetCore.Components.Forms.Tests" />

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,6 @@ public virtual Task DispatchEventAsync(ulong eventHandlerId, EventFieldInfo? fie
505505
{
506506
var receiverName = (callback.Receiver?.GetType() ?? callback.Delegate.Target?.GetType())?.FullName;
507507
var methodName = callback.Delegate.Method?.Name;
508-
ComponentMetrics.EventDurationSync(eventStartTimestamp, receiverName, methodName, attributeName);
509508
_ = ComponentMetrics.CaptureEventDurationAsync(task, eventStartTimestamp, receiverName, methodName, attributeName);
510509
}
511510
if (ComponentMetrics != null && ComponentMetrics.IsEventExceptionEnabled)

src/Components/Components/src/Rendering/ComponentState.cs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public ComponentState(Renderer renderer, int componentId, IComponent component,
5353
_hasAnyCascadingParameterSubscriptions = AddCascadingParameterSubscriptions();
5454
}
5555

56-
if (_renderer.ComponentMetrics != null && (_renderer.ComponentMetrics.IsDiffDurationEnabled || _renderer.ComponentMetrics.IsStateDurationEnabled || _renderer.ComponentMetrics.IsStateExceptionEnabled))
56+
if (_renderer.ComponentMetrics != null && (_renderer.ComponentMetrics.IsParametersDurationEnabled || _renderer.ComponentMetrics.IsParametersExceptionEnabled))
5757
{
5858
_componentTypeName = component.GetType().FullName;
5959
}
@@ -108,7 +108,6 @@ internal void RenderIntoBatch(RenderBatchBuilder batchBuilder, RenderFragment re
108108

109109
_nextRenderTree.Clear();
110110

111-
var diffStartTimestamp = _renderer.ComponentMetrics != null && _renderer.ComponentMetrics.IsDiffDurationEnabled ? Stopwatch.GetTimestamp() : 0;
112111
try
113112
{
114113
renderFragment(_nextRenderTree);
@@ -125,8 +124,6 @@ internal void RenderIntoBatch(RenderBatchBuilder batchBuilder, RenderFragment re
125124
// We don't want to make errors from this be recoverable, because there's no legitimate reason for them to happen
126125
_nextRenderTree.AssertTreeIsValid(Component);
127126

128-
var startCount = batchBuilder.EditsBuffer.Count;
129-
130127
// Swap the old and new tree builders
131128
(CurrentRenderTree, _nextRenderTree) = (_nextRenderTree, CurrentRenderTree);
132129

@@ -138,11 +135,6 @@ internal void RenderIntoBatch(RenderBatchBuilder batchBuilder, RenderFragment re
138135
CurrentRenderTree.GetFrames());
139136
batchBuilder.UpdatedComponentDiffs.Append(diff);
140137
batchBuilder.InvalidateParameterViews();
141-
142-
if (_renderer.ComponentMetrics != null && _renderer.ComponentMetrics.IsDiffDurationEnabled)
143-
{
144-
_renderer.ComponentMetrics.DiffDuration(diffStartTimestamp, _componentTypeName, batchBuilder.EditsBuffer.Count - startCount);
145-
}
146138
}
147139

148140
// Callers expect this method to always return a faulted task.
@@ -249,24 +241,23 @@ private void SupplyCombinedParameters(ParameterView directAndCascadingParameters
249241
Task setParametersAsyncTask;
250242
try
251243
{
252-
var stateStartTimestamp = _renderer.ComponentMetrics != null && _renderer.ComponentMetrics.IsStateDurationEnabled ? Stopwatch.GetTimestamp() : 0;
244+
var stateStartTimestamp = _renderer.ComponentMetrics != null && _renderer.ComponentMetrics.IsParametersDurationEnabled ? Stopwatch.GetTimestamp() : 0;
253245

254246
setParametersAsyncTask = Component.SetParametersAsync(directAndCascadingParameters);
255247

256248
// collect metrics
257-
if (_renderer.ComponentMetrics != null && _renderer.ComponentMetrics.IsStateDurationEnabled)
249+
if (_renderer.ComponentMetrics != null && _renderer.ComponentMetrics.IsParametersDurationEnabled)
258250
{
259-
_renderer.ComponentMetrics.ParametersDurationSync(stateStartTimestamp, _componentTypeName);
260251
_ = _renderer.ComponentMetrics.CaptureParametersDurationAsync(setParametersAsyncTask, stateStartTimestamp, _componentTypeName);
261252
}
262-
if (_renderer.ComponentMetrics != null && _renderer.ComponentMetrics.IsStateExceptionEnabled)
253+
if (_renderer.ComponentMetrics != null && _renderer.ComponentMetrics.IsParametersExceptionEnabled)
263254
{
264255
_ = _renderer.ComponentMetrics.CapturePropertiesFailedAsync(setParametersAsyncTask, _componentTypeName);
265256
}
266257
}
267258
catch (Exception ex)
268259
{
269-
if (_renderer.ComponentMetrics != null && _renderer.ComponentMetrics.IsStateExceptionEnabled)
260+
if (_renderer.ComponentMetrics != null && _renderer.ComponentMetrics.IsParametersExceptionEnabled)
270261
{
271262
_renderer.ComponentMetrics.PropertiesFailed(ex.GetType().FullName, _componentTypeName);
272263
}

0 commit comments

Comments
 (0)