...
1
16
17 package prometheusextension
18
19 import (
20 "time"
21
22 "github.com/prometheus/client_golang/prometheus"
23 )
24
25
26
27 type GaugeVecOps interface {
28 GetMetricWith(prometheus.Labels) (GaugeOps, error)
29 GetMetricWithLabelValues(lvs ...string) (GaugeOps, error)
30 With(prometheus.Labels) GaugeOps
31 WithLabelValues(...string) GaugeOps
32 CurryWith(prometheus.Labels) (GaugeVecOps, error)
33 MustCurryWith(prometheus.Labels) GaugeVecOps
34 }
35
36 type TimingHistogramVec struct {
37 *prometheus.MetricVec
38 }
39
40 var _ GaugeVecOps = &TimingHistogramVec{}
41 var _ prometheus.Collector = &TimingHistogramVec{}
42
43 func NewTimingHistogramVec(opts TimingHistogramOpts, labelNames ...string) *TimingHistogramVec {
44 return NewTestableTimingHistogramVec(time.Now, opts, labelNames...)
45 }
46
47 func NewTestableTimingHistogramVec(nowFunc func() time.Time, opts TimingHistogramOpts, labelNames ...string) *TimingHistogramVec {
48 desc := prometheus.NewDesc(
49 prometheus.BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
50 wrapTimingHelp(opts.Help),
51 labelNames,
52 opts.ConstLabels,
53 )
54 return &TimingHistogramVec{
55 MetricVec: prometheus.NewMetricVec(desc, func(lvs ...string) prometheus.Metric {
56 metric, err := newTimingHistogram(nowFunc, desc, opts, lvs...)
57 if err != nil {
58 panic(err)
59 }
60 return metric
61 }),
62 }
63 }
64
65 func (hv *TimingHistogramVec) GetMetricWith(labels prometheus.Labels) (GaugeOps, error) {
66 metric, err := hv.MetricVec.GetMetricWith(labels)
67 if metric != nil {
68 return metric.(GaugeOps), err
69 }
70 return nil, err
71 }
72
73 func (hv *TimingHistogramVec) GetMetricWithLabelValues(lvs ...string) (GaugeOps, error) {
74 metric, err := hv.MetricVec.GetMetricWithLabelValues(lvs...)
75 if metric != nil {
76 return metric.(GaugeOps), err
77 }
78 return nil, err
79 }
80
81 func (hv *TimingHistogramVec) With(labels prometheus.Labels) GaugeOps {
82 h, err := hv.GetMetricWith(labels)
83 if err != nil {
84 panic(err)
85 }
86 return h
87 }
88
89 func (hv *TimingHistogramVec) WithLabelValues(lvs ...string) GaugeOps {
90 h, err := hv.GetMetricWithLabelValues(lvs...)
91 if err != nil {
92 panic(err)
93 }
94 return h
95 }
96
97 func (hv *TimingHistogramVec) CurryWith(labels prometheus.Labels) (GaugeVecOps, error) {
98 vec, err := hv.MetricVec.CurryWith(labels)
99 if vec != nil {
100 return &TimingHistogramVec{MetricVec: vec}, err
101 }
102 return nil, err
103 }
104
105 func (hv *TimingHistogramVec) MustCurryWith(labels prometheus.Labels) GaugeVecOps {
106 vec, err := hv.CurryWith(labels)
107 if err != nil {
108 panic(err)
109 }
110 return vec
111 }
112
View as plain text