-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
tag_store.go
177 lines (156 loc) · 4.26 KB
/
tag_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
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
package gnmi
import (
"fmt"
"path/filepath"
"sort"
"strings"
"github.com/influxdata/telegraf/internal"
gnmiLib "github.com/openconfig/gnmi/proto/gnmi"
)
type tagStore struct {
unconditional map[string]string
names map[string]map[string]string
elements elementsStore
}
type elementsStore struct {
required [][]string
tags map[string]map[string]string
}
func newTagStore(subs []TagSubscription) *tagStore {
store := tagStore{
unconditional: make(map[string]string),
names: make(map[string]map[string]string),
elements: elementsStore{
required: make([][]string, 0, len(subs)),
tags: make(map[string]map[string]string),
},
}
for _, s := range subs {
if s.Match == "elements" {
store.elements.required = append(store.elements.required, s.Elements)
}
}
return &store
}
// Store tags extracted from TagSubscriptions
func (s *tagStore) insert(subscription TagSubscription, path *gnmiLib.Path, values map[string]interface{}, tags map[string]string) error {
switch subscription.Match {
case "unconditional":
for k, v := range values {
tagName := subscription.Name + "/" + filepath.Base(k)
sv, err := internal.ToString(v)
if err != nil {
return fmt.Errorf("conversion error for %v: %w", v, err)
}
if sv == "" {
delete(s.unconditional, tagName)
} else {
s.unconditional[tagName] = sv
}
}
case "name":
// Get the lookup key
key, found := tags["name"]
if !found {
return nil
}
// Make sure we have a valid map for the key
if _, exists := s.names[key]; !exists {
s.names[key] = make(map[string]string)
}
// Add the values
for k, v := range values {
tagName := subscription.Name + "/" + filepath.Base(k)
sv, err := internal.ToString(v)
if err != nil {
return fmt.Errorf("conversion error for %v: %w", v, err)
}
if sv == "" {
delete(s.names[key], tagName)
} else {
s.names[key][tagName] = sv
}
}
case "elements":
key, match := s.getElementsKeys(path, subscription.Elements)
if !match || len(values) == 0 {
return nil
}
// Make sure we have a valid map for the key
if _, exists := s.elements.tags[key]; !exists {
s.elements.tags[key] = make(map[string]string)
}
// Add the values
for k, v := range values {
tagName := subscription.Name + "/" + filepath.Base(k)
sv, err := internal.ToString(v)
if err != nil {
return fmt.Errorf("conversion error for %v: %w", v, err)
}
if sv == "" {
delete(s.elements.tags[key], tagName)
} else {
s.elements.tags[key][tagName] = sv
}
}
default:
return fmt.Errorf("unknown match strategy %q", subscription.Match)
}
return nil
}
func (s *tagStore) lookup(path *gnmiLib.Path, metricTags map[string]string) map[string]string {
// Add all unconditional tags
tags := make(map[string]string, len(s.unconditional))
for k, v := range s.unconditional {
tags[k] = v
}
// Match names
key, found := metricTags["name"]
if found {
for k, v := range s.names[key] {
tags[k] = v
}
}
// Match elements
for _, requiredKeys := range s.elements.required {
key, match := s.getElementsKeys(path, requiredKeys)
if !match {
continue
}
for k, v := range s.elements.tags[key] {
tags[k] = v
}
}
return tags
}
func (s *tagStore) getElementsKeys(path *gnmiLib.Path, elements []string) (string, bool) {
keyElements := pathKeys(path)
// Search for the required path elements and collect a ordered
// list of their values to in the form
// elementName1={keyA=valueA,keyB=valueB,...},...,elementNameN={keyY=valueY,keyZ=valueZ}
// where each elements' key-value list is enclosed in curly brackets.
keyParts := make([]string, 0, len(elements))
for _, requiredElement := range elements {
var found bool
var elementKVs []string
for _, el := range keyElements {
if el.Name == requiredElement {
for k, v := range el.Key {
elementKVs = append(elementKVs, k+"="+v)
}
found = true
break
}
}
// The element was not found, but all must match
if !found {
return "", false
}
// We need to order the element's key-value pairs as the map
// returns elements in random order
sort.Strings(elementKVs)
// Collect the element
keyParts = append(keyParts, requiredElement+"={"+strings.Join(elementKVs, ",")+"}")
}
return strings.Join(keyParts, ","), true
}