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
}
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)
}
}
}