assign service metadata to labels

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
2019-01-26 12:33:51 +03:00
parent fd8eb72e9c
commit ffb1c2f311
2 changed files with 110 additions and 18 deletions

View File

@@ -2,23 +2,51 @@ package prometheus
import (
"context"
"fmt"
"github.com/micro/go-micro/server"
"github.com/prometheus/client_golang/prometheus"
)
func NewHandlerWrapper() server.HandlerWrapper {
var (
defaultMetricPrefix = "micro"
)
func NewHandlerWrapper(opts ...server.Option) server.HandlerWrapper {
md := make(map[string]string)
sopts := server.Options{}
for _, opt := range opts {
opt(&sopts)
}
for k, v := range sopts.Metadata {
md[fmt.Sprintf("%s_%s", defaultMetricPrefix, k)] = v
}
if len(sopts.Name) > 0 {
md[fmt.Sprintf("%s_%s", defaultMetricPrefix, "name")] = sopts.Name
}
if len(sopts.Id) > 0 {
md[fmt.Sprintf("%s_%s", defaultMetricPrefix, "id")] = sopts.Id
}
if len(sopts.Version) > 0 {
md[fmt.Sprintf("%s_%s", defaultMetricPrefix, "version")] = sopts.Version
}
opsCounter := prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "micro_request_total",
Help: "How many go-micro requests processed, partitioned by method and status",
Name: fmt.Sprintf("%s_request_total", defaultMetricPrefix),
Help: "How many go-micro requests processed, partitioned by method and status",
ConstLabels: md,
},
[]string{"method", "status"},
)
timeCounter := prometheus.NewSummaryVec(
prometheus.SummaryOpts{
Name: "micro_request_duration_microseconds",
Help: "Service method request latencies in microseconds",
Name: fmt.Sprintf("%s_request_duration_microseconds", defaultMetricPrefix),
Help: "Service method request latencies in microseconds",
ConstLabels: md,
},
[]string{"method"},
)