Properly tag request logs in metrics

This commit is contained in:
Ske 2020-08-27 16:26:37 +02:00
parent e556cc71fa
commit e27826955e

View File

@ -56,6 +56,9 @@ namespace PluralKit.Bot
if (response.RequestMessage.RequestUri.Host != "discord.com") if (response.RequestMessage.RequestUri.Host != "discord.com")
return; return;
var routePath = NormalizeRoutePath(response.RequestMessage.RequestUri.LocalPath.Replace("/api/v7", ""));
var route = $"{response.RequestMessage.Method} {routePath}";
using (LogContext.PushProperty("Elastic", "yes?")) using (LogContext.PushProperty("Elastic", "yes?"))
{ {
if ((int) response.StatusCode >= 400 && (int) response.StatusCode < 500) if ((int) response.StatusCode >= 400 && (int) response.StatusCode < 500)
@ -64,8 +67,6 @@ namespace PluralKit.Bot
LogContext.PushProperty("ResponseBody", content); LogContext.PushProperty("ResponseBody", content);
} }
var routePath = NormalizeRoutePath(response.RequestMessage.RequestUri.LocalPath.Replace("/api/v7", ""));
var route = $"{response.RequestMessage.Method} {routePath}";
LogContext.PushProperty("RequestUrlRoute", route); LogContext.PushProperty("RequestUrlRoute", route);
_logger.Information( _logger.Information(
@ -77,8 +78,11 @@ namespace PluralKit.Bot
activity.Duration.TotalMilliseconds); activity.Duration.TotalMilliseconds);
} }
var timer = _metrics.Provider.Timer.Instance(BotMetrics.DiscordApiRequests); var timer = _metrics.Provider.Timer.Instance(BotMetrics.DiscordApiRequests, new MetricTags(
timer.Record(activity.Duration.Ticks / 10, TimeUnit.Microseconds, ((int) response.StatusCode).ToString()); new[] {"endpoint", "status_code"},
new[] {route, ((int) response.StatusCode).ToString()}
));
timer.Record(activity.Duration.Ticks / 10, TimeUnit.Microseconds);
} }
public void OnNext(KeyValuePair<string, object> value) public void OnNext(KeyValuePair<string, object> value)