From 1d8453af9d6c9b0a90b5daadb1d1952a0c00dad7 Mon Sep 17 00:00:00 2001 From: bryanqiu Date: Wed, 21 Dec 2022 16:46:56 +0800 Subject: [PATCH] surport outside instance --- prometheus.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/prometheus.go b/prometheus.go index de25bd9..7e131f3 100644 --- a/prometheus.go +++ b/prometheus.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "net/http" + "strings" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" @@ -39,9 +40,16 @@ func dumpMetricExporterPusher() error { return errors.New("not initilized, forgot call 'SetupMetricsExporterPusher'?") } -func InitMetrics() { - instance := getInstanceIpAddress() - log.Infof("self instance ip: [%s]", instance) +func InitMetrics(instanceValue string) { + var instance = instanceValue + if instanceValue == "" { + instance = getInstanceIpAddress() + } else if ap := strings.Split(instanceValue, ":"); len(ap) == 2 && + ap[0] == "0.0.0.0" || ap[0] == "" { + instance = getInstanceIpAddress() + ":" + ap[1] + } + log.Infof("self instance: [%s]", instance) + metricApiCounter = prometheus.NewCounterVec( prometheus.CounterOpts{ Name: "api_requests_total", @@ -59,6 +67,7 @@ func InitMetrics() { }, []string{"api", "errcode", "appid"}, ) + metricRegistry = prometheus.NewRegistry() metricRegistry.MustRegister(metricApiCounter) metricRegistry.MustRegister(metricApiSummary)