Destination response time metrics #2132
Answered
by
MihaZupan
shawntoffel
asked this question in
Q&A
-
Hello, I want to add metrics/logs for the response time of the destination endpoint. In other words, the time from proxy -> destination -> proxy excluding any Yarp related middleware. I've created an approach based on the Metrics sample. Is this a valid way to accomplish this? Is there a better way? Ideally, I can also log the destinationId and path (I can get path from HttpContext). Thank you. public class HttpRequestMetrics
{
private static readonly AsyncLocal<HttpRequestMetrics> Local = new();
public static HttpRequestMetrics Current => Local.Value ??= new HttpRequestMetrics();
public DateTime StartTime { get; set; }
public DateTime StopTime { get; set; }
public TimeSpan ElapsedTime => StopTime - StartTime;
}
public class HttpClientTelemetryConsumer : IHttpTelemetryConsumer
{
public void OnRequestStart(DateTime timestamp, string scheme, string host, int port, string pathAndQuery, int versionMajor, int versionMinor, HttpVersionPolicy versionPolicy)
=> HttpRequestMetrics.Current.StartTime = timestamp;
public void OnRequestStop(DateTime timestamp)
=> HttpRequestMetrics.Current.StopTime = timestamp;
}
public class PerRequestYarpMetricCollectionMiddleware
{
private readonly RequestDelegate _next;
private readonly ILogger<PerRequestYarpMetricCollectionMiddleware> _logger;
public PerRequestYarpMetricCollectionMiddleware(RequestDelegate next, ILogger<PerRequestYarpMetricCollectionMiddleware> logger)
{
_logger = logger;
_next = next;
}
public async Task InvokeAsync(HttpContext context)
{
var metrics = HttpRequestMetrics.Current;
await _next(context);
_logger.LogInformation($"Request to {context.Request.Path} took {metrics.ElapsedTime.Milliseconds}ms");
}
} |
Beta Was this translation helpful? Give feedback.
Answered by
MihaZupan
May 11, 2023
Replies: 1 comment 1 reply
-
Yes, this looks okay. Depending on what exactly it is you are interested in, you can tweak when you collect the start/stop timestamps. For example:
|
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
shawntoffel
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes, this looks okay.
Depending on what exactly it is you are interested in, you can tweak when you collect the start/stop timestamps.
For example:
OnRequestStart
/OnRequestStop
will include any time taken to establish a new connection to the destination (including TLS handshake)OnRequestHeadersStop
/OnResponseHeadersStop
would be the closest to how long it took the proxy to receive the response headers after sending the request out