vitess.io/vitess@v0.16.2/go/vt/status/status.go (about) 1 /* 2 Copyright 2019 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 status defines a few useful functions for our binaries, 18 // mainly to link the status page with a vtctld instance. 19 package status 20 21 import ( 22 "fmt" 23 "html/template" 24 "net/url" 25 "strings" 26 27 "github.com/spf13/pflag" 28 29 "vitess.io/vitess/go/vt/servenv" 30 ) 31 32 var vtctldAddr string 33 34 func registerFlags(fs *pflag.FlagSet) { 35 fs.StringVar(&vtctldAddr, "vtctld_addr", vtctldAddr, "address of a vtctld instance") 36 } 37 38 func init() { 39 servenv.OnParseFor("vtcombo", registerFlags) 40 servenv.OnParseFor("vtgate", registerFlags) 41 servenv.OnParseFor("vttablet", registerFlags) 42 } 43 44 // MakeVtctldRedirect returns an absolute vtctld url that will 45 // redirect to the page for the topology object specified in q. 46 func MakeVtctldRedirect(text string, q map[string]string) template.HTML { 47 query := url.Values{} 48 for k, v := range q { 49 query.Set(k, v) 50 } 51 url := "explorers/redirect" + "?" + query.Encode() 52 return VtctldLink(text, url) 53 } 54 55 // VtctldLink returns the HTML to display a link to the fully 56 // qualified vtctld url whose path is given as parameter. 57 // If no vtctld_addr flag was passed in, we just return the text with no link. 58 func VtctldLink(text, urlPath string) template.HTML { 59 if vtctldAddr == "" { 60 return template.HTML(text) 61 } 62 var fullURL string 63 if strings.HasSuffix(vtctldAddr, "/") { 64 fullURL = vtctldAddr + urlPath 65 } else { 66 fullURL = vtctldAddr + "/" + urlPath 67 } 68 69 return template.HTML(fmt.Sprintf(`<a href="%v">%v</a>`, fullURL, text)) 70 } 71 72 // VtctldKeyspace returns the keyspace name, possibly linked to the 73 // keyspace page in vtctld. 74 func VtctldKeyspace(keyspace string) template.HTML { 75 return MakeVtctldRedirect(keyspace, 76 map[string]string{ 77 "type": "keyspace", 78 "keyspace": keyspace, 79 }) 80 } 81 82 // VtctldShard returns the shard name, possibly linked to the shard 83 // page in vtctld. 84 func VtctldShard(keyspace, shard string) template.HTML { 85 return MakeVtctldRedirect(shard, map[string]string{ 86 "type": "shard", 87 "keyspace": keyspace, 88 "shard": shard, 89 }) 90 } 91 92 // VtctldSrvCell returns the cell name, possibly linked to the 93 // serving graph page in vtctld for that page. 94 func VtctldSrvCell(cell string) template.HTML { 95 return VtctldLink(cell, "serving_graph/"+cell) 96 } 97 98 // VtctldSrvKeyspace returns the keyspace name, possibly linked to the 99 // SrvKeyspace page in vtctld. 100 func VtctldSrvKeyspace(cell, keyspace string) template.HTML { 101 return MakeVtctldRedirect(keyspace, map[string]string{ 102 "type": "srv_keyspace", 103 "cell": cell, 104 "keyspace": keyspace, 105 }) 106 } 107 108 // VtctldReplication returns 'cell/keyspace/shard', possibly linked to the 109 // ShardReplication page in vtctld. 110 func VtctldReplication(cell, keyspace, shard string) template.HTML { 111 return MakeVtctldRedirect(fmt.Sprintf("%v/%v/%v", cell, keyspace, shard), 112 map[string]string{ 113 "type": "replication", 114 "keyspace": keyspace, 115 "shard": shard, 116 "cell": cell, 117 }) 118 } 119 120 // VtctldTablet returns the tablet alias, possibly linked to the 121 // Tablet page in vtctld. 122 func VtctldTablet(aliasName string) template.HTML { 123 return MakeVtctldRedirect(aliasName, map[string]string{ 124 "type": "tablet", 125 "alias": aliasName, 126 }) 127 } 128 129 // StatusFuncs returns a FuncMap that contains all of our methods here. 130 // It is exported so tests can use them. 131 var StatusFuncs = template.FuncMap{ 132 "github_com_vitessio_vitess_vtctld_keyspace": VtctldKeyspace, 133 "github_com_vitessio_vitess_vtctld_shard": VtctldShard, 134 "github_com_vitessio_vitess_vtctld_srv_cell": VtctldSrvCell, 135 "github_com_vitessio_vitess_vtctld_srv_keyspace": VtctldSrvKeyspace, 136 "github_com_vitessio_vitess_vtctld_replication": VtctldReplication, 137 "github_com_vitessio_vitess_vtctld_tablet": VtctldTablet, 138 } 139 140 func init() { 141 servenv.AddStatusFuncs(StatusFuncs) 142 }