diff --git a/pkg/mcs/resource_manager/server/apis/v1/api.go b/pkg/mcs/resource_manager/server/apis/v1/api.go index c6c0fc8e9e1..0029dd9eeab 100644 --- a/pkg/mcs/resource_manager/server/apis/v1/api.go +++ b/pkg/mcs/resource_manager/server/apis/v1/api.go @@ -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" @@ -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, diff --git a/pkg/mcs/tso/server/apis/v1/api.go b/pkg/mcs/tso/server/apis/v1/api.go index 775eea4b08d..af269cc95da 100644 --- a/pkg/mcs/tso/server/apis/v1/api.go +++ b/pkg/mcs/tso/server/apis/v1/api.go @@ -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" @@ -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, diff --git a/pkg/mcs/utils/util.go b/pkg/mcs/utils/util.go index 68a0efc31fa..1d66fc08c1c 100644 --- a/pkg/mcs/utils/util.go +++ b/pkg/mcs/utils/util.go @@ -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" ) @@ -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) } } diff --git a/tests/integrations/mcs/resource_manager/server_test.go b/tests/integrations/mcs/resource_manager/server_test.go index fee317b6b17..191c88b70d8 100644 --- a/tests/integrations/mcs/resource_manager/server_test.go +++ b/tests/integrations/mcs/resource_manager/server_test.go @@ -15,8 +15,6 @@ package resourcemanager_test import ( - "bytes" - "compress/gzip" "context" "encoding/json" "io" @@ -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") } } diff --git a/tests/integrations/mcs/tso/server_test.go b/tests/integrations/mcs/tso/server_test.go index 483f71992ef..30a98fadf46 100644 --- a/tests/integrations/mcs/tso/server_test.go +++ b/tests/integrations/mcs/tso/server_test.go @@ -15,8 +15,6 @@ package tso import ( - "bytes" - "compress/gzip" "context" "fmt" "io" @@ -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") }