Skip to content

Commit

Permalink
WIP PoC with native histograms
Browse files Browse the repository at this point in the history
  • Loading branch information
maximerety committed Jan 24, 2023
1 parent 5fbda0d commit a054663
Showing 1 changed file with 37 additions and 11 deletions.
48 changes: 37 additions & 11 deletions prom.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ type pmapGauge struct {
values map[string]prometheus.GaugeVec
}

type NativeHistogramOpts struct {
BucketFactor float64
ZeroThreshold float64
MaxBucketNumber uint32
MinResetDuration time.Duration
MaxZeroThreshold float64
}

// Prometheus contains the metrics gathered by the instance and its path.
type Prometheus struct {
reqCnt *prometheus.CounterVec
Expand All @@ -49,14 +57,15 @@ type Prometheus struct {

customGauges pmapGauge

MetricsPath string
Namespace string
Subsystem string
Token string
Ignored pmapb
Engine *gin.Engine
Buckets []float64
Registry *prometheus.Registry
MetricsPath string
Namespace string
Subsystem string
Token string
Ignored pmapb
Engine *gin.Engine
Buckets []float64
NativeHistogramOpts NativeHistogramOpts
Registry *prometheus.Registry

RequestCounterMetricName string
RequestDurationMetricName string
Expand Down Expand Up @@ -171,6 +180,12 @@ func Buckets(b []float64) func(*Prometheus) {
}
}

func NativeHistogram(nhopts NativeHistogramOpts) func(*Prometheus) {
return func(p *Prometheus) {
p.NativeHistogramOpts = nhopts
}
}

// Subsystem is an option allowing to set the subsystem when intitializing
// with New.
func Subsystem(sub string) func(*Prometheus) {
Expand Down Expand Up @@ -295,13 +310,24 @@ func (p *Prometheus) register() {
)
registerer.MustRegister(p.reqCnt)

p.reqDur = prometheus.NewHistogramVec(prometheus.HistogramOpts{
histogramOpts := prometheus.HistogramOpts{
Namespace: p.Namespace,
Subsystem: p.Subsystem,
Buckets: p.Buckets,
Name: p.RequestDurationMetricName,
Help: "The HTTP request latency bucket.",
}, []string{"method", "path", "host"})
}

if p.NativeHistogramOpts.BucketFactor != 0 {
histogramOpts.NativeHistogramBucketFactor = p.NativeHistogramOpts.BucketFactor
histogramOpts.NativeHistogramZeroThreshold = p.NativeHistogramOpts.ZeroThreshold
histogramOpts.NativeHistogramMaxBucketNumber = p.NativeHistogramOpts.MaxBucketNumber
histogramOpts.NativeHistogramMinResetDuration = p.NativeHistogramOpts.MinResetDuration
histogramOpts.NativeHistogramMaxZeroThreshold = p.NativeHistogramOpts.MaxZeroThreshold
} else {
histogramOpts.Buckets = p.Buckets
}

p.reqDur = prometheus.NewHistogramVec(histogramOpts, []string{"method", "path", "host"})
registerer.MustRegister(p.reqDur)

p.reqSz = prometheus.NewSummary(
Expand Down

0 comments on commit a054663

Please sign in to comment.