forked from kubeshop/tracetest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_store.go
95 lines (74 loc) · 2.03 KB
/
data_store.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
package expression
import (
"fmt"
"strconv"
"github.com/kubeshop/tracetest/server/traces"
"github.com/kubeshop/tracetest/server/variableset"
)
type DataStore interface {
Source() string
Get(name string) (string, error)
}
var attributeAlias = map[string]string{
"name": "tracetest.span.name",
}
type AttributeDataStore struct {
Span traces.Span
}
func (ds AttributeDataStore) Source() string {
return "attr"
}
func (ds AttributeDataStore) getFromAlias(name string) (string, error) {
alias, found := attributeAlias[name]
if !found {
return "", fmt.Errorf(`attribute "%s" not found`, name)
}
value := ds.Span.Attributes.Get(alias)
if value == "" {
return "", fmt.Errorf(`attribute "%s" not found`, name)
}
return value, nil
}
func (ds AttributeDataStore) Get(name string) (string, error) {
if !ds.Span.ID.IsValid() {
// It's probably a nil span and we never got a matching selector,
// so instead of returning a resolution error, let's return a non-matching
// span error instead
return "", fmt.Errorf(`there are no matching spans to retrieve the attribute "%s" from. To fix this error, create a selector matching at least one span.`, name)
}
value := ds.Span.Attributes.Get(name)
if value == "" {
return ds.getFromAlias(name)
}
return value, nil
}
type MetaAttributesDataStore struct {
SelectedSpans []traces.Span
}
func (ds MetaAttributesDataStore) Source() string {
return metaPrefix
}
func (ds MetaAttributesDataStore) Get(name string) (string, error) {
switch name {
case "count":
return ds.count(), nil
}
return "", fmt.Errorf("unknown meta attribute %s%s", metaPrefix, name)
}
func (ds MetaAttributesDataStore) count() string {
return strconv.Itoa(len(ds.SelectedSpans))
}
type VariableDataStore struct {
Values []variableset.VariableSetValue
}
func (ds VariableDataStore) Source() string {
return "env"
}
func (ds VariableDataStore) Get(name string) (string, error) {
for _, v := range ds.Values {
if v.Key == name {
return v.Value, nil
}
}
return "", fmt.Errorf(`variable "%s" not found`, name)
}