From 84cdacdbcfafe1224542f1e794b5311878cb01ee Mon Sep 17 00:00:00 2001 From: Nick Mills-Barrett Date: Wed, 3 Jan 2024 17:35:11 +0000 Subject: [PATCH] Use request logger in routes/provider --- internal/api/routes.go | 11 +++++++---- internal/provider/provider.go | 3 +-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/internal/api/routes.go b/internal/api/routes.go index 5d9af06..15bd8d0 100644 --- a/internal/api/routes.go +++ b/internal/api/routes.go @@ -6,6 +6,7 @@ import ( "github.com/go-chi/chi/v5" "github.com/gorilla/websocket" + "github.com/rs/zerolog/hlog" "github.com/beeper/registration-relay/internal/provider" ) @@ -19,7 +20,7 @@ func (a *api) bridgeExecuteCommand(w http.ResponseWriter, r *http.Request) { return } - log := a.log.With().Str("code", code).Logger() + log := hlog.FromRequest(r).With().Str("code", code).Logger() provider, exists := provider.GetProvider(code) if !exists { @@ -42,16 +43,18 @@ func (a *api) bridgeExecuteCommand(w http.ResponseWriter, r *http.Request) { } func (a *api) providerWebsocket(w http.ResponseWriter, r *http.Request) { + log := hlog.FromRequest(r) + conn, err := upgrader.Upgrade(w, r, nil) if err != nil { - a.log.Err(err).Msg("Failed to upgrade websocket connection") + log.Err(err).Msg("Failed to upgrade websocket connection") w.WriteHeader(http.StatusInternalServerError) return } defer conn.Close() - provider := provider.NewProvider(conn, a.secret) + provider := provider.NewProvider(conn, a.secret, log) provider.WebsocketLoop() - a.log.Info().Msg("Websocket connection closed") + log.Info().Msg("Websocket connection closed") } diff --git a/internal/provider/provider.go b/internal/provider/provider.go index fd41562..906e2eb 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -12,7 +12,6 @@ import ( "github.com/gorilla/websocket" "github.com/rs/zerolog" - "github.com/rs/zerolog/log" "github.com/beeper/registration-relay/internal/metrics" "github.com/beeper/registration-relay/internal/util" @@ -101,7 +100,7 @@ type provider struct { globalSecret []byte } -func NewProvider(ws *websocket.Conn, secret []byte) *provider { +func NewProvider(ws *websocket.Conn, secret []byte, log *zerolog.Logger) *provider { logger := log.With(). Str("component", "provider"). Logger()