github.com/hellofresh/janus@v0.0.0-20230925145208-ce8de8183c67/pkg/metrics/metrics_context.go (about) 1 package metrics 2 3 import ( 4 "context" 5 6 "github.com/hellofresh/stats-go" 7 "github.com/hellofresh/stats-go/client" 8 log "github.com/sirupsen/logrus" 9 ) 10 11 type statsKeyType int 12 13 const statsKey statsKeyType = iota 14 15 // NewContext returns a context that has a stats Client 16 func NewContext(ctx context.Context, client client.Client) context.Context { 17 return context.WithValue(ctx, statsKey, client) 18 } 19 20 // WithContext returns a stats Client with as much context as possible 21 func WithContext(ctx context.Context) client.Client { 22 ctxStats, ok := ctx.Value(statsKey).(client.Client) 23 if !ok { 24 log.Error("Could not retrieve stats client from the context") 25 26 ctxStats, _ := stats.NewClient("noop://") 27 return ctxStats 28 } 29 return ctxStats 30 }