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

api: fix health status with tls enabled #969

Merged
merged 8 commits into from
Mar 5, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion server/api/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (h *labelsHandler) GetStores(w http.ResponseWriter, r *http.Request) {

stores := cluster.GetStores()
storesInfo := &storesInfo{
Stores: make([]*storeInfo, 0, len(stores)),
Stores: make([]*StoreInfo, 0, len(stores)),
}

stores = filter.filter(stores)
Expand Down
5 changes: 5 additions & 0 deletions server/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,10 @@ func NewHandler(svr *server.Server) http.Handler {

engine.UseHandler(router)

tlsConfig, _ := svr.GetSecurityConfig().ToTLSConfig()
Copy link
Contributor

Choose a reason for hiding this comment

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

ignore error?

client = &http.Client{Transport: &http.Transport{
TLSClientConfig: tlsConfig,
}}

return engine
}
25 changes: 14 additions & 11 deletions server/api/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ import (
"github.com/unrolled/render"
)

type metaStore struct {
// MetaStore contains meta information about a store.
type MetaStore struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

why public heere?

Copy link
Member Author

Choose a reason for hiding this comment

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

tidb_exporter need these structs to unmarshal

*metapb.Store
StateName string `json:"state_name"`
}

type storeStatus struct {
// StoreStatus contains status about a store.
type StoreStatus struct {
Capacity typeutil.ByteSize `json:"capacity,omitempty"`
Available typeutil.ByteSize `json:"available,omitempty"`
LeaderCount int `json:"leader_count,omitempty"`
Expand All @@ -53,23 +55,24 @@ type storeStatus struct {
Uptime *typeutil.Duration `json:"uptime,omitempty"`
}

type storeInfo struct {
Store *metaStore `json:"store"`
Status *storeStatus `json:"status"`
// StoreInfo contains information about a store.
type StoreInfo struct {
Store *MetaStore `json:"store"`
Status *StoreStatus `json:"status"`
}

const (
disconnectedName = "Disconnected"
downStateName = "Down"
)

func newStoreInfo(store *core.StoreInfo, maxStoreDownTime time.Duration) *storeInfo {
s := &storeInfo{
Store: &metaStore{
func newStoreInfo(store *core.StoreInfo, maxStoreDownTime time.Duration) *StoreInfo {
s := &StoreInfo{
Store: &MetaStore{
Store: store.Store,
StateName: store.State.String(),
},
Status: &storeStatus{
Status: &StoreStatus{
Capacity: typeutil.ByteSize(store.Stats.GetCapacity()),
Available: typeutil.ByteSize(store.Stats.GetAvailable()),
LeaderCount: store.LeaderCount,
Expand Down Expand Up @@ -111,7 +114,7 @@ func newStoreInfo(store *core.StoreInfo, maxStoreDownTime time.Duration) *storeI

type storesInfo struct {
Count int `json:"count"`
Stores []*storeInfo `json:"stores"`
Stores []*StoreInfo `json:"stores"`
}

type storeHandler struct {
Expand Down Expand Up @@ -324,7 +327,7 @@ func (h *storesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {

stores := cluster.GetStores()
storesInfo := &storesInfo{
Stores: make([]*storeInfo, 0, len(stores)),
Stores: make([]*StoreInfo, 0, len(stores)),
}

urlFilter, err := newStoreStateFilter(r.URL)
Expand Down
14 changes: 10 additions & 4 deletions server/api/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"github.com/juju/errors"
)

var client = &http.Client{}

func readJSON(r io.ReadCloser, data interface{}) error {
defer r.Close()

Expand All @@ -38,8 +40,8 @@ func readJSON(r io.ReadCloser, data interface{}) error {
return nil
}

func postJSON(cli *http.Client, url string, data []byte) error {
resp, err := cli.Post(url, "application/json", bytes.NewBuffer(data))
func postJSON(url string, data []byte) error {
resp, err := client.Post(url, "application/json", bytes.NewBuffer(data))
if err != nil {
return errors.Trace(err)
}
Expand All @@ -59,7 +61,7 @@ func doDelete(url string) error {
if err != nil {
return err
}
res, err := http.DefaultClient.Do(req)
res, err := client.Do(req)
if err != nil {
return err
}
Expand All @@ -68,7 +70,11 @@ func doDelete(url string) error {
}

func doGet(url string) error {
resp, err := http.Get(url)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
resp, err := client.Do(req)
if err != nil {
return errors.Trace(err)
}
Expand Down