mirror of https://github.com/milvus-io/milvus.git
Add env variable for metrics port (#16465)
Signed-off-by: Weida Zhu <weida.zhu@zilliz.com>pull/16477/head
parent
20a4fddf6c
commit
a2011c1f25
|
|
@ -17,7 +17,11 @@
|
||||||
package metrics
|
package metrics
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
// nolint:gosec
|
// nolint:gosec
|
||||||
_ "net/http/pprof"
|
_ "net/http/pprof"
|
||||||
|
|
||||||
|
|
@ -28,6 +32,9 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
DefaultListenPort = "9091"
|
||||||
|
ListenPortEnvKey = "METRICS_PORT"
|
||||||
|
|
||||||
milvusNamespace = "milvus"
|
milvusNamespace = "milvus"
|
||||||
|
|
||||||
AbandonLabel = "abandon"
|
AbandonLabel = "abandon"
|
||||||
|
|
@ -78,8 +85,20 @@ func ServeHTTP(r *prometheus.Registry) {
|
||||||
http.Handle("/metrics", promhttp.HandlerFor(r, promhttp.HandlerOpts{}))
|
http.Handle("/metrics", promhttp.HandlerFor(r, promhttp.HandlerOpts{}))
|
||||||
http.Handle("/metrics_default", promhttp.Handler())
|
http.Handle("/metrics_default", promhttp.Handler())
|
||||||
go func() {
|
go func() {
|
||||||
if err := http.ListenAndServe(":9091", nil); err != nil {
|
bindAddr := getMetricsAddr()
|
||||||
|
log.Debug("metrics listen", zap.Any("addr", bindAddr))
|
||||||
|
if err := http.ListenAndServe(bindAddr, nil); err != nil {
|
||||||
log.Error("handle metrics failed", zap.Error(err))
|
log.Error("handle metrics failed", zap.Error(err))
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getMetricsAddr() string {
|
||||||
|
port := os.Getenv(ListenPortEnvKey)
|
||||||
|
_, err := strconv.Atoi(port)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Sprintf(":%s", DefaultListenPort)
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Sprintf(":%s", port)
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,9 +17,11 @@
|
||||||
package metrics
|
package metrics
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestRegisterMetrics(t *testing.T) {
|
func TestRegisterMetrics(t *testing.T) {
|
||||||
|
|
@ -33,4 +35,12 @@ func TestRegisterMetrics(t *testing.T) {
|
||||||
RegisterProxy(r)
|
RegisterProxy(r)
|
||||||
RegisterQueryNode(r)
|
RegisterQueryNode(r)
|
||||||
RegisterQueryCoord(r)
|
RegisterQueryCoord(r)
|
||||||
|
ServeHTTP(r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetMetricsAddr(t *testing.T) {
|
||||||
|
assert.Equal(t, getMetricsAddr(), ":"+DefaultListenPort)
|
||||||
|
testPort := "9092"
|
||||||
|
os.Setenv(ListenPortEnvKey, testPort)
|
||||||
|
assert.Equal(t, getMetricsAddr(), ":"+testPort)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue