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
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 &&
@ -54,7 +54,7 @@ func InitMetrics(instanceValue string) {
prometheus.CounterOpts{
Name: "api_requests_total",
Help: "How many HTTP requests processed",
ConstLabels: prometheus.Labels{"instance": instance},
//ConstLabels: prometheus.Labels{"instance": instance},
},
[]string{"api", "errcode", "appid"},
)
@ -63,7 +63,7 @@ func InitMetrics(instanceValue string) {
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},
//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()