Files
git.stella-ops.org/src/__Libraries/StellaOps.Cryptography/CryptoComplianceDiagnostics.cs
master cc69d332e3
Some checks failed
Docs CI / lint-and-preview (push) Has been cancelled
Add unit tests for RabbitMq and Udp transport servers and clients
- Implemented comprehensive unit tests for RabbitMqTransportServer, covering constructor, disposal, connection management, event handlers, and exception handling.
- Added configuration tests for RabbitMqTransportServer to validate SSL, durable queues, auto-recovery, and custom virtual host options.
- Created unit tests for UdpFrameProtocol, including frame parsing and serialization, header size validation, and round-trip data preservation.
- Developed tests for UdpTransportClient, focusing on connection handling, event subscriptions, and exception scenarios.
- Established tests for UdpTransportServer, ensuring proper start/stop behavior, connection state management, and event handling.
- Included tests for UdpTransportOptions to verify default values and modification capabilities.
- Enhanced service registration tests for Udp transport services in the dependency injection container.
2025-12-05 19:01:12 +02:00

118 lines
3.5 KiB
C#

using System.Diagnostics;
using System.Diagnostics.Metrics;
namespace StellaOps.Cryptography;
/// <summary>
/// Telemetry diagnostics for crypto compliance operations.
/// </summary>
public sealed class CryptoComplianceDiagnostics : IDisposable
{
/// <summary>
/// Activity source name for distributed tracing.
/// </summary>
public const string ActivitySourceName = "StellaOps.Crypto.Compliance";
/// <summary>
/// Meter name for metrics.
/// </summary>
public const string MeterName = "StellaOps.Crypto.Compliance";
private readonly ActivitySource _activitySource;
private readonly Meter _meter;
// Counters
private readonly Counter<long> _hashOperations;
private readonly Counter<long> _complianceViolations;
private readonly Histogram<double> _hashDurationMs;
public CryptoComplianceDiagnostics()
{
_activitySource = new ActivitySource(ActivitySourceName, "1.0.0");
_meter = new Meter(MeterName, "1.0.0");
_hashOperations = _meter.CreateCounter<long>(
name: "crypto.hash.operations",
unit: "{operation}",
description: "Total number of hash operations performed.");
_complianceViolations = _meter.CreateCounter<long>(
name: "crypto.compliance.violations",
unit: "{violation}",
description: "Number of compliance violations detected.");
_hashDurationMs = _meter.CreateHistogram<double>(
name: "crypto.hash.duration",
unit: "ms",
description: "Duration of hash operations in milliseconds.");
}
/// <summary>
/// Starts an activity for a hash operation.
/// </summary>
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;
}
/// <summary>
/// Records a completed hash operation.
/// </summary>
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);
}
/// <summary>
/// Records a compliance violation.
/// </summary>
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);
}
/// <summary>
/// Disposes of the diagnostics resources.
/// </summary>
public void Dispose()
{
_activitySource.Dispose();
_meter.Dispose();
}
}