Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mcs: support metrics HTTP interface for tso/resource manager server #6329

Merged
merged 2 commits into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 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,9 @@ 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"
"github.com/tikv/pd/pkg/utils/apiutil/multiservicesapi"
)
Expand Down Expand Up @@ -75,6 +77,7 @@ func NewService(srv *rmserver.Service) *Service {
c.Next()
})
apiHandlerEngine.Use(multiservicesapi.ServiceRedirector())
apiHandlerEngine.GET("metrics", utils.PromHandler(promhttp.Handler()))
endpoint := apiHandlerEngine.Group(APIPathPrefix)
s := &Service{
manager: manager,
Expand Down
3 changes: 3 additions & 0 deletions pkg/mcs/tso/server/apis/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ 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"
"github.com/tikv/pd/pkg/utils/apiutil"
"github.com/tikv/pd/pkg/utils/apiutil/multiservicesapi"
Expand Down Expand Up @@ -80,6 +82,7 @@ func NewService(srv *tsoserver.Service) *Service {
c.Next()
})
apiHandlerEngine.Use(multiservicesapi.ServiceRedirector())
apiHandlerEngine.GET("metrics", utils.PromHandler(promhttp.Handler()))
endpoint := apiHandlerEngine.Group(APIPathPrefix)
s := &Service{
srv: srv,
Expand Down
9 changes: 9 additions & 0 deletions pkg/mcs/utils/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ package utils

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

"github.com/gin-gonic/gin"
"github.com/pkg/errors"
"github.com/tikv/pd/pkg/utils/etcdutil"
"go.etcd.io/etcd/clientv3"
Expand Down Expand Up @@ -46,3 +48,10 @@ func InitClusterID(ctx context.Context, client *clientv3.Client) (id uint64, err
}
return 0, errors.Errorf("failed to init cluster ID after retrying %d times", maxRetryTimes)
}

// PromHandler is a handler to get prometheus metrics.
func PromHandler(handler http.Handler) gin.HandlerFunc {
return func(c *gin.Context) {
handler.ServeHTTP(c.Writer, c.Request)
}
}
18 changes: 18 additions & 0 deletions tests/integrations/mcs/resource_manager/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package resourcemanager_test

import (
"bytes"
"compress/gzip"
"context"
"encoding/json"
"io"
Expand Down Expand Up @@ -92,4 +94,20 @@ func TestResourceManagerServer(t *testing.T) {
re.NoError(err)
re.Equal("{\"name\":\"pingcap\",\"mode\":1,\"r_u_settings\":{\"r_u\":{\"state\":{\"initialized\":false}}},\"priority\":0}", string(respString))
}

// Test metrics handler
{
resp, err := http.Get(addr + "/metrics")
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)
re.NoError(err)
re.Contains(string(output), "resource_manager_server_info")
}
}
36 changes: 36 additions & 0 deletions tests/integrations/mcs/tso/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
package tso

import (
"bytes"
"compress/gzip"
"context"
"fmt"
"io"
"net/http"
"strconv"
"strings"
Expand Down Expand Up @@ -345,3 +348,36 @@ func TestAdvertiseAddr(t *testing.T) {
tsoServerConf := s.GetConfig()
re.Equal(u, tsoServerConf.AdvertiseListenAddr)
}

func TestMetrics(t *testing.T) {
re := require.New(t)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cluster, err := tests.NewTestAPICluster(ctx, 1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we put cluster and service initialization into suite setup and share it with other similar test cases to save test time?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, maybe do it in another PR.

defer cluster.Destroy()
re.NoError(err)

err = cluster.RunInitialServers()
re.NoError(err)

leaderName := cluster.WaitLeader()
leader := cluster.GetServer(leaderName)

u := tempurl.Alloc()
s, cleanup := mcs.StartSingleTSOTestServer(ctx, re, leader.GetAddr(), u)
defer cleanup()

resp, err := http.Get(s.GetConfig().GetAdvertiseListenAddr() + "/metrics")
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)
re.NoError(err)
re.Contains(string(output), "tso_server_info")
}