-
Notifications
You must be signed in to change notification settings - Fork 535
/
Copy pathhttp.go
778 lines (655 loc) · 23.2 KB
/
http.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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
package api
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"math"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"github.com/go-logfmt/logfmt"
"github.com/google/uuid"
"github.com/gorilla/mux"
"github.com/prometheus/common/model"
"github.com/grafana/dskit/httpgrpc"
"github.com/grafana/tempo/pkg/tempopb"
"github.com/grafana/tempo/pkg/traceql"
"github.com/grafana/tempo/pkg/util"
"github.com/grafana/tempo/tempodb"
)
const (
URLParamTraceID = "traceID"
// search
urlParamQuery = "q"
urlParamTags = "tags"
urlParamMinDuration = "minDuration"
urlParamMaxDuration = "maxDuration"
urlParamLimit = "limit"
urlParamStart = "start"
urlParamEnd = "end"
urlParamSpansPerSpanSet = "spss"
urlParamStep = "step"
urlParamSince = "since"
urlParamExemplars = "exemplars"
// backend search querier
urlParamStartPage = "startPage"
urlParamPagesToSearch = "pagesToSearch"
urlParamBlockID = "blockID"
urlParamEncoding = "encoding"
urlParamIndexPageSize = "indexPageSize"
urlParamTotalRecords = "totalRecords"
urlParamDataEncoding = "dataEncoding"
urlParamVersion = "version"
urlParamSize = "size"
urlParamFooterSize = "footerSize"
urlParamDedicatedColumns = "dc"
// search tags
urlParamScope = "scope"
// generator summary
urlParamGroupBy = "groupBy"
// urlParamMetric = "metric"
HeaderAccept = "Accept"
HeaderContentType = "Content-Type"
HeaderAcceptProtobuf = "application/protobuf"
HeaderAcceptJSON = "application/json"
PathPrefixQuerier = "/querier"
PathPrefixGenerator = "/generator"
PathTraces = "/api/traces/{traceID}"
PathSearch = "/api/search"
PathSearchTags = "/api/search/tags"
PathSearchTagValues = "/api/search/tag/{" + MuxVarTagName + "}/values"
PathEcho = "/api/echo"
PathBuildInfo = "/api/status/buildinfo"
PathUsageStats = "/status/usage-stats"
PathSpanMetrics = "/api/metrics"
PathSpanMetricsSummary = "/api/metrics/summary"
PathMetricsQueryInstant = "/api/metrics/query"
PathMetricsQueryRange = "/api/metrics/query_range"
// PathOverrides user configurable overrides
PathOverrides = "/api/overrides"
PathSearchTagValuesV2 = "/api/v2/search/tag/{" + MuxVarTagName + "}/values"
PathSearchTagsV2 = "/api/v2/search/tags"
PathTracesV2 = "/api/v2/traces/{traceID}"
QueryModeKey = "mode"
QueryModeIngesters = "ingesters"
QueryModeBlocks = "blocks"
QueryModeAll = "all"
BlockStartKey = "blockStart"
BlockEndKey = "blockEnd"
defaultLimit = 20
defaultSpansPerSpanSet = 3
defaultSince = 1 * time.Hour
)
func ParseTraceID(r *http.Request) ([]byte, error) {
vars := mux.Vars(r)
traceID, ok := vars[URLParamTraceID]
if !ok {
return nil, fmt.Errorf("please provide a traceID")
}
byteID, err := util.HexStringToTraceID(traceID)
if err != nil {
return nil, err
}
return byteID, nil
}
// ParseSearchRequest takes an http.Request and decodes query params to create a tempopb.SearchRequest
func ParseSearchRequest(r *http.Request) (*tempopb.SearchRequest, error) {
req := &tempopb.SearchRequest{
Tags: map[string]string{},
SpansPerSpanSet: defaultSpansPerSpanSet,
}
vals := r.URL.Query()
if s, ok := extractQueryParam(vals, urlParamStart); ok {
start, err := strconv.ParseInt(s, 10, 32)
if err != nil {
return nil, fmt.Errorf("invalid start: %w", err)
}
req.Start = uint32(start)
}
if s, ok := extractQueryParam(vals, urlParamEnd); ok {
end, err := strconv.ParseInt(s, 10, 32)
if err != nil {
return nil, fmt.Errorf("invalid end: %w", err)
}
req.End = uint32(end)
}
query, queryFound := extractQueryParam(vals, urlParamQuery)
if queryFound {
req.Query = query
}
encodedTags, tagsFound := extractQueryParam(vals, urlParamTags)
if tagsFound {
// tags and traceQL API are mutually exclusive
if queryFound {
return nil, fmt.Errorf("invalid request: can't specify tags and q in the same query")
}
decoder := logfmt.NewDecoder(strings.NewReader(encodedTags))
for decoder.ScanRecord() {
for decoder.ScanKeyval() {
key := string(decoder.Key())
if _, ok := req.Tags[key]; ok {
return nil, fmt.Errorf("invalid tags: tag %s has been set twice", key)
}
req.Tags[key] = string(decoder.Value())
}
}
if err := decoder.Err(); err != nil {
var syntaxErr *logfmt.SyntaxError
if ok := errors.As(err, &syntaxErr); ok {
return nil, fmt.Errorf("invalid tags: %s at pos %d", syntaxErr.Msg, syntaxErr.Pos)
}
return nil, fmt.Errorf("invalid tags: %w", err)
}
}
// if we don't have a query or tags, and we don't see start or end treat this like an old style search
// if we have no tags but we DO have start/end we have to treat this like a range search with no
// tags specified.
if !queryFound && !tagsFound && req.Start == 0 && req.End == 0 {
// Passing tags as individual query parameters is not supported anymore, clients should use the tags
// query parameter instead. We still parse these tags since the initial Grafana implementation uses this.
// As Grafana gets updated and/or versions using this get old we can remove this section.
for k, v := range vals {
// Skip reserved keywords
if k == urlParamQuery || k == urlParamTags || k == urlParamMinDuration || k == urlParamMaxDuration || k == urlParamLimit || k == urlParamSpansPerSpanSet || k == urlParamStart || k == urlParamEnd {
continue
}
if len(v) > 0 && v[0] != "" {
req.Tags[k] = v[0]
}
}
}
if s, ok := extractQueryParam(vals, urlParamMinDuration); ok {
dur, err := time.ParseDuration(s)
if err != nil {
return nil, fmt.Errorf("invalid minDuration: %w", err)
}
req.MinDurationMs = uint32(dur.Milliseconds())
}
if s, ok := extractQueryParam(vals, urlParamMaxDuration); ok {
dur, err := time.ParseDuration(s)
if err != nil {
return nil, fmt.Errorf("invalid maxDuration: %w", err)
}
req.MaxDurationMs = uint32(dur.Milliseconds())
if req.MinDurationMs != 0 && req.MinDurationMs > req.MaxDurationMs {
return nil, errors.New("invalid maxDuration: must be greater than minDuration")
}
}
if s, ok := extractQueryParam(vals, urlParamLimit); ok {
limit, err := strconv.Atoi(s)
if err != nil {
return nil, fmt.Errorf("invalid limit: %w", err)
}
if limit <= 0 {
return nil, errors.New("invalid limit: must be a positive number")
}
req.Limit = uint32(limit)
}
if s, ok := extractQueryParam(vals, urlParamSpansPerSpanSet); ok {
spansPerSpanSet, err := strconv.Atoi(s)
if err != nil {
return nil, fmt.Errorf("invalid spss: %w", err)
}
if spansPerSpanSet <= 0 {
return nil, errors.New("invalid spss: must be a positive number")
}
req.SpansPerSpanSet = uint32(spansPerSpanSet)
}
// start and end == 0 is fine
if req.End == 0 && req.Start == 0 {
return req, nil
}
// if start or end are non-zero do some checks
if req.End <= req.Start {
return nil, fmt.Errorf("http parameter start must be before end. received start=%d end=%d", req.Start, req.End)
}
return req, nil
}
func ParseSpanMetricsRequest(r *http.Request) (*tempopb.SpanMetricsRequest, error) {
req := &tempopb.SpanMetricsRequest{}
vals := r.URL.Query()
groupBy := vals.Get(urlParamGroupBy)
req.GroupBy = groupBy
query := vals.Get(urlParamQuery)
req.Query = query
l := vals.Get(urlParamLimit)
if l != "" {
limit, err := strconv.Atoi(l)
if err != nil {
return nil, fmt.Errorf("invalid limit: %w", err)
}
req.Limit = uint64(limit)
}
if s, ok := extractQueryParam(vals, urlParamStart); ok {
start, err := strconv.ParseInt(s, 10, 32)
if err != nil {
return nil, fmt.Errorf("invalid start: %w", err)
}
req.Start = uint32(start)
}
if s, ok := extractQueryParam(vals, urlParamEnd); ok {
end, err := strconv.ParseInt(s, 10, 32)
if err != nil {
return nil, fmt.Errorf("invalid end: %w", err)
}
req.End = uint32(end)
}
return req, nil
}
func ParseSpanMetricsSummaryRequest(r *http.Request) (*tempopb.SpanMetricsSummaryRequest, error) {
req := &tempopb.SpanMetricsSummaryRequest{}
vals := r.URL.Query()
groupBy := vals.Get(urlParamGroupBy)
req.GroupBy = groupBy
query := vals.Get(urlParamQuery)
req.Query = query
l := vals.Get(urlParamLimit)
if l != "" {
limit, err := strconv.Atoi(l)
if err != nil {
return nil, fmt.Errorf("invalid limit: %w", err)
}
req.Limit = uint64(limit)
}
if s, ok := extractQueryParam(vals, urlParamStart); ok {
start, err := strconv.ParseInt(s, 10, 32)
if err != nil {
return nil, fmt.Errorf("invalid start: %w", err)
}
req.Start = uint32(start)
}
if s, ok := extractQueryParam(vals, urlParamEnd); ok {
end, err := strconv.ParseInt(s, 10, 32)
if err != nil {
return nil, fmt.Errorf("invalid end: %w", err)
}
req.End = uint32(end)
}
return req, nil
}
func ParseQueryInstantRequest(r *http.Request) (*tempopb.QueryInstantRequest, error) {
req := &tempopb.QueryInstantRequest{}
vals := r.URL.Query()
// check "query" first. this was originally added for prom compatibility and Grafana still uses it.
if s, ok := extractQueryParam(vals, "query"); ok {
req.Query = s
}
// also check the `q` parameter. this is what all other Tempo endpoints take for a TraceQL query.
if s, ok := extractQueryParam(vals, urlParamQuery); ok {
req.Query = s
}
start, end, _ := bounds(vals)
req.Start = uint64(start.UnixNano())
req.End = uint64(end.UnixNano())
return req, nil
}
func ParseQueryRangeRequest(r *http.Request) (*tempopb.QueryRangeRequest, error) {
req := &tempopb.QueryRangeRequest{}
vals := r.URL.Query()
// check "query" first. this was originally added for prom compatibility and Grafana still uses it.
if s, ok := extractQueryParam(vals, "query"); ok {
req.Query = s
}
// also check the `q` parameter. this is what all other Tempo endpoints take for a TraceQL query.
if s, ok := extractQueryParam(vals, urlParamQuery); ok {
req.Query = s
}
if s, ok := extractQueryParam(vals, QueryModeKey); ok {
req.QueryMode = s
}
start, end, _ := bounds(vals)
req.Start = uint64(start.UnixNano())
req.End = uint64(end.UnixNano())
step, err := step(vals, start, end)
if err != nil {
return nil, httpgrpc.Errorf(http.StatusBadRequest, err.Error())
}
req.Step = uint64(step.Nanoseconds())
// New RF1 params
blockID, _ := extractQueryParam(vals, urlParamBlockID)
if blockID, err := uuid.Parse(blockID); err == nil {
req.BlockID = blockID.String()
}
startPage, _ := extractQueryParam(vals, urlParamStartPage)
if startPage, err := strconv.Atoi(startPage); err == nil {
req.StartPage = uint32(startPage)
}
pagesToSearch, _ := extractQueryParam(vals, urlParamPagesToSearch)
if of, err := strconv.Atoi(pagesToSearch); err == nil {
req.PagesToSearch = uint32(of)
}
version, _ := extractQueryParam(vals, urlParamVersion)
req.Version = version
encoding, _ := extractQueryParam(vals, urlParamEncoding)
req.Encoding = encoding
size, _ := extractQueryParam(vals, urlParamSize)
if size, err := strconv.Atoi(size); err == nil {
req.Size_ = uint64(size)
}
footerSize, _ := extractQueryParam(vals, urlParamFooterSize)
if footerSize, err := strconv.Atoi(footerSize); err == nil {
req.FooterSize = uint32(footerSize)
}
dedicatedColumns, _ := extractQueryParam(vals, urlParamDedicatedColumns)
if len(dedicatedColumns) > 0 {
err := json.Unmarshal([]byte(dedicatedColumns), &req.DedicatedColumns)
if err != nil {
return nil, httpgrpc.Errorf(http.StatusBadRequest, fmt.Errorf("failed to parse dedicated columns: %w", err).Error())
}
}
exemplars, _ := extractQueryParam(vals, urlParamExemplars)
if exemplars, err := strconv.Atoi(exemplars); err == nil {
req.Exemplars = uint32(exemplars)
}
return req, nil
}
func BuildQueryInstantRequest(req *http.Request, searchReq *tempopb.QueryInstantRequest) *http.Request {
if req == nil {
req = &http.Request{
URL: &url.URL{},
}
}
if searchReq == nil {
return req
}
qb := newQueryBuilder("")
qb.addParam(urlParamStart, strconv.FormatUint(searchReq.Start, 10))
qb.addParam(urlParamEnd, strconv.FormatUint(searchReq.End, 10))
qb.addParam(urlParamQuery, searchReq.Query)
req.URL.RawQuery = qb.query()
return req
}
// BuildQueryRangeRequest takes a tempopb.QueryRangeRequest and populates the passed http.Request
// dedicatedColumnsJSON should be generated using the DedicatedColumnsToJSON struct which produces the expected string
// value and memoizes results to prevent redundant marshaling.
func BuildQueryRangeRequest(req *http.Request, searchReq *tempopb.QueryRangeRequest, dedicatedColumnsJSON string) *http.Request {
if req == nil {
req = &http.Request{
URL: &url.URL{},
}
}
if searchReq == nil {
return req
}
qb := newQueryBuilder("")
qb.addParam(urlParamStart, strconv.FormatUint(searchReq.Start, 10))
qb.addParam(urlParamEnd, strconv.FormatUint(searchReq.End, 10))
if searchReq.Step != 0 { // if step != 0 leave the param out and Tempo will calculate it
qb.addParam(urlParamStep, time.Duration(searchReq.Step).String())
}
qb.addParam(QueryModeKey, searchReq.QueryMode)
// New RF1 params
qb.addParam(urlParamBlockID, searchReq.BlockID)
qb.addParam(urlParamStartPage, strconv.Itoa(int(searchReq.StartPage)))
qb.addParam(urlParamPagesToSearch, strconv.Itoa(int(searchReq.PagesToSearch)))
qb.addParam(urlParamVersion, searchReq.Version)
qb.addParam(urlParamEncoding, searchReq.Encoding)
qb.addParam(urlParamSize, strconv.Itoa(int(searchReq.Size_)))
qb.addParam(urlParamFooterSize, strconv.Itoa(int(searchReq.FooterSize)))
if len(dedicatedColumnsJSON) > 0 && dedicatedColumnsJSON != "null" { // if a caller marshals a nil dedicated cols we will receive the string "null"
qb.addParam(urlParamDedicatedColumns, dedicatedColumnsJSON)
}
if len(searchReq.Query) > 0 {
qb.addParam(urlParamQuery, searchReq.Query)
}
qb.addParam(urlParamExemplars, strconv.FormatUint(uint64(searchReq.Exemplars), 10))
req.URL.RawQuery = qb.query()
return req
}
// Generic helper to append query parameters to an http request with less allocations
func BuildQueryRequest(req *http.Request, queryParams map[string]string) *http.Request {
if req == nil {
req = &http.Request{
URL: &url.URL{},
}
}
qb := newQueryBuilder(req.URL.RawQuery)
for k, v := range queryParams {
qb.addParam(k, v)
}
req.URL.RawQuery = qb.query()
return req
}
func bounds(vals url.Values) (time.Time, time.Time, error) {
var (
now = time.Now()
start, _ = extractQueryParam(vals, urlParamStart)
end, _ = extractQueryParam(vals, urlParamEnd)
since, _ = extractQueryParam(vals, urlParamSince)
)
return determineBounds(now, start, end, since)
}
func determineBounds(now time.Time, startString, endString, sinceString string) (time.Time, time.Time, error) {
since := defaultSince
if sinceString != "" {
d, err := model.ParseDuration(sinceString)
if err != nil {
return time.Time{}, time.Time{}, fmt.Errorf("could not parse 'since' parameter: %w", err)
}
since = time.Duration(d)
}
end, err := parseTimestamp(endString, now)
if err != nil {
return time.Time{}, time.Time{}, fmt.Errorf("could not parse 'end' parameter: %w", err)
}
// endOrNow is used to apply a default for the start time or an offset if 'since' is provided.
// we want to use the 'end' time so long as it's not in the future as this should provide
// a more intuitive experience when end time is in the future.
endOrNow := end
if end.After(now) {
endOrNow = now
}
start, err := parseTimestamp(startString, endOrNow.Add(-since))
if err != nil {
return time.Time{}, time.Time{}, fmt.Errorf("could not parse 'start' parameter: %w", err)
}
return start, end, nil
}
// parseTimestamp parses a ns unix timestamp from a string
// if the value is empty it returns a default value passed as second parameter
func parseTimestamp(value string, def time.Time) (time.Time, error) {
if value == "" {
return def, nil
}
if strings.Contains(value, ".") {
if t, err := strconv.ParseFloat(value, 64); err == nil {
s, ns := math.Modf(t)
ns = math.Round(ns*1000) / 1000
return time.Unix(int64(s), int64(ns*float64(time.Second))), nil
}
}
nanos, err := strconv.ParseInt(value, 10, 64)
if err != nil {
if ts, err := time.Parse(time.RFC3339Nano, value); err == nil {
return ts, nil
}
return time.Time{}, err
}
if len(value) <= 10 {
return time.Unix(nanos, 0), nil
}
return time.Unix(0, nanos), nil
}
func step(vals url.Values, start, end time.Time) (time.Duration, error) {
value, _ := extractQueryParam(vals, urlParamStep)
if value == "" {
return time.Duration(traceql.DefaultQueryRangeStep(uint64(start.UnixNano()), uint64(end.UnixNano()))), nil
}
return parseSecondsOrDuration(value)
}
func parseSecondsOrDuration(value string) (time.Duration, error) {
if d, err := strconv.ParseFloat(value, 64); err == nil {
ts := d * float64(time.Second)
if ts > float64(math.MaxInt64) || ts < float64(math.MinInt64) {
return 0, fmt.Errorf("cannot parse %q to a valid duration. It overflows int64", value)
}
return time.Duration(ts), nil
}
if d, err := time.ParseDuration(value); err == nil {
return time.Duration(d), nil
}
return 0, fmt.Errorf("cannot parse %q to a valid duration", value)
}
// BuildSearchRequest takes a tempopb.SearchRequest and populates the passed http.Request
// with the appropriate params. If no http.Request is provided a new one is created.
func BuildSearchRequest(req *http.Request, searchReq *tempopb.SearchRequest) (*http.Request, error) {
if req == nil {
req = &http.Request{
URL: &url.URL{},
}
}
if searchReq == nil {
return req, nil
}
qb := newQueryBuilder("")
qb.addParam(urlParamStart, strconv.FormatUint(uint64(searchReq.Start), 10))
qb.addParam(urlParamEnd, strconv.FormatUint(uint64(searchReq.End), 10))
if searchReq.Limit != 0 {
qb.addParam(urlParamLimit, strconv.FormatUint(uint64(searchReq.Limit), 10))
}
if searchReq.MaxDurationMs != 0 {
qb.addParam(urlParamMaxDuration, strconv.FormatUint(uint64(searchReq.MaxDurationMs), 10)+"ms")
}
if searchReq.MinDurationMs != 0 {
qb.addParam(urlParamMinDuration, strconv.FormatUint(uint64(searchReq.MinDurationMs), 10)+"ms")
}
if searchReq.SpansPerSpanSet != 0 {
qb.addParam(urlParamSpansPerSpanSet, strconv.FormatUint(uint64(searchReq.SpansPerSpanSet), 10))
}
if len(searchReq.Query) > 0 {
qb.addParam(urlParamQuery, searchReq.Query)
}
if len(searchReq.Tags) > 0 {
builder := &strings.Builder{}
encoder := logfmt.NewEncoder(builder)
for k, v := range searchReq.Tags {
err := encoder.EncodeKeyval(k, v)
if err != nil {
return nil, err
}
}
qb.addParam(urlParamTags, builder.String())
}
req.URL.RawQuery = qb.query()
return req, nil
}
// BuildSearchBlockRequest takes a tempopb.SearchBlockRequest and populates the passed http.Request
// with the appropriate params. If no http.Request is provided a new one is created.
// dedicatedColumnsJSON should be generated using the DedicatedColumnsToJSON struct which produces the expected string
// value and memoizes results to prevent redundant marshaling.
func BuildSearchBlockRequest(req *http.Request, searchReq *tempopb.SearchBlockRequest, dedicatedColumnsJSON string) (*http.Request, error) {
if req == nil {
req = &http.Request{
URL: &url.URL{},
}
}
req, err := BuildSearchRequest(req, searchReq.SearchReq)
if err != nil {
return nil, err
}
qb := newQueryBuilder(req.URL.RawQuery)
qb.addParam(urlParamBlockID, searchReq.BlockID)
qb.addParam(urlParamPagesToSearch, strconv.FormatUint(uint64(searchReq.PagesToSearch), 10))
qb.addParam(urlParamSize, strconv.FormatUint(searchReq.Size_, 10))
qb.addParam(urlParamStartPage, strconv.FormatUint(uint64(searchReq.StartPage), 10))
qb.addParam(urlParamEncoding, searchReq.Encoding)
qb.addParam(urlParamIndexPageSize, strconv.FormatUint(uint64(searchReq.IndexPageSize), 10))
qb.addParam(urlParamTotalRecords, strconv.FormatUint(uint64(searchReq.TotalRecords), 10))
qb.addParam(urlParamDataEncoding, searchReq.DataEncoding)
qb.addParam(urlParamVersion, searchReq.Version)
qb.addParam(urlParamFooterSize, strconv.FormatUint(uint64(searchReq.FooterSize), 10))
if len(dedicatedColumnsJSON) > 0 && dedicatedColumnsJSON != "null" { // if a caller marshals a nil dedicated cols we will receive the string "null"
qb.addParam(urlParamDedicatedColumns, dedicatedColumnsJSON)
}
req.URL.RawQuery = qb.query()
return req, nil
}
func extractQueryParam(v url.Values, param string) (string, bool) {
value := v.Get(param)
return value, value != ""
}
// ValidateAndSanitizeRequest validates params for trace by id api
// return values are (blockStart, blockEnd, queryMode, start, end, error)
func ValidateAndSanitizeRequest(r *http.Request) (string, string, string, int64, int64, error) {
vals := r.URL.Query()
q, _ := extractQueryParam(vals, QueryModeKey)
// validate queryMode. it should either be empty or one of (QueryModeIngesters|QueryModeBlocks|QueryModeAll)
var queryMode string
var startTime int64
var endTime int64
var blockStart string
var blockEnd string
if len(q) == 0 || q == QueryModeAll {
queryMode = QueryModeAll
} else if q == QueryModeIngesters {
queryMode = QueryModeIngesters
} else if q == QueryModeBlocks {
queryMode = QueryModeBlocks
} else {
return "", "", "", 0, 0, fmt.Errorf("invalid value for mode %s", q)
}
// no need to validate/sanitize other parameters if queryMode == QueryModeIngesters
if queryMode == QueryModeIngesters {
return "", "", queryMode, 0, 0, nil
}
if start, ok := extractQueryParam(vals, BlockStartKey); ok {
_, err := uuid.Parse(start)
if err != nil {
return "", "", "", 0, 0, fmt.Errorf("invalid value for blockstart: %w", err)
}
blockStart = start
} else {
blockStart = tempodb.BlockIDMin
}
if end, ok := extractQueryParam(vals, BlockEndKey); ok {
_, err := uuid.Parse(end)
if err != nil {
return "", "", "", 0, 0, fmt.Errorf("invalid value for blockEnd: %w", err)
}
blockEnd = end
} else {
blockEnd = tempodb.BlockIDMax
}
if s, ok := extractQueryParam(vals, urlParamStart); ok {
var err error
startTime, err = strconv.ParseInt(s, 10, 64)
if err != nil {
return "", "", "", 0, 0, fmt.Errorf("invalid start: %w", err)
}
} else {
startTime = 0
}
if s, ok := extractQueryParam(vals, urlParamEnd); ok {
var err error
endTime, err = strconv.ParseInt(s, 10, 64)
if err != nil {
return "", "", "", 0, 0, fmt.Errorf("invalid end: %w", err)
}
} else {
endTime = 0
}
if startTime != 0 && endTime != 0 && endTime <= startTime {
return "", "", "", 0, 0, fmt.Errorf("http parameter start must be before end. received start=%d end=%d", startTime, endTime)
}
return blockStart, blockEnd, queryMode, startTime, endTime, nil
}
func ReadBodyToBuffer(resp *http.Response) (*bytes.Buffer, error) {
length := resp.ContentLength
// if ContentLength is -1 if the length is unknown. default to bytes.MinRead (its what buffer.ReadFrom does)
if length < 0 {
length = bytes.MinRead
}
// buffer.ReadFrom always allocs at least bytes.MinRead past the end of the actual required length b/c of how io.EOF is handled. this prevents extending the internal
// slice unnecessarily. https://github.com/golang/go/issues/21852
length += bytes.MinRead
// alloc a buffer to store the response body
buffer := bytes.NewBuffer(make([]byte, 0, length))
_, err := buffer.ReadFrom(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %w", err)
}
return buffer, nil
}