Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore (Telemetry) Make time bucket count of Prometheus configurable #99

Merged
merged 7 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions telemetry/prometheus/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,19 @@ import (
"regexp"
)

const (
// Default bucket count 1000 can satisfy the precision of p99 for most histogram stats.
DefaultBucketCount = 1000
)

type Config struct {
Enabled bool
Namespace string // optional
Subsystem string // optional
Enabled bool
Namespace string // optional
Subsystem string // optional
HistogramBucketCount int // Number of buckets for histogram, default to 1000
// Number of buckets for time buckets, default to 1000.
// The bucket size is 0.01s(10ms), so the maximum covered time range is 10ms * TimeBucketCount.
TimeBucketCount int
}

func (c *Config) Validate() error {
Expand Down
18 changes: 16 additions & 2 deletions telemetry/prometheus/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type metrics struct {

// NewMetrics initializes a new instance of Prometheus metrics.
func NewMetrics(cfg *Config) (*metrics, error) { //nolint:revive // only used as Metrics interface.
setDefaultCfg(cfg)
calbera marked this conversation as resolved.
Show resolved Hide resolved
if err := cfg.Validate(); err != nil {
return nil, err
}
Expand Down Expand Up @@ -173,7 +174,8 @@ func (p *metrics) Histogram(name string, value float64, tags []string, rate floa
Namespace: p.cfg.Namespace,
Subsystem: p.cfg.Subsystem,
Help: name + " histogram",
Buckets: prometheus.LinearBuckets(0, rate, 10), // Adjust bucketing as necessary
// The maximum covered stats range is rate * HistogramBucketCount
Buckets: prometheus.LinearBuckets(0, rate, p.cfg.HistogramBucketCount),
}, labels)
prometheus.MustRegister(histogramVec)
p.histogramVecs[name] = histogramVec
Expand All @@ -196,13 +198,15 @@ func (p *metrics) Time(name string, value time.Duration, tags []string) {
Namespace: p.cfg.Namespace,
Subsystem: p.cfg.Subsystem,
Help: name + " timing histogram",
Buckets: prometheus.LinearBuckets(0, 1, 10), // Adjust bucketing as necessary
// Given bucket=0.01s(10ms), the maximum covered time range is 10ms * TimeBucketCount
Buckets: prometheus.LinearBuckets(0, 0.01, p.cfg.TimeBucketCount),
}, labels)
prometheus.MustRegister(histogramVec)
p.histogramVecs[name] = histogramVec
}

// Convert time.Duration to seconds since Prometheus prefers base units
gordonbear marked this conversation as resolved.
Show resolved Hide resolved
// see https://prometheus.io/docs/practices/naming/#base-units
histogramVec.WithLabelValues(labelValues...).Observe(value.Seconds())
}

Expand Down Expand Up @@ -250,3 +254,13 @@ func forceValidName(name string) string {

return string(runes)
}

// Set default values if not provided.
func setDefaultCfg(cfg *Config) {
if cfg.HistogramBucketCount <= 0 {
cfg.HistogramBucketCount = DefaultBucketCount
}
if cfg.TimeBucketCount <= 0 {
cfg.TimeBucketCount = DefaultBucketCount
}
}
Loading