-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Blazor - rendering metrics #61516
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
Merged
pavelsavara
merged 4 commits into
dotnet:main
from
pavelsavara:blazor_rendering_metrics
Apr 17, 2025
Merged
Blazor - rendering metrics #61516
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
src/Components/Components/src/Rendering/RenderingMetrics.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// 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; | ||
using System.Diagnostics.Metrics; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Microsoft.AspNetCore.Components.Rendering; | ||
|
||
internal sealed class RenderingMetrics : IDisposable | ||
{ | ||
public const string MeterName = "Microsoft.AspNetCore.Components.Rendering"; | ||
|
||
private readonly Meter _meter; | ||
private readonly Counter<long> _renderTotalCounter; | ||
private readonly UpDownCounter<long> _renderActiveCounter; | ||
private readonly Histogram<double> _renderDuration; | ||
|
||
public RenderingMetrics(IMeterFactory meterFactory) | ||
{ | ||
Debug.Assert(meterFactory != null); | ||
|
||
_meter = meterFactory.Create(MeterName); | ||
|
||
_renderTotalCounter = _meter.CreateCounter<long>( | ||
"aspnetcore.components.rendering.count", | ||
unit: "{renders}", | ||
description: "Number of component renders performed."); | ||
|
||
_renderActiveCounter = _meter.CreateUpDownCounter<long>( | ||
"aspnetcore.components.rendering.active_renders", | ||
unit: "{renders}", | ||
description: "Number of component renders performed."); | ||
|
||
_renderDuration = _meter.CreateHistogram<double>( | ||
"aspnetcore.components.rendering.duration", | ||
unit: "ms", | ||
description: "Duration of component rendering operations per component.", | ||
advice: new InstrumentAdvice<double> { HistogramBucketBoundaries = MetricsConstants.ShortSecondsBucketBoundaries }); | ||
} | ||
|
||
public void RenderStart(string componentType) | ||
{ | ||
var tags = new TagList(); | ||
tags = InitializeRequestTags(componentType, tags); | ||
|
||
if (_renderActiveCounter.Enabled) | ||
{ | ||
_renderActiveCounter.Add(1, tags); | ||
} | ||
if (_renderTotalCounter.Enabled) | ||
{ | ||
_renderTotalCounter.Add(1, tags); | ||
} | ||
} | ||
|
||
public void RenderEnd(string componentType, Exception? exception, long startTimestamp, long currentTimestamp) | ||
{ | ||
// Tags must match request start. | ||
var tags = new TagList(); | ||
tags = InitializeRequestTags(componentType, tags); | ||
|
||
if (_renderActiveCounter.Enabled) | ||
{ | ||
_renderActiveCounter.Add(-1, tags); | ||
} | ||
|
||
if (_renderDuration.Enabled) | ||
{ | ||
if (exception != null) | ||
{ | ||
TryAddTag(ref tags, "error.type", exception.GetType().FullName); | ||
} | ||
|
||
var duration = Stopwatch.GetElapsedTime(startTimestamp, currentTimestamp); | ||
_renderDuration.Record(duration.TotalMilliseconds, tags); | ||
} | ||
} | ||
|
||
private static TagList InitializeRequestTags(string componentType, TagList tags) | ||
{ | ||
tags.Add("component.type", componentType); | ||
return tags; | ||
} | ||
|
||
public bool IsDurationEnabled() => _renderDuration.Enabled; | ||
|
||
public void Dispose() | ||
{ | ||
_meter.Dispose(); | ||
} | ||
|
||
private static bool TryAddTag(ref TagList tags, string name, object? value) | ||
{ | ||
for (var i = 0; i < tags.Count; i++) | ||
{ | ||
if (tags[i].Key == name) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
tags.Add(new KeyValuePair<string, object?>(name, value)); | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
It would be good to register
RenderingMetrics
as singleton. But I think it should be done in one of the "Extensions" helpers and I'm not sure which. This could be done in next PR when @javiercn is back.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.
There is one place for server https://github.com/dotnet/aspnetcore/blob/main/src/Components/Endpoints/src/DependencyInjection/RazorComponentsServiceCollectionExtensions.cs#L36 and one for WebAssembly https://github.com/dotnet/aspnetcore/blob/main/src/Components/WebAssembly/WebAssembly/src/Hosting/WebAssemblyHostBuilder.cs#L299