diff --git a/src/SignalR/common/Http.Connections/src/Internal/HttpConnectionsMetrics.cs b/src/SignalR/common/Http.Connections/src/Internal/HttpConnectionsMetrics.cs index a927ca3b3dcd..46556872bf05 100644 --- a/src/SignalR/common/Http.Connections/src/Internal/HttpConnectionsMetrics.cs +++ b/src/SignalR/common/Http.Connections/src/Internal/HttpConnectionsMetrics.cs @@ -31,12 +31,13 @@ public HttpConnectionsMetrics(IMeterFactory meterFactory) _meter = meterFactory.Create(MeterName); _connectionDuration = _meter.CreateHistogram( - "signalr-http-transport-connection-duration", + "signalr.server.connection.duration", unit: "s", description: "The duration of connections on the server."); _currentConnectionsCounter = _meter.CreateUpDownCounter( - "signalr-http-transport-current-connections", + "signalr.server.active_connections", + unit: "{connection}", description: "Number of connections that are currently active on the server."); } @@ -46,8 +47,8 @@ public void ConnectionStop(in MetricsContext metricsContext, HttpTransportType t { var duration = Stopwatch.GetElapsedTime(startTimestamp, currentTimestamp); _connectionDuration.Record(duration.TotalSeconds, - new KeyValuePair("status", status.ToString()), - new KeyValuePair("transport", transportType.ToString())); + new KeyValuePair("signalr.connection.status", ResolveStopStatus(status)), + new KeyValuePair("signalr.transport", ResolveTransportType(transportType))); } } @@ -58,7 +59,7 @@ public void ConnectionTransportStart(in MetricsContext metricsContext, HttpTrans // Tags must match transport end. if (metricsContext.CurrentConnectionsCounterEnabled) { - _currentConnectionsCounter.Add(1, new KeyValuePair("transport", transportType.ToString())); + _currentConnectionsCounter.Add(1, new KeyValuePair("signalr.transport", ResolveTransportType(transportType))); } } @@ -70,11 +71,33 @@ public void TransportStop(in MetricsContext metricsContext, HttpTransportType tr // If the transport type is none then the transport was never started for this connection. if (transportType != HttpTransportType.None) { - _currentConnectionsCounter.Add(-1, new KeyValuePair("transport", transportType.ToString())); + _currentConnectionsCounter.Add(-1, new KeyValuePair("signalr.transport", ResolveTransportType(transportType))); } } } + private static string ResolveTransportType(HttpTransportType transportType) + { + return transportType switch + { + HttpTransportType.ServerSentEvents => "server_sent_events", + HttpTransportType.LongPolling => "long_polling", + HttpTransportType.WebSockets => "web_sockets", + _ => throw new InvalidOperationException("Unexpected value: " + transportType) + }; + } + + private static string ResolveStopStatus(HttpConnectionStopStatus connectionStopStatus) + { + return connectionStopStatus switch + { + HttpConnectionStopStatus.NormalClosure => "normal_closure", + HttpConnectionStopStatus.Timeout => "timeout", + HttpConnectionStopStatus.AppShutdown => "app_shutdown", + _ => throw new InvalidOperationException("Unexpected value: " + connectionStopStatus) + }; + } + public void Dispose() { _meter.Dispose(); diff --git a/src/SignalR/common/Http.Connections/test/HttpConnectionDispatcherTests.cs b/src/SignalR/common/Http.Connections/test/HttpConnectionDispatcherTests.cs index 8774cc2ae5f7..4f1cdbd5c223 100644 --- a/src/SignalR/common/Http.Connections/test/HttpConnectionDispatcherTests.cs +++ b/src/SignalR/common/Http.Connections/test/HttpConnectionDispatcherTests.cs @@ -1094,8 +1094,8 @@ public async Task Metrics() using (StartVerifiableLog()) { var testMeterFactory = new TestMeterFactory(); - using var connectionDuration = new MetricCollector(testMeterFactory, HttpConnectionsMetrics.MeterName, "signalr-http-transport-connection-duration"); - using var currentConnections = new MetricCollector(testMeterFactory, HttpConnectionsMetrics.MeterName, "signalr-http-transport-current-connections"); + using var connectionDuration = new MetricCollector(testMeterFactory, HttpConnectionsMetrics.MeterName, "signalr.server.connection.duration"); + using var currentConnections = new MetricCollector(testMeterFactory, HttpConnectionsMetrics.MeterName, "signalr.server.active_connections"); var metrics = new HttpConnectionsMetrics(testMeterFactory); var manager = CreateConnectionManager(LoggerFactory, metrics); @@ -1122,8 +1122,8 @@ public async Task Metrics() var exists = manager.TryGetConnection(connection.ConnectionId, out _); Assert.False(exists); - Assert.Collection(connectionDuration.GetMeasurementSnapshot(), m => AssertDuration(m, HttpConnectionStopStatus.NormalClosure, HttpTransportType.LongPolling)); - Assert.Collection(currentConnections.GetMeasurementSnapshot(), m => AssertTransport(m, 1, HttpTransportType.LongPolling), m => AssertTransport(m, -1, HttpTransportType.LongPolling)); + Assert.Collection(connectionDuration.GetMeasurementSnapshot(), m => AssertDuration(m, "normal_closure", "long_polling")); + Assert.Collection(currentConnections.GetMeasurementSnapshot(), m => AssertTransport(m, 1, "long_polling"), m => AssertTransport(m, -1, "long_polling")); } } @@ -1150,8 +1150,8 @@ public async Task Metrics_ListenStartAfterConnection_Empty() await dispatcher.ExecuteAsync(context, new HttpConnectionDispatcherOptions(), app); Assert.Equal(StatusCodes.Status200OK, context.Response.StatusCode); - using var connectionDuration = new MetricCollector(testMeterFactory, HttpConnectionsMetrics.MeterName, "signalr-http-transport-connection-duration"); - using var currentConnections = new MetricCollector(testMeterFactory, HttpConnectionsMetrics.MeterName, "signalr-http-transport-current-connections"); + using var connectionDuration = new MetricCollector(testMeterFactory, HttpConnectionsMetrics.MeterName, "signalr.server.connection.duration"); + using var currentConnections = new MetricCollector(testMeterFactory, HttpConnectionsMetrics.MeterName, "signalr.server.active_connections"); await dispatcher.ExecuteAsync(context, new HttpConnectionDispatcherOptions(), app); @@ -1166,17 +1166,17 @@ public async Task Metrics_ListenStartAfterConnection_Empty() } } - private static void AssertTransport(CollectedMeasurement measurement, long expected, HttpTransportType transportType) + private static void AssertTransport(CollectedMeasurement measurement, long expected, string transportType) { Assert.Equal(expected, measurement.Value); - Assert.Equal(transportType.ToString(), (string)measurement.Tags["transport"]); + Assert.Equal(transportType.ToString(), (string)measurement.Tags["signalr.transport"]); } - private static void AssertDuration(CollectedMeasurement measurement, HttpConnectionStopStatus status, HttpTransportType transportType) + private static void AssertDuration(CollectedMeasurement measurement, string status, string transportType) { Assert.True(measurement.Value > 0); - Assert.Equal(status.ToString(), (string)measurement.Tags["status"]); - Assert.Equal(transportType.ToString(), (string)measurement.Tags["transport"]); + Assert.Equal(status.ToString(), (string)measurement.Tags["signalr.connection.status"]); + Assert.Equal(transportType.ToString(), (string)measurement.Tags["signalr.transport"]); } [Fact]