diff --git a/pulsar-function-go/pf/stats.go b/pulsar-function-go/pf/stats.go index 3aac194228263..8159cac075c70 100644 --- a/pulsar-function-go/pf/stats.go +++ b/pulsar-function-go/pf/stats.go @@ -316,12 +316,14 @@ func (stat *StatWithLabelValues) reset() { func NewMetricsServicer(goInstance *goInstance) *MetricsServicer { serveMux := http.NewServeMux() - serveMux.Handle("/metrics", promhttp.HandlerFor( + pHandler := promhttp.HandlerFor( reg, promhttp.HandlerOpts{ EnableOpenMetrics: true, }, - )) + ) + serveMux.Handle("/", pHandler) + serveMux.Handle("/metrics", pHandler) server := &http.Server{ Addr: fmt.Sprintf(":%d", goInstance.context.GetMetricsPort()), Handler: serveMux, diff --git a/pulsar-function-go/pf/stats_test.go b/pulsar-function-go/pf/stats_test.go index 09b93b9c5452b..3e38d1060f088 100644 --- a/pulsar-function-go/pf/stats_test.go +++ b/pulsar-function-go/pf/stats_test.go @@ -25,6 +25,7 @@ import ( "math" "net/http" "testing" + "time" "github.com/golang/protobuf/proto" "github.com/prometheus/client_golang/prometheus" @@ -193,12 +194,23 @@ func TestMetricsServer(t *testing.T) { metricsServicer := NewMetricsServicer(gi) metricsServicer.serve() gi.stats.incrTotalReceived() + time.Sleep(time.Second * 1) - resp, err := http.Get(fmt.Sprintf("http://localhost:%d/metrics", gi.context.GetMetricsPort())) + resp, err := http.Get(fmt.Sprintf("http://localhost:%d/", gi.context.GetMetricsPort())) assert.Equal(t, nil, err) + assert.NotEqual(t, nil, resp) assert.Equal(t, 200, resp.StatusCode) body, err := ioutil.ReadAll(resp.Body) assert.Equal(t, nil, err) assert.NotEmpty(t, body) resp.Body.Close() + + resp, err = http.Get(fmt.Sprintf("http://localhost:%d/metrics", gi.context.GetMetricsPort())) + assert.Equal(t, nil, err) + assert.NotEqual(t, nil, resp) + assert.Equal(t, 200, resp.StatusCode) + body, err = ioutil.ReadAll(resp.Body) + assert.Equal(t, nil, err) + assert.NotEmpty(t, body) + resp.Body.Close() }