/
router_prometheus.go
143 lines (114 loc) · 3.45 KB
/
router_prometheus.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
package router
import (
"context"
"net/http"
"net/http/httputil"
"net/url"
"strings"
"time"
"github.com/gin-gonic/gin"
"github.com/toolkits/pkg/ginx"
pkgprom "github.com/didi/nightingale/v5/src/pkg/prom"
"github.com/didi/nightingale/v5/src/webapi/config"
"github.com/didi/nightingale/v5/src/webapi/prom"
"github.com/prometheus/common/model"
)
type queryFormItem struct {
Start int64 `json:"start" binding:"required"`
End int64 `json:"end" binding:"required"`
Step int64 `json:"step" binding:"required"`
Query string `json:"query" binding:"required"`
}
type batchQueryForm struct {
Queries []queryFormItem `json:"queries" binding:"required"`
}
func promBatchQueryRange(c *gin.Context) {
xcluster := c.GetHeader("X-Cluster")
if xcluster == "" {
ginx.Bomb(http.StatusBadRequest, "header(X-Cluster) is blank")
}
var f batchQueryForm
ginx.Dangerous(c.BindJSON(&f))
cluster, exist := prom.Clusters.Get(xcluster)
if !exist {
ginx.Bomb(http.StatusBadRequest, "cluster(%s) not found", xcluster)
}
var lst []model.Value
for _, item := range f.Queries {
r := pkgprom.Range{
Start: time.Unix(item.Start, 0),
End: time.Unix(item.End, 0),
Step: time.Duration(item.Step) * time.Second,
}
resp, _, err := cluster.PromClient.QueryRange(context.Background(), item.Query, r)
ginx.Dangerous(err)
lst = append(lst, resp)
}
ginx.NewRender(c).Data(lst, nil)
}
func prometheusProxy(c *gin.Context) {
xcluster := c.GetHeader("X-Cluster")
if xcluster == "" {
c.String(http.StatusBadRequest, "X-Cluster missed")
return
}
cluster, exists := prom.Clusters.Get(xcluster)
if !exists {
c.String(http.StatusBadRequest, "No such cluster: %s", xcluster)
return
}
target, err := url.Parse(cluster.Opts.Prom)
if err != nil {
c.String(http.StatusInternalServerError, "invalid prometheus url: %s", cluster.Opts.Prom)
return
}
director := func(req *http.Request) {
req.URL.Scheme = target.Scheme
req.URL.Host = target.Host
req.Host = target.Host
req.Header.Set("Host", target.Host)
// fe request e.g. /api/n9e/prometheus/api/v1/query
index := strings.Index(req.URL.Path, "/prometheus")
if index == -1 {
panic("url path invalid")
}
req.URL.Path = strings.TrimRight(target.Path, "/") + req.URL.Path[index+11:]
if target.RawQuery == "" || req.URL.RawQuery == "" {
req.URL.RawQuery = target.RawQuery + req.URL.RawQuery
} else {
req.URL.RawQuery = target.RawQuery + "&" + req.URL.RawQuery
}
if _, ok := req.Header["User-Agent"]; !ok {
req.Header.Set("User-Agent", "")
}
if cluster.Opts.BasicAuthUser != "" {
req.SetBasicAuth(cluster.Opts.BasicAuthUser, cluster.Opts.BasicAuthPass)
}
headerCount := len(cluster.Opts.Headers)
if headerCount > 0 && headerCount%2 == 0 {
for i := 0; i < len(cluster.Opts.Headers); i += 2 {
req.Header.Set(cluster.Opts.Headers[i], cluster.Opts.Headers[i+1])
if cluster.Opts.Headers[i] == "Host" {
req.Host = cluster.Opts.Headers[i+1]
}
}
}
}
errFunc := func(w http.ResponseWriter, r *http.Request, err error) {
http.Error(w, err.Error(), http.StatusBadGateway)
}
proxy := &httputil.ReverseProxy{
Director: director,
Transport: cluster.Transport,
ErrorHandler: errFunc,
}
proxy.ServeHTTP(c.Writer, c.Request)
}
func clustersGets(c *gin.Context) {
count := len(config.C.Clusters)
names := make([]string, 0, count)
for i := 0; i < count; i++ {
names = append(names, config.C.Clusters[i].Name)
}
ginx.NewRender(c).Data(names, nil)
}