add grouping

This commit is contained in:
bryanqiu 2023-03-06 11:55:47 +08:00
parent 426a77f4cf
commit 93bdc425e5

View File

@ -18,6 +18,7 @@ var (
metricRegistry *prometheus.Registry = nil metricRegistry *prometheus.Registry = nil
metricApiCounter *prometheus.CounterVec = nil metricApiCounter *prometheus.CounterVec = nil
metricApiSummary *prometheus.SummaryVec = nil metricApiSummary *prometheus.SummaryVec = nil
instance string = "--unknown--"
) )
type metricExporterHandler struct { type metricExporterHandler struct {
@ -41,7 +42,6 @@ func dumpMetricExporterPusher() error {
} }
func InitMetrics(instanceValue string) { func InitMetrics(instanceValue string) {
var instance = instanceValue
if instanceValue == "" { if instanceValue == "" {
instance = getInstanceIpAddress() instance = getInstanceIpAddress()
} else if ap := strings.Split(instanceValue, ":"); len(ap) == 2 && } else if ap := strings.Split(instanceValue, ":"); len(ap) == 2 &&
@ -52,18 +52,18 @@ func InitMetrics(instanceValue string) {
metricApiCounter = prometheus.NewCounterVec( metricApiCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{ prometheus.CounterOpts{
Name: "api_requests_total", Name: "api_requests_total",
Help: "How many HTTP requests processed", Help: "How many HTTP requests processed",
ConstLabels: prometheus.Labels{"instance": instance}, //ConstLabels: prometheus.Labels{"instance": instance},
}, },
[]string{"api", "errcode", "appid"}, []string{"api", "errcode", "appid"},
) )
metricApiSummary = prometheus.NewSummaryVec( metricApiSummary = prometheus.NewSummaryVec(
prometheus.SummaryOpts{ prometheus.SummaryOpts{
Name: "api_requests_summary", Name: "api_requests_summary",
Help: "The api request summary of cost.", Help: "The api request summary of cost.",
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001}, Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
ConstLabels: prometheus.Labels{"instance": instance}, //ConstLabels: prometheus.Labels{"instance": instance},
}, },
[]string{"api", "errcode", "appid"}, []string{"api", "errcode", "appid"},
) )
@ -90,7 +90,11 @@ func SetupMetricsExporterPusher(pushgateway string, jobname string) {
jobname = fmt.Sprintf("api-metrics-job-%s", getExeFilename()) 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 { MetricExporterPusher = func() error {
RecordMetrics("push_metrics_to_prometheus", "0", "selfmonitoring", 1.0) RecordMetrics("push_metrics_to_prometheus", "0", "selfmonitoring", 1.0)
return pusher.Push() return pusher.Push()