Skip to content

Commit 6508bd2

Browse files
committed
custom application metrics
1 parent 4968bc6 commit 6508bd2

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

service/metrics/rpc.go

+26
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,29 @@ func (rpc *rpcServer) Add(m *Metric, ok *bool) error {
7070
*ok = true
7171
return nil
7272
}
73+
74+
// Set the metric value (only for gaude).
75+
func (rpc *rpcServer) Set(m *Metric, ok *bool) error {
76+
c := rpc.svc.Collector(m.Name)
77+
if c == nil {
78+
return fmt.Errorf("undefined collector `%s`", m.Name)
79+
}
80+
81+
switch c.(type) {
82+
case prometheus.Gauge:
83+
c.(prometheus.Gauge).Set(m.Value)
84+
85+
case *prometheus.GaugeVec:
86+
if len(m.Labels) == 0 {
87+
return fmt.Errorf("required labels for collector `%s`", m.Name)
88+
}
89+
90+
c.(*prometheus.GaugeVec).WithLabelValues(m.Labels...).Set(m.Value)
91+
92+
default:
93+
return fmt.Errorf("collector `%s` is not `gauge` type", m.Name)
94+
}
95+
96+
*ok = true
97+
return nil
98+
}

0 commit comments

Comments
 (0)