| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | using System.Diagnostics; |
| | 4 | | using System.Diagnostics.Metrics; |
| | 5 | |
|
| | 6 | | namespace IceRpc.Internal; |
| | 7 | |
|
| | 8 | | /// <summary>A helper class used to report client and server connection metrics.</summary> |
| | 9 | | internal sealed class Metrics : IDisposable |
| | 10 | | { |
| 4 | 11 | | internal static readonly Metrics ClientMetrics = new("IceRpc.Client"); |
| 4 | 12 | | internal static readonly Metrics ServerMetrics = new("IceRpc.Server"); |
| | 13 | |
|
| | 14 | | // The number of active connections. |
| | 15 | | private long _activeConnections; |
| | 16 | |
|
| | 17 | | private readonly Meter _meter; |
| | 18 | |
|
| | 19 | | // The number of connections that are being connected. |
| | 20 | | private long _pendingConnections; |
| | 21 | |
|
| | 22 | | // The number of connection that have been created. |
| | 23 | | private long _totalConnections; |
| | 24 | |
|
| | 25 | | // The number of connections that start connecting and failed to connect, or connected successfully but |
| | 26 | | // later terminate due to a failure. |
| | 27 | | private long _totalFailedConnections; |
| | 28 | |
|
| | 29 | | /// <inheritdoc/> |
| 6 | 30 | | public void Dispose() => _meter.Dispose(); |
| | 31 | |
|
| 14 | 32 | | internal Metrics(string meterName) |
| 14 | 33 | | { |
| 14 | 34 | | _meter = new Meter(meterName); |
| | 35 | |
|
| 14 | 36 | | _meter.CreateObservableUpDownCounter( |
| 14 | 37 | | "active-connections", |
| 6 | 38 | | () => Volatile.Read(ref _activeConnections), |
| 14 | 39 | | "Connections", |
| 14 | 40 | | "Active Connections"); |
| | 41 | |
|
| 14 | 42 | | _meter.CreateObservableUpDownCounter( |
| 14 | 43 | | "pending-connections", |
| 6 | 44 | | () => Volatile.Read(ref _pendingConnections), |
| 14 | 45 | | "Connections", |
| 14 | 46 | | "Pending Connections"); |
| | 47 | |
|
| 14 | 48 | | _meter.CreateObservableCounter( |
| 14 | 49 | | "total-connections", |
| 6 | 50 | | () => Volatile.Read(ref _totalConnections), |
| 14 | 51 | | "Connections", |
| 14 | 52 | | "Total Connections"); |
| | 53 | |
|
| 14 | 54 | | _meter.CreateObservableCounter( |
| 14 | 55 | | "total-failed-connections", |
| 6 | 56 | | () => Volatile.Read(ref _totalFailedConnections), |
| 14 | 57 | | "Connections", |
| 14 | 58 | | "Total Failed Connections"); |
| 14 | 59 | | } |
| | 60 | |
|
| | 61 | | internal void ConnectStart() |
| 313 | 62 | | { |
| 313 | 63 | | Debug.Assert(_totalConnections >= 0); |
| 313 | 64 | | Debug.Assert(_pendingConnections >= 0); |
| 313 | 65 | | Interlocked.Increment(ref _totalConnections); |
| 313 | 66 | | Interlocked.Increment(ref _pendingConnections); |
| 313 | 67 | | } |
| | 68 | |
|
| | 69 | | internal void ConnectStop() |
| 311 | 70 | | { |
| 311 | 71 | | Debug.Assert(_pendingConnections > 0); |
| 311 | 72 | | Interlocked.Decrement(ref _pendingConnections); |
| 311 | 73 | | } |
| | 74 | |
|
| | 75 | | internal void ConnectSuccess() |
| 235 | 76 | | { |
| 235 | 77 | | Debug.Assert(_activeConnections >= 0); |
| 235 | 78 | | Interlocked.Increment(ref _activeConnections); |
| 235 | 79 | | } |
| | 80 | |
|
| | 81 | | internal void ConnectionFailure() |
| 227 | 82 | | { |
| 227 | 83 | | Debug.Assert(_totalFailedConnections >= 0); |
| 227 | 84 | | Debug.Assert(_totalFailedConnections < _totalConnections); |
| 227 | 85 | | Interlocked.Increment(ref _totalFailedConnections); |
| 227 | 86 | | } |
| | 87 | |
|
| | 88 | | internal void ConnectionDisconnected() |
| 231 | 89 | | { |
| 231 | 90 | | Debug.Assert(_activeConnections > 0); |
| 231 | 91 | | Interlocked.Decrement(ref _activeConnections); |
| 231 | 92 | | } |
| | 93 | | } |