From 93bdc425e537be297044661e639bbd8cb5118956 Mon Sep 17 00:00:00 2001 From: bryanqiu Date: Mon, 6 Mar 2023 11:55:47 +0800 Subject: [PATCH] add grouping --- prometheus.go | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/prometheus.go b/prometheus.go index 7e131f3..bb00201 100644 --- a/prometheus.go +++ b/prometheus.go @@ -18,6 +18,7 @@ var ( metricRegistry *prometheus.Registry = nil metricApiCounter *prometheus.CounterVec = nil metricApiSummary *prometheus.SummaryVec = nil + instance string = "--unknown--" ) type metricExporterHandler struct { @@ -41,7 +42,6 @@ func dumpMetricExporterPusher() error { } func InitMetrics(instanceValue string) { - var instance = instanceValue if instanceValue == "" { instance = getInstanceIpAddress() } else if ap := strings.Split(instanceValue, ":"); len(ap) == 2 && @@ -52,18 +52,18 @@ func InitMetrics(instanceValue string) { metricApiCounter = prometheus.NewCounterVec( prometheus.CounterOpts{ - Name: "api_requests_total", - Help: "How many HTTP requests processed", - ConstLabels: prometheus.Labels{"instance": instance}, + Name: "api_requests_total", + Help: "How many HTTP requests processed", + //ConstLabels: prometheus.Labels{"instance": instance}, }, []string{"api", "errcode", "appid"}, ) metricApiSummary = prometheus.NewSummaryVec( prometheus.SummaryOpts{ - Name: "api_requests_summary", - Help: "The api request summary of cost.", - Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001}, - ConstLabels: prometheus.Labels{"instance": instance}, + Name: "api_requests_summary", + Help: "The api request summary of cost.", + Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001}, + //ConstLabels: prometheus.Labels{"instance": instance}, }, []string{"api", "errcode", "appid"}, ) @@ -90,7 +90,11 @@ func SetupMetricsExporterPusher(pushgateway string, jobname string) { jobname = fmt.Sprintf("api-metrics-job-%s", getExeFilename()) } - var pusher = push.New(pushgateway, jobname).Gatherer(metricRegistry) + var pusher = push. + New(pushgateway, jobname). + Grouping("instance", instance). + Gatherer(metricRegistry) + MetricExporterPusher = func() error { RecordMetrics("push_metrics_to_prometheus", "0", "selfmonitoring", 1.0) return pusher.Push()