From 6aff955f073ecffe34524c957876cce8951c1a44 Mon Sep 17 00:00:00 2001 From: Valentin Vesvalo Date: Mon, 28 Jan 2019 17:47:32 +0300 Subject: [PATCH] add histogram metric type and more common metrics naming --- prometheus.go | 21 ++++++++++++++++----- prometheus_test.go | 26 +++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/prometheus.go b/prometheus.go index d63427e..e3550b1 100644 --- a/prometheus.go +++ b/prometheus.go @@ -42,17 +42,27 @@ func NewHandlerWrapper(opts ...server.Option) server.HandlerWrapper { []string{"method", "status"}, ) - timeCounter := prometheus.NewSummaryVec( + timeCounterSummary := prometheus.NewSummaryVec( prometheus.SummaryOpts{ - Name: fmt.Sprintf("%s_request_duration_microseconds", defaultMetricPrefix), - Help: "Service method request latencies in microseconds", + Name: fmt.Sprintf("%s_upstream_latency_microseconds", defaultMetricPrefix), + Help: "Service backend method request latencies in microseconds", + ConstLabels: md, + }, + []string{"method"}, + ) + + timeCounterHistogram := prometheus.NewHistogramVec( + prometheus.HistogramOpts{ + Name: fmt.Sprintf("%s_request_duration_seconds", defaultMetricPrefix), + Help: "Service method request time in seconds", ConstLabels: md, }, []string{"method"}, ) prometheus.MustRegister(opsCounter) - prometheus.MustRegister(timeCounter) + prometheus.MustRegister(timeCounterSummary) + prometheus.MustRegister(timeCounterHistogram) return func(fn server.HandlerFunc) server.HandlerFunc { return func(ctx context.Context, req server.Request, rsp interface{}) error { @@ -60,7 +70,8 @@ func NewHandlerWrapper(opts ...server.Option) server.HandlerWrapper { timer := prometheus.NewTimer(prometheus.ObserverFunc(func(v float64) { us := v * 1000000 // make microseconds - timeCounter.WithLabelValues(name).Observe(us) + timeCounterSummary.WithLabelValues(name).Observe(us) + timeCounterHistogram.WithLabelValues(name).Observe(v) })) defer timer.ObserveDuration() diff --git a/prometheus_test.go b/prometheus_test.go index 4b8ab44..9fffe86 100644 --- a/prometheus_test.go +++ b/prometheus_test.go @@ -85,7 +85,7 @@ func TestPrometheusMetrics(t *testing.T) { list, _ := prometheus.DefaultGatherer.Gather() - metric := findMetricByName(list, dto.MetricType_SUMMARY, "micro_request_duration_microseconds") + metric := findMetricByName(list, dto.MetricType_SUMMARY, "micro_upstream_latency_microseconds") for _, v := range metric.Metric[0].Label { switch *v.Name { @@ -109,6 +109,30 @@ func TestPrometheusMetrics(t *testing.T) { assert.Equal(t, uint64(2), *metric.Metric[0].Summary.SampleCount) assert.True(t, *metric.Metric[0].Summary.SampleSum > 0) + metric = findMetricByName(list, dto.MetricType_HISTOGRAM, "micro_request_duration_seconds") + + for _, v := range metric.Metric[0].Label { + switch *v.Name { + case "micro_dc": + assert.Equal(t, "dc1", *v.Value) + case "micro_node": + assert.Equal(t, "node1", *v.Value) + case "micro_version": + assert.Equal(t, version, *v.Value) + case "micro_id": + assert.Equal(t, id, *v.Value) + case "micro_name": + assert.Equal(t, name, *v.Value) + case "method": + assert.Equal(t, "Test.Method", *v.Value) + default: + t.Fatalf("unknown %v with %v", *v.Name, *v.Value) + } + } + + assert.Equal(t, uint64(2), *metric.Metric[0].Histogram.SampleCount) + assert.True(t, *metric.Metric[0].Histogram.SampleSum > 0) + metric = findMetricByName(list, dto.MetricType_COUNTER, "micro_request_total") for _, v := range metric.Metric[0].Label {