弃用 OpenTelemetry,并建立日志驱动的真实慢接口基线

This commit is contained in:
2026-08-11 11:32:50 +08:00 Unverified
parent 9708f81764
commit 292d029459
9 changed files with 87 additions and 157 deletions
@@ -0,0 +1,49 @@
using System.Diagnostics;
using Jiaowu.Api.Infrastructure.Observability;
namespace Jiaowu.Api.Infrastructure.Middleware;
public sealed class SlowRequestLoggingMiddleware(
RequestDelegate next,
ObservabilityOptions options,
ILogger<SlowRequestLoggingMiddleware> logger)
{
public async Task InvokeAsync(HttpContext context)
{
if (!options.Enabled ||
!context.Request.Path.StartsWithSegments("/api"))
{
await next(context);
return;
}
var startedAt = Stopwatch.GetTimestamp();
await next(context);
var durationMilliseconds = Stopwatch.GetElapsedTime(startedAt).TotalMilliseconds;
var endpoint = context.GetEndpoint()?.DisplayName ?? context.Request.Path.Value ?? "/api";
if (durationMilliseconds >= options.SlowRequestThresholdMilliseconds ||
context.Response.StatusCode >= StatusCodes.Status500InternalServerError)
{
logger.LogWarning(
"Slow API request completed: {Method} {Path} ({Endpoint}) returned {StatusCode} in {DurationMs:F1} ms, request {RequestId}.",
context.Request.Method,
context.Request.Path.Value,
endpoint,
context.Response.StatusCode,
durationMilliseconds,
context.TraceIdentifier);
}
else if (options.LogAllApiRequests)
{
logger.LogInformation(
"API request completed: {Method} {Path} ({Endpoint}) returned {StatusCode} in {DurationMs:F1} ms, request {RequestId}.",
context.Request.Method,
context.Request.Path.Value,
endpoint,
context.Response.StatusCode,
durationMilliseconds,
context.TraceIdentifier);
}
}
}