Skip to content

Commit 6b9df95

Browse files
freeznetcodelipenghui
authored andcommitted
[go-functions] fix metrics server handler error (#9394)
### Motivation #9318 add metrics server to go function, but didnt serve `"/"` endpoint in metrics server, which will cause some metrics calls failed. ### Modifications add handler for `"/"` in `MetricsServicer` ### Verifying this change - [x] Make sure that the change passes the CI checks. (cherry picked from commit c99e1a0)
1 parent d48a575 commit 6b9df95

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

pulsar-function-go/pf/stats.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,12 +316,14 @@ func (stat *StatWithLabelValues) reset() {
316316

317317
func NewMetricsServicer(goInstance *goInstance) *MetricsServicer {
318318
serveMux := http.NewServeMux()
319-
serveMux.Handle("/metrics", promhttp.HandlerFor(
319+
pHandler := promhttp.HandlerFor(
320320
reg,
321321
promhttp.HandlerOpts{
322322
EnableOpenMetrics: true,
323323
},
324-
))
324+
)
325+
serveMux.Handle("/", pHandler)
326+
serveMux.Handle("/metrics", pHandler)
325327
server := &http.Server{
326328
Addr: fmt.Sprintf(":%d", goInstance.context.GetMetricsPort()),
327329
Handler: serveMux,

pulsar-function-go/pf/stats_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"math"
2626
"net/http"
2727
"testing"
28+
"time"
2829

2930
"github.com/golang/protobuf/proto"
3031
"github.com/prometheus/client_golang/prometheus"
@@ -193,12 +194,23 @@ func TestMetricsServer(t *testing.T) {
193194
metricsServicer := NewMetricsServicer(gi)
194195
metricsServicer.serve()
195196
gi.stats.incrTotalReceived()
197+
time.Sleep(time.Second * 1)
196198

197-
resp, err := http.Get(fmt.Sprintf("http://localhost:%d/metrics", gi.context.GetMetricsPort()))
199+
resp, err := http.Get(fmt.Sprintf("http://localhost:%d/", gi.context.GetMetricsPort()))
198200
assert.Equal(t, nil, err)
201+
assert.NotEqual(t, nil, resp)
199202
assert.Equal(t, 200, resp.StatusCode)
200203
body, err := ioutil.ReadAll(resp.Body)
201204
assert.Equal(t, nil, err)
202205
assert.NotEmpty(t, body)
203206
resp.Body.Close()
207+
208+
resp, err = http.Get(fmt.Sprintf("http://localhost:%d/metrics", gi.context.GetMetricsPort()))
209+
assert.Equal(t, nil, err)
210+
assert.NotEqual(t, nil, resp)
211+
assert.Equal(t, 200, resp.StatusCode)
212+
body, err = ioutil.ReadAll(resp.Body)
213+
assert.Equal(t, nil, err)
214+
assert.NotEmpty(t, body)
215+
resp.Body.Close()
204216
}

0 commit comments

Comments
 (0)