< Summary

Information
Class: RoleRadar.Domain.Common.RoleRadarStoreMetrics
Assembly: RoleRadar.Domain
File(s): /home/runner/work/roleradar/roleradar/src/RoleRadar.Domain/Common/RoleRadarStoreMetrics.cs
Line coverage
100%
Covered lines: 45
Uncovered lines: 0
Coverable lines: 45
Total lines: 102
Line coverage: 100%
Branch coverage
100%
Covered branches: 8
Total branches: 8
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%22100%
RecordOperation(...)100%44100%
RecordReadiness(...)100%22100%
ResetForTesting()100%11100%

File(s)

/home/runner/work/roleradar/roleradar/src/RoleRadar.Domain/Common/RoleRadarStoreMetrics.cs

#LineLine coverage
 1using System.Collections.Concurrent;
 2using System.Diagnostics;
 3using System.Diagnostics.Metrics;
 4
 5namespace 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>
 13public 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
 23825    public static readonly Meter MeterInstance = new(MeterName);
 26
 23827    public static readonly Histogram<double> OperationDuration =
 23828        MeterInstance.CreateHistogram<double>(
 23829            OperationDurationInstrument,
 23830            unit: "s",
 23831            description: "Duration of datastore operations");
 32
 23833    public static readonly Histogram<double> ReadinessDuration =
 23834        MeterInstance.CreateHistogram<double>(
 23835            ReadinessDurationInstrument,
 23836            unit: "s",
 23837            description: "Duration of datastore readiness probe");
 38
 23839    private static readonly ConcurrentDictionary<string, int> LastReadinessStatusBySystem = new(StringComparer.Ordinal);
 40
 23841    public static readonly ObservableGauge<int> ReadinessStatus =
 23842        MeterInstance.CreateObservableGauge<int>(
 23843            ReadinessStatusInstrument,
 8844            () => LastReadinessStatusBySystem.Select(kvp => new Measurement<int>(
 8845                kvp.Value,
 8846                new KeyValuePair<string, object?>("db.system.name", kvp.Key),
 8847                new KeyValuePair<string, object?>("status", kvp.Value == 1 ? StatusHealthy : StatusUnhealthy))),
 23848            unit: "{status}",
 23849            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    {
 3401058        ArgumentException.ThrowIfNullOrWhiteSpace(systemName);
 3401059        ArgumentException.ThrowIfNullOrWhiteSpace(operationName);
 60
 3401061        var tags = new TagList
 3401062        {
 3401063            { "db.system.name", systemName },
 3401064            { "db.operation.name", operationName }
 3401065        };
 66
 3401067        if (!string.IsNullOrWhiteSpace(collectionName))
 68        {
 3121669            tags.Add("db.collection.name", collectionName);
 70        }
 71
 3401072        if (!string.IsNullOrWhiteSpace(errorType))
 73        {
 29674            tags.Add("error.type", errorType);
 75        }
 76
 3401077        OperationDuration.Record(durationSeconds, tags);
 3401078    }
 79
 80    public static void RecordReadiness(
 81        string systemName,
 82        bool healthy,
 83        double durationSeconds)
 84    {
 107485        ArgumentException.ThrowIfNullOrWhiteSpace(systemName);
 86
 107487        LastReadinessStatusBySystem[systemName] = healthy ? 1 : 0;
 88
 107489        var tags = new TagList
 107490        {
 107491            { "db.system.name", systemName },
 107492            { "status", healthy ? StatusHealthy : StatusUnhealthy }
 107493        };
 94
 107495        ReadinessDuration.Record(durationSeconds, tags);
 107496    }
 97
 98    public static void ResetForTesting()
 99    {
 4100        LastReadinessStatusBySystem.Clear();
 4101    }
 102}