github.com/thanos-io/thanos@v0.32.5/internal/cortex/querier/stats/time_middleware.go (about)

     1  // Copyright (c) The Cortex Authors.
     2  // Licensed under the Apache License 2.0.
     3  
     4  package stats
     5  
     6  import (
     7  	"net/http"
     8  	"time"
     9  )
    10  
    11  // WallTimeMiddleware tracks the wall time.
    12  type WallTimeMiddleware struct{}
    13  
    14  // NewWallTimeMiddleware makes a new WallTimeMiddleware.
    15  func NewWallTimeMiddleware() WallTimeMiddleware {
    16  	return WallTimeMiddleware{}
    17  }
    18  
    19  // Wrap implements middleware.Interface.
    20  func (m WallTimeMiddleware) Wrap(next http.Handler) http.Handler {
    21  	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    22  		if !IsEnabled(r.Context()) {
    23  			next.ServeHTTP(w, r)
    24  			return
    25  		}
    26  
    27  		startTime := time.Now()
    28  		next.ServeHTTP(w, r)
    29  
    30  		stats := FromContext(r.Context())
    31  		stats.AddWallTime(time.Since(startTime))
    32  	})
    33  }