Skip to content

Annotate histograms with bucket boundaries #56500

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
merged 3 commits into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Hosting/Hosting/src/Internal/HostingMetrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public HostingMetrics(IMeterFactory meterFactory)
_requestDuration = _meter.CreateHistogram<double>(
"http.server.request.duration",
unit: "s",
description: "Duration of HTTP server requests.");
description: "Duration of HTTP server requests.",
advice: new InstrumentAdvice<double> { HistogramBucketBoundaries = MetricsConstants.ShortSecondsBucketBoundaries });
}

// Note: Calling code checks whether counter is enabled.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<Compile Include="$(SharedSourceRoot)ErrorPage\**\*.cs" />
<Compile Include="$(SharedSourceRoot)StaticWebAssets\**\*.cs" LinkBase="StaticWebAssets" />
<Compile Include="$(SharedSourceRoot)Metrics\MetricsExtensions.cs" />
<Compile Include="$(SharedSourceRoot)Metrics\MetricsConstants.cs" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<Reference Include="System.Threading.RateLimiting" />

<Compile Include="$(SharedSourceRoot)ValueStopwatch\*.cs" />
<Compile Include="$(SharedSourceRoot)Metrics\MetricsConstants.cs" />
</ItemGroup>

<ItemGroup>
Expand Down
7 changes: 5 additions & 2 deletions src/Middleware/RateLimiting/src/RateLimitingMetrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Diagnostics;
using System.Diagnostics.Metrics;
using System.Runtime.CompilerServices;
using Microsoft.AspNetCore.Http;

namespace Microsoft.AspNetCore.RateLimiting;

Expand All @@ -30,7 +31,8 @@ public RateLimitingMetrics(IMeterFactory meterFactory)
_requestLeaseDurationCounter = _meter.CreateHistogram<double>(
"aspnetcore.rate_limiting.request_lease.duration",
unit: "s",
description: "The duration of rate limiting leases held by HTTP requests on the server.");
description: "The duration of rate limiting leases held by HTTP requests on the server.",
advice: new InstrumentAdvice<double> { HistogramBucketBoundaries = MetricsConstants.ShortSecondsBucketBoundaries });

_queuedRequestsCounter = _meter.CreateUpDownCounter<long>(
"aspnetcore.rate_limiting.queued_requests",
Expand All @@ -40,7 +42,8 @@ public RateLimitingMetrics(IMeterFactory meterFactory)
_queuedRequestDurationCounter = _meter.CreateHistogram<double>(
"aspnetcore.rate_limiting.request.time_in_queue",
unit: "s",
description: "The duration of HTTP requests in a queue, waiting to acquire a rate limiting lease.");
description: "The duration of HTTP requests in a queue, waiting to acquire a rate limiting lease.",
advice: new InstrumentAdvice<double> { HistogramBucketBoundaries = MetricsConstants.ShortSecondsBucketBoundaries });

_requestsCounter = _meter.CreateCounter<long>(
"aspnetcore.rate_limiting.requests",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Runtime.CompilerServices;
using System.Security.Authentication;
using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.Http;

namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure;

Expand Down Expand Up @@ -43,7 +44,8 @@ public KestrelMetrics(IMeterFactory meterFactory)
_connectionDuration = _meter.CreateHistogram<double>(
"kestrel.connection.duration",
unit: "s",
description: "The duration of connections on the server.");
description: "The duration of connections on the server.",
advice: new InstrumentAdvice<double> { HistogramBucketBoundaries = MetricsConstants.LongSecondsBucketBoundaries });

_rejectedConnectionsCounter = _meter.CreateCounter<long>(
"kestrel.rejected_connections",
Expand All @@ -68,7 +70,8 @@ public KestrelMetrics(IMeterFactory meterFactory)
_tlsHandshakeDuration = _meter.CreateHistogram<double>(
"kestrel.tls_handshake.duration",
unit: "s",
description: "The duration of TLS handshakes on the server.");
description: "The duration of TLS handshakes on the server.",
advice: new InstrumentAdvice<double> { HistogramBucketBoundaries = MetricsConstants.ShortSecondsBucketBoundaries });

_activeTlsHandshakesCounter = _meter.CreateUpDownCounter<long>(
"kestrel.active_tls_handshakes",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<Compile Include="$(SharedSourceRoot)CancellationTokenSourcePool.cs" />
<Compile Include="$(SharedSourceRoot)Debugger\DictionaryItemDebugView.cs" LinkBase="Shared" />
<Compile Include="$(SharedSourceRoot)Debugger\StringValuesDictionaryDebugView.cs" LinkBase="Shared" />
<Compile Include="$(SharedSourceRoot)Metrics\MetricsConstants.cs" LinkBase="Shared" />
<Compile Include="$(RepoRoot)src\Shared\TaskToApm.cs" Link="Internal\TaskToApm.cs" />
</ItemGroup>

Expand Down
13 changes: 13 additions & 0 deletions src/Shared/Metrics/MetricsConstants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.AspNetCore.Http;

internal static class MetricsConstants
{
// Follows boundaries from http.server.request.duration/http.client.request.duration
public static readonly IReadOnlyList<double> ShortSecondsBucketBoundaries = [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];

// Not based on a standard. Larger bucket sizes for longer lasting operations, e.g. HTTP connection duration. See https://github.com/open-telemetry/semantic-conventions/issues/336
public static readonly IReadOnlyList<double> LongSecondsBucketBoundaries = [0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1, 2, 5, 10, 30, 60, 120, 300];
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public HttpConnectionsMetrics(IMeterFactory meterFactory)
_connectionDuration = _meter.CreateHistogram<double>(
"signalr.server.connection.duration",
unit: "s",
description: "The duration of connections on the server.");
description: "The duration of connections on the server.",
advice: new InstrumentAdvice<double> { HistogramBucketBoundaries = MetricsConstants.LongSecondsBucketBoundaries });

_currentConnectionsCounter = _meter.CreateUpDownCounter<long>(
"signalr.server.active_connections",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<Compile Include="$(SharedSourceRoot)WebEncoders\**\*.cs" />
<Compile Include="$(SharedSourceRoot)ValueTaskExtensions\**\*.cs" />
<Compile Include="$(SharedSourceRoot)NonCapturingTimer\*.cs" />
<Compile Include="$(SharedSourceRoot)Metrics\MetricsConstants.cs" />
<Compile Include="$(SharedSourceRoot)ThrowHelpers\ArgumentNullThrowHelper.cs" LinkBase="Shared" />
<Compile Include="$(SharedSourceRoot)ThrowHelpers\ArgumentOutOfRangeThrowHelper.cs" LinkBase="Shared" />
<Compile Include="$(SharedSourceRoot)CallerArgument\CallerArgumentExpressionAttribute.cs" LinkBase="Shared" />
Expand Down
Loading