diff options
author | Wolfy-J <[email protected]> | 2019-06-27 11:03:11 +0300 |
---|---|---|
committer | Wolfy-J <[email protected]> | 2019-06-27 11:03:11 +0300 |
commit | de5b5a37eb362daeebbec04f82a4265e5414b6e3 (patch) | |
tree | 29c22776a14b46fd14acef5ad0441af5b9118ede | |
parent | e39fae803ee90384fb1fa1bf3104e583f02666fa (diff) |
more rpc methods for app metrics
-rw-r--r-- | cmd/rr/http/metrics.go | 2 | ||||
-rw-r--r-- | service/metrics/rpc.go | 60 |
2 files changed, 60 insertions, 2 deletions
diff --git a/cmd/rr/http/metrics.go b/cmd/rr/http/metrics.go index 32e4a6cd..21bbbaf1 100644 --- a/cmd/rr/http/metrics.go +++ b/cmd/rr/http/metrics.go @@ -55,7 +55,7 @@ func newCollector() *metricCollector { ), requestDuration: prometheus.NewHistogramVec( prometheus.HistogramOpts{ - Name: "rr_http_request_seconds", + Name: "rr_http_request_duration_seconds", Help: "HTTP request duration.", }, []string{"status"}, diff --git a/service/metrics/rpc.go b/service/metrics/rpc.go index b0023d73..30ad6c62 100644 --- a/service/metrics/rpc.go +++ b/service/metrics/rpc.go @@ -7,6 +7,7 @@ import ( type rpcServer struct{ svc *Service } +// Metric represent single metric produced by the application. type Metric struct { // Collector name. Name string @@ -71,6 +72,63 @@ func (rpc *rpcServer) Add(m *Metric, ok *bool) error { return nil } +// Sub subtract the value from the specific metric (gauge only). +func (rpc *rpcServer) Sub(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).Sub(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...).Sub(m.Value) + default: + return fmt.Errorf("collector `%s` does not support method `Sub`", m.Name) + } + + *ok = true + return nil +} + +// Observe the value (histogram and summary only). +func (rpc *rpcServer) Observe(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.SummaryVec: + if len(m.Labels) == 0 { + return fmt.Errorf("required labels for collector `%s`", m.Name) + } + + c.(*prometheus.SummaryVec).WithLabelValues(m.Labels...).Observe(m.Value) + + case prometheus.Histogram: + c.(prometheus.Histogram).Observe(m.Value) + + case *prometheus.HistogramVec: + if len(m.Labels) == 0 { + return fmt.Errorf("required labels for collector `%s`", m.Name) + } + + c.(*prometheus.HistogramVec).WithLabelValues(m.Labels...).Observe(m.Value) + default: + return fmt.Errorf("collector `%s` does not support method `Observe`", m.Name) + } + + *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) @@ -90,7 +148,7 @@ func (rpc *rpcServer) Set(m *Metric, ok *bool) error { c.(*prometheus.GaugeVec).WithLabelValues(m.Labels...).Set(m.Value) default: - return fmt.Errorf("collector `%s` is not `gauge` type", m.Name) + return fmt.Errorf("collector `%s` does not support method `Set`", m.Name) } *ok = true |