From 7c5c1d640691ff0d234399dbe1275de5ab21c9e9 Mon Sep 17 00:00:00 2001 From: Marvin Preuss Date: Mon, 23 Sep 2024 12:48:35 +0200 Subject: [PATCH] fix: only write metrics if there is a useragent --- internal/server/tail.go | 46 ++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/internal/server/tail.go b/internal/server/tail.go index fcf42d0..e61d24b 100644 --- a/internal/server/tail.go +++ b/internal/server/tail.go @@ -39,27 +39,31 @@ func parseFunc(t *tail.Tail) { continue } - metrics.GetOrCreateCounter( - fmt.Sprintf( - `%s_http_request_total{host="%s", proto="%s", method="%s", status="%d", user_agent="%s"}`, - metricBaseName, - l.Request.Host, - l.Request.Proto, - l.Request.Method, - l.Status, - l.Request.Headers.UserAgent[0], - ), - ).Inc() + if len(l.Request.Headers.UserAgent) != 0 { + metrics.GetOrCreateCounter( + fmt.Sprintf( + `%s_http_request_total{host="%s", proto="%s", method="%s", status="%d", user_agent="%s"}`, + metricBaseName, + l.Request.Host, + l.Request.Proto, + l.Request.Method, + l.Status, + l.Request.Headers.UserAgent[0], + ), + ).Inc() - metrics.GetOrCreateHistogram( - fmt.Sprintf( - `%s_http_request_duration_seconds{host="%s", proto="%s", method="%s", status="%d"}`, - metricBaseName, - l.Request.Host, - l.Request.Proto, - l.Request.Method, - l.Status, - ), - ).Update(l.Duration) + metrics.GetOrCreateHistogram( + fmt.Sprintf( + `%s_http_request_duration_seconds{host="%s", proto="%s", method="%s", status="%d"}`, + metricBaseName, + l.Request.Host, + l.Request.Proto, + l.Request.Method, + l.Status, + ), + ).Update(l.Duration) + } else { + slog.Warn("got empty user agent", "headers", l.Request.Headers) + } } }