github.com/thanos-io/thanos@v0.32.5/pkg/extprom/http/instrument_tenant_server.go (about)

     1  // Copyright (c) The Thanos Authors.
     2  // Licensed under the Apache License 2.0.
     3  
     4  package http
     5  
     6  import (
     7  	"net/http"
     8  
     9  	"github.com/prometheus/client_golang/prometheus"
    10  )
    11  
    12  type tenantInstrumentationMiddleware struct {
    13  	metrics          *defaultMetrics
    14  	tenantHeaderName string
    15  }
    16  
    17  // NewTenantInstrumentationMiddleware provides the same instrumentation as defaultInstrumentationMiddleware,
    18  // but with a tenant label fetched from the given tenantHeaderName header.
    19  // Passing nil as buckets uses the default buckets.
    20  func NewTenantInstrumentationMiddleware(tenantHeaderName string, reg prometheus.Registerer, buckets []float64) InstrumentationMiddleware {
    21  	return &tenantInstrumentationMiddleware{
    22  		tenantHeaderName: tenantHeaderName,
    23  		metrics:          newDefaultMetrics(reg, buckets, []string{"tenant"}),
    24  	}
    25  }
    26  
    27  // NewHandler wraps the given HTTP handler for instrumentation. It
    28  // registers four metric collectors (if not already done) and reports HTTP
    29  // metrics to the (newly or already) registered collectors: http_requests_total
    30  // (CounterVec), http_request_duration_seconds (Histogram),
    31  // http_request_size_bytes (Summary), http_response_size_bytes (Summary).
    32  // Each has a constant label named "handler" with the provided handlerName as value.
    33  func (ins *tenantInstrumentationMiddleware) NewHandler(handlerName string, next http.Handler) http.HandlerFunc {
    34  	tenantWrapper := func(w http.ResponseWriter, r *http.Request) {
    35  		tenant := r.Header.Get(ins.tenantHeaderName)
    36  		baseLabels := prometheus.Labels{"handler": handlerName, "tenant": tenant}
    37  		handlerStack := httpInstrumentationHandler(baseLabels, ins.metrics, next)
    38  		handlerStack.ServeHTTP(w, r)
    39  	}
    40  	return tenantWrapper
    41  }