-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[receive] Export metrics about remote write requests per tenant (#5424)
* Add write metrics to Thanos Receive Signed-off-by: Douglas Camata <159076+douglascamata@users.noreply.github.com> * Let the middleware count inflight HTTP requests Signed-off-by: Douglas Camata <159076+douglascamata@users.noreply.github.com> * Update Receive write metrics type & definition Signed-off-by: Douglas Camata <159076+douglascamata@users.noreply.github.com> * Put option back in its place to avoid big diff Signed-off-by: Douglas Camata <159076+douglascamata@users.noreply.github.com> * Fetch tenant from headers instead of context It might not be in the context in some cases. Signed-off-by: Douglas Camata <159076+douglascamata@users.noreply.github.com> * Delete unnecessary tenant parser middleware Signed-off-by: Douglas Camata <159076+douglascamata@users.noreply.github.com> * Refactor & reuse code for HTTP instrumentation Signed-off-by: Douglas Camata <159076+douglascamata@users.noreply.github.com> * Add missing copyright to some files Signed-off-by: Douglas Camata <159076+douglascamata@users.noreply.github.com> * Add changelog entry for Receive & new HTTP metrics Signed-off-by: Douglas Camata <159076+douglascamata@users.noreply.github.com> * Remove TODO added by accident Signed-off-by: Douglas Camata <159076+douglascamata@users.noreply.github.com> * Make error handling code shorter Co-authored-by: Bartlomiej Plotka <bwplotka@gmail.com> Signed-off-by: Douglas Camata <159076+douglascamata@users.noreply.github.com> * Make switch statement simpler Signed-off-by: Douglas Camata <159076+douglascamata@users.noreply.github.com> * Remove method label from timeseries' metrics Signed-off-by: Douglas Camata <159076+douglascamata@users.noreply.github.com> * Count samples of all series instead of each Signed-off-by: Douglas Camata <159076+douglascamata@users.noreply.github.com> * Remove in-flight requests metric Will add this in a follow-up PR to keep this small. Signed-off-by: Douglas Camata <159076+douglascamata@users.noreply.github.com> * Change timeseries/samples metrics to histograms The buckets were picked based on the fact that Prometheus' default remote write configuration (see https://prometheus.io/docs/practices/remote_write/#memory-usage) set a max of 500 samples sent per second. Signed-off-by: Douglas Camata <159076+douglascamata@users.noreply.github.com> * Fix Prometheus registry for histograms Signed-off-by: Douglas Camata <159076+douglascamata@users.noreply.github.com> * Fix comment in NewHandler functions There are now four metrics instead of five. Signed-off-by: Douglas Camata <159076+douglascamata@users.noreply.github.com> Co-authored-by: Bartlomiej Plotka <bwplotka@gmail.com>
- Loading branch information
1 parent
ce84ec5
commit a0f4181
Showing
5 changed files
with
178 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright (c) The Thanos Authors. | ||
// Licensed under the Apache License 2.0. | ||
|
||
package http | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
) | ||
|
||
type tenantInstrumentationMiddleware struct { | ||
metrics *defaultMetrics | ||
tenantHeaderName string | ||
} | ||
|
||
// NewTenantInstrumentationMiddleware provides the same instrumentation as defaultInstrumentationMiddleware, | ||
// but with a tenant label fetched from the given tenantHeaderName header. | ||
// Passing nil as buckets uses the default buckets. | ||
func NewTenantInstrumentationMiddleware(tenantHeaderName string, reg prometheus.Registerer, buckets []float64) InstrumentationMiddleware { | ||
return &tenantInstrumentationMiddleware{ | ||
tenantHeaderName: tenantHeaderName, | ||
metrics: newDefaultMetrics(reg, buckets, []string{"tenant"}), | ||
} | ||
} | ||
|
||
// NewHandler wraps the given HTTP handler for instrumentation. It | ||
// registers four metric collectors (if not already done) and reports HTTP | ||
// metrics to the (newly or already) registered collectors: http_requests_total | ||
// (CounterVec), http_request_duration_seconds (Histogram), | ||
// http_request_size_bytes (Summary), http_response_size_bytes (Summary). | ||
// Each has a constant label named "handler" with the provided handlerName as value. | ||
func (ins *tenantInstrumentationMiddleware) NewHandler(handlerName string, next http.Handler) http.HandlerFunc { | ||
tenantWrapper := func(w http.ResponseWriter, r *http.Request) { | ||
tenant := r.Header.Get(ins.tenantHeaderName) | ||
baseLabels := prometheus.Labels{"handler": handlerName, "tenant": tenant} | ||
handlerStack := httpInstrumentationHandler(baseLabels, ins.metrics, next) | ||
handlerStack.ServeHTTP(w, r) | ||
} | ||
return tenantWrapper | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright (c) The Thanos Authors. | ||
// Licensed under the Apache License 2.0. | ||
|
||
package http | ||
|
||
import ( | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promauto" | ||
) | ||
|
||
type defaultMetrics struct { | ||
requestDuration *prometheus.HistogramVec | ||
requestSize *prometheus.SummaryVec | ||
requestsTotal *prometheus.CounterVec | ||
responseSize *prometheus.SummaryVec | ||
} | ||
|
||
func newDefaultMetrics(reg prometheus.Registerer, buckets []float64, extraLabels []string) *defaultMetrics { | ||
if buckets == nil { | ||
buckets = []float64{0.001, 0.01, 0.1, 0.3, 0.6, 1, 3, 6, 9, 20, 30, 60, 90, 120, 240, 360, 720} | ||
} | ||
|
||
return &defaultMetrics{ | ||
requestDuration: promauto.With(reg).NewHistogramVec( | ||
prometheus.HistogramOpts{ | ||
Name: "http_request_duration_seconds", | ||
Help: "Tracks the latencies for HTTP requests.", | ||
Buckets: buckets, | ||
}, | ||
append([]string{"code", "handler", "method"}, extraLabels...), | ||
), | ||
requestSize: promauto.With(reg).NewSummaryVec( | ||
prometheus.SummaryOpts{ | ||
Name: "http_request_size_bytes", | ||
Help: "Tracks the size of HTTP requests.", | ||
}, | ||
append([]string{"code", "handler", "method"}, extraLabels...), | ||
), | ||
requestsTotal: promauto.With(reg).NewCounterVec( | ||
prometheus.CounterOpts{ | ||
Name: "http_requests_total", | ||
Help: "Tracks the number of HTTP requests.", | ||
}, | ||
append([]string{"code", "handler", "method"}, extraLabels...), | ||
), | ||
responseSize: promauto.With(reg).NewSummaryVec( | ||
prometheus.SummaryOpts{ | ||
Name: "http_response_size_bytes", | ||
Help: "Tracks the size of HTTP responses.", | ||
}, | ||
append([]string{"code", "handler", "method"}, extraLabels...), | ||
), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters