From 68e6d08c768b789987a753d42f66caf73ce10ee1 Mon Sep 17 00:00:00 2001 From: Daenney Date: Fri, 17 Feb 2023 12:02:29 +0100 Subject: [feature] Add a request ID and include it in logs (#1476) This adds a lightweight form of tracing to GTS. Each incoming request is assigned a Request ID which we then pass on and log in all our log lines. Any function that gets called downstream from an HTTP handler should now emit a requestID=value pair whenever it logs something. Co-authored-by: kim --- internal/router/router.go | 14 +++++++------- internal/router/template.go | 6 +++--- 2 files changed, 10 insertions(+), 10 deletions(-) (limited to 'internal/router') diff --git a/internal/router/router.go b/internal/router/router.go index 5225214ab..0b9b63494 100644 --- a/internal/router/router.go +++ b/internal/router/router.go @@ -101,10 +101,10 @@ func (r *router) Start() { ) // Start the LetsEncrypt autocert manager HTTP server. - log.Infof("letsencrypt listening on %s", srv.Addr) + log.Infof(nil, "letsencrypt listening on %s", srv.Addr) if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed { - log.Fatalf("letsencrypt: listen: %s", err) + log.Fatalf(nil, "letsencrypt: listen: %s", err) } }() @@ -119,23 +119,23 @@ func (r *router) Start() { r.srv.Handler = debug.WithPprof(r.srv.Handler) if debug.DEBUG { // Profiling requires timeouts longer than 30s, so reset these. - log.Warn("resetting http.Server{} timeout to support profiling") + log.Warn(nil, "resetting http.Server{} timeout to support profiling") r.srv.ReadTimeout = 0 r.srv.WriteTimeout = 0 } // Start the main listener. go func() { - log.Infof("listening on %s", r.srv.Addr) + log.Infof(nil, "listening on %s", r.srv.Addr) if err := listen(); err != nil && err != http.ErrServerClosed { - log.Fatalf("listen: %s", err) + log.Fatalf(nil, "listen: %s", err) } }() } // Stop shuts down the router nicely func (r *router) Stop(ctx context.Context) error { - log.Infof("shutting down http router with %s grace period", shutdownTimeout) + log.Infof(nil, "shutting down http router with %s grace period", shutdownTimeout) timeout, cancel := context.WithTimeout(ctx, shutdownTimeout) defer cancel() @@ -143,7 +143,7 @@ func (r *router) Stop(ctx context.Context) error { return fmt.Errorf("error shutting down http router: %s", err) } - log.Info("http router closed connections and shut down gracefully") + log.Info(nil, "http router closed connections and shut down gracefully") return nil } diff --git a/internal/router/template.go b/internal/router/template.go index dedfd2011..88adb8a9b 100644 --- a/internal/router/template.go +++ b/internal/router/template.go @@ -87,7 +87,7 @@ func noescapeAttr(str string) template.HTMLAttr { func timestamp(stamp string) string { t, err := util.ParseISO8601(stamp) if err != nil { - log.Errorf("error parsing timestamp %s: %s", stamp, err) + log.Errorf(nil, "error parsing timestamp %s: %s", stamp, err) return badTimestamp } @@ -110,7 +110,7 @@ func timestamp(stamp string) string { func timestampPrecise(stamp string) string { t, err := util.ParseISO8601(stamp) if err != nil { - log.Errorf("error parsing timestamp %s: %s", stamp, err) + log.Errorf(nil, "error parsing timestamp %s: %s", stamp, err) return badTimestamp } return t.Local().Format(dateYearTime) @@ -119,7 +119,7 @@ func timestampPrecise(stamp string) string { func timestampVague(stamp string) string { t, err := util.ParseISO8601(stamp) if err != nil { - log.Errorf("error parsing timestamp %s: %s", stamp, err) + log.Errorf(nil, "error parsing timestamp %s: %s", stamp, err) return badTimestamp } return t.Format(monthYear) -- cgit v1.2.3