diff options
-rw-r--r-- | service/metrics/rpc.go | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/service/metrics/rpc.go b/service/metrics/rpc.go index 5d1b8102..b0023d73 100644 --- a/service/metrics/rpc.go +++ b/service/metrics/rpc.go @@ -70,3 +70,29 @@ func (rpc *rpcServer) Add(m *Metric, ok *bool) error { *ok = true return nil } + +// Set the metric value (only for gaude). +func (rpc *rpcServer) Set(m *Metric, ok *bool) error { + c := rpc.svc.Collector(m.Name) + if c == nil { + return fmt.Errorf("undefined collector `%s`", m.Name) + } + + switch c.(type) { + case prometheus.Gauge: + c.(prometheus.Gauge).Set(m.Value) + + case *prometheus.GaugeVec: + if len(m.Labels) == 0 { + return fmt.Errorf("required labels for collector `%s`", m.Name) + } + + c.(*prometheus.GaugeVec).WithLabelValues(m.Labels...).Set(m.Value) + + default: + return fmt.Errorf("collector `%s` is not `gauge` type", m.Name) + } + + *ok = true + return nil +} |