github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/pkg/querier/queryrange/queryrangebase/instrumentation.go (about)

     1  package queryrangebase
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	"github.com/prometheus/client_golang/prometheus"
     8  	"github.com/prometheus/client_golang/prometheus/promauto"
     9  	"github.com/weaveworks/common/instrument"
    10  )
    11  
    12  // InstrumentMiddleware can be inserted into the middleware chain to expose timing information.
    13  func InstrumentMiddleware(name string, metrics *InstrumentMiddlewareMetrics) Middleware {
    14  	var durationCol instrument.Collector
    15  
    16  	// Support the case metrics shouldn't be tracked (ie. unit tests).
    17  	if metrics != nil {
    18  		durationCol = instrument.NewHistogramCollector(metrics.duration)
    19  	} else {
    20  		durationCol = &NoopCollector{}
    21  	}
    22  
    23  	return MiddlewareFunc(func(next Handler) Handler {
    24  		return HandlerFunc(func(ctx context.Context, req Request) (Response, error) {
    25  			var resp Response
    26  			err := instrument.CollectedRequest(ctx, name, durationCol, instrument.ErrorCode, func(ctx context.Context) error {
    27  				var err error
    28  				resp, err = next.Do(ctx, req)
    29  				return err
    30  			})
    31  			return resp, err
    32  		})
    33  	})
    34  }
    35  
    36  // InstrumentMiddlewareMetrics holds the metrics tracked by InstrumentMiddleware.
    37  type InstrumentMiddlewareMetrics struct {
    38  	duration *prometheus.HistogramVec
    39  }
    40  
    41  // NewInstrumentMiddlewareMetrics makes a new InstrumentMiddlewareMetrics.
    42  func NewInstrumentMiddlewareMetrics(registerer prometheus.Registerer) *InstrumentMiddlewareMetrics {
    43  	return &InstrumentMiddlewareMetrics{
    44  		duration: promauto.With(registerer).NewHistogramVec(prometheus.HistogramOpts{
    45  			Namespace: "cortex",
    46  			Name:      "frontend_query_range_duration_seconds",
    47  			Help:      "Total time spent in seconds doing query range requests.",
    48  			Buckets:   prometheus.DefBuckets,
    49  		}, []string{"method", "status_code"}),
    50  	}
    51  }
    52  
    53  // NoopCollector is a noop collector that can be used as placeholder when no metric
    54  // should tracked by the instrumentation.
    55  type NoopCollector struct{}
    56  
    57  // Register implements instrument.Collector.
    58  func (c *NoopCollector) Register() {}
    59  
    60  // Before implements instrument.Collector.
    61  func (c *NoopCollector) Before(ctx context.Context, method string, start time.Time) {}
    62  
    63  // After implements instrument.Collector.
    64  func (c *NoopCollector) After(ctx context.Context, method, statusCode string, start time.Time) {}