vitess.io/vitess@v0.16.2/go/vt/vtadmin/http/experimental/tablets.go (about) 1 /* 2 Copyright 2021 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package experimental 18 19 import ( 20 "bytes" 21 "context" 22 "encoding/json" 23 "io" 24 "net/http" 25 "text/template" 26 27 vtadminhttp "vitess.io/vitess/go/vt/vtadmin/http" 28 29 vtadminpb "vitess.io/vitess/go/vt/proto/vtadmin" 30 ) 31 32 // TabletDebugVarsPassthrough makes a passthrough request to a tablet's 33 // /debug/vars route, after looking up the tablet via VTAdmin's GetTablet 34 // rpc. 35 func TabletDebugVarsPassthrough(ctx context.Context, r vtadminhttp.Request, api *vtadminhttp.API) *vtadminhttp.JSONResponse { 36 vars := r.Vars() 37 38 alias, err := vars.GetTabletAlias("tablet") 39 if err != nil { 40 return vtadminhttp.NewJSONResponse(nil, err) 41 } 42 43 tablet, err := api.Server().GetTablet(ctx, &vtadminpb.GetTabletRequest{ 44 Alias: alias, 45 ClusterIds: r.URL.Query()["cluster"], 46 }) 47 48 if err != nil { 49 return vtadminhttp.NewJSONResponse(nil, err) 50 } 51 52 debugVars, err := getDebugVars(ctx, api, tablet) 53 return vtadminhttp.NewJSONResponse(debugVars, err) 54 } 55 56 func getDebugVars(ctx context.Context, api *vtadminhttp.API, tablet *vtadminpb.Tablet) (map[string]any, error) { 57 tmpl, err := template.New("tablet-fqdn").Parse(api.Options().ExperimentalOptions.TabletURLTmpl) 58 if err != nil { 59 return nil, err 60 } 61 62 buf := bytes.NewBuffer(nil) 63 if err := tmpl.Execute(buf, tablet); err != nil { 64 return nil, err 65 } 66 _, _ = buf.WriteString("/debug/vars") 67 68 url := buf.String() 69 req, err := http.NewRequestWithContext(ctx, "GET", url, nil) 70 if err != nil { 71 return nil, err 72 } 73 74 resp, err := http.DefaultClient.Do(req) 75 if err != nil { 76 return nil, err 77 } 78 79 defer resp.Body.Close() 80 81 data, err := io.ReadAll(resp.Body) 82 if err != nil { 83 return nil, err 84 } 85 86 var debugVars map[string]any 87 if err := json.Unmarshal(data, &debugVars); err != nil { 88 return nil, err 89 } 90 91 return debugVars, nil 92 }