-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
query_parser.go
410 lines (375 loc) · 14.1 KB
/
query_parser.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
// Copyright (c) 2019 The Jaeger Authors.
// 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"
"errors"
"fmt"
"net/http"
"strconv"
"strings"
"time"
"github.com/jaegertracing/jaeger/model"
"github.com/jaegertracing/jaeger/proto-gen/api_v2/metrics"
"github.com/jaegertracing/jaeger/storage/metricsstore"
"github.com/jaegertracing/jaeger/storage/spanstore"
)
const (
defaultQueryLimit = 100
operationParam = "operation"
tagParam = "tag"
tagsParam = "tags"
startTimeParam = "start"
limitParam = "limit"
minDurationParam = "minDuration"
maxDurationParam = "maxDuration"
serviceParam = "service"
spanKindParam = "spanKind"
endTimeParam = "end"
prettyPrintParam = "prettyPrint"
)
var (
errMaxDurationGreaterThanMin = fmt.Errorf("'%s' should be greater than '%s'", maxDurationParam, minDurationParam)
// errServiceParameterRequired occurs when no service name is defined.
errServiceParameterRequired = fmt.Errorf("parameter '%s' is required", serviceParam)
jaegerToOtelSpanKind = map[string]string{
"unspecified": metrics.SpanKind_SPAN_KIND_UNSPECIFIED.String(),
"internal": metrics.SpanKind_SPAN_KIND_INTERNAL.String(),
"server": metrics.SpanKind_SPAN_KIND_SERVER.String(),
"client": metrics.SpanKind_SPAN_KIND_CLIENT.String(),
"producer": metrics.SpanKind_SPAN_KIND_PRODUCER.String(),
"consumer": metrics.SpanKind_SPAN_KIND_CONSUMER.String(),
}
)
type (
// queryParser handles the parsing of query parameters for traces.
queryParser struct {
traceQueryLookbackDuration time.Duration
timeNow func() time.Time
}
traceQueryParameters struct {
spanstore.TraceQueryParameters
traceIDs []model.TraceID
}
dependenciesQueryParameters struct {
endTs time.Time
lookback time.Duration
}
durationParser = func(s string) (time.Duration, error)
)
func newDurationStringParser() durationParser {
return func(s string) (time.Duration, error) {
return time.ParseDuration(s)
}
}
func newDurationUnitsParser(units time.Duration) durationParser {
return func(s string) (time.Duration, error) {
i, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return 0, err
}
return time.Duration(i) * (units), nil
}
}
// parseTraceQueryParams takes a request and constructs a model of parameters.
//
// Why start/end parameters are expressed in microseconds:
// Span searches operate on span latencies, which are expressed as microseconds in the data model, hence why
// support for high accuracy in search query parameters is required.
// Microsecond precision is a legacy artifact from zipkin origins where timestamps and durations
// are in microseconds (see: https://zipkin.io/pages/instrumenting.html).
//
// Why duration parameters are expressed as duration strings like "1ms":
// The search UI itself does not insist on exact units because it supports string like 1ms.
// Go makes parsing duration strings like "1ms" very easy, hence why parsing of such strings is
// deferred to the backend rather than Jaeger UI.
//
// Trace query syntax:
//
// query ::= param | param '&' query
// param ::= service | operation | limit | start | end | minDuration | maxDuration | tag | tags
// service ::= 'service=' strValue
// operation ::= 'operation=' strValue
// limit ::= 'limit=' intValue
// start ::= 'start=' intValue in unix microseconds
// end ::= 'end=' intValue in unix microseconds
// minDuration ::= 'minDuration=' strValue (units are "ns", "us" (or "µs"), "ms", "s", "m", "h")
// maxDuration ::= 'maxDuration=' strValue (units are "ns", "us" (or "µs"), "ms", "s", "m", "h")
// tag ::= 'tag=' key | 'tag=' keyvalue
// key := strValue
// keyValue := strValue ':' strValue
// tags :== 'tags=' jsonMap
func (p *queryParser) parseTraceQueryParams(r *http.Request) (*traceQueryParameters, error) {
service := r.FormValue(serviceParam)
operation := r.FormValue(operationParam)
startTime, err := p.parseTime(r, startTimeParam, time.Microsecond)
if err != nil {
return nil, err
}
endTime, err := p.parseTime(r, endTimeParam, time.Microsecond)
if err != nil {
return nil, err
}
tags, err := p.parseTags(r.Form[tagParam], r.Form[tagsParam])
if err != nil {
return nil, err
}
limitParam := r.FormValue(limitParam)
limit := defaultQueryLimit
if limitParam != "" {
limitParsed, err := strconv.ParseInt(limitParam, 10, 32)
if err != nil {
return nil, err
}
limit = int(limitParsed)
}
parser := newDurationStringParser()
minDuration, err := parseDuration(r, minDurationParam, parser, 0)
if err != nil {
return nil, err
}
maxDuration, err := parseDuration(r, maxDurationParam, parser, 0)
if err != nil {
return nil, err
}
var traceIDs []model.TraceID
for _, id := range r.Form[traceIDParam] {
if traceID, err := model.TraceIDFromString(id); err == nil {
traceIDs = append(traceIDs, traceID)
} else {
return nil, fmt.Errorf("cannot parse traceID param: %w", err)
}
}
traceQuery := &traceQueryParameters{
TraceQueryParameters: spanstore.TraceQueryParameters{
ServiceName: service,
OperationName: operation,
StartTimeMin: startTime,
StartTimeMax: endTime,
Tags: tags,
NumTraces: limit,
DurationMin: minDuration,
DurationMax: maxDuration,
},
traceIDs: traceIDs,
}
if err := p.validateQuery(traceQuery); err != nil {
return nil, err
}
return traceQuery, nil
}
// parseDependenciesQueryParams takes a request and constructs a model of dependencies query parameters.
//
// The dependencies API does not operate on the latency space, instead its timestamps are just time range selections,
// and the typical backend granularity of those is on the order of 15min or more. As such, microseconds aren't
// useful in this domain and milliseconds are sufficient for both times and durations.
func (p *queryParser) parseDependenciesQueryParams(r *http.Request) (dqp dependenciesQueryParameters, err error) {
dqp.endTs, err = p.parseTime(r, endTsParam, time.Millisecond)
if err != nil {
return dqp, err
}
dqp.lookback, err = parseDuration(r, lookbackParam, newDurationUnitsParser(time.Millisecond), defaultDependencyLookbackDuration)
return dqp, err
}
// parseMetricsQueryParams takes a request and constructs a model of metrics query parameters.
//
// Why the API is designed using an end time (endTs) and lookback:
// The typical usage of the metrics APIs is to view the most recent metrics from now looking
// back a certain period of time, given the value of metrics generally degrades with time. As such, the API
// is also designed to mirror the user interface inputs.
//
// Why times are expressed as unix milliseconds:
// - The minimum step size for Prometheus-compliant metrics backends is 1ms,
// hence millisecond precision on times is sufficient.
// - The metrics API is designed with one primary client in mind, the Jaeger UI. As it is a React.js application,
// the maximum supported built-in time precision is milliseconds, making it a convenient precision to use for such a client.
//
// Why durations are expressed as unix milliseconds:
// - Given the endTs time is expressed as milliseconds, it follows that lookback durations should use the
// same time units to compute the start time.
// - As above, the minimum step size for Prometheus-compliant metrics backends is 1ms.
// - Other durations are in milliseconds to maintain consistency of units with other parameters in the metrics APIs.
// - As the primary client for the metrics API is the Jaeger UI, it is programmatically simpler to supply the
// integer representations of durations in milliseconds rather than the human-readable representation such as "1ms".
//
// Metrics query syntax:
//
// query ::= services , [ '&' optionalParams ]
// optionalParams := param | param '&' optionalParams
// param ::= groupByOperation | endTs | lookback | step | ratePer | spanKinds
// services ::= service | service '&' services
// service ::= 'service=' strValue
// groupByOperation ::= 'groupByOperation=' boolValue
// endTs ::= 'endTs=' intValue in unix milliseconds
// lookback ::= 'lookback=' intValue duration in milliseconds
// step ::= 'step=' intValue duration in milliseconds
// ratePer ::= 'ratePer=' intValue duration in milliseconds
// spanKinds ::= spanKind | spanKind '&' spanKinds
// spanKind ::= 'spanKind=' spanKindType
// spanKindType ::= "unspecified" | "internal" | "server" | "client" | "producer" | "consumer"
func (p *queryParser) parseMetricsQueryParams(r *http.Request) (bqp metricsstore.BaseQueryParameters, err error) {
query := r.URL.Query()
services, ok := query[serviceParam]
if !ok {
return bqp, newParseError(errors.New("please provide at least one service name"), serviceParam)
}
bqp.ServiceNames = services
bqp.GroupByOperation, err = parseBool(r, groupByOperationParam)
if err != nil {
return bqp, err
}
bqp.SpanKinds, err = parseSpanKinds(r, spanKindParam, defaultMetricsSpanKinds)
if err != nil {
return bqp, err
}
endTs, err := p.parseTime(r, endTsParam, time.Millisecond)
if err != nil {
return bqp, err
}
parser := newDurationUnitsParser(time.Millisecond)
lookback, err := parseDuration(r, lookbackParam, parser, defaultMetricsQueryLookbackDuration)
if err != nil {
return bqp, err
}
step, err := parseDuration(r, stepParam, parser, defaultMetricsQueryStepDuration)
if err != nil {
return bqp, err
}
ratePer, err := parseDuration(r, rateParam, parser, defaultMetricsQueryRateDuration)
if err != nil {
return bqp, err
}
bqp.EndTime = &endTs
bqp.Lookback = &lookback
bqp.Step = &step
bqp.RatePer = &ratePer
return bqp, err
}
// parseTime parses the time parameter of an HTTP request that is represented the number of "units" since epoch.
// If the time parameter is empty, the current time will be returned.
func (p *queryParser) parseTime(r *http.Request, paramName string, units time.Duration) (time.Time, error) {
formValue := r.FormValue(paramName)
if formValue == "" {
if paramName == startTimeParam {
return p.timeNow().Add(-1 * p.traceQueryLookbackDuration), nil
}
return p.timeNow(), nil
}
t, err := strconv.ParseInt(formValue, 10, 64)
if err != nil {
return time.Time{}, newParseError(err, paramName)
}
return time.Unix(0, 0).Add(time.Duration(t) * units), nil
}
// parseDuration parses the duration parameter of an HTTP request using the provided durationParser.
// If the duration parameter is empty, the given defaultDuration will be returned.
func parseDuration(r *http.Request, paramName string, parse durationParser, defaultDuration time.Duration) (time.Duration, error) {
formValue := r.FormValue(paramName)
if formValue == "" {
return defaultDuration, nil
}
d, err := parse(formValue)
if err != nil {
return 0, newParseError(err, paramName)
}
return d, nil
}
func parseBool(r *http.Request, paramName string) (b bool, err error) {
formVal := r.FormValue(paramName)
if formVal == "" {
return false, nil
}
b, err = strconv.ParseBool(formVal)
if err != nil {
return b, newParseError(err, paramName)
}
return b, nil
}
// parseSpanKindParam parses the input span kinds to filter for in the metrics query.
//
// Valid input span kinds include:
// - "unspecified": when no span kind specified in span.
// - "internal": internal operation within an application, instead of application boundaries.
// - "server": server-side handling span.
// - "client": outbound service call span.
// - "producer": producer sending a message to broker.
// - "consumer": consumer consuming a message from a broker.
//
// The output span kinds are the string representations from the OpenTelemetry model/proto/metrics/otelspankind.proto.
// That is, the following map to the above valid inputs:
// - "SPAN_KIND_UNSPECIFIED"
// - "SPAN_KIND_INTERNAL"
// - "SPAN_KIND_SERVER"
// - "SPAN_KIND_CLIENT"
// - "SPAN_KIND_PRODUCER"
// - "SPAN_KIND_CONSUMER"
func parseSpanKinds(r *http.Request, paramName string, defaultSpanKinds []string) ([]string, error) {
query := r.URL.Query()
jaegerSpanKinds, ok := query[paramName]
if !ok {
return defaultSpanKinds, nil
}
otelSpanKinds, err := mapSpanKindsToOpenTelemetry(jaegerSpanKinds)
if err != nil {
return defaultSpanKinds, newParseError(err, paramName)
}
return otelSpanKinds, nil
}
func mapSpanKindsToOpenTelemetry(spanKinds []string) ([]string, error) {
otelSpanKinds := make([]string, len(spanKinds))
for i, spanKind := range spanKinds {
if v, ok := jaegerToOtelSpanKind[spanKind]; ok {
otelSpanKinds[i] = v
} else {
return otelSpanKinds, fmt.Errorf("unsupported span kind: '%s'", spanKind)
}
}
return otelSpanKinds, nil
}
func (p *queryParser) validateQuery(traceQuery *traceQueryParameters) error {
if len(traceQuery.traceIDs) == 0 && traceQuery.ServiceName == "" {
return errServiceParameterRequired
}
if traceQuery.DurationMin != 0 && traceQuery.DurationMax != 0 {
if traceQuery.DurationMax < traceQuery.DurationMin {
return errMaxDurationGreaterThanMin
}
}
return nil
}
func (p *queryParser) parseTags(simpleTags []string, jsonTags []string) (map[string]string, error) {
retMe := make(map[string]string)
for _, tag := range simpleTags {
keyAndValue := strings.Split(tag, ":")
if l := len(keyAndValue); l > 1 {
retMe[keyAndValue[0]] = strings.Join(keyAndValue[1:], ":")
} else {
return nil, fmt.Errorf("malformed 'tag' parameter, expecting key:value, received: %s", tag)
}
}
for _, tags := range jsonTags {
var fromJSON map[string]string
if err := json.Unmarshal([]byte(tags), &fromJSON); err != nil {
return nil, fmt.Errorf("malformed 'tags' parameter, cannot unmarshal JSON: %w", err)
}
for k, v := range fromJSON {
retMe[k] = v
}
}
return retMe, nil
}
func newParseError(err error, paramName string) error {
return fmt.Errorf("unable to parse param '%s': %w", paramName, err)
}