diff options
author | Vladimir Shushlin <v.shushlin@gmail.com> | 2022-01-28 15:47:48 +0300 |
---|---|---|
committer | Vladimir Shushlin <v.shushlin@gmail.com> | 2022-01-31 16:37:22 +0300 |
commit | adc0b9233fa4a0b2b449e27336d9ae39c75819ba (patch) | |
tree | 3c21a4379bfdbcc459b27742f4dbf97122aacfc8 /app.go | |
parent | 008e2afc8d6de32a30696dbf813ebc0fdf5192c7 (diff) |
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
Diffstat (limited to 'app.go')
-rw-r--r-- | app.go | 33 |
1 files changed, 13 insertions, 20 deletions
@@ -138,7 +138,7 @@ func (a *theApp) tryAuxiliaryHandlers(w http.ResponseWriter, r *http.Request, ht } // healthCheckMiddleware is serving the application status check -func (a *theApp) healthCheckMiddleware(handler http.Handler) (http.Handler, error) { +func (a *theApp) healthCheckMiddleware(handler http.Handler) http.Handler { healthCheck := http.HandlerFunc(func(w http.ResponseWriter, _r *http.Request) { if a.isReady() { w.Write([]byte("success\n")) @@ -147,19 +147,14 @@ func (a *theApp) healthCheckMiddleware(handler http.Handler) (http.Handler, erro } }) - loggedHealthCheck, err := logging.BasicAccessLogger(healthCheck, a.config.Log.Format, nil) - if err != nil { - return nil, err - } - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.RequestURI == a.config.General.StatusPath { - loggedHealthCheck.ServeHTTP(w, r) + healthCheck.ServeHTTP(w, r) return } handler.ServeHTTP(w, r) - }), nil + }) } // auxiliaryMiddleware will handle status updates, not-ready requests and other @@ -234,24 +229,13 @@ func (a *theApp) buildHandlerPipeline() (http.Handler, error) { handler = a.auxiliaryMiddleware(handler) handler = a.Auth.AuthenticationMiddleware(handler, a.source) handler = a.AcmeMiddleware.AcmeMiddleware(handler) - handler, err := logging.BasicAccessLogger(handler, a.config.Log.Format, domain.LogFields) - if err != nil { - return nil, err - } - - // Metrics - metricsMiddleware := labmetrics.NewHandlerFactory(labmetrics.WithNamespace("gitlab_pages")) - handler = metricsMiddleware(handler) handler = routing.NewMiddleware(handler, a.source) handler = handlers.Ratelimiter(handler, &a.config.RateLimit) // Health Check - handler, err = a.healthCheckMiddleware(handler) - if err != nil { - return nil, err - } + handler = a.healthCheckMiddleware(handler) // Custom response headers handler = customheaders.NewMiddleware(handler, a.CustomHeaders) @@ -262,6 +246,15 @@ func (a *theApp) buildHandlerPipeline() (http.Handler, error) { correlationOpts = append(correlationOpts, correlation.WithPropagation()) } handler = handlePanicMiddleware(handler) + + // Access logs and metrics + handler, err := logging.BasicAccessLogger(handler, a.config.Log.Format) + if err != nil { + return nil, err + } + metricsMiddleware := labmetrics.NewHandlerFactory(labmetrics.WithNamespace("gitlab_pages")) + handler = metricsMiddleware(handler) + handler = correlation.InjectCorrelationID(handler, correlationOpts...) // These middlewares MUST be added in the end. |