-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
api.go
145 lines (127 loc) · 3.89 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
Copyright 2019 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package vtgate
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"vitess.io/vitess/go/vt/discovery"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/servenv"
)
// This file implements a REST-style API for the vtgate web interface.
const (
apiPrefix = "/api/"
jsonContentType = "application/json; charset=utf-8"
)
func httpErrorf(w http.ResponseWriter, r *http.Request, format string, args ...any) {
errMsg := fmt.Sprintf(format, args...)
log.Errorf("HTTP error on %v: %v, request: %#v", r.URL.Path, errMsg, r)
http.Error(w, errMsg, http.StatusInternalServerError)
}
func handleAPI(apiPath string, handlerFunc func(w http.ResponseWriter, r *http.Request) error) {
servenv.HTTPHandleFunc(apiPrefix+apiPath, func(w http.ResponseWriter, r *http.Request) {
defer func() {
if x := recover(); x != nil {
httpErrorf(w, r, "uncaught panic: %v", x)
}
}()
if err := handlerFunc(w, r); err != nil {
httpErrorf(w, r, "%v", err)
}
})
}
func handleCollection(collection string, getFunc func(*http.Request) (any, error)) {
handleAPI(collection+"/", func(w http.ResponseWriter, r *http.Request) error {
// Get the requested object.
obj, err := getFunc(r)
if err != nil {
return fmt.Errorf("can't get %v: %v", collection, err)
}
// JSON encode response.
data, err := json.MarshalIndent(obj, "", " ")
if err != nil {
return fmt.Errorf("cannot marshal data: %v", err)
}
w.Header().Set("Content-Type", jsonContentType)
w.Write(data)
return nil
})
}
func getItemPath(url string) string {
// Strip API prefix.
if !strings.HasPrefix(url, apiPrefix) {
return ""
}
url = url[len(apiPrefix):]
// Strip collection name.
parts := strings.SplitN(url, "/", 2)
if len(parts) != 2 {
return ""
}
return parts[1]
}
func initAPI(hc discovery.HealthCheck) {
// Healthcheck real time status per (cell, keyspace, tablet type, metric).
handleCollection("health-check", func(r *http.Request) (any, error) {
cacheStatus := hc.CacheStatus()
itemPath := getItemPath(r.URL.Path)
if itemPath == "" {
return cacheStatus, nil
}
parts := strings.SplitN(itemPath, "/", 2)
collectionFilter := parts[0]
if collectionFilter == "" {
return cacheStatus, nil
}
if len(parts) != 2 {
return nil, fmt.Errorf("invalid health-check path: %q expected path: / or /cell/<cell> or /keyspace/<keyspace> or /tablet/mysql_hostname", itemPath)
}
value := parts[1]
switch collectionFilter {
case "cell":
{
filteredStatus := make(discovery.TabletsCacheStatusList, 0)
for _, tabletCacheStatus := range cacheStatus {
if tabletCacheStatus.Cell == value {
filteredStatus = append(filteredStatus, tabletCacheStatus)
}
}
return filteredStatus, nil
}
case "keyspace":
{
filteredStatus := make(discovery.TabletsCacheStatusList, 0)
for _, tabletCacheStatus := range cacheStatus {
if tabletCacheStatus.Target.Keyspace == value {
filteredStatus = append(filteredStatus, tabletCacheStatus)
}
}
return filteredStatus, nil
}
case "tablet":
{
// Return a _specific tablet_
for _, tabletCacheStatus := range cacheStatus {
for _, tabletStats := range tabletCacheStatus.TabletsStats {
if tabletStats.Tablet.MysqlHostname == value {
return tabletStats, nil
}
}
}
}
}
return nil, fmt.Errorf("cannot find health for: %s", itemPath)
})
}