Skip to content

Commit 7bd1a95

Browse files
committed
Refactor context creation
1 parent 3c438be commit 7bd1a95

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

src/Servers/Kestrel/Core/src/Internal/ConnectionDispatcher.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,7 @@ async Task AcceptConnectionsAsync()
5454

5555
// Cache counter enabled state at the start of the connection.
5656
// This ensures that the state is consistent for the entire connection.
57-
var metricsContext = new ConnectionMetricsContext(connection,
58-
Metrics.CurrentConnectionsCounterEnabled, Metrics.ConnectionDurationEnabled, Metrics.QueuedConnectionsCounterEnabled,
59-
Metrics.QueuedRequestsCounterEnabled, Metrics.CurrentUpgradedRequestsCounterEnabled, Metrics.CurrentTlsHandshakesCounterEnabled);
57+
var metricsContext = Metrics.CreateContext(connection);
6058

6159
var kestrelConnection = new KestrelConnection<T>(
6260
id, _serviceContext, _transportConnectionManager, _connectionDelegate, connection, Log, metricsContext);

src/Servers/Kestrel/Core/src/Internal/Infrastructure/KestrelMetrics.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using Microsoft.AspNetCore.Connections;
45
using Microsoft.Extensions.Metrics;
56
using System.Diagnostics;
67
using System.Diagnostics.Metrics;
@@ -24,13 +25,6 @@ internal sealed class KestrelMetrics
2425
private readonly Histogram<double> _tlsHandshakeDuration;
2526
private readonly UpDownCounter<long> _currentTlsHandshakesCounter;
2627

27-
public bool CurrentConnectionsCounterEnabled => _currentConnectionsCounter.Enabled;
28-
public bool ConnectionDurationEnabled => _connectionDuration.Enabled;
29-
public bool QueuedConnectionsCounterEnabled => _queuedConnectionsCounter.Enabled;
30-
public bool QueuedRequestsCounterEnabled => _queuedRequestsCounter.Enabled;
31-
public bool CurrentUpgradedRequestsCounterEnabled => _currentUpgradedRequestsCounter.Enabled;
32-
public bool CurrentTlsHandshakesCounterEnabled => _currentTlsHandshakesCounter.Enabled;
33-
3428
public KestrelMetrics(IMeterFactory meterFactory)
3529
{
3630
_meter = meterFactory.CreateMeter(MeterName);
@@ -296,9 +290,15 @@ private static void InitializeConnectionTags(ref TagList tags, in ConnectionMetr
296290
if (metricsContext.ConnectionContext.LocalEndPoint is { } localEndpoint)
297291
{
298292
// TODO: Improve getting string allocation for endpoint. Currently allocates.
299-
// Possible solution is to cache in the endpoint: https://github.com/dotnet/runtime/issues/84515
300-
// Alternatively, add cache to ConnectionContext.
293+
// Considering adding a way to cache on ConnectionContext.
301294
tags.Add("endpoint", localEndpoint.ToString());
302295
}
303296
}
297+
298+
public ConnectionMetricsContext CreateContext(BaseConnectionContext connection)
299+
{
300+
return new ConnectionMetricsContext(connection,
301+
_currentConnectionsCounter.Enabled, _connectionDuration.Enabled, _queuedConnectionsCounter.Enabled,
302+
_queuedRequestsCounter.Enabled, _currentUpgradedRequestsCounter.Enabled, _currentTlsHandshakesCounter.Enabled);
303+
}
304304
}

0 commit comments

Comments
 (0)