diff --git a/src/OpenTelemetry/Metrics/AggregatorStore.cs b/src/OpenTelemetry/Metrics/AggregatorStore.cs index b7b432be00c..69e68f4b6c1 100644 --- a/src/OpenTelemetry/Metrics/AggregatorStore.cs +++ b/src/OpenTelemetry/Metrics/AggregatorStore.cs @@ -24,21 +24,6 @@ internal sealed class AggregatorStore { private static readonly string MetricPointCapHitFixMessage = "Consider opting in for the experimental SDK feature to emit all the throttled metrics under the overflow attribute by setting env variable OTEL_DOTNET_EXPERIMENTAL_METRICS_EMIT_OVERFLOW_ATTRIBUTE = true. You could also modify instrumentation to reduce the number of unique key/value pair combinations. Or use Views to drop unwanted tags. Or use MeterProviderBuilder.SetMaxMetricPointsPerMetricStream to set higher limit."; private static readonly Comparison> DimensionComparisonDelegate = (x, y) => x.Key.CompareTo(y.Key); - private static readonly IReadOnlyDictionary<(string, string), double[]> DefaultHistogramBoundMappings = new Dictionary<(string, string), double[]>() - { - { ("Microsoft.AspNetCore.Hosting", "http.server.request.duration"), Metric.DefaultHistogramBoundsSeconds }, - { ("Microsoft.AspNetCore.Http.Connections", "signalr.server.connection.duration"), Metric.DefaultHistogramBoundsSeconds }, - { ("Microsoft.AspNetCore.RateLimiting", "aspnetcore.rate_limiting.request.time_in_queue"), Metric.DefaultHistogramBoundsSeconds }, - { ("Microsoft.AspNetCore.RateLimiting", "aspnetcore.rate_limiting.request_lease.duration"), Metric.DefaultHistogramBoundsSeconds }, - { ("Microsoft.AspNetCore.Server.Kestrel", "kestrel.connection.duration"), Metric.DefaultHistogramBoundsSeconds }, - { ("Microsoft.AspNetCore.Server.Kestrel", "kestrel.tls_handshake.duration"), Metric.DefaultHistogramBoundsSeconds }, - { ("OpenTelemetry.Instrumentation.AspNetCore", "http.server.duration"), Metric.DefaultHistogramBoundsSeconds }, - { ("OpenTelemetry.Instrumentation.Http", "http.client.duration"), Metric.DefaultHistogramBoundsSeconds }, - { ("System.Net.Http", "http.client.connection.duration"), Metric.DefaultHistogramBoundsSeconds }, - { ("System.Net.Http", "http.client.request.duration"), Metric.DefaultHistogramBoundsSeconds }, - { ("System.Net.Http", "http.client.request.time_in_queue"), Metric.DefaultHistogramBoundsSeconds }, - { ("System.Net.NameResolution", "dns.lookups.duration"), Metric.DefaultHistogramBoundsSeconds }, - }; private readonly object lockZeroTags = new(); private readonly object lockOverflowTag = new(); @@ -215,10 +200,10 @@ internal MetricPointsAccessor GetMetricPoints() private static double[] FindDefaultHistogramBounds(in MetricStreamIdentity metricStreamIdentity) { - if (metricStreamIdentity.Unit == "s" && DefaultHistogramBoundMappings - .TryGetValue((metricStreamIdentity.MeterName, metricStreamIdentity.InstrumentName), out double[] histogramBounds)) + if (metricStreamIdentity.Unit == "s" && Metric.DefaultHistogramBoundMappings + .Contains((metricStreamIdentity.MeterName, metricStreamIdentity.InstrumentName))) { - return histogramBounds; + return Metric.DefaultHistogramBoundsSeconds; } return Metric.DefaultHistogramBounds; diff --git a/src/OpenTelemetry/Metrics/Metric.cs b/src/OpenTelemetry/Metrics/Metric.cs index 515eec0a173..5828d64e194 100644 --- a/src/OpenTelemetry/Metrics/Metric.cs +++ b/src/OpenTelemetry/Metrics/Metric.cs @@ -29,6 +29,21 @@ public sealed class Metric internal static readonly double[] DefaultHistogramBounds = new double[] { 0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000 }; internal static readonly double[] DefaultHistogramBoundsSeconds = new double[] { 0, 0.005, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1, 2.5, 5, 7.5, 10 }; + internal static readonly HashSet<(string, string)> DefaultHistogramBoundMappings = new() + { + ("Microsoft.AspNetCore.Hosting", "http.server.request.duration"), + ("Microsoft.AspNetCore.Http.Connections", "signalr.server.connection.duration"), + ("Microsoft.AspNetCore.RateLimiting", "aspnetcore.rate_limiting.request.time_in_queue"), + ("Microsoft.AspNetCore.RateLimiting", "aspnetcore.rate_limiting.request_lease.duration"), + ("Microsoft.AspNetCore.Server.Kestrel", "kestrel.connection.duration"), + ("Microsoft.AspNetCore.Server.Kestrel", "kestrel.tls_handshake.duration"), + ("OpenTelemetry.Instrumentation.AspNetCore", "http.server.duration"), + ("OpenTelemetry.Instrumentation.Http", "http.client.duration"), + ("System.Net.Http", "http.client.connection.duration"), + ("System.Net.Http", "http.client.request.duration"), + ("System.Net.Http", "http.client.request.time_in_queue"), + ("System.Net.NameResolution", "dns.lookups.duration"), + }; private readonly AggregatorStore aggStore; diff --git a/test/OpenTelemetry.Tests/Metrics/AggregatorTestsBase.cs b/test/OpenTelemetry.Tests/Metrics/AggregatorTestsBase.cs index d994122f799..9e51d2ed09b 100644 --- a/test/OpenTelemetry.Tests/Metrics/AggregatorTestsBase.cs +++ b/test/OpenTelemetry.Tests/Metrics/AggregatorTestsBase.cs @@ -251,6 +251,7 @@ public void MultiThreadedHistogramUpdateAndSnapShotTest() [InlineData("System.Net.Http", "http.client.request.duration")] [InlineData("System.Net.Http", "http.client.request.time_in_queue")] [InlineData("System.Net.NameResolution", "dns.lookups.duration")] + [InlineData("General.App", "simple.alternative.counter")] public void HistogramBucketsDefaultUpdatesForSecondsTest(string meterName, string instrumentName) { RunTest(meterName, instrumentName, unit: "s"); @@ -275,7 +276,8 @@ void RunTest(string meterName, string instrumentName, string unit) Assert.NotNull(aggregatorStore.HistogramBounds); Assert.Equal( - unit == "s" ? Metric.DefaultHistogramBoundsSeconds : Metric.DefaultHistogramBounds, + unit == "s" && Metric.DefaultHistogramBoundMappings.Contains((meterName, instrumentName)) ? + Metric.DefaultHistogramBoundsSeconds : Metric.DefaultHistogramBounds, aggregatorStore.HistogramBounds); } }