|
| 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.Components; |
| 7 | +using Microsoft.AspNetCore.Http; |
| 8 | + |
| 9 | +namespace Microsoft.AspNetCore.Components; |
| 10 | + |
| 11 | +internal sealed class ComponentsMetrics : IDisposable |
| 12 | +{ |
| 13 | + public const string MeterName = "Microsoft.AspNetCore.Components"; |
| 14 | + public const string LifecycleMeterName = "Microsoft.AspNetCore.Components.Lifecycle"; |
| 15 | + private readonly Meter _meter; |
| 16 | + private readonly Meter _lifeCycleMeter; |
| 17 | + |
| 18 | + private readonly Counter<long> _navigationCount; |
| 19 | + |
| 20 | + private readonly Histogram<double> _eventDuration; |
| 21 | + private readonly Histogram<double> _parametersDuration; |
| 22 | + private readonly Histogram<double> _batchDuration; |
| 23 | + |
| 24 | + public bool IsNavigationEnabled => _navigationCount.Enabled; |
| 25 | + |
| 26 | + public bool IsEventEnabled => _eventDuration.Enabled; |
| 27 | + |
| 28 | + public bool IsParametersEnabled => _parametersDuration.Enabled; |
| 29 | + |
| 30 | + public bool IsBatchEnabled => _batchDuration.Enabled; |
| 31 | + |
| 32 | + public ComponentsMetrics(IMeterFactory meterFactory) |
| 33 | + { |
| 34 | + Debug.Assert(meterFactory != null); |
| 35 | + |
| 36 | + _meter = meterFactory.Create(MeterName); |
| 37 | + _lifeCycleMeter = meterFactory.Create(LifecycleMeterName); |
| 38 | + |
| 39 | + _navigationCount = _meter.CreateCounter<long>( |
| 40 | + "aspnetcore.components.navigation", |
| 41 | + unit: "{route}", |
| 42 | + description: "Total number of route changes."); |
| 43 | + |
| 44 | + _eventDuration = _meter.CreateHistogram( |
| 45 | + "aspnetcore.components.event_handler", |
| 46 | + unit: "s", |
| 47 | + description: "Duration of processing browser event. It includes business logic of the component but not affected child components.", |
| 48 | + advice: new InstrumentAdvice<double> { HistogramBucketBoundaries = MetricsConstants.ShortSecondsBucketBoundaries }); |
| 49 | + |
| 50 | + _parametersDuration = _lifeCycleMeter.CreateHistogram( |
| 51 | + "aspnetcore.components.update_parameters", |
| 52 | + unit: "s", |
| 53 | + description: "Duration of processing component parameters. It includes business logic of the component.", |
| 54 | + advice: new InstrumentAdvice<double> { HistogramBucketBoundaries = MetricsConstants.BlazorRenderingSecondsBucketBoundaries }); |
| 55 | + |
| 56 | + _batchDuration = _lifeCycleMeter.CreateHistogram( |
| 57 | + "aspnetcore.components.render_diff", |
| 58 | + unit: "s", |
| 59 | + description: "Duration of rendering component tree and producing HTML diff. It includes business logic of the changed components.", |
| 60 | + advice: new InstrumentAdvice<double> { HistogramBucketBoundaries = MetricsConstants.BlazorRenderingSecondsBucketBoundaries }); |
| 61 | + } |
| 62 | + |
| 63 | + public void Navigation(string componentType, string route) |
| 64 | + { |
| 65 | + var tags = new TagList |
| 66 | + { |
| 67 | + { "aspnetcore.components.type", componentType ?? "unknown" }, |
| 68 | + { "aspnetcore.components.route", route ?? "unknown" }, |
| 69 | + }; |
| 70 | + |
| 71 | + _navigationCount.Add(1, tags); |
| 72 | + } |
| 73 | + |
| 74 | + public async Task CaptureEventDuration(Task task, long startTimestamp, string? componentType, string? methodName, string? attributeName) |
| 75 | + { |
| 76 | + var tags = new TagList |
| 77 | + { |
| 78 | + { "aspnetcore.components.type", componentType ?? "unknown" }, |
| 79 | + { "aspnetcore.components.method", methodName ?? "unknown" }, |
| 80 | + { "aspnetcore.components.attribute.name", attributeName ?? "unknown" } |
| 81 | + }; |
| 82 | + |
| 83 | + try |
| 84 | + { |
| 85 | + await task; |
| 86 | + } |
| 87 | + catch (Exception ex) |
| 88 | + { |
| 89 | + tags.Add("error.type", ex.GetType().FullName ?? "unknown"); |
| 90 | + } |
| 91 | + var duration = Stopwatch.GetElapsedTime(startTimestamp); |
| 92 | + _eventDuration.Record(duration.TotalSeconds, tags); |
| 93 | + } |
| 94 | + |
| 95 | + public void FailEventSync(Exception ex, long startTimestamp, string? componentType, string? methodName, string? attributeName) |
| 96 | + { |
| 97 | + var tags = new TagList |
| 98 | + { |
| 99 | + { "aspnetcore.components.type", componentType ?? "unknown" }, |
| 100 | + { "aspnetcore.components.method", methodName ?? "unknown" }, |
| 101 | + { "aspnetcore.components.attribute.name", attributeName ?? "unknown" }, |
| 102 | + { "error.type", ex.GetType().FullName ?? "unknown" } |
| 103 | + }; |
| 104 | + var duration = Stopwatch.GetElapsedTime(startTimestamp); |
| 105 | + _eventDuration.Record(duration.TotalSeconds, tags); |
| 106 | + } |
| 107 | + |
| 108 | + public async Task CaptureParametersDuration(Task task, long startTimestamp, string? componentType) |
| 109 | + { |
| 110 | + var tags = new TagList |
| 111 | + { |
| 112 | + { "aspnetcore.components.type", componentType ?? "unknown" }, |
| 113 | + }; |
| 114 | + |
| 115 | + try |
| 116 | + { |
| 117 | + await task; |
| 118 | + } |
| 119 | + catch(Exception ex) |
| 120 | + { |
| 121 | + tags.Add("error.type", ex.GetType().FullName ?? "unknown"); |
| 122 | + } |
| 123 | + var duration = Stopwatch.GetElapsedTime(startTimestamp); |
| 124 | + _parametersDuration.Record(duration.TotalSeconds, tags); |
| 125 | + } |
| 126 | + |
| 127 | + public void FailParametersSync(Exception ex, long startTimestamp, string? componentType) |
| 128 | + { |
| 129 | + var duration = Stopwatch.GetElapsedTime(startTimestamp); |
| 130 | + var tags = new TagList |
| 131 | + { |
| 132 | + { "aspnetcore.components.type", componentType ?? "unknown" }, |
| 133 | + { "error.type", ex.GetType().FullName ?? "unknown" } |
| 134 | + }; |
| 135 | + _parametersDuration.Record(duration.TotalSeconds, tags); |
| 136 | + } |
| 137 | + |
| 138 | + public async Task CaptureBatchDuration(Task task, long startTimestamp, int diffLength) |
| 139 | + { |
| 140 | + var tags = new TagList |
| 141 | + { |
| 142 | + { "aspnetcore.components.diff.length", BucketDiffLength(diffLength) } |
| 143 | + }; |
| 144 | + |
| 145 | + try |
| 146 | + { |
| 147 | + await task; |
| 148 | + } |
| 149 | + catch (Exception ex) |
| 150 | + { |
| 151 | + tags.Add("error.type", ex.GetType().FullName ?? "unknown"); |
| 152 | + } |
| 153 | + var duration = Stopwatch.GetElapsedTime(startTimestamp); |
| 154 | + _batchDuration.Record(duration.TotalSeconds, tags); |
| 155 | + } |
| 156 | + |
| 157 | + public void FailBatchSync(Exception ex, long startTimestamp) |
| 158 | + { |
| 159 | + var duration = Stopwatch.GetElapsedTime(startTimestamp); |
| 160 | + var tags = new TagList |
| 161 | + { |
| 162 | + { "aspnetcore.components.diff.length", 0 }, |
| 163 | + { "error.type", ex.GetType().FullName ?? "unknown" } |
| 164 | + }; |
| 165 | + _batchDuration.Record(duration.TotalSeconds, tags); |
| 166 | + } |
| 167 | + |
| 168 | + private static int BucketDiffLength(int diffLength) |
| 169 | + { |
| 170 | + return diffLength switch |
| 171 | + { |
| 172 | + <= 1 => 1, |
| 173 | + <= 2 => 2, |
| 174 | + <= 5 => 5, |
| 175 | + <= 10 => 10, |
| 176 | + <= 20 => 20, |
| 177 | + <= 50 => 50, |
| 178 | + <= 100 => 100, |
| 179 | + <= 500 => 500, |
| 180 | + <= 1000 => 1000, |
| 181 | + <= 10000 => 10000, |
| 182 | + _ => 10001, |
| 183 | + }; |
| 184 | + } |
| 185 | + |
| 186 | + public void Dispose() |
| 187 | + { |
| 188 | + _meter.Dispose(); |
| 189 | + _lifeCycleMeter.Dispose(); |
| 190 | + } |
| 191 | +} |
0 commit comments