Skip to content

Commit

Permalink
Some performance optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
vadimalekseev committed Jan 9, 2024
1 parent ea087c0 commit 03e5619
Show file tree
Hide file tree
Showing 7 changed files with 280 additions and 317 deletions.
43 changes: 43 additions & 0 deletions metric/held_counter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package metric

import (
"time"

"github.com/prometheus/client_golang/prometheus"
)

type HeldCounter struct {
*heldMetric[prometheus.Counter]
}

func (h HeldCounter) Inc() {
h.metric.Inc()
h.updateUsage()
}

func (h HeldCounter) Add(v float64) {
h.metric.Add(v)
h.updateUsage()
}

type HeldCounterVec struct {
store *heldMetricsStore[prometheus.Counter]
vec *prometheus.CounterVec
}

func NewHeldCounterVec(cv *prometheus.CounterVec) HeldCounterVec {
return HeldCounterVec{
vec: cv,
store: newHeldLabelsStore[prometheus.Counter](),
}
}

func (h HeldCounterVec) WithLabelValues(lvs ...string) HeldCounter {
return HeldCounter{
heldMetric: h.store.GetOrCreate(lvs, h.vec.WithLabelValues),
}
}

func (h HeldCounterVec) ReleaseOldMetrics(holdDuration time.Duration) {
h.store.ReleaseOldMetrics(holdDuration, h.vec)
}
58 changes: 58 additions & 0 deletions metric/held_gauge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package metric

import (
"time"

"github.com/prometheus/client_golang/prometheus"
)

type HeldGauge struct {
*heldMetric[prometheus.Gauge]
}

func (h HeldGauge) Set(v float64) {
h.metric.Set(v)
h.updateUsage()
}

func (h HeldGauge) Inc() {
h.metric.Inc()
h.updateUsage()
}

func (h HeldGauge) Dec() {
h.metric.Dec()
h.updateUsage()
}

func (h HeldGauge) Add(v float64) {
h.metric.Add(v)
h.updateUsage()
}

func (h HeldGauge) Sub(v float64) {
h.metric.Sub(v)
h.updateUsage()
}

type HeldGaugeVec struct {
store *heldMetricsStore[prometheus.Gauge]
vec *prometheus.GaugeVec
}

func NewHeldGaugeVec(gv *prometheus.GaugeVec) HeldGaugeVec {
return HeldGaugeVec{
vec: gv,
store: newHeldLabelsStore[prometheus.Gauge](),
}
}

func (h HeldGaugeVec) WithLabelValues(lvs ...string) HeldGauge {
return HeldGauge{
heldMetric: h.store.GetOrCreate(lvs, h.vec.WithLabelValues),
}
}

func (h HeldGaugeVec) ReleaseOldMetrics(holdDuration time.Duration) {
h.store.ReleaseOldMetrics(holdDuration, h.vec)
}
40 changes: 40 additions & 0 deletions metric/held_histogram.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package metric

import (
"time"

"github.com/prometheus/client_golang/prometheus"
)

type HeldHistogram struct {
*heldMetric[prometheus.Histogram]
}

func (h HeldHistogram) Observe(v float64) {
h.metric.Observe(v)
h.updateUsage()
}

type HeldHistogramVec struct {
store *heldMetricsStore[prometheus.Histogram]
vec *prometheus.HistogramVec
}

func NewHeldHistogramVec(hv *prometheus.HistogramVec) HeldHistogramVec {
return HeldHistogramVec{
vec: hv,
store: newHeldLabelsStore[prometheus.Histogram](),
}
}

func (h HeldHistogramVec) WithLabelValues(lvs ...string) HeldHistogram {
return HeldHistogram{
heldMetric: h.store.GetOrCreate(lvs, func(s ...string) prometheus.Histogram {
return h.vec.WithLabelValues(s...).(prometheus.Histogram)
}),
}
}

func (h HeldHistogramVec) ReleaseOldMetrics(holdDuration time.Duration) {
h.store.ReleaseOldMetrics(holdDuration, h.vec)
}
Loading

0 comments on commit 03e5619

Please sign in to comment.