Skip to content

Commit a9e9092

Browse files
committed
feat: add logic of prometheus counter, histogram, and update gauge
1 parent e4e5221 commit a9e9092

File tree

3 files changed

+101
-5
lines changed

3 files changed

+101
-5
lines changed

gmicro/core/metric/counter.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package metric
2+
3+
import "github.com/prometheus/client_golang/prometheus"
4+
5+
type (
6+
CounterVecOpts VectorOpts // CounterVecOpts 计数选项
7+
8+
// CounterVec 计数向量重写了 prometheus.CounterVec
9+
CounterVec interface {
10+
Inc(labels ...string) // Inc 增加1标签
11+
Add(v float64, labels ...string) // Add 添加任意值标签
12+
}
13+
14+
// promCounterVec 封装了 prometheus.CounterVec
15+
promCounterVec struct {
16+
counter *prometheus.CounterVec
17+
}
18+
)
19+
20+
// NewCounterVec 封装 prometheus.NewCounterVec 为了增加 labels 参数
21+
func NewCounterVec(cfg *CounterVecOpts) CounterVec {
22+
if cfg == nil {
23+
return nil
24+
}
25+
26+
vec := prometheus.NewCounterVec(prometheus.CounterOpts{
27+
Namespace: cfg.Namespace,
28+
Subsystem: cfg.Subsystem,
29+
Name: cfg.Name,
30+
Help: cfg.Help,
31+
}, cfg.Labels)
32+
prometheus.MustRegister(vec)
33+
34+
return &promCounterVec{counter: vec}
35+
}
36+
37+
func (p *promCounterVec) Inc(labels ...string) {
38+
p.counter.WithLabelValues(labels...).Inc()
39+
}
40+
41+
func (p *promCounterVec) Add(v float64, labels ...string) {
42+
p.counter.WithLabelValues(labels...).Add(v)
43+
}

gmicro/core/metric/gauge.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ type (
77

88
GaugeVec interface {
99
Set(v float64, labels ...string) // Set 设置标签
10-
Inc(labels ...string) // Inc 增加标签
11-
Add(v float64, labels ...string) // Add 添加标签
10+
Inc(labels ...string) // Inc 增加1标签
11+
Dec(labels ...string) // Dec 减少1标签
12+
Add(v float64, labels ...string) // Add 添加任意值标签
13+
Sub(v float64, labels ...string) // Sub 减少任意标签
1214
}
1315

1416
promGaugeVec struct {
@@ -34,14 +36,22 @@ func NewGaugeVec(cfg *GaugeVecOpts) GaugeVec {
3436
return &promGaugeVec{gauge: vec}
3537
}
3638

37-
func (p promGaugeVec) Set(v float64, labels ...string) {
39+
func (p *promGaugeVec) Set(v float64, labels ...string) {
3840
p.gauge.WithLabelValues(labels...).Set(v)
3941
}
4042

41-
func (p promGaugeVec) Inc(labels ...string) {
43+
func (p *promGaugeVec) Inc(labels ...string) {
4244
p.gauge.WithLabelValues(labels...).Inc()
4345
}
4446

45-
func (p promGaugeVec) Add(v float64, labels ...string) {
47+
func (p *promGaugeVec) Dec(labels ...string) {
48+
p.gauge.WithLabelValues(labels...).Dec()
49+
}
50+
51+
func (p *promGaugeVec) Add(v float64, labels ...string) {
4652
p.gauge.WithLabelValues(labels...).Add(v)
4753
}
54+
55+
func (p *promGaugeVec) Sub(v float64, labels ...string) {
56+
p.gauge.WithLabelValues(labels...).Sub(v)
57+
}

gmicro/core/metric/histogram.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package metric
2+
3+
import "github.com/prometheus/client_golang/prometheus"
4+
5+
type (
6+
// HistogramVecOpts 柱状图选项
7+
HistogramVecOpts struct {
8+
VectorOpts
9+
Buckets []float64 //柱状图指标新增桶的概念
10+
}
11+
12+
// A HistogramVec interface represents a histogram vector.
13+
HistogramVec interface {
14+
// Observe adds observation v to labels.
15+
Observe(v int64, labels ...string)
16+
}
17+
18+
promHistogramVec struct {
19+
histogram *prometheus.HistogramVec
20+
}
21+
)
22+
23+
// NewHistogramVec returns a HistogramVec.
24+
func NewHistogramVec(cfg *HistogramVecOpts) HistogramVec {
25+
if cfg == nil {
26+
return nil
27+
}
28+
29+
vec := prometheus.NewHistogramVec(prometheus.HistogramOpts{
30+
Namespace: cfg.Namespace,
31+
Subsystem: cfg.Subsystem,
32+
Name: cfg.Name,
33+
Help: cfg.Help,
34+
Buckets: cfg.Buckets,
35+
}, cfg.Labels)
36+
prometheus.MustRegister(vec)
37+
38+
return &promHistogramVec{histogram: vec}
39+
}
40+
41+
func (p promHistogramVec) Observe(v int64, labels ...string) {
42+
p.histogram.WithLabelValues(labels...).Observe(float64(v))
43+
}

0 commit comments

Comments
 (0)