From adc0b9233fa4a0b2b449e27336d9ae39c75819ba Mon Sep 17 00:00:00 2001 From: Vladimir Shushlin Date: Fri, 28 Jan 2022 15:47:48 +0300 Subject: fix: fix metrics and logs not including domain resolution time Currently we do logging and metrics capturing after we did the domain information lookup. It allows us to add more information to access logs. But it also distorts metrics because domain information lookup takes time. This logic was originally introduced in https://gitlab.com/gitlab-org/gitlab-pages/-/merge_requests/157/diffs It didn't matter back than because we didn't lookup domain via API as we do now. Now it does matter. So this commits moves metrics and logging middlewares almost to the top of pipeline. Changelog: fixed --- internal/logging/logging.go | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) (limited to 'internal/logging/logging.go') diff --git a/internal/logging/logging.go b/internal/logging/logging.go index edc670d6..6e8533d8 100644 --- a/internal/logging/logging.go +++ b/internal/logging/logging.go @@ -52,34 +52,24 @@ func getAccessLogger(format string) (*logrus.Logger, error) { } // BasicAccessLogger configures the GitLab pages basic HTTP access logger middleware -func BasicAccessLogger(handler http.Handler, format string, extraFields log.ExtraFieldsGeneratorFunc) (http.Handler, error) { +func BasicAccessLogger(handler http.Handler, format string) (http.Handler, error) { accessLogger, err := getAccessLogger(format) if err != nil { return nil, err } return log.AccessLogger(handler, - log.WithExtraFields(enrichExtraFields(extraFields)), + log.WithExtraFields(extraFields), log.WithAccessLogger(accessLogger), log.WithXFFAllowed(func(sip string) bool { return false }), ), nil } -func enrichExtraFields(extraFields log.ExtraFieldsGeneratorFunc) log.ExtraFieldsGeneratorFunc { - return func(r *http.Request) log.Fields { - enrichedFields := log.Fields{ - "correlation_id": correlation.ExtractFromContext(r.Context()), - "pages_https": request.IsHTTPS(r), - "pages_host": r.Host, - } - - if extraFields != nil { - for field, value := range extraFields(r) { - enrichedFields[field] = value - } - } - - return enrichedFields +func extraFields(r *http.Request) log.Fields { + return log.Fields{ + "correlation_id": correlation.ExtractFromContext(r.Context()), + "pages_https": request.IsHTTPS(r), + "pages_host": r.Host, } } -- cgit v1.2.3