Skip to content

Commit

Permalink
mcs: fix double compression of prom handler (#6339)
Browse files Browse the repository at this point in the history
ref prometheus/client_golang#622, ref #5895

Signed-off-by: Ryan Leung <[email protected]>
  • Loading branch information
rleungx authored Apr 20, 2023
1 parent 3cc745a commit 9078ea5
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 25 deletions.
3 changes: 1 addition & 2 deletions pkg/mcs/resource_manager/server/apis/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
rmpb "github.com/pingcap/kvproto/pkg/resource_manager"
"github.com/prometheus/client_golang/prometheus/promhttp"
rmserver "github.com/tikv/pd/pkg/mcs/resource_manager/server"
"github.com/tikv/pd/pkg/mcs/utils"
"github.com/tikv/pd/pkg/utils/apiutil"
Expand Down Expand Up @@ -77,7 +76,7 @@ func NewService(srv *rmserver.Service) *Service {
c.Next()
})
apiHandlerEngine.Use(multiservicesapi.ServiceRedirector())
apiHandlerEngine.GET("metrics", utils.PromHandler(promhttp.Handler()))
apiHandlerEngine.GET("metrics", utils.PromHandler())
endpoint := apiHandlerEngine.Group(APIPathPrefix)
s := &Service{
manager: manager,
Expand Down
3 changes: 1 addition & 2 deletions pkg/mcs/tso/server/apis/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"github.com/prometheus/client_golang/prometheus/promhttp"
tsoserver "github.com/tikv/pd/pkg/mcs/tso/server"
"github.com/tikv/pd/pkg/mcs/utils"
"github.com/tikv/pd/pkg/tso"
Expand Down Expand Up @@ -82,7 +81,7 @@ func NewService(srv *tsoserver.Service) *Service {
c.Next()
})
apiHandlerEngine.Use(multiservicesapi.ServiceRedirector())
apiHandlerEngine.GET("metrics", utils.PromHandler(promhttp.Handler()))
apiHandlerEngine.GET("metrics", utils.PromHandler())
endpoint := apiHandlerEngine.Group(APIPathPrefix)
s := &Service{
srv: srv,
Expand Down
10 changes: 7 additions & 3 deletions pkg/mcs/utils/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ package utils

import (
"context"
"net/http"
"time"

"github.com/gin-gonic/gin"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/tikv/pd/pkg/utils/etcdutil"
"go.etcd.io/etcd/clientv3"
)
Expand Down Expand Up @@ -50,8 +51,11 @@ func InitClusterID(ctx context.Context, client *clientv3.Client) (id uint64, err
}

// PromHandler is a handler to get prometheus metrics.
func PromHandler(handler http.Handler) gin.HandlerFunc {
func PromHandler() gin.HandlerFunc {
return func(c *gin.Context) {
handler.ServeHTTP(c.Writer, c.Request)
// register promhttp.HandlerOpts DisableCompression
promhttp.InstrumentMetricHandler(prometheus.DefaultRegisterer, promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{
DisableCompression: true,
})).ServeHTTP(c.Writer, c.Request)
}
}
11 changes: 2 additions & 9 deletions tests/integrations/mcs/resource_manager/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
package resourcemanager_test

import (
"bytes"
"compress/gzip"
"context"
"encoding/json"
"io"
Expand Down Expand Up @@ -101,13 +99,8 @@ func TestResourceManagerServer(t *testing.T) {
re.NoError(err)
defer resp.Body.Close()
re.Equal(http.StatusOK, resp.StatusCode)
respString, err := io.ReadAll(resp.Body)
re.NoError(err)
reader := bytes.NewReader(respString)
gzipReader, err := gzip.NewReader(reader)
re.NoError(err)
output, err := io.ReadAll(gzipReader)
respBytes, err := io.ReadAll(resp.Body)
re.NoError(err)
re.Contains(string(output), "resource_manager_server_info")
re.Contains(string(respBytes), "resource_manager_server_info")
}
}
11 changes: 2 additions & 9 deletions tests/integrations/mcs/tso/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
package tso

import (
"bytes"
"compress/gzip"
"context"
"fmt"
"io"
Expand Down Expand Up @@ -364,12 +362,7 @@ func TestMetrics(t *testing.T) {
re.NoError(err)
defer resp.Body.Close()
re.Equal(http.StatusOK, resp.StatusCode)
respString, err := io.ReadAll(resp.Body)
respBytes, err := io.ReadAll(resp.Body)
re.NoError(err)
reader := bytes.NewReader(respString)
gzipReader, err := gzip.NewReader(reader)
re.NoError(err)
output, err := io.ReadAll(gzipReader)
re.NoError(err)
re.Contains(string(output), "tso_server_info")
re.Contains(string(respBytes), "tso_server_info")
}

0 comments on commit 9078ea5

Please sign in to comment.