github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/cdc/api/status/status.go (about) 1 // Copyright 2021 PingCAP, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package status 15 16 import ( 17 "context" 18 "fmt" 19 "io" 20 "net/http" 21 "os" 22 23 "github.com/gin-gonic/gin" 24 "github.com/pingcap/tiflow/cdc/api" 25 "github.com/pingcap/tiflow/cdc/api/middleware" 26 "github.com/pingcap/tiflow/cdc/capture" 27 "github.com/pingcap/tiflow/pkg/etcd" 28 "github.com/pingcap/tiflow/pkg/version" 29 ) 30 31 // status of cdc server 32 type status struct { 33 Version string `json:"version"` 34 GitHash string `json:"git_hash"` 35 ID string `json:"id"` 36 Pid int `json:"pid"` 37 IsOwner bool `json:"is_owner"` 38 } 39 40 type statusAPI struct { 41 capture capture.Capture 42 } 43 44 // RegisterStatusAPIRoutes registers routes for status. 45 func RegisterStatusAPIRoutes(router *gin.Engine, capture capture.Capture) { 46 statusAPI := statusAPI{capture: capture} 47 router.GET("/status", gin.WrapF(statusAPI.handleStatus)) 48 router.GET("/debug/info", middleware.AuthenticateMiddleware(capture), gin.WrapF(statusAPI.handleDebugInfo)) 49 } 50 51 func (h *statusAPI) writeEtcdInfo(ctx context.Context, cli etcd.CDCEtcdClient, w io.Writer) { 52 kvs, err := cli.GetAllCDCInfo(ctx) 53 if err != nil { 54 fmt.Fprintf(w, "failed to get info: %s\n\n", err.Error()) 55 return 56 } 57 58 for _, kv := range kvs { 59 fmt.Fprintf(w, "%s\n\t%s\n\n", string(kv.Key), string(kv.Value)) 60 } 61 } 62 63 func (h *statusAPI) handleDebugInfo(w http.ResponseWriter, req *http.Request) { 64 ctx := req.Context() 65 h.capture.WriteDebugInfo(ctx, w) 66 fmt.Fprintf(w, "\n\n*** etcd info ***:\n\n") 67 h.writeEtcdInfo(ctx, h.capture.GetEtcdClient(), w) 68 } 69 70 func (h *statusAPI) handleStatus(w http.ResponseWriter, req *http.Request) { 71 st := status{ 72 Version: version.ReleaseVersion, 73 GitHash: version.GitHash, 74 Pid: os.Getpid(), 75 } 76 77 if h.capture != nil { 78 info, err := h.capture.Info() 79 if err != nil { 80 api.WriteError(w, http.StatusInternalServerError, err) 81 return 82 } 83 84 st.ID = info.ID 85 st.IsOwner = h.capture.IsController() 86 } 87 api.WriteData(w, st) 88 }