Skip to content

Commit

Permalink
native histograms: add common defaults for latency metrics
Browse files Browse the repository at this point in the history
To be reused in Mimir and GEM and possibly more.

Signed-off-by: György Krajcsovits <[email protected]>
  • Loading branch information
krajorama committed Apr 8, 2024
1 parent 1435abf commit d884a64
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 14 deletions.
8 changes: 5 additions & 3 deletions cache/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"

"github.com/grafana/dskit/instrument/nativehistogram"
)

const (
Expand Down Expand Up @@ -104,9 +106,9 @@ func newClientMetrics(reg prometheus.Registerer) *clientMetrics {
Help: "Duration of operations against cache.",
Buckets: []float64{0.001, 0.005, 0.01, 0.025, 0.05, 0.1, 0.2, 0.5, 1, 3, 6, 10},
// Use defaults recommended by Prometheus for native histograms.
NativeHistogramBucketFactor: 1.1,
NativeHistogramMaxBucketNumber: 100,
NativeHistogramMinResetDuration: time.Hour,
NativeHistogramBucketFactor: nativehistogram.LatencyBucketFactor,
NativeHistogramMaxBucketNumber: nativehistogram.LatencyMaxBucketNumber,
NativeHistogramMinResetDuration: nativehistogram.LatencyMinResetDuration,
}, []string{"operation"})
cm.duration.WithLabelValues(opGetMulti)
cm.duration.WithLabelValues(opSet)
Expand Down
8 changes: 5 additions & 3 deletions gate/gate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"

"github.com/grafana/dskit/instrument/nativehistogram"
)

var ErrMaxConcurrent = errors.New("max concurrent requests inflight")
Expand Down Expand Up @@ -69,9 +71,9 @@ func NewInstrumented(reg prometheus.Registerer, maxConcurrent int, gate Gate) Ga
Help: "How many seconds it took for queries to wait at the gate.",
Buckets: []float64{0.01, 0.1, 0.3, 0.6, 1, 3, 6, 9, 20, 30, 60, 90, 120, 240, 360, 720},
// Use defaults recommended by Prometheus for native histograms.
NativeHistogramBucketFactor: 1.1,
NativeHistogramMaxBucketNumber: 100,
NativeHistogramMinResetDuration: time.Hour,
NativeHistogramBucketFactor: nativehistogram.LatencyBucketFactor,
NativeHistogramMaxBucketNumber: nativehistogram.LatencyMaxBucketNumber,
NativeHistogramMinResetDuration: nativehistogram.LatencyMinResetDuration,
}, []string{"outcome"}),
}

Expand Down
12 changes: 12 additions & 0 deletions instrument/nativehistogram/native_histogram.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-License-Identifier: Apache-2.0

package nativehistogram

import "time"

const (
// Define the default values for latency related native histogram configuration.
LatencyBucketFactor = 1.1
LatencyMaxBucketNumber = 100
LatencyMinResetDuration = time.Hour
)
8 changes: 4 additions & 4 deletions kv/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package kv
import (
"context"
"strconv"
"time"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"

"github.com/grafana/dskit/httpgrpc"
"github.com/grafana/dskit/instrument"
"github.com/grafana/dskit/instrument/nativehistogram"
)

// RegistererWithKVName wraps the provided Registerer with the KV name label. If a nil reg
Expand Down Expand Up @@ -55,9 +55,9 @@ func newMetricsClient(backend string, c Client, reg prometheus.Registerer) Clien
Help: "Time spent on kv store requests.",
Buckets: prometheus.DefBuckets,
// Use defaults recommended by Prometheus for native histograms.
NativeHistogramBucketFactor: 1.1,
NativeHistogramMaxBucketNumber: 100,
NativeHistogramMinResetDuration: time.Hour,
NativeHistogramBucketFactor: nativehistogram.LatencyBucketFactor,
NativeHistogramMaxBucketNumber: nativehistogram.LatencyMaxBucketNumber,
NativeHistogramMinResetDuration: nativehistogram.LatencyMinResetDuration,
ConstLabels: prometheus.Labels{
"type": backend,
},
Expand Down
7 changes: 3 additions & 4 deletions server/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
package server

import (
"time"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"

"github.com/grafana/dskit/instrument"
"github.com/grafana/dskit/instrument/nativehistogram"
"github.com/grafana/dskit/middleware"
)

Expand Down Expand Up @@ -43,8 +42,8 @@ func NewServerMetrics(cfg Config) *Metrics {
Help: "Time (in seconds) spent serving HTTP requests.",
Buckets: instrument.DefBuckets,
NativeHistogramBucketFactor: cfg.MetricsNativeHistogramFactor,
NativeHistogramMaxBucketNumber: 100,
NativeHistogramMinResetDuration: time.Hour,
NativeHistogramMaxBucketNumber: nativehistogram.LatencyMaxBucketNumber,
NativeHistogramMinResetDuration: nativehistogram.LatencyMinResetDuration,
}, []string{"method", "route", "status_code", "ws"}),
ReceivedMessageSize: reg.NewHistogramVec(prometheus.HistogramOpts{
Namespace: cfg.MetricsNamespace,
Expand Down

0 comments on commit d884a64

Please sign in to comment.