Skip to content

Instantly share code, notes, and snippets.

@aevitas
Created December 13, 2019 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aevitas/0df15474cbf2437e278739986a6b599c to your computer and use it in GitHub Desktop.
Save aevitas/0df15474cbf2437e278739986a6b599c to your computer and use it in GitHub Desktop.
public class ResponseMetricMiddleware
{
private readonly RequestDelegate _request;
public ResponseMetricMiddleware(RequestDelegate request)
{
_request = request ?? throw new ArgumentNullException(nameof(request));
}
public async Task Invoke(HttpContext httpContext, MetricReporter reporter)
{
var path = httpContext.Request.Path.Value;
if (path == "/metrics")
{
await _request.Invoke(httpContext);
return;
}
var sw = Stopwatch.StartNew();
try
{
await _request.Invoke(httpContext);
}
finally
{
sw.Stop();
reporter.RegisterRequest();
reporter.RegisterResponseTime(httpContext.Response.StatusCode, httpContext.Request.Method, sw.Elapsed);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment