github.com/hellofresh/janus@v0.0.0-20230925145208-ce8de8183c67/pkg/middleware/stats_tagger.go (about)

     1  package middleware
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"go.opencensus.io/tag"
     7  )
     8  
     9  // StatsTagger is a middleware that takes a list of tags and adds them into context to be propagated
    10  type StatsTagger struct {
    11  	tags []tag.Mutator
    12  }
    13  
    14  // NewStatsTagger creates a new instance of StatsTagger
    15  func NewStatsTagger(tags []tag.Mutator) *StatsTagger {
    16  	metricKeyInserter := &StatsTagger{tags}
    17  	return metricKeyInserter
    18  }
    19  
    20  // Handler is the middleware function
    21  func (h *StatsTagger) Handler(handler http.Handler) http.Handler {
    22  	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    23  		ctx := r.Context()
    24  
    25  		for _, t := range h.tags {
    26  			ctx, _ = tag.New(ctx, t)
    27  		}
    28  
    29  		handler.ServeHTTP(w, r.WithContext(ctx))
    30  	})
    31  }