Skip to content

Commit

Permalink
[go-functions] fix metrics server handler error (apache#9394)
Browse files Browse the repository at this point in the history
### Motivation

apache#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.
  • Loading branch information
freeznet authored Feb 2, 2021
1 parent f711969 commit c99e1a0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
6 changes: 4 additions & 2 deletions pulsar-function-go/pf/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 13 additions & 1 deletion pulsar-function-go/pf/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"math"
"net/http"
"testing"
"time"

"github.com/golang/protobuf/proto"
"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -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()
}

0 comments on commit c99e1a0

Please sign in to comment.