Created
December 13, 2019 14:45
-
-
Save aevitas/0df15474cbf2437e278739986a6b599c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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