-
Notifications
You must be signed in to change notification settings - Fork 2
/
queryUtils.go
126 lines (108 loc) · 3.75 KB
/
queryUtils.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
package store
import (
"bytes"
"encoding/json"
"fmt"
"github.com/logzio/jaeger-logzio/store/objects"
"time"
"github.com/jaegertracing/jaeger/storage/spanstore"
"github.com/jaegertracing/jaeger/model"
"github.com/olivere/elastic"
"github.com/pkg/errors"
)
func convertTraceIDsStringsToModels(traceIDs []string) ([]model.TraceID, error) {
traceIDsModels := make([]model.TraceID, len(traceIDs))
for i, ID := range traceIDs {
traceID, err := model.TraceIDFromString(ID)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("Making traceID from string '%s' failed", ID))
}
traceIDsModels[i] = traceID
}
return traceIDsModels, nil
}
func unmarshalJSONSpan(esSpanRaw *elastic.SearchHit) (*objects.LogzioSpan, error) {
esSpanInByteArray := esSpanRaw.Source
var jsonSpan objects.LogzioSpan
decoder := json.NewDecoder(bytes.NewReader(*esSpanInByteArray))
decoder.UseNumber()
if err := decoder.Decode(&jsonSpan); err != nil {
return nil, err
}
return &jsonSpan, nil
}
func buildTraceIDAggregation(numOfTraces int) elastic.Aggregation {
return elastic.NewTermsAggregation().
Size(numOfTraces).
Field(traceIDField).
Order(startTimeField, false).
SubAggregation(startTimeField, buildTraceIDSubAggregation())
}
func buildTraceIDSubAggregation() elastic.Aggregation {
return elastic.NewMaxAggregation().
Field(startTimeField)
}
func buildDurationQuery(durationMin time.Duration, durationMax time.Duration) elastic.Query {
minDurationMicros := model.DurationAsMicroseconds(durationMin)
maxDurationMicros := defaultMaxDuration
if durationMax != 0 {
maxDurationMicros = model.DurationAsMicroseconds(durationMax)
}
return elastic.NewRangeQuery(durationField).Gte(minDurationMicros).Lte(maxDurationMicros)
}
func buildStartTimeQuery(startTimeMin time.Time, startTimeMax time.Time) elastic.Query {
minStartTimeMicros := model.TimeAsEpochMicroseconds(startTimeMin)
maxStartTimeMicros := model.TimeAsEpochMicroseconds(startTimeMax)
return elastic.NewRangeQuery(startTimeField).Gte(minStartTimeMicros).Lte(maxStartTimeMicros)
}
func buildServiceNameQuery(serviceName string) elastic.Query {
return elastic.NewMatchQuery(serviceNameField, serviceName)
}
func buildOperationNameQuery(operationName string) elastic.Query {
return elastic.NewMatchQuery(operationNameField, operationName)
}
func buildNestedQuery(field string, k string, v string) elastic.Query {
keyField := fmt.Sprintf("%s.%s", field, tagKeyField)
valueField := fmt.Sprintf("%s.%s", field, tagValueField)
keyQuery := elastic.NewMatchQuery(keyField, k)
valueQuery := elastic.NewMatchQuery(valueField, v)
tagBoolQuery := elastic.NewBoolQuery().Must(keyQuery, valueQuery)
return elastic.NewNestedQuery(field, tagBoolQuery)
}
func buildObjectQuery(field string, k string, v string) elastic.Query {
keyField := fmt.Sprintf("%s.%s", field, k)
keyQuery := elastic.NewMatchQuery(keyField, v)
return elastic.NewBoolQuery().Must(keyQuery)
}
func bucketToStringArray(buckets []*elastic.AggregationBucketKeyItem) ([]string, error) {
strings := make([]string, len(buckets))
for i, keyitem := range buckets {
str, ok := keyitem.Key.(string)
if !ok {
return nil, errors.New("Non-string key found in aggregation")
}
strings[i] = str
}
return strings, nil
}
func validateQuery(p *spanstore.TraceQueryParameters) error {
if p == nil {
return ErrMalformedRequestObject
}
if p.ServiceName == "" && len(p.Tags) > 0 {
return ErrServiceNameNotSet
}
if p.StartTimeMin.IsZero() || p.StartTimeMax.IsZero() {
return ErrStartAndEndTimeNotSet
}
if p.StartTimeMax.Before(p.StartTimeMin) {
return ErrStartTimeMinGreaterThanMax
}
if p.DurationMin != 0 && p.DurationMax != 0 && p.DurationMin > p.DurationMax {
return ErrDurationMinGreaterThanMax
}
if p.NumTraces > 1000 {
p.NumTraces = 1000
}
return nil
}