|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.Diagnostics.Metrics; |
| 5 | +using System.Linq; |
| 6 | +using FirebirdSql.Data.FirebirdClient; |
| 7 | + |
| 8 | +namespace FirebirdSql.Data.Metrics |
| 9 | +{ |
| 10 | + internal static class FbMetricsStore |
| 11 | + { |
| 12 | + private const string ConnectionPoolNameAttributeName = "db.client.connection.pool.name"; |
| 13 | + private const string ConnectionStateAttributeName = "db.client.connection.state"; |
| 14 | + private const string ConnectionStateIdleValue = "idle"; |
| 15 | + private const string ConnectionStateUsedValue = "used"; |
| 16 | + |
| 17 | + internal static readonly Meter Source = new("FirebirdSql.Data", "1.0.0"); |
| 18 | + |
| 19 | + static readonly Histogram<double> OperationDuration; |
| 20 | + static readonly Histogram<double> ConnectionCreateTime; |
| 21 | + |
| 22 | + static FbMetricsStore() |
| 23 | + { |
| 24 | + // Reference: https://github.com/open-telemetry/semantic-conventions/blob/main/docs/database/database-spans.md |
| 25 | + |
| 26 | + OperationDuration = Source.CreateHistogram<double>( |
| 27 | + "db.client.operation.duration", |
| 28 | + unit: "s", |
| 29 | + description: "Duration of database client operations." |
| 30 | + ); |
| 31 | + |
| 32 | + Source.CreateObservableUpDownCounter( |
| 33 | + "db.client.connection.count", |
| 34 | + GetConnectionCount, |
| 35 | + unit: "{connection}", |
| 36 | + description: "The number of connections that are currently in state described by the 'state' attribute." |
| 37 | + ); |
| 38 | + |
| 39 | + // db.client.connection.idle.max |
| 40 | + // The maximum number of idle open connections allowed |
| 41 | + |
| 42 | + // db.client.connection.idle.min |
| 43 | + // The minimum number of idle open connections allowed |
| 44 | + |
| 45 | + Source.CreateObservableUpDownCounter( |
| 46 | + "db.client.connection.max", |
| 47 | + GetConnectionMax, |
| 48 | + unit: "{connection}", |
| 49 | + description: "The maximum number of open connections allowed." |
| 50 | + ); |
| 51 | + |
| 52 | + // db.client.connection.pending_requests |
| 53 | + // The number of current pending requests for an open connection |
| 54 | + |
| 55 | + // db.client.connection.timeouts |
| 56 | + // The number of connection timeouts that have occurred trying to obtain a connection from the pool |
| 57 | + |
| 58 | + ConnectionCreateTime = Source.CreateHistogram<double>( |
| 59 | + "db.client.connection.create_time", |
| 60 | + unit: "s", |
| 61 | + description: "The time it took to create a new connection." |
| 62 | + ); |
| 63 | + |
| 64 | + // db.client.connection.wait_time |
| 65 | + // The time it took to obtain an open connection from the pool |
| 66 | + |
| 67 | + // db.client.connection.use_time |
| 68 | + // The time between borrowing a connection and returning it to the pool |
| 69 | + } |
| 70 | + |
| 71 | + internal static long CommandStart() => Stopwatch.GetTimestamp(); |
| 72 | + |
| 73 | + internal static void CommandStop(long startedAtTicks, FbConnection connection) |
| 74 | + { |
| 75 | + if (OperationDuration.Enabled && startedAtTicks > 0) |
| 76 | + { |
| 77 | + var elapsedTicks = Stopwatch.GetTimestamp() - startedAtTicks; |
| 78 | + var elapsedSeconds = TimeSpan.FromTicks(elapsedTicks).TotalSeconds; |
| 79 | + |
| 80 | + OperationDuration.Record(elapsedSeconds, connection.MetricsConnectionAttributes); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + internal static long ConnectionOpening() => Stopwatch.GetTimestamp(); |
| 85 | + |
| 86 | + internal static void ConnectionOpened(long startedAtTicks, string poolName) |
| 87 | + { |
| 88 | + if (ConnectionCreateTime.Enabled && startedAtTicks > 0) |
| 89 | + { |
| 90 | + var elapsedTicks = Stopwatch.GetTimestamp() - startedAtTicks; |
| 91 | + var elapsedSeconds = TimeSpan.FromTicks(elapsedTicks).TotalSeconds; |
| 92 | + |
| 93 | + ConnectionCreateTime.Record(elapsedSeconds, [new(ConnectionPoolNameAttributeName, poolName)]); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + static IEnumerable<Measurement<int>> GetConnectionCount() => |
| 98 | + FbConnectionPoolManager.Instance.GetMetrics() |
| 99 | + .SelectMany(kvp => new List<Measurement<int>> |
| 100 | + { |
| 101 | + new( |
| 102 | + kvp.Value.idleCount, |
| 103 | + new(ConnectionPoolNameAttributeName, kvp.Key), |
| 104 | + new(ConnectionStateAttributeName, ConnectionStateIdleValue) |
| 105 | + ), |
| 106 | + |
| 107 | + new( |
| 108 | + kvp.Value.busyCount, |
| 109 | + new(ConnectionPoolNameAttributeName, kvp.Key), |
| 110 | + new(ConnectionStateAttributeName, ConnectionStateUsedValue) |
| 111 | + ), |
| 112 | + }); |
| 113 | + |
| 114 | + static IEnumerable<Measurement<int>> GetConnectionMax() => |
| 115 | + FbConnectionPoolManager.Instance.GetMetrics() |
| 116 | + .SelectMany(kvp => new List<Measurement<int>> |
| 117 | + { |
| 118 | + new( |
| 119 | + kvp.Value.maxSize, |
| 120 | + [new(ConnectionPoolNameAttributeName, kvp.Key)] |
| 121 | + ), |
| 122 | + }); |
| 123 | + } |
| 124 | +} |
0 commit comments