wrapper: prometheus attach labels to all metrics

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2019-02-04 01:26:31 +03:00
parent 6aff955f07
commit d927ad4673
2 changed files with 22 additions and 12 deletions

View File

@ -37,7 +37,6 @@ func NewHandlerWrapper(opts ...server.Option) server.HandlerWrapper {
prometheus.CounterOpts{ prometheus.CounterOpts{
Name: fmt.Sprintf("%s_request_total", defaultMetricPrefix), Name: fmt.Sprintf("%s_request_total", defaultMetricPrefix),
Help: "How many go-micro requests processed, partitioned by method and status", Help: "How many go-micro requests processed, partitioned by method and status",
ConstLabels: md,
}, },
[]string{"method", "status"}, []string{"method", "status"},
) )
@ -46,7 +45,6 @@ func NewHandlerWrapper(opts ...server.Option) server.HandlerWrapper {
prometheus.SummaryOpts{ prometheus.SummaryOpts{
Name: fmt.Sprintf("%s_upstream_latency_microseconds", defaultMetricPrefix), Name: fmt.Sprintf("%s_upstream_latency_microseconds", defaultMetricPrefix),
Help: "Service backend method request latencies in microseconds", Help: "Service backend method request latencies in microseconds",
ConstLabels: md,
}, },
[]string{"method"}, []string{"method"},
) )
@ -55,14 +53,22 @@ func NewHandlerWrapper(opts ...server.Option) server.HandlerWrapper {
prometheus.HistogramOpts{ prometheus.HistogramOpts{
Name: fmt.Sprintf("%s_request_duration_seconds", defaultMetricPrefix), Name: fmt.Sprintf("%s_request_duration_seconds", defaultMetricPrefix),
Help: "Service method request time in seconds", Help: "Service method request time in seconds",
ConstLabels: md,
}, },
[]string{"method"}, []string{"method"},
) )
prometheus.MustRegister(opsCounter) reg := prometheus.NewRegistry()
prometheus.MustRegister(timeCounterSummary) wrapreg := prometheus.WrapRegistererWith(md, reg)
prometheus.MustRegister(timeCounterHistogram) wrapreg.MustRegister(
prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{}),
prometheus.NewGoCollector(),
opsCounter,
timeCounterSummary,
timeCounterHistogram,
)
prometheus.DefaultGatherer = reg
prometheus.DefaultRegisterer = wrapreg
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 {

View File

@ -87,6 +87,10 @@ func TestPrometheusMetrics(t *testing.T) {
metric := findMetricByName(list, dto.MetricType_SUMMARY, "micro_upstream_latency_microseconds") metric := findMetricByName(list, dto.MetricType_SUMMARY, "micro_upstream_latency_microseconds")
if metric == nil || metric.Metric == nil || len(metric.Metric) == 0 {
t.Fatalf("no metrics returned")
}
for _, v := range metric.Metric[0].Label { for _, v := range metric.Metric[0].Label {
switch *v.Name { switch *v.Name {
case "micro_dc": case "micro_dc":