| | | 1 | | using System.Collections.Concurrent; |
| | | 2 | | using System.Diagnostics; |
| | | 3 | | using System.Diagnostics.Metrics; |
| | | 4 | | |
| | | 5 | | namespace RoleRadar.Domain.Common; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Feature 015 Gate 4 (RR-69, RR-72, ADR 0052): |
| | | 9 | | /// Metrics for datastore command duration, readiness status, and readiness probe duration. |
| | | 10 | | /// Defined in Domain so both Infrastructure and Identity may reference them directly without |
| | | 11 | | /// violating layering rules. |
| | | 12 | | /// </summary> |
| | | 13 | | public static class RoleRadarStoreMetrics |
| | | 14 | | { |
| | | 15 | | public const string MeterName = "RoleRadar.Store"; |
| | | 16 | | public const string OperationDurationInstrument = "roleradar.store.operation.duration"; |
| | | 17 | | public const string ReadinessStatusInstrument = "roleradar.store.readiness.status"; |
| | | 18 | | public const string ReadinessDurationInstrument = "roleradar.store.readiness.duration"; |
| | | 19 | | |
| | | 20 | | public const string SystemMongoDb = "mongodb"; |
| | | 21 | | public const string SystemSqlite = "sqlite"; |
| | | 22 | | public const string StatusHealthy = "healthy"; |
| | | 23 | | public const string StatusUnhealthy = "unhealthy"; |
| | | 24 | | |
| | 238 | 25 | | public static readonly Meter MeterInstance = new(MeterName); |
| | | 26 | | |
| | 238 | 27 | | public static readonly Histogram<double> OperationDuration = |
| | 238 | 28 | | MeterInstance.CreateHistogram<double>( |
| | 238 | 29 | | OperationDurationInstrument, |
| | 238 | 30 | | unit: "s", |
| | 238 | 31 | | description: "Duration of datastore operations"); |
| | | 32 | | |
| | 238 | 33 | | public static readonly Histogram<double> ReadinessDuration = |
| | 238 | 34 | | MeterInstance.CreateHistogram<double>( |
| | 238 | 35 | | ReadinessDurationInstrument, |
| | 238 | 36 | | unit: "s", |
| | 238 | 37 | | description: "Duration of datastore readiness probe"); |
| | | 38 | | |
| | 238 | 39 | | private static readonly ConcurrentDictionary<string, int> LastReadinessStatusBySystem = new(StringComparer.Ordinal); |
| | | 40 | | |
| | 238 | 41 | | public static readonly ObservableGauge<int> ReadinessStatus = |
| | 238 | 42 | | MeterInstance.CreateObservableGauge<int>( |
| | 238 | 43 | | ReadinessStatusInstrument, |
| | 88 | 44 | | () => LastReadinessStatusBySystem.Select(kvp => new Measurement<int>( |
| | 88 | 45 | | kvp.Value, |
| | 88 | 46 | | new KeyValuePair<string, object?>("db.system.name", kvp.Key), |
| | 88 | 47 | | new KeyValuePair<string, object?>("status", kvp.Value == 1 ? StatusHealthy : StatusUnhealthy))), |
| | 238 | 48 | | unit: "{status}", |
| | 238 | 49 | | description: "Datastore readiness status (1 = healthy, 0 = unhealthy)"); |
| | | 50 | | |
| | | 51 | | public static void RecordOperation( |
| | | 52 | | double durationSeconds, |
| | | 53 | | string systemName, |
| | | 54 | | string operationName, |
| | | 55 | | string? collectionName = null, |
| | | 56 | | string? errorType = null) |
| | | 57 | | { |
| | 34010 | 58 | | ArgumentException.ThrowIfNullOrWhiteSpace(systemName); |
| | 34010 | 59 | | ArgumentException.ThrowIfNullOrWhiteSpace(operationName); |
| | | 60 | | |
| | 34010 | 61 | | var tags = new TagList |
| | 34010 | 62 | | { |
| | 34010 | 63 | | { "db.system.name", systemName }, |
| | 34010 | 64 | | { "db.operation.name", operationName } |
| | 34010 | 65 | | }; |
| | | 66 | | |
| | 34010 | 67 | | if (!string.IsNullOrWhiteSpace(collectionName)) |
| | | 68 | | { |
| | 31216 | 69 | | tags.Add("db.collection.name", collectionName); |
| | | 70 | | } |
| | | 71 | | |
| | 34010 | 72 | | if (!string.IsNullOrWhiteSpace(errorType)) |
| | | 73 | | { |
| | 296 | 74 | | tags.Add("error.type", errorType); |
| | | 75 | | } |
| | | 76 | | |
| | 34010 | 77 | | OperationDuration.Record(durationSeconds, tags); |
| | 34010 | 78 | | } |
| | | 79 | | |
| | | 80 | | public static void RecordReadiness( |
| | | 81 | | string systemName, |
| | | 82 | | bool healthy, |
| | | 83 | | double durationSeconds) |
| | | 84 | | { |
| | 1074 | 85 | | ArgumentException.ThrowIfNullOrWhiteSpace(systemName); |
| | | 86 | | |
| | 1074 | 87 | | LastReadinessStatusBySystem[systemName] = healthy ? 1 : 0; |
| | | 88 | | |
| | 1074 | 89 | | var tags = new TagList |
| | 1074 | 90 | | { |
| | 1074 | 91 | | { "db.system.name", systemName }, |
| | 1074 | 92 | | { "status", healthy ? StatusHealthy : StatusUnhealthy } |
| | 1074 | 93 | | }; |
| | | 94 | | |
| | 1074 | 95 | | ReadinessDuration.Record(durationSeconds, tags); |
| | 1074 | 96 | | } |
| | | 97 | | |
| | | 98 | | public static void ResetForTesting() |
| | | 99 | | { |
| | 4 | 100 | | LastReadinessStatusBySystem.Clear(); |
| | 4 | 101 | | } |
| | | 102 | | } |