forked from jaegertracing/jaeger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
451 lines (408 loc) · 13 KB
/
handler.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
// Copyright (c) 2017 Uber Technologies, Inc.
//
// 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 app
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"time"
"github.com/gorilla/mux"
"github.com/opentracing-contrib/go-stdlib/nethttp"
"github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
"go.uber.org/zap"
"github.com/uber/jaeger/model"
"github.com/uber/jaeger/model/adjuster"
uiconv "github.com/uber/jaeger/model/converter/json"
ui "github.com/uber/jaeger/model/json"
"github.com/uber/jaeger/pkg/multierror"
"github.com/uber/jaeger/storage/dependencystore"
"github.com/uber/jaeger/storage/spanstore"
)
const (
traceIDParam = "traceID"
endTsParam = "endTs"
lookbackParam = "lookback"
defaultDependencyLookbackDuration = time.Hour * 24
defaultTraceQueryLookbackDuration = time.Hour * 24 * 2
defaultHTTPPrefix = "api"
)
var (
errNoArchiveSpanStorage = errors.New("archive span storage was not configured")
)
// HTTPHandler handles http requests
type HTTPHandler interface {
RegisterRoutes(router *mux.Router)
}
type structuredResponse struct {
Data interface{} `json:"data"`
Total int `json:"total"`
Limit int `json:"limit"`
Offset int `json:"offset"`
Errors []structuredError `json:"errors"`
}
type structuredError struct {
Code int `json:"code,omitempty"`
Msg string `json:"msg"`
TraceID ui.TraceID `json:"traceID,omitempty"`
}
// APIHandler implements the query service public API by registering routes at httpPrefix
type APIHandler struct {
spanReader spanstore.Reader
archiveSpanReader spanstore.Reader
archiveSpanWriter spanstore.Writer
dependencyReader dependencystore.Reader
adjuster adjuster.Adjuster
logger *zap.Logger
queryParser queryParser
httpPrefix string
tracer opentracing.Tracer
}
// NewAPIHandler returns an APIHandler
func NewAPIHandler(spanReader spanstore.Reader, dependencyReader dependencystore.Reader, options ...HandlerOption) *APIHandler {
aH := &APIHandler{
spanReader: spanReader,
dependencyReader: dependencyReader,
queryParser: queryParser{
traceQueryLookbackDuration: defaultTraceQueryLookbackDuration,
timeNow: time.Now,
},
}
for _, option := range options {
option(aH)
}
if aH.httpPrefix == "" {
aH.httpPrefix = defaultHTTPPrefix
}
if aH.adjuster == nil {
aH.adjuster = adjuster.Sequence(StandardAdjusters...)
}
if aH.logger == nil {
aH.logger = zap.NewNop()
}
if aH.tracer == nil {
aH.tracer = opentracing.NoopTracer{}
}
return aH
}
// RegisterRoutes registers routes for this handler on the given router
func (aH *APIHandler) RegisterRoutes(router *mux.Router) {
aH.handleFunc(router, aH.getTrace, "/traces/{%s}", traceIDParam).Methods(http.MethodGet)
aH.handleFunc(router, aH.archiveTrace, "/archive/{%s}", traceIDParam).Methods(http.MethodPost)
aH.handleFunc(router, aH.search, "/traces").Methods(http.MethodGet)
aH.handleFunc(router, aH.getServices, "/services").Methods(http.MethodGet)
// TODO change the UI to use this endpoint. Requires ?service= parameter.
aH.handleFunc(router, aH.getOperations, "/operations").Methods(http.MethodGet)
// TODO - remove this when UI catches up
aH.handleFunc(router, aH.getOperationsLegacy, "/services/{%s}/operations", serviceParam).Methods(http.MethodGet)
aH.handleFunc(router, aH.dependencies, "/dependencies").Methods(http.MethodGet)
}
func (aH *APIHandler) handleFunc(
router *mux.Router,
f func(http.ResponseWriter, *http.Request),
route string,
args ...interface{},
) *mux.Route {
route = aH.route(route, args...)
traceMiddleware := nethttp.Middleware(
aH.tracer,
http.HandlerFunc(f),
nethttp.OperationNameFunc(func(r *http.Request) string {
return route
}))
return router.HandleFunc(route, traceMiddleware.ServeHTTP)
}
func (aH *APIHandler) route(route string, args ...interface{}) string {
args = append([]interface{}{aH.httpPrefix}, args...)
return fmt.Sprintf("/%s"+route, args...)
}
func (aH *APIHandler) getServices(w http.ResponseWriter, r *http.Request) {
services, err := aH.spanReader.GetServices()
if aH.handleError(w, err, http.StatusInternalServerError) {
return
}
structuredRes := structuredResponse{
Data: services,
Total: len(services),
}
aH.writeJSON(w, &structuredRes)
}
func (aH *APIHandler) getOperationsLegacy(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
service := vars[serviceParam] //given how getOperationsLegacy is used, service will always be a non-empty string
operations, err := aH.spanReader.GetOperations(service)
if aH.handleError(w, err, http.StatusInternalServerError) {
return
}
structuredRes := structuredResponse{
Data: operations,
Total: len(operations),
}
aH.writeJSON(w, &structuredRes)
}
func (aH *APIHandler) getOperations(w http.ResponseWriter, r *http.Request) {
service := r.FormValue(serviceParam)
if service == "" {
if aH.handleError(w, ErrServiceParameterRequired, http.StatusBadRequest) {
return
}
}
operations, err := aH.spanReader.GetOperations(service)
if aH.handleError(w, err, http.StatusInternalServerError) {
return
}
structuredRes := structuredResponse{
Data: operations,
Total: len(operations),
}
aH.writeJSON(w, &structuredRes)
}
func (aH *APIHandler) search(w http.ResponseWriter, r *http.Request) {
tQuery, err := aH.queryParser.parse(r)
if aH.handleError(w, err, http.StatusBadRequest) {
return
}
var uiErrors []structuredError
var tracesFromStorage []*model.Trace
if len(tQuery.traceIDs) > 0 {
tracesFromStorage, uiErrors, err = aH.tracesByIDs(tQuery.traceIDs)
if aH.handleError(w, err, http.StatusInternalServerError) {
return
}
} else {
tracesFromStorage, err = aH.spanReader.FindTraces(&tQuery.TraceQueryParameters)
if aH.handleError(w, err, http.StatusInternalServerError) {
return
}
}
uiTraces := make([]*ui.Trace, len(tracesFromStorage))
for i, v := range tracesFromStorage {
uiTrace, uiErr := aH.convertModelToUI(v)
if uiErr != nil {
uiErrors = append(uiErrors, *uiErr)
}
uiTraces[i] = uiTrace
}
structuredRes := structuredResponse{
Data: uiTraces,
Errors: uiErrors,
}
aH.writeJSON(w, &structuredRes)
}
func (aH *APIHandler) tracesByIDs(traceIDs []model.TraceID) ([]*model.Trace, []structuredError, error) {
var errors []structuredError
retMe := make([]*model.Trace, 0, len(traceIDs))
for _, traceID := range traceIDs {
if trace, err := aH.spanReader.GetTrace(traceID); err != nil {
if err != spanstore.ErrTraceNotFound {
return nil, nil, err
}
errors = append(errors, structuredError{
Msg: err.Error(),
TraceID: ui.TraceID(traceID.String()),
})
} else {
retMe = append(retMe, trace)
}
}
return retMe, errors, nil
}
func (aH *APIHandler) dependencies(w http.ResponseWriter, r *http.Request) {
endTsMillis, err := strconv.ParseInt(r.FormValue(endTsParam), 10, 64)
if aH.handleError(w, errors.Wrapf(err, "Unable to parse %s", endTimeParam), http.StatusBadRequest) {
return
}
var lookback time.Duration
if formValue := r.FormValue(lookbackParam); len(formValue) > 0 {
lookback, err = time.ParseDuration(formValue + "ms")
if aH.handleError(w, errors.Wrapf(err, "Unable to parse %s", lookbackParam), http.StatusBadRequest) {
return
}
}
service := r.FormValue(serviceParam)
if lookback == 0 {
lookback = defaultDependencyLookbackDuration
}
endTs := time.Unix(0, 0).Add(time.Duration(endTsMillis) * time.Millisecond)
dependencies, err := aH.dependencyReader.GetDependencies(endTs, lookback)
if aH.handleError(w, err, http.StatusInternalServerError) {
return
}
filteredDependencies := aH.filterDependenciesByService(dependencies, service)
structuredRes := structuredResponse{
Data: aH.deduplicateDependencies(filteredDependencies),
}
aH.writeJSON(w, &structuredRes)
}
func (aH *APIHandler) convertModelToUI(traceFromStorage *model.Trace) (*ui.Trace, *structuredError) {
var errors []error
trace, err := aH.adjuster.Adjust(traceFromStorage)
if err != nil {
errors = append(errors, err)
}
uiTrace := uiconv.FromDomain(trace)
var uiError *structuredError
if err := multierror.Wrap(errors); err != nil {
uiError = &structuredError{
Msg: err.Error(),
TraceID: uiTrace.TraceID,
}
}
return uiTrace, uiError
}
func (aH *APIHandler) deduplicateDependencies(dependencies []model.DependencyLink) []ui.DependencyLink {
type Key struct {
parent string
child string
}
links := make(map[Key]uint64)
for _, l := range dependencies {
links[Key{l.Parent, l.Child}] += l.CallCount
}
result := make([]ui.DependencyLink, 0, len(links))
for k, v := range links {
result = append(result, ui.DependencyLink{Parent: k.parent, Child: k.child, CallCount: v})
}
return result
}
func (aH *APIHandler) filterDependenciesByService(
dependencies []model.DependencyLink,
service string,
) []model.DependencyLink {
if len(service) == 0 {
return dependencies
}
var filteredDependencies []model.DependencyLink
for _, dependency := range dependencies {
if dependency.Parent == service || dependency.Child == service {
filteredDependencies = append(filteredDependencies, dependency)
}
}
return filteredDependencies
}
// Parses trace ID from URL like /traces/{trace-id}
func (aH *APIHandler) parseTraceID(w http.ResponseWriter, r *http.Request) (model.TraceID, bool) {
vars := mux.Vars(r)
traceIDVar := vars[traceIDParam]
traceID, err := model.TraceIDFromString(traceIDVar)
if aH.handleError(w, err, http.StatusBadRequest) {
return traceID, false
}
return traceID, true
}
// getTrace implements the REST API /traces/{trace-id}
func (aH *APIHandler) getTrace(w http.ResponseWriter, r *http.Request) {
aH.getTraceFromReaders(w, r, aH.spanReader, aH.archiveSpanReader)
}
// getTraceFromReader parses trace ID from the path, loads the trace from specified Reader,
// formats it in the UI JSON format, and responds to the client.
func (aH *APIHandler) getTraceFromReaders(
w http.ResponseWriter,
r *http.Request,
reader spanstore.Reader,
backupReader spanstore.Reader,
) {
aH.withTraceFromReader(w, r, reader, backupReader, func(trace *model.Trace) {
var uiErrors []structuredError
uiTrace, uiErr := aH.convertModelToUI(trace)
if uiErr != nil {
uiErrors = append(uiErrors, *uiErr)
}
structuredRes := structuredResponse{
Data: []*ui.Trace{
uiTrace,
},
Errors: uiErrors,
}
aH.writeJSON(w, &structuredRes)
})
}
// withTraceFromReader tries to load a trace from Reader and if successful
// execute process() function passing it that trace.
func (aH *APIHandler) withTraceFromReader(
w http.ResponseWriter,
r *http.Request,
reader spanstore.Reader,
backupReader spanstore.Reader,
process func(trace *model.Trace),
) {
traceID, ok := aH.parseTraceID(w, r)
if !ok {
return
}
trace, err := reader.GetTrace(traceID)
if err == spanstore.ErrTraceNotFound {
if backupReader == nil {
aH.handleError(w, err, http.StatusNotFound)
return
}
trace, err = backupReader.GetTrace(traceID)
if err == spanstore.ErrTraceNotFound {
aH.handleError(w, err, http.StatusNotFound)
return
}
}
if aH.handleError(w, err, http.StatusInternalServerError) {
return
}
process(trace)
}
// archiveTrace implements the REST API POST:/archive/{trace-id}.
// It reads the trace from the main Reader and saves it to archive Writer.
func (aH *APIHandler) archiveTrace(w http.ResponseWriter, r *http.Request) {
if aH.archiveSpanWriter == nil {
aH.handleError(w, errNoArchiveSpanStorage, http.StatusInternalServerError)
return
}
aH.withTraceFromReader(w, r, aH.spanReader, nil, func(trace *model.Trace) {
var writeErrors []error
for _, span := range trace.Spans {
err := aH.archiveSpanWriter.WriteSpan(span)
if err != nil {
writeErrors = append(writeErrors, err)
}
}
err := multierror.Wrap(writeErrors)
if aH.handleError(w, err, http.StatusInternalServerError) {
return
}
structuredRes := structuredResponse{
Data: []string{}, // doens't matter, just want an empty array
Errors: []structuredError{},
}
aH.writeJSON(w, &structuredRes)
})
}
func (aH *APIHandler) handleError(w http.ResponseWriter, err error, statusCode int) bool {
if err == nil {
return false
}
structuredResp := structuredResponse{
Errors: []structuredError{
{
Code: statusCode,
Msg: err.Error(),
},
},
}
resp, _ := json.Marshal(&structuredResp)
http.Error(w, string(resp), statusCode)
return true
}
func (aH *APIHandler) writeJSON(w http.ResponseWriter, response *structuredResponse) {
resp, _ := json.Marshal(response)
w.Header().Set("Content-Type", "application/json")
w.Write(resp)
}