-
Notifications
You must be signed in to change notification settings - Fork 474
/
traceQLBuilder.go
79 lines (67 loc) · 1.88 KB
/
traceQLBuilder.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
package tempo
import (
"fmt"
"reflect"
"strings"
)
type operandType string
const (
AND operandType = "&&"
OR operandType = "||"
EQUAL operandType = "="
NOTEQUAL operandType = "!="
REGEX operandType = "=~"
)
type TraceQL struct {
operator1 interface{}
operand operandType
operator2 interface{}
}
// Groups are ()
type Group struct {
group []TraceQL
operand operandType
}
// Subqueries are {}
type Subquery struct {
trace TraceQL
}
func printOperator(operator interface{}) string {
queryString := ""
valueType := reflect.TypeOf(operator)
switch valueType.String() {
case "tempo.Subquery":
queryString = fmt.Sprintf("{ %s }", printOperator(operator.(Subquery).trace))
case "tempo.Group":
queryString = "( "
for i, op := range operator.(Group).group {
queryString += fmt.Sprintf(" %s ", printOperator(op))
if i < len(operator.(Group).group)-1 {
queryString += fmt.Sprintf(" %s ", operator.(Group).operand)
}
}
queryString += ")"
case "tempo.TraceQL":
if operator.(TraceQL).operator1 != nil {
if reflect.TypeOf(operator.(TraceQL).operator2).String() == "string" {
queryString = fmt.Sprintf("%s %s \"%s\" ", operator.(TraceQL).operator1,
operator.(TraceQL).operand, operator.(TraceQL).operator2)
} else {
if reflect.TypeOf(operator.(TraceQL).operator2).String() == "tempo.unquoted" {
queryString = fmt.Sprintf("%s %s %s ", operator.(TraceQL).operator1,
operator.(TraceQL).operand, operator.(TraceQL).operator2)
} else {
queryString = fmt.Sprintf("%s %s %s ", printOperator(operator.(TraceQL).operator1),
operator.(TraceQL).operand, printOperator(operator.(TraceQL).operator2))
}
}
}
case "string":
queryString = fmt.Sprintf("%s", operator)
}
return queryString
}
func printSelect(fields []string) string {
selects := strings.Join(fields, ", ")
return fmt.Sprintf("select(%s)", selects)
}