wrapper: prometheus attach labels to all metrics
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
parent
6aff955f07
commit
d927ad4673
@ -35,34 +35,40 @@ func NewHandlerWrapper(opts ...server.Option) server.HandlerWrapper {
|
|||||||
|
|
||||||
opsCounter := prometheus.NewCounterVec(
|
opsCounter := prometheus.NewCounterVec(
|
||||||
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"},
|
||||||
)
|
)
|
||||||
|
|
||||||
timeCounterSummary := prometheus.NewSummaryVec(
|
timeCounterSummary := prometheus.NewSummaryVec(
|
||||||
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"},
|
||||||
)
|
)
|
||||||
|
|
||||||
timeCounterHistogram := prometheus.NewHistogramVec(
|
timeCounterHistogram := prometheus.NewHistogramVec(
|
||||||
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 {
|
||||||
|
@ -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":
|
||||||
|
Loading…
Reference in New Issue
Block a user