/
promethues.go
351 lines (315 loc) · 9.17 KB
/
promethues.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// Copyright 2018 Istio 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 service provides prometheus service for user to query metrics.
package service
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strconv"
"strings"
"time"
"github.com/prometheus/client_golang/api"
"github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prometheus/common/model"
"github.com/xiaomi/naftis/src/api/bootstrap"
"github.com/xiaomi/naftis/src/api/log"
)
// Prom wraps prom service for easily use.
var Prom *prom
type prom struct {
promHandler
api v1.API
}
const (
localPromURL = "http://localhost:9090"
istioPromURLFmt = "http://prometheus.%s:9090"
)
// InitProm initializes prometheus service.
func InitProm() {
addr := localPromURL
if bootstrap.Args.InCluster {
addr = fmt.Sprintf(istioPromURLFmt, bootstrap.Args.IstioNamespace)
}
client, err := api.NewClient(api.Config{Address: addr})
if err != nil {
log.Error("[Prom] init fail")
}
Prom = &prom{
promHandler: promHandler{
addr: addr,
static: &Static{Nodes: make(map[string]struct{})},
writer: GenerateD3JSON,
},
api: v1.NewAPI(client),
}
}
// Query queries prometheus Value with specific query string.
func (p *prom) Query(q string) (model.Value, error) {
val, _, err := p.api.Query(context.Background(), q, time.Now())
return val, err
}
// Graph represents a service graph generated.
type Graph struct {
Nodes map[string]struct{} `json:"nodes"`
Edges []*Edge `json:"edges"`
}
type (
// Static represents a service graph generated by API calls that is
// meant to persist across generation requests. It must be merged with
// a Dynamic graph to provide a complete service graph for Istio.
Static struct {
Nodes map[string]struct{}
}
// Dynamic represents a service graph generated on the fly.
Dynamic struct {
Nodes map[string]struct{} `json:"nodes"`
Edges []*Edge `json:"edges"`
}
// Edge represents an edge in a dynamic service graph.
Edge struct {
Source string `json:"source"`
Target string `json:"target"`
Labels Attributes `json:"labels"`
}
// Attributes contain a set of annotations for an edge.
Attributes map[string]string
// SerializeFn provides a mechanism for writing out the service graph.
SerializeFn func(w io.Writer, g *Dynamic) error
)
// AddEdge adds a new edge to an existing dynamic graph.
func (d *Dynamic) AddEdge(src, target string, lbls map[string]string) {
d.Edges = append(d.Edges, &Edge{src, target, lbls})
d.Nodes[src] = struct{}{}
d.Nodes[target] = struct{}{}
}
// Merge adds all of the nodes in the static graph into the dynamic graph.
func (d *Dynamic) Merge(static *Static) {
for node := range static.Nodes {
d.Nodes[node] = struct{}{}
}
}
type (
// d3Graph represents a service d3 graph generated.
d3Graph struct {
Nodes []d3Node `json:"nodes"`
Links []d3Link `json:"links"`
}
d3Node struct {
Name string `json:"name"`
}
d3Link struct {
Source int `json:"source"`
Target int `json:"target"`
Labels Attributes `json:"labels"`
}
)
func indexOf(nodes []d3Node, name string) (int, error) {
for i, v := range nodes {
if v.Name == name {
return i, nil
}
}
return 0, errors.New("invalid graph")
}
// GenerateD3JSON converts the standard Dynamic graph to d3Graph, then
// serializes to JSON.
func GenerateD3JSON(w io.Writer, g *Dynamic) error {
graph := d3Graph{
Nodes: make([]d3Node, 0, len(g.Nodes)),
Links: make([]d3Link, 0, len(g.Edges)),
}
for k := range g.Nodes {
n := d3Node{
Name: k,
}
graph.Nodes = append(graph.Nodes, n)
}
for _, v := range g.Edges {
s, err := indexOf(graph.Nodes, v.Source)
if err != nil {
return err
}
t, err := indexOf(graph.Nodes, v.Target)
if err != nil {
return err
}
l := d3Link{
Source: s,
Target: t,
Labels: v.Labels,
}
graph.Links = append(graph.Links, l)
}
return json.NewEncoder(w).Encode(graph)
}
const reqsFmt = "sum(rate(istio_requests_total{reporter=\"destination\"%s}[%s])) by (source_workload, destination_workload, source_app, destination_app)"
const tcpFmt = "sum(rate(istio_tcp_received_bytes_total{reporter=\"destination\"%s}[%s])) by (source_workload, destination_workload, source_app, destination_app)"
const emptyFilter = " > 0"
type genOpts struct {
timeHorizon string
filterEmpty bool
dstNamespace string
dstWorkload string
srcNamespace string
srcWorkload string
}
type promHandler struct {
addr string
static *Static
writer SerializeFn
}
// NewPromHandler returns a new http.Handler that will serve servicegraph data
// based on queries against a prometheus backend.
func NewPromHandler(addr string, static *Static, writer SerializeFn) http.Handler {
return &promHandler{addr, static, writer}
}
// AddEdge adds a new edge to an existing dynamic graph.
func (g *Graph) AddEdge(src, target string, lbls map[string]string) {
g.Edges = append(g.Edges, &Edge{src, target, lbls})
g.Nodes[src] = struct{}{}
g.Nodes[target] = struct{}{}
}
func (p *promHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
timeHorizon := r.URL.Query().Get("time_horizon")
if timeHorizon == "" {
timeHorizon = "5m"
}
filterEmpty := false
filterEmptyStr := r.URL.Query().Get("filter_empty")
if filterEmptyStr == "true" {
filterEmpty = true
}
dstNamespace := r.URL.Query().Get("destination_namespace")
dstWorkload := r.URL.Query().Get("destination_workload")
srcNamespace := r.URL.Query().Get("source_namespace")
srcWorkload := r.URL.Query().Get("source_workload")
// validate time_horizon
if _, err := model.ParseDuration(timeHorizon); err != nil {
writeError(w, fmt.Errorf("could not parse time_horizon: %v", err))
return
}
g, err := p.generate(genOpts{
timeHorizon,
filterEmpty,
dstNamespace,
dstWorkload,
srcNamespace,
srcWorkload,
})
g.Merge(p.static)
if err != nil {
writeError(w, err)
return
}
err = p.writer(w, g)
if err != nil {
writeError(w, err)
return
}
}
func writeError(w http.ResponseWriter, err error) {
w.WriteHeader(http.StatusInternalServerError)
_, writeErr := w.Write([]byte(err.Error()))
log.Error("[Graph] writeError", "err", writeErr, "orgerr", err)
}
func (p *promHandler) generate(opts genOpts) (*Dynamic, error) {
client, err := api.NewClient(api.Config{Address: p.addr})
if err != nil {
return nil, err
}
api := v1.NewAPI(client)
serviceFilter := generateServiceFilter(opts)
query := fmt.Sprintf(reqsFmt, serviceFilter, opts.timeHorizon)
if opts.filterEmpty {
query += emptyFilter
}
graph, err := extractGraph(api, query, "reqs/sec")
if err != nil {
return nil, err
}
query = fmt.Sprintf(tcpFmt, serviceFilter, opts.timeHorizon)
if opts.filterEmpty {
query += emptyFilter
}
tcpGraph, err := extractGraph(api, query, "bytes/sec")
if err != nil {
return nil, err
}
return merge(graph, tcpGraph)
}
func generateServiceFilter(opts genOpts) string {
filterParams := make([]string, 0, 4)
if opts.dstNamespace != "" {
filterParams = append(filterParams, "destination_namespace=\""+opts.dstNamespace+"\"")
}
if opts.dstWorkload != "" {
filterParams = append(filterParams, "destination_workload=\""+opts.dstWorkload+"\"")
}
if opts.srcNamespace != "" {
filterParams = append(filterParams, "source_workload_namespace=\""+opts.srcNamespace+"\"")
}
if opts.srcWorkload != "" {
filterParams = append(filterParams, "source_workload=\""+opts.srcWorkload+"\"")
}
filterStr := strings.Join(filterParams, ", ")
if filterStr != "" {
filterStr = ", " + filterStr
}
return filterStr
}
func merge(g1, g2 *Dynamic) (*Dynamic, error) {
d := Dynamic{Nodes: map[string]struct{}{}, Edges: []*Edge{}}
d.Edges = append(d.Edges, g1.Edges...)
d.Edges = append(d.Edges, g2.Edges...)
for nodeName, nodeValue := range g1.Nodes {
d.Nodes[nodeName] = nodeValue
}
for nodeName, nodeValue := range g2.Nodes {
d.Nodes[nodeName] = nodeValue
}
return &d, nil
}
func extractGraph(api v1.API, query, label string) (*Dynamic, error) {
val, _, err := api.Query(context.Background(), query, time.Now())
if err != nil {
return nil, err
}
switch val.Type() {
case model.ValVector:
matrix := val.(model.Vector)
d := Dynamic{Nodes: map[string]struct{}{}, Edges: []*Edge{}}
for _, sample := range matrix {
// todo: add error checking here
metric := sample.Metric
srcWorkload := string(metric["source_workload"])
src := string(metric["source_app"])
dstWorkload := string(metric["destination_workload"])
dst := string(metric["destination_app"])
value := sample.Value
d.AddEdge(
src+" ("+srcWorkload+")",
dst+" ("+dstWorkload+")",
Attributes{
label: strconv.FormatFloat(float64(value), 'f', 6, 64),
})
}
return &d, nil
default:
return nil, fmt.Errorf("unknown value type returned from query: %#v", val)
}
}