feat: add tracing enrichment and prometheus exporter

This commit is contained in:
msumshk
2025-12-02 22:29:38 +08:00
parent 0d2ad0aecb
commit 2121432d5d
13 changed files with 163 additions and 41 deletions

View File

@@ -3,11 +3,12 @@ using System.Threading;
namespace TakeoutSaaS.Shared.Abstractions.Diagnostics;
/// <summary>
/// 轻量级 TraceId 上下文,便于跨层访问当前请求的追踪标识。
/// 轻量级 TraceId/SpanId 上下文,便于跨层访问当前请求的追踪标识。
/// </summary>
public static class TraceContext
{
private static readonly AsyncLocal<string?> TraceIdHolder = new();
private static readonly AsyncLocal<string?> SpanIdHolder = new();
/// <summary>
/// 当前请求的 TraceId。
@@ -18,8 +19,21 @@ public static class TraceContext
set => TraceIdHolder.Value = value;
}
/// <summary>
/// 当前请求的 SpanId。
/// </summary>
public static string? SpanId
{
get => SpanIdHolder.Value;
set => SpanIdHolder.Value = value;
}
/// <summary>
/// 清理 TraceId避免 AsyncLocal 污染其它请求。
/// </summary>
public static void Clear() => TraceIdHolder.Value = null;
public static void Clear()
{
TraceIdHolder.Value = null;
SpanIdHolder.Value = null;
}
}