Skip to content

Commit

Permalink
debug_api: Add more endpoints (pingcap#927)
Browse files Browse the repository at this point in the history
  • Loading branch information
shhdgit committed Jun 15, 2021
1 parent 179a6ff commit 12b0014
Show file tree
Hide file tree
Showing 44 changed files with 2,626 additions and 780 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
github.com/gtank/cryptopasta v0.0.0-20170601214702-1f550f6f2f69
github.com/joho/godotenv v1.3.0
github.com/joomcode/errorx v1.0.1
github.com/minio/sio v0.3.0
github.com/oleiade/reflections v1.0.1
github.com/pingcap/check v0.0.0-20191216031241-8a5a85928f12
github.com/pingcap/errors v0.11.5-0.20200917111840-a15ef68f753d
Expand Down
4 changes: 3 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ github.com/mattn/go-sqlite3 v1.14.5 h1:1IdxlwTNazvbKJQSxoJ5/9ECbEeaTTyeU7sEAZ5KK
github.com/mattn/go-sqlite3 v1.14.5/go.mod h1:WVKg1VTActs4Qso6iwGbiFih2UIHo0ENGwNd0Lj+XmI=
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/minio/sio v0.3.0 h1:syEFBewzOMOYVzSTFpp1MqpSZk8rUNbz8VIIc+PNzus=
github.com/minio/sio v0.3.0/go.mod h1:8b0yPp2avGThviy/+OCJBI6OMpvxoUuiLvE6F1lebhw=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
Expand Down Expand Up @@ -349,13 +351,13 @@ go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9E
go.uber.org/zap v1.9.1/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
go.uber.org/zap v1.12.0/go.mod h1:zwrFLgMcdUuIBviXEYEH1YKNaOBnKXsx2IPda5bBwHM=
go.uber.org/zap v1.15.0 h1:ZZCA22JRF2gQE5FoNmhmrf7jeJJ2uhqDUNRYKm8dvmM=
go.uber.org/zap v1.15.0/go.mod h1:Mb2vm2krFEG5DV0W9qcHBYFtp/Wku1cvYaqPsS/WYfc=
go.uber.org/zap v1.16.0 h1:uFRZXykJGK9lLY4HtgSw44DnIcAM+kRBP7x5m+NpAOM=
go.uber.org/zap v1.16.0/go.mod h1:MA8QOfq0BHJwdXa996Y4dYkAqRKB8/1K1QMMZVaNZjQ=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190513172903-22d7a77e9e5f/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200204104054-c9f3fb736b72/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
Expand Down
65 changes: 35 additions & 30 deletions pkg/apiserver/debugapi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,25 @@ package debugapi

import (
"fmt"
"time"

"go.uber.org/fx"

"github.com/pingcap/tidb-dashboard/pkg/apiserver/debugapi/endpoint"
"github.com/pingcap/tidb-dashboard/pkg/apiserver/model"
"github.com/pingcap/tidb-dashboard/pkg/httpc"
"github.com/pingcap/tidb-dashboard/pkg/pd"
"github.com/pingcap/tidb-dashboard/pkg/tidb"
"github.com/pingcap/tidb-dashboard/pkg/tiflash"
"github.com/pingcap/tidb-dashboard/pkg/tikv"
)

const (
defaultTimeout = time.Second * 45 // Default profiling can be as long as 30s.
)

type Client interface {
Send(request *Request) ([]byte, error)
Get(request *Request) ([]byte, error)
Get(request *endpoint.Request) (*httpc.Response, error)
}

type ClientMap map[model.NodeKind]Client
Expand All @@ -42,65 +48,64 @@ func newClientMap(tidbImpl tidbImplement, tikvImpl tikvImplement, tiflashImpl ti
return &clientMap
}

func defaultSendRequest(client Client, req *Request) ([]byte, error) {
func SendRequest(client Client, req *endpoint.Request) (*httpc.Response, error) {
switch req.Method {
case EndpointMethodGet:
case endpoint.MethodGet:
return client.Get(req)
default:
return nil, fmt.Errorf("invalid request method `%s`, host: %s, path: %s", req.Method, req.Host, req.Path)
}
}

func buildRelativeURI(path string, query string) string {
if len(query) == 0 {
return path
}
return fmt.Sprintf("%s?%s", path, query)
}

type tidbImplement struct {
fx.In
Client *tidb.Client
}

func (impl *tidbImplement) Get(req *Request) ([]byte, error) {
return impl.Client.WithEnforcedStatusAPIAddress(req.Host, req.Port).SendGetRequest(req.Path)
}

func (impl *tidbImplement) Send(req *Request) ([]byte, error) {
return defaultSendRequest(impl, req)
func (impl *tidbImplement) Get(req *endpoint.Request) (*httpc.Response, error) {
return impl.Client.
WithEnforcedStatusAPIAddress(req.Host, req.Port).
WithStatusAPITimeout(defaultTimeout).
Get(buildRelativeURI(req.Path, req.Query))
}

// TODO: tikv/tiflash/pd forwarder impl

type tikvImplement struct {
fx.In
Client *tikv.Client
}

func (impl *tikvImplement) Get(req *Request) ([]byte, error) {
return impl.Client.SendGetRequest(req.Host, req.Port, req.Path)
}

func (impl *tikvImplement) Send(req *Request) ([]byte, error) {
return defaultSendRequest(impl, req)
func (impl *tikvImplement) Get(req *endpoint.Request) (*httpc.Response, error) {
return impl.Client.
WithTimeout(defaultTimeout).
Get(req.Host, req.Port, buildRelativeURI(req.Path, req.Query))
}

type tiflashImplement struct {
fx.In
Client *tiflash.Client
}

func (impl *tiflashImplement) Get(req *Request) ([]byte, error) {
return impl.Client.SendGetRequest(req.Host, req.Port, req.Path)
}

func (impl *tiflashImplement) Send(req *Request) ([]byte, error) {
return defaultSendRequest(impl, req)
func (impl *tiflashImplement) Get(req *endpoint.Request) (*httpc.Response, error) {
return impl.Client.
WithTimeout(defaultTimeout).
Get(req.Host, req.Port, buildRelativeURI(req.Path, req.Query))
}

type pdImplement struct {
fx.In
Client *pd.Client
}

func (impl *pdImplement) Get(req *Request) ([]byte, error) {
return impl.Client.SendGetRequest(req.Path)
}

func (impl *pdImplement) Send(req *Request) ([]byte, error) {
return defaultSendRequest(impl, req)
func (impl *pdImplement) Get(req *endpoint.Request) (*httpc.Response, error) {
return impl.Client.
WithAddress(req.Host, req.Port).
WithTimeout(defaultTimeout).
Get(buildRelativeURI(req.Path, req.Query))
}
172 changes: 0 additions & 172 deletions pkg/apiserver/debugapi/endpoint.go

This file was deleted.

Loading

0 comments on commit 12b0014

Please sign in to comment.