up
Some checks failed
AOC Guard CI / aoc-guard (push) Has been cancelled
AOC Guard CI / aoc-verify (push) Has been cancelled
Docs CI / lint-and-preview (push) Has been cancelled
Policy Lint & Smoke / policy-lint (push) Has been cancelled

This commit is contained in:
StellaOps Bot
2025-11-27 23:44:42 +02:00
parent ef6e4b2067
commit 3b96b2e3ea
298 changed files with 47516 additions and 1168 deletions

View File

@@ -9,4 +9,9 @@ public interface ITelemetryContextAccessor
/// Gets or sets the current telemetry context.
/// </summary>
TelemetryContext? Context { get; set; }
/// <summary>
/// Gets or sets the current telemetry context (alias for <see cref="Context"/>).
/// </summary>
TelemetryContext? Current { get; set; }
}

View File

@@ -11,6 +11,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Grpc.AspNetCore" Version="2.70.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.12.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.12.0" />
@@ -19,10 +20,6 @@
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.12.0" />
</ItemGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\\..\\..\\AirGap\\StellaOps.AirGap.Policy\\StellaOps.AirGap.Policy\\StellaOps.AirGap.Policy.csproj" />
</ItemGroup>

View File

@@ -1,5 +1,4 @@
using System.Diagnostics;
using System.Threading;
namespace StellaOps.Telemetry.Core;
@@ -9,73 +8,63 @@ namespace StellaOps.Telemetry.Core;
public sealed class TelemetryContext
{
/// <summary>
/// Creates a new <see cref="TelemetryContext"/> using the current activity if present.
/// Initializes a new instance of the <see cref="TelemetryContext"/> class.
/// </summary>
public static TelemetryContext FromActivity(Activity? activity, string? tenantId = null, string? actor = null, string? imposedRule = null)
public TelemetryContext()
{
var traceId = activity?.TraceId.ToString() ?? activity?.RootId ?? string.Empty;
if (string.IsNullOrWhiteSpace(traceId))
{
traceId = ActivityTraceId.CreateRandom().ToString();
}
return new TelemetryContext(traceId, tenantId, actor, imposedRule);
}
/// <summary>
/// Initializes a new instance of the <see cref="TelemetryContext"/> class.
/// </summary>
public TelemetryContext(string traceId, string? tenantId, string? actor, string? imposedRule)
public TelemetryContext(string? correlationId, string? tenantId, string? actor, string? imposedRule)
{
TraceId = string.IsNullOrWhiteSpace(traceId) ? ActivityTraceId.CreateRandom().ToString() : traceId.Trim();
CorrelationId = correlationId?.Trim();
TenantId = tenantId?.Trim();
Actor = actor?.Trim();
ImposedRule = imposedRule?.Trim();
}
/// <summary>
/// Gets the distributed trace identifier.
/// Creates a new <see cref="TelemetryContext"/> using the current activity if present.
/// </summary>
public string TraceId { get; }
/// <summary>
/// Gets the tenant identifier when provided.
/// </summary>
public string? TenantId { get; }
/// <summary>
/// Gets the actor identifier (user or service principal).
/// </summary>
public string? Actor { get; }
/// <summary>
/// Gets the imposed rule or decision metadata when present.
/// </summary>
public string? ImposedRule { get; }
}
/// <summary>
/// Provides access to the current <see cref="TelemetryContext"/> using AsyncLocal storage.
/// </summary>
public sealed class TelemetryContextAccessor : ITelemetryContextAccessor
{
private readonly AsyncLocal<TelemetryContext?> _localContext = new();
/// <inheritdoc />
public TelemetryContext? Current
public static TelemetryContext FromActivity(Activity? activity, string? tenantId = null, string? actor = null, string? imposedRule = null)
{
get => _localContext.Value;
set => _localContext.Value = value;
}
}
var correlationId = activity?.TraceId.ToString() ?? activity?.RootId ?? string.Empty;
if (string.IsNullOrWhiteSpace(correlationId))
{
correlationId = ActivityTraceId.CreateRandom().ToString();
}
return new TelemetryContext(correlationId, tenantId, actor, imposedRule);
}
/// <summary>
/// Accessor abstraction for telemetry context.
/// </summary>
public interface ITelemetryContextAccessor
{
/// <summary>
/// Gets or sets the current context bound to the async flow.
/// Gets or sets the correlation identifier (distributed trace ID).
/// </summary>
TelemetryContext? Current { get; set; }
public string? CorrelationId { get; set; }
/// <summary>
/// Gets or sets the trace identifier (alias for <see cref="CorrelationId"/>).
/// </summary>
public string? TraceId
{
get => CorrelationId;
set => CorrelationId = value;
}
/// <summary>
/// Gets or sets the tenant identifier when provided.
/// </summary>
public string? TenantId { get; set; }
/// <summary>
/// Gets or sets the actor identifier (user or service principal).
/// </summary>
public string? Actor { get; set; }
/// <summary>
/// Gets or sets the imposed rule or decision metadata when present.
/// </summary>
public string? ImposedRule { get; set; }
}

View File

@@ -29,6 +29,13 @@ public sealed class TelemetryContextAccessor : ITelemetryContextAccessor
}
}
/// <inheritdoc/>
public TelemetryContext? Current
{
get => Context;
set => Context = value;
}
/// <summary>
/// Creates a scope that restores the context when disposed.
/// Useful for background jobs and async continuations.