From eebedb253769d6c679ad5881d32d233814765fb9 Mon Sep 17 00:00:00 2001 From: mtrinh11 Date: Wed, 30 Mar 2022 15:56:04 -0700 Subject: [PATCH 1/3] added SetLoggerInfo to aliceChain --- basculehttp/chain.go | 11 ++++++----- basculehttp/log.go | 6 ++++++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/basculehttp/chain.go b/basculehttp/chain.go index ce55508..aacfa2a 100644 --- a/basculehttp/chain.go +++ b/basculehttp/chain.go @@ -31,15 +31,16 @@ type MetricListenerIn struct { // ChainIn is used for uber fx wiring. type ChainIn struct { fx.In - SetLogger alice.Constructor `name:"alice_set_logger"` - Constructor alice.Constructor `name:"alice_constructor"` - Enforcer alice.Constructor `name:"alice_enforcer"` - Listener alice.Constructor `name:"alice_listener"` + SetLogger alice.Constructor `name:"alice_set_logger"` + Constructor alice.Constructor `name:"alice_constructor"` + Enforcer alice.Constructor `name:"alice_enforcer"` + Listener alice.Constructor `name:"alice_listener"` + SetPostLoggerInfo alice.Constructor `name:"alice_set_info"` } // Build provides the alice constructors chained together in a set order. func (c ChainIn) Build() alice.Chain { - return alice.New(c.SetLogger, c.Constructor, c.Enforcer, c.Listener) + return alice.New(c.SetLogger, c.Constructor, c.Enforcer, c.Listener, c.SetPostLoggerInfo) } // ProvideServerChain builds the alice middleware and then provides them diff --git a/basculehttp/log.go b/basculehttp/log.go index 4fce0cd..21d938d 100644 --- a/basculehttp/log.go +++ b/basculehttp/log.go @@ -136,6 +136,12 @@ func ProvideLogger() fx.Option { return WithELogger(getZapLogger(getLogger)) }, }, + + // add info to logger + fx.Annotated{ + Name: "alice_set_info", + Target: SetBasculeInfo, + }, ), ) } From 4733fc98f0277f464b3caf891cafddbe43d67719 Mon Sep 17 00:00:00 2001 From: mtrinh11 Date: Wed, 30 Mar 2022 15:56:34 -0700 Subject: [PATCH 2/3] changed field name --- basculehttp/chain.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/basculehttp/chain.go b/basculehttp/chain.go index aacfa2a..d33d1cb 100644 --- a/basculehttp/chain.go +++ b/basculehttp/chain.go @@ -31,16 +31,16 @@ type MetricListenerIn struct { // ChainIn is used for uber fx wiring. type ChainIn struct { fx.In - SetLogger alice.Constructor `name:"alice_set_logger"` - Constructor alice.Constructor `name:"alice_constructor"` - Enforcer alice.Constructor `name:"alice_enforcer"` - Listener alice.Constructor `name:"alice_listener"` - SetPostLoggerInfo alice.Constructor `name:"alice_set_info"` + SetLogger alice.Constructor `name:"alice_set_logger"` + Constructor alice.Constructor `name:"alice_constructor"` + Enforcer alice.Constructor `name:"alice_enforcer"` + Listener alice.Constructor `name:"alice_listener"` + SetLoggerInfo alice.Constructor `name:"alice_set_info"` } // Build provides the alice constructors chained together in a set order. func (c ChainIn) Build() alice.Chain { - return alice.New(c.SetLogger, c.Constructor, c.Enforcer, c.Listener, c.SetPostLoggerInfo) + return alice.New(c.SetLogger, c.Constructor, c.Enforcer, c.Listener, c.SetLoggerInfo) } // ProvideServerChain builds the alice middleware and then provides them From 3f3941725abc5f3841c48fe01dff7811b26aa631 Mon Sep 17 00:00:00 2001 From: mtrinh11 Date: Thu, 31 Mar 2022 15:40:11 -0700 Subject: [PATCH 3/3] moved context from paramater to body --- basculehttp/chain.go | 2 +- basculehttp/log.go | 13 ++++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/basculehttp/chain.go b/basculehttp/chain.go index d33d1cb..58cef13 100644 --- a/basculehttp/chain.go +++ b/basculehttp/chain.go @@ -35,7 +35,7 @@ type ChainIn struct { Constructor alice.Constructor `name:"alice_constructor"` Enforcer alice.Constructor `name:"alice_enforcer"` Listener alice.Constructor `name:"alice_listener"` - SetLoggerInfo alice.Constructor `name:"alice_set_info"` + SetLoggerInfo alice.Constructor `name:"alice_set_logger_info"` } // Build provides the alice constructors chained together in a set order. diff --git a/basculehttp/log.go b/basculehttp/log.go index 21d938d..038e904 100644 --- a/basculehttp/log.go +++ b/basculehttp/log.go @@ -139,29 +139,32 @@ func ProvideLogger() fx.Option { // add info to logger fx.Annotated{ - Name: "alice_set_info", + Name: "alice_set_logger_info", Target: SetBasculeInfo, }, ), ) } -// SetBasculeInfo takes the logger and bascule Auth out of the context and adds +// SetBasculeInfo creates an alice constructor that takes +// the logger and bascule Auth and adds // relevant bascule information to the logger before putting the logger // back in the context. -func SetBasculeInfo(ctx context.Context) alice.Constructor { +func SetBasculeInfo() alice.Constructor { return func(delegate http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + logger := sallust.Get(ctx) + var satClientID = "N/A" // retrieve satClientID from request context if auth, ok := bascule.FromContext(r.Context()); ok { satClientID = auth.Token.Principal() } - logger := sallust.Get(ctx) logger.With(zap.String("satClientID", satClientID)) - sallust.With(ctx, logger) + r = r.WithContext(sallust.With(ctx, logger)) delegate.ServeHTTP(w, r) })