Skip to content

Commit e4e5221

Browse files
committed
feat: add logic of prometheus and gauge type
1 parent 347f50a commit e4e5221

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

gmicro/core/metric/gauge.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package metric
2+
3+
import "github.com/prometheus/client_golang/prometheus"
4+
5+
type (
6+
GaugeVecOpts VectorOpts // GaugeVecOpts 指标向量选项
7+
8+
GaugeVec interface {
9+
Set(v float64, labels ...string) // Set 设置标签
10+
Inc(labels ...string) // Inc 增加标签
11+
Add(v float64, labels ...string) // Add 添加标签
12+
}
13+
14+
promGaugeVec struct {
15+
gauge *prometheus.GaugeVec
16+
}
17+
)
18+
19+
// NewGaugeVec 封装 prometheus.NewGaugeVec 为了增加 labels 参数
20+
func NewGaugeVec(cfg *GaugeVecOpts) GaugeVec {
21+
if cfg == nil {
22+
return nil
23+
}
24+
25+
vec := prometheus.NewGaugeVec(prometheus.GaugeOpts{
26+
Namespace: cfg.Namespace,
27+
Subsystem: cfg.Subsystem,
28+
Name: cfg.Name,
29+
Help: cfg.Help,
30+
}, cfg.Labels)
31+
// 反向注册,将指标注册到指标注册器中
32+
prometheus.MustRegister(vec)
33+
//将注册到指标注册器中的指标包装成promGaugeVec
34+
return &promGaugeVec{gauge: vec}
35+
}
36+
37+
func (p promGaugeVec) Set(v float64, labels ...string) {
38+
p.gauge.WithLabelValues(labels...).Set(v)
39+
}
40+
41+
func (p promGaugeVec) Inc(labels ...string) {
42+
p.gauge.WithLabelValues(labels...).Inc()
43+
}
44+
45+
func (p promGaugeVec) Add(v float64, labels ...string) {
46+
p.gauge.WithLabelValues(labels...).Add(v)
47+
}

gmicro/core/metric/metric.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package metric
2+
3+
// VectorOpts 指标选项
4+
type VectorOpts struct {
5+
Namespace string // 命名空间
6+
Subsystem string // 子系统
7+
Name string // 指标名称
8+
Help string // 指标帮助信息
9+
Labels []string // 指标标签
10+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package serverinterceptors

0 commit comments

Comments
 (0)