|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Diagnostics; |
| 5 | +using System.Diagnostics.Metrics; |
| 6 | +using Microsoft.AspNetCore.Http; |
| 7 | + |
| 8 | +namespace Microsoft.AspNetCore.Components.Rendering; |
| 9 | + |
| 10 | +internal sealed class RenderingMetrics : IDisposable |
| 11 | +{ |
| 12 | + public const string MeterName = "Microsoft.AspNetCore.Components.Rendering"; |
| 13 | + |
| 14 | + private readonly Meter _meter; |
| 15 | + private readonly Counter<long> _renderTotalCounter; |
| 16 | + private readonly UpDownCounter<long> _renderActiveCounter; |
| 17 | + private readonly Histogram<double> _renderDuration; |
| 18 | + |
| 19 | + public RenderingMetrics(IMeterFactory meterFactory) |
| 20 | + { |
| 21 | + Debug.Assert(meterFactory != null); |
| 22 | + |
| 23 | + _meter = meterFactory.Create(MeterName); |
| 24 | + |
| 25 | + _renderTotalCounter = _meter.CreateCounter<long>( |
| 26 | + "aspnetcore.components.rendering.count", |
| 27 | + unit: "{renders}", |
| 28 | + description: "Number of component renders performed."); |
| 29 | + |
| 30 | + _renderActiveCounter = _meter.CreateUpDownCounter<long>( |
| 31 | + "aspnetcore.components.rendering.active_renders", |
| 32 | + unit: "{renders}", |
| 33 | + description: "Number of component renders performed."); |
| 34 | + |
| 35 | + _renderDuration = _meter.CreateHistogram<double>( |
| 36 | + "aspnetcore.components.rendering.duration", |
| 37 | + unit: "ms", |
| 38 | + description: "Duration of component rendering operations per component.", |
| 39 | + advice: new InstrumentAdvice<double> { HistogramBucketBoundaries = MetricsConstants.ShortSecondsBucketBoundaries }); |
| 40 | + } |
| 41 | + |
| 42 | + public void RenderStart(string componentType) |
| 43 | + { |
| 44 | + var tags = new TagList(); |
| 45 | + tags = InitializeRequestTags(componentType, tags); |
| 46 | + |
| 47 | + if (_renderActiveCounter.Enabled) |
| 48 | + { |
| 49 | + _renderActiveCounter.Add(1, tags); |
| 50 | + } |
| 51 | + if (_renderTotalCounter.Enabled) |
| 52 | + { |
| 53 | + _renderTotalCounter.Add(1, tags); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + public void RenderEnd(string componentType, Exception? exception, long startTimestamp, long currentTimestamp) |
| 58 | + { |
| 59 | + // Tags must match request start. |
| 60 | + var tags = new TagList(); |
| 61 | + tags = InitializeRequestTags(componentType, tags); |
| 62 | + |
| 63 | + if (_renderActiveCounter.Enabled) |
| 64 | + { |
| 65 | + _renderActiveCounter.Add(-1, tags); |
| 66 | + } |
| 67 | + |
| 68 | + if (_renderDuration.Enabled) |
| 69 | + { |
| 70 | + if (exception != null) |
| 71 | + { |
| 72 | + TryAddTag(ref tags, "error.type", exception.GetType().FullName); |
| 73 | + } |
| 74 | + |
| 75 | + var duration = Stopwatch.GetElapsedTime(startTimestamp, currentTimestamp); |
| 76 | + _renderDuration.Record(duration.TotalMilliseconds, tags); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private static TagList InitializeRequestTags(string componentType, TagList tags) |
| 81 | + { |
| 82 | + tags.Add("component.type", componentType); |
| 83 | + return tags; |
| 84 | + } |
| 85 | + |
| 86 | + public bool IsDurationEnabled() => _renderDuration.Enabled; |
| 87 | + |
| 88 | + public void Dispose() |
| 89 | + { |
| 90 | + _meter.Dispose(); |
| 91 | + } |
| 92 | + |
| 93 | + private static bool TryAddTag(ref TagList tags, string name, object? value) |
| 94 | + { |
| 95 | + for (var i = 0; i < tags.Count; i++) |
| 96 | + { |
| 97 | + if (tags[i].Key == name) |
| 98 | + { |
| 99 | + return false; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + tags.Add(new KeyValuePair<string, object?>(name, value)); |
| 104 | + return true; |
| 105 | + } |
| 106 | +} |
0 commit comments