using System.Diagnostics; using System.Diagnostics.Metrics; namespace StellaOps.Cryptography; /// /// Telemetry diagnostics for crypto compliance operations. /// public sealed class CryptoComplianceDiagnostics : IDisposable { /// /// Activity source name for distributed tracing. /// public const string ActivitySourceName = "StellaOps.Crypto.Compliance"; /// /// Meter name for metrics. /// public const string MeterName = "StellaOps.Crypto.Compliance"; private readonly ActivitySource _activitySource; private readonly Meter _meter; // Counters private readonly Counter _hashOperations; private readonly Counter _complianceViolations; private readonly Histogram _hashDurationMs; public CryptoComplianceDiagnostics() { _activitySource = new ActivitySource(ActivitySourceName, "1.0.0"); _meter = new Meter(MeterName, "1.0.0"); _hashOperations = _meter.CreateCounter( name: "crypto.hash.operations", unit: "{operation}", description: "Total number of hash operations performed."); _complianceViolations = _meter.CreateCounter( name: "crypto.compliance.violations", unit: "{violation}", description: "Number of compliance violations detected."); _hashDurationMs = _meter.CreateHistogram( name: "crypto.hash.duration", unit: "ms", description: "Duration of hash operations in milliseconds."); } /// /// Starts an activity for a hash operation. /// public Activity? StartHashOperation(string purpose, string algorithm, string profile) { var activity = _activitySource.StartActivity("crypto.hash", ActivityKind.Internal); if (activity is not null) { activity.SetTag("crypto.purpose", purpose); activity.SetTag("crypto.algorithm", algorithm); activity.SetTag("crypto.profile", profile); } return activity; } /// /// Records a completed hash operation. /// public void RecordHashOperation( string profile, string purpose, string algorithm, TimeSpan duration, bool success = true) { var tags = new TagList { { "profile", profile }, { "purpose", purpose }, { "algorithm", algorithm }, { "success", success.ToString().ToLowerInvariant() } }; _hashOperations.Add(1, tags); _hashDurationMs.Record(duration.TotalMilliseconds, tags); } /// /// Records a compliance violation. /// public void RecordComplianceViolation( string profile, string purpose, string requestedAlgorithm, string expectedAlgorithm, bool wasBlocked) { var tags = new TagList { { "profile", profile }, { "purpose", purpose }, { "requested_algorithm", requestedAlgorithm }, { "expected_algorithm", expectedAlgorithm }, { "blocked", wasBlocked.ToString().ToLowerInvariant() } }; _complianceViolations.Add(1, tags); } /// /// Disposes of the diagnostics resources. /// public void Dispose() { _activitySource.Dispose(); _meter.Dispose(); } }