-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
346 lines (303 loc) · 8.18 KB
/
main.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
// Package main implements a service that queries jaeger-query and
// returns data suitable for use by the Grafana SimpleJSON plugin.
package main // import "github.com/QubitProducts/jaeger-simplejson"
import (
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"log"
"net/http"
"net/url"
"strings"
"time"
simplejson "github.com/tcolgate/grafana-simple-json-go"
)
var addr string
var baseURL string
var linkURL string
func init() {
flag.StringVar(&addr, "addr", ":8080", "address to listen on")
flag.StringVar(&baseURL, "baseURL", "", "api URL")
flag.StringVar(&linkURL, "linkURL", "", "external jaeger UI URL")
}
type jaegerSJHandler struct {
base *url.URL
linkURL *url.URL
}
type traceTag struct {
Key string `json:"key"`
Type string `json:"type"`
Value interface{} `json:"value"`
}
type traceSpan struct {
StartTime int64 `json:"startTime"`
Duration int64 `json:"duration"`
OperationName string `json:"operationName"`
TraceID string `json:"traceID"`
Tags []traceTag `json:"tags"`
ProcessID string `json:"processID"`
process string
}
type traceProcesses struct {
ServiceName string `json:"serviceName"`
Tags []traceTag `json:"tags"`
}
type traceResp struct {
TraceID string `json:"traceID"`
Spans []traceSpan `json:"spans"`
Processes map[string]traceProcesses `json:"processes"`
}
func (jh *jaegerSJHandler) traceURL(id string) string {
return fmt.Sprintf("%v/trace/%v", jh.linkURL, id)
}
func (jh *jaegerSJHandler) runQuery(ctx context.Context, from, to time.Time, service string, limit int) ([]traceResp, error) {
u, err := url.Parse(fmt.Sprintf("%s/api/traces", jh.base))
if err != nil {
return nil, err
}
q := u.Query()
q.Set("start", fmt.Sprintf("%v", from.UnixNano()/1000))
q.Set("end", fmt.Sprintf("%v", to.UnixNano()/1000))
q.Set("service", service)
if limit != 0 {
q.Set("limit", fmt.Sprintf("%d", limit))
}
u.RawQuery = q.Encode()
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
return nil, err
}
resp, err := http.DefaultClient.Do(req.WithContext(ctx))
if err != nil {
return nil, err
}
traces := struct {
Data []traceResp `json:"data"`
Errors []json.RawMessage `json:"errors"`
}{}
dec := json.NewDecoder(resp.Body)
err = dec.Decode(&traces)
if err != nil {
return nil, err
}
if len(traces.Errors) > 0 {
for _, e := range traces.Errors {
log.Printf("error: %v", string(e))
}
return nil, errors.New("errors in response from jaeger")
}
return traces.Data, nil
}
func (jh *jaegerSJHandler) GrafanaQuery(ctx context.Context, target string, args simplejson.QueryArguments) ([]simplejson.DataPoint, error) {
tt, err := jh.runQuery(ctx, args.From, args.To, target, args.MaxDPs)
if err != nil {
return nil, err
}
var res []simplejson.DataPoint
for i := range tt {
start := int64(1<<63 - 1)
var serviceDuration int64
ss := tt[i].Spans
for j := range ss {
if proc, ok := tt[i].Processes[ss[j].ProcessID]; ok &&
proc.ServiceName == target &&
ss[j].Duration > serviceDuration {
if ss[j].StartTime < start {
start = ss[j].StartTime
serviceDuration = ss[j].Duration
}
}
}
if serviceDuration != 0 {
res = append(res, simplejson.DataPoint{Time: time.Unix(0, start*1000), Value: float64(serviceDuration) / 1000000})
}
}
return res, nil
}
func (jh *jaegerSJHandler) GrafanaQueryTable(ctx context.Context, target string, args simplejson.TableQueryArguments) ([]simplejson.TableColumn, error) {
tt, err := jh.runQuery(ctx, args.From, args.To, target, 0)
if err != nil {
return nil, err
}
var times simplejson.TableTimeColumn
var ids simplejson.TableStringColumn
var links simplejson.TableStringColumn
var html simplejson.TableStringColumn
var operations simplejson.TableStringColumn
var durs simplejson.TableNumberColumn
var serviceDurs simplejson.TableNumberColumn
var spanCounts simplejson.TableNumberColumn
var errCounts simplejson.TableNumberColumn
for i := range tt {
ids = append(ids, tt[i].TraceID)
links = append(links, jh.traceURL(tt[i].TraceID))
html = append(html, fmt.Sprintf(`<a href="%v" target="_blank">%v</a>`, jh.traceURL(tt[i].TraceID), tt[i].TraceID))
spanCounts = append(spanCounts, float64(len(tt[i].Spans)))
start := int64(1<<63 - 1)
var operation string
var serviceDuration int64
var duration int64
var errs int64
ss := tt[i].Spans
for j := range ss {
if ss[j].StartTime < start {
start = ss[j].StartTime
}
if ss[j].Duration > duration {
duration = ss[j].Duration
}
if proc, ok := tt[i].Processes[ss[j].ProcessID]; ok &&
proc.ServiceName == target &&
ss[j].Duration > serviceDuration {
operation = ss[j].OperationName
serviceDuration = ss[j].Duration
}
for k := range ss[j].Tags {
if ss[j].Tags[k].Key == "error" &&
ss[j].Tags[k].Type == "bool" {
errs++
}
}
}
operations = append(operations, operation)
times = append(times, time.Unix(0, start*1000))
errCounts = append(errCounts, float64(errs))
durs = append(durs, float64(float64(duration)/1000000))
serviceDurs = append(serviceDurs, float64(float64(serviceDuration)/1000000))
}
res := []simplejson.TableColumn{
{
Text: "Time",
Data: times,
},
{
Text: "trace_id",
Data: ids,
},
{
Text: "operation",
Data: operations,
},
{
Text: "link",
Data: links,
},
{
Text: "html",
Data: html,
},
{
Text: "duration",
Data: durs,
},
{
Text: "serviceDuration",
Data: serviceDurs,
},
{
Text: "spans",
Data: spanCounts,
},
{
Text: "errors",
Data: errCounts,
},
}
return res, nil
}
func (jh *jaegerSJHandler) GrafanaAnnotations(ctx context.Context, query string, args simplejson.AnnotationsArguments) ([]simplejson.Annotation, error) {
tt, err := jh.runQuery(ctx, args.From, args.To, query, 0)
if err != nil {
return nil, err
}
answers := []simplejson.Annotation{}
for i := range tt {
start := int64(1<<63 - 1)
var tags []string
ss := tt[i].Spans
for j := range ss {
if proc, ok := tt[i].Processes[ss[j].ProcessID]; ok &&
proc.ServiceName == query &&
ss[j].StartTime < start {
// This should be the starting span for the queried service
start = ss[j].StartTime
tags = nil
for k := range proc.Tags {
tags = append(tags, fmt.Sprintf("%v=%v", proc.Tags[k].Key, proc.Tags[k].Value))
}
for k := range ss[j].Tags {
tags = append(tags, fmt.Sprintf("%v=%v", ss[j].Tags[k].Key, ss[j].Tags[k].Value))
}
}
}
answers = append(answers, simplejson.Annotation{
Title: tt[i].TraceID,
Text: fmt.Sprintf(`<a href="%v/trace/%v" target="_blank">%v</a>`, jh.linkURL, tt[i].TraceID, tt[i].TraceID),
Time: time.Unix(0, start*1000),
Tags: tags,
})
}
return answers, nil
}
func (jh *jaegerSJHandler) GrafanaSearch(ctx context.Context, target string) ([]string, error) {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/api/services", jh.base.String()), nil)
if err != nil {
log.Printf("url err %v", err)
return nil, err
}
resp, err := http.DefaultClient.Do(req.WithContext(ctx))
if err != nil {
log.Printf("url err %v", err)
return nil, err
}
services := struct {
Data []string `json:"data"`
Errors []json.RawMessage `json:"errors"`
}{}
dec := json.NewDecoder(resp.Body)
err = dec.Decode(&services)
if err != nil {
log.Printf("json err %v", err)
return nil, err
}
answers := []string{}
for _, s := range services.Data {
if target == "*" || strings.HasPrefix(s, target) {
answers = append(answers, s)
}
}
return answers, nil
}
func main() {
flag.Parse()
if baseURL == "" {
log.Fatalf("baseURL must be set")
}
base, err := url.Parse(baseURL)
if err != nil {
log.Fatalf("%v", err)
}
jaegersj := &jaegerSJHandler{
base: base,
linkURL: base,
}
if linkURL != "" {
link, err := url.Parse(linkURL)
if err != nil {
log.Fatalf("%v", err)
}
jaegersj.linkURL = link
}
h := simplejson.New(
simplejson.WithQuerier(jaegersj),
simplejson.WithTableQuerier(jaegersj),
simplejson.WithSearcher(jaegersj),
simplejson.WithAnnotator(jaegersj),
)
err = http.ListenAndServe(addr, h)
if err != nil {
log.Fatalf("error, %v", err)
}
}