Some checks failed
Docs CI / lint-and-preview (push) Has been cancelled
AOC Guard CI / aoc-guard (push) Has been cancelled
AOC Guard CI / aoc-verify (push) Has been cancelled
Concelier Attestation Tests / attestation-tests (push) Has been cancelled
Export Center CI / export-ci (push) Has been cancelled
Notify Smoke Test / Notify Unit Tests (push) Has been cancelled
Notify Smoke Test / Notifier Service Tests (push) Has been cancelled
Notify Smoke Test / Notification Smoke Test (push) Has been cancelled
Policy Lint & Smoke / policy-lint (push) Has been cancelled
Scanner Analyzers / Discover Analyzers (push) Has been cancelled
Scanner Analyzers / Build Analyzers (push) Has been cancelled
Scanner Analyzers / Test Language Analyzers (push) Has been cancelled
Scanner Analyzers / Validate Test Fixtures (push) Has been cancelled
Scanner Analyzers / Verify Deterministic Output (push) Has been cancelled
Signals CI & Image / signals-ci (push) Has been cancelled
Signals Reachability Scoring & Events / reachability-smoke (push) Has been cancelled
Signals Reachability Scoring & Events / sign-and-upload (push) Has been cancelled
244 lines
7.3 KiB
C#
244 lines
7.3 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using StellaOps.Notifier.Worker.Retention;
|
|
|
|
namespace StellaOps.Notifier.Worker.Observability;
|
|
|
|
/// <summary>
|
|
/// Extension methods for registering observability services.
|
|
/// </summary>
|
|
public static class ObservabilityServiceExtensions
|
|
{
|
|
/// <summary>
|
|
/// Adds all observability services (metrics, tracing, dead-letter, chaos, retention).
|
|
/// </summary>
|
|
public static IServiceCollection AddNotifierObservability(
|
|
this IServiceCollection services,
|
|
IConfiguration configuration)
|
|
{
|
|
return services
|
|
.AddNotifierMetrics(configuration)
|
|
.AddNotifierTracing(configuration)
|
|
.AddDeadLetterHandling(configuration)
|
|
.AddChaosEngine(configuration)
|
|
.AddRetentionPolicies(configuration);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds notifier metrics services.
|
|
/// </summary>
|
|
public static IServiceCollection AddNotifierMetrics(
|
|
this IServiceCollection services,
|
|
IConfiguration configuration)
|
|
{
|
|
services.Configure<NotifierMetricsOptions>(
|
|
configuration.GetSection(NotifierMetricsOptions.SectionName));
|
|
|
|
services.AddSingleton<INotifierMetrics, DefaultNotifierMetrics>();
|
|
|
|
return services;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds notifier tracing services.
|
|
/// </summary>
|
|
public static IServiceCollection AddNotifierTracing(
|
|
this IServiceCollection services,
|
|
IConfiguration configuration)
|
|
{
|
|
services.Configure<NotifierTracingOptions>(
|
|
configuration.GetSection(NotifierTracingOptions.SectionName));
|
|
|
|
services.AddSingleton<INotifierTracing, DefaultNotifierTracing>();
|
|
|
|
return services;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds dead-letter handling services.
|
|
/// </summary>
|
|
public static IServiceCollection AddDeadLetterHandling(
|
|
this IServiceCollection services,
|
|
IConfiguration configuration)
|
|
{
|
|
services.Configure<DeadLetterOptions>(
|
|
configuration.GetSection(DeadLetterOptions.SectionName));
|
|
|
|
services.AddSingleton<IDeadLetterHandler, InMemoryDeadLetterHandler>();
|
|
|
|
return services;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds chaos engine services.
|
|
/// </summary>
|
|
public static IServiceCollection AddChaosEngine(
|
|
this IServiceCollection services,
|
|
IConfiguration configuration)
|
|
{
|
|
services.Configure<ChaosEngineOptions>(
|
|
configuration.GetSection(ChaosEngineOptions.SectionName));
|
|
|
|
services.AddSingleton<IChaosEngine, DefaultChaosEngine>();
|
|
|
|
return services;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds retention policy services.
|
|
/// </summary>
|
|
public static IServiceCollection AddRetentionPolicies(
|
|
this IServiceCollection services,
|
|
IConfiguration configuration)
|
|
{
|
|
services.Configure<RetentionOptions>(
|
|
configuration.GetSection(RetentionOptions.SectionName));
|
|
|
|
services.AddSingleton<IRetentionPolicyService, DefaultRetentionPolicyService>();
|
|
|
|
return services;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Builder for customizing observability services.
|
|
/// </summary>
|
|
public static ObservabilityServiceBuilder AddNotifierObservability(this IServiceCollection services)
|
|
{
|
|
return new ObservabilityServiceBuilder(services);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Builder for customizing observability services.
|
|
/// </summary>
|
|
public sealed class ObservabilityServiceBuilder
|
|
{
|
|
private readonly IServiceCollection _services;
|
|
|
|
public ObservabilityServiceBuilder(IServiceCollection services)
|
|
{
|
|
_services = services ?? throw new ArgumentNullException(nameof(services));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Configures metrics options.
|
|
/// </summary>
|
|
public ObservabilityServiceBuilder ConfigureMetrics(Action<NotifierMetricsOptions> configure)
|
|
{
|
|
_services.Configure(configure);
|
|
return this;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Configures tracing options.
|
|
/// </summary>
|
|
public ObservabilityServiceBuilder ConfigureTracing(Action<NotifierTracingOptions> configure)
|
|
{
|
|
_services.Configure(configure);
|
|
return this;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Configures dead-letter options.
|
|
/// </summary>
|
|
public ObservabilityServiceBuilder ConfigureDeadLetter(Action<DeadLetterOptions> configure)
|
|
{
|
|
_services.Configure(configure);
|
|
return this;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Configures chaos engine options.
|
|
/// </summary>
|
|
public ObservabilityServiceBuilder ConfigureChaos(Action<ChaosEngineOptions> configure)
|
|
{
|
|
_services.Configure(configure);
|
|
return this;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Configures retention options.
|
|
/// </summary>
|
|
public ObservabilityServiceBuilder ConfigureRetention(Action<RetentionOptions> configure)
|
|
{
|
|
_services.Configure(configure);
|
|
return this;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Uses a custom metrics implementation.
|
|
/// </summary>
|
|
public ObservabilityServiceBuilder UseCustomMetrics<T>() where T : class, INotifierMetrics
|
|
{
|
|
_services.AddSingleton<INotifierMetrics, T>();
|
|
return this;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Uses a custom tracing implementation.
|
|
/// </summary>
|
|
public ObservabilityServiceBuilder UseCustomTracing<T>() where T : class, INotifierTracing
|
|
{
|
|
_services.AddSingleton<INotifierTracing, T>();
|
|
return this;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Uses a custom dead-letter handler.
|
|
/// </summary>
|
|
public ObservabilityServiceBuilder UseCustomDeadLetterHandler<T>() where T : class, IDeadLetterHandler
|
|
{
|
|
_services.AddSingleton<IDeadLetterHandler, T>();
|
|
return this;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Uses a custom chaos engine.
|
|
/// </summary>
|
|
public ObservabilityServiceBuilder UseCustomChaosEngine<T>() where T : class, IChaosEngine
|
|
{
|
|
_services.AddSingleton<IChaosEngine, T>();
|
|
return this;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Uses a custom retention policy service.
|
|
/// </summary>
|
|
public ObservabilityServiceBuilder UseCustomRetentionService<T>() where T : class, IRetentionPolicyService
|
|
{
|
|
_services.AddSingleton<IRetentionPolicyService, T>();
|
|
return this;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Builds the services with default implementations.
|
|
/// </summary>
|
|
public IServiceCollection Build()
|
|
{
|
|
// Register defaults if not already registered
|
|
_services.TryAddSingleton<INotifierMetrics, DefaultNotifierMetrics>();
|
|
_services.TryAddSingleton<INotifierTracing, DefaultNotifierTracing>();
|
|
_services.TryAddSingleton<IDeadLetterHandler, InMemoryDeadLetterHandler>();
|
|
_services.TryAddSingleton<IChaosEngine, DefaultChaosEngine>();
|
|
_services.TryAddSingleton<IRetentionPolicyService, DefaultRetentionPolicyService>();
|
|
|
|
return _services;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Extension methods for service collection to ensure singleton registration.
|
|
/// </summary>
|
|
file static class ServiceCollectionExtensions
|
|
{
|
|
public static void TryAddSingleton<TService, TImplementation>(this IServiceCollection services)
|
|
where TService : class
|
|
where TImplementation : class, TService
|
|
{
|
|
if (!services.Any(s => s.ServiceType == typeof(TService)))
|
|
{
|
|
services.AddSingleton<TService, TImplementation>();
|
|
}
|
|
}
|
|
}
|