add histogram metric type and more common metrics naming

This commit is contained in:
Valentin Vesvalo 2019-01-28 17:47:32 +03:00 committed by Vasiliy Tolstov
parent ffb1c2f311
commit 6aff955f07
2 changed files with 41 additions and 6 deletions

View File

@ -42,17 +42,27 @@ func NewHandlerWrapper(opts ...server.Option) server.HandlerWrapper {
[]string{"method", "status"}, []string{"method", "status"},
) )
timeCounter := prometheus.NewSummaryVec( timeCounterSummary := prometheus.NewSummaryVec(
prometheus.SummaryOpts{ prometheus.SummaryOpts{
Name: fmt.Sprintf("%s_request_duration_microseconds", defaultMetricPrefix), Name: fmt.Sprintf("%s_upstream_latency_microseconds", defaultMetricPrefix),
Help: "Service method request latencies in microseconds", 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, ConstLabels: md,
}, },
[]string{"method"}, []string{"method"},
) )
prometheus.MustRegister(opsCounter) prometheus.MustRegister(opsCounter)
prometheus.MustRegister(timeCounter) prometheus.MustRegister(timeCounterSummary)
prometheus.MustRegister(timeCounterHistogram)
return func(fn server.HandlerFunc) server.HandlerFunc { return func(fn server.HandlerFunc) server.HandlerFunc {
return func(ctx context.Context, req server.Request, rsp interface{}) error { 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) { timer := prometheus.NewTimer(prometheus.ObserverFunc(func(v float64) {
us := v * 1000000 // make microseconds us := v * 1000000 // make microseconds
timeCounter.WithLabelValues(name).Observe(us) timeCounterSummary.WithLabelValues(name).Observe(us)
timeCounterHistogram.WithLabelValues(name).Observe(v)
})) }))
defer timer.ObserveDuration() defer timer.ObserveDuration()

View File

@ -85,7 +85,7 @@ func TestPrometheusMetrics(t *testing.T) {
list, _ := prometheus.DefaultGatherer.Gather() 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 { for _, v := range metric.Metric[0].Label {
switch *v.Name { switch *v.Name {
@ -109,6 +109,30 @@ func TestPrometheusMetrics(t *testing.T) {
assert.Equal(t, uint64(2), *metric.Metric[0].Summary.SampleCount) assert.Equal(t, uint64(2), *metric.Metric[0].Summary.SampleCount)
assert.True(t, *metric.Metric[0].Summary.SampleSum > 0) 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") metric = findMetricByName(list, dto.MetricType_COUNTER, "micro_request_total")
for _, v := range metric.Metric[0].Label { for _, v := range metric.Metric[0].Label {