33 lines
1.1 KiB
C#
33 lines
1.1 KiB
C#
using System.Diagnostics;
|
|
|
|
namespace FinalFactory.Rendezvous.Server.Observability;
|
|
|
|
internal sealed class TelemetryMiddleware(
|
|
RequestDelegate next,
|
|
RendezvousTelemetry telemetry)
|
|
{
|
|
public async Task InvokeAsync(HttpContext context)
|
|
{
|
|
string operation = context.GetEndpoint()?.Metadata.GetMetadata<IEndpointNameMetadata>()
|
|
?.EndpointName ?? "Unmatched";
|
|
long started = Stopwatch.GetTimestamp();
|
|
using Activity? activity = telemetry.StartActivity(
|
|
$"HTTP {operation}",
|
|
ActivityKind.Server);
|
|
string correlationId = activity?.TraceId.ToString() ?? Guid.NewGuid().ToString("N");
|
|
context.Response.Headers["X-Rendezvous-Correlation-ID"] = correlationId;
|
|
activity?.SetTag("rendezvous.operation", operation);
|
|
try
|
|
{
|
|
await next(context).ConfigureAwait(false);
|
|
}
|
|
finally
|
|
{
|
|
telemetry.RecordHttp(
|
|
operation,
|
|
context.Response.StatusCode,
|
|
Stopwatch.GetElapsedTime(started).TotalMilliseconds);
|
|
}
|
|
}
|
|
}
|