fix: only write metrics if there is a useragent
Some checks failed
test / test (push) Waiting to run
release / release (push) Has been cancelled

This commit is contained in:
Marvin Preuss 2024-09-23 12:48:35 +02:00
parent 757d47fc41
commit 7c5c1d6406

View File

@ -39,27 +39,31 @@ func parseFunc(t *tail.Tail) {
continue continue
} }
metrics.GetOrCreateCounter( if len(l.Request.Headers.UserAgent) != 0 {
fmt.Sprintf( metrics.GetOrCreateCounter(
`%s_http_request_total{host="%s", proto="%s", method="%s", status="%d", user_agent="%s"}`, fmt.Sprintf(
metricBaseName, `%s_http_request_total{host="%s", proto="%s", method="%s", status="%d", user_agent="%s"}`,
l.Request.Host, metricBaseName,
l.Request.Proto, l.Request.Host,
l.Request.Method, l.Request.Proto,
l.Status, l.Request.Method,
l.Request.Headers.UserAgent[0], l.Status,
), l.Request.Headers.UserAgent[0],
).Inc() ),
).Inc()
metrics.GetOrCreateHistogram( metrics.GetOrCreateHistogram(
fmt.Sprintf( fmt.Sprintf(
`%s_http_request_duration_seconds{host="%s", proto="%s", method="%s", status="%d"}`, `%s_http_request_duration_seconds{host="%s", proto="%s", method="%s", status="%d"}`,
metricBaseName, metricBaseName,
l.Request.Host, l.Request.Host,
l.Request.Proto, l.Request.Proto,
l.Request.Method, l.Request.Method,
l.Status, l.Status,
), ),
).Update(l.Duration) ).Update(l.Duration)
} else {
slog.Warn("got empty user agent", "headers", l.Request.Headers)
}
} }
} }