-
Notifications
You must be signed in to change notification settings - Fork 4
/
metadata.go
176 lines (162 loc) · 6.13 KB
/
metadata.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
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package metrics
import (
"errors"
"fmt"
"sort"
promproto "github.com/prometheus/client_model/go"
)
// Match the Prometheus error text.
var errInconsistentCardinality = errors.New("inconsistent tag cardinality")
// metadata stores our internal representation of metric specs. Adding this
// layer of indirection between the user-facing specs and the metric
// constructors serves two purposes: it centralizes the logic for calculating
// a variety of derived values, and it lets the remainder of the package
// assume that all user-supplied data has already been fully validated.
type metadata struct {
Name, Help *string // proto wants pointers
Dims string
DisablePush bool
constTagPairs []*promproto.LabelPair
varTagNames []string // unscrubbed
}
func newMetadata(o Spec) (metadata, error) {
// TODO: Consider checking for duplicate tags with Bloom filters, allocating
// maps only if we suspect a duplicate.
sortedConstNames := make([]string, 0, len(o.ConstTags))
for k := range o.ConstTags {
sortedConstNames = append(sortedConstNames, k)
}
sort.Strings(sortedConstNames)
constNameSet := make(map[string]struct{}, len(sortedConstNames))
sortedScrubbedConstNames := make([]string, len(sortedConstNames))
sortedScrubbedConstVals := make([]string, len(sortedConstNames))
for i, name := range sortedConstNames {
scrubbedName := scrubName(name)
if _, ok := constNameSet[scrubbedName]; ok {
return metadata{}, fmt.Errorf("duplicate constant tag name %q", scrubbedName)
}
constNameSet[scrubbedName] = struct{}{}
sortedScrubbedConstNames[i] = scrubbedName
sortedScrubbedConstVals[i] = scrubTagValue(o.ConstTags[name])
}
varNameSet := make(map[string]struct{}, len(o.VarTags))
sortedScrubbedVarNames := make([]string, len(o.VarTags))
for i, name := range o.VarTags {
scrubbedName := scrubName(name)
if _, ok := varNameSet[scrubbedName]; ok {
return metadata{}, fmt.Errorf("duplicate variable tag name %q", scrubbedName)
}
if _, ok := constNameSet[scrubbedName]; ok {
return metadata{}, fmt.Errorf("variable tag name %q is also a constant tag name", scrubbedName)
}
varNameSet[scrubbedName] = struct{}{}
sortedScrubbedVarNames[i] = scrubbedName
}
sort.Strings(sortedScrubbedVarNames)
var pairs []*promproto.LabelPair
if len(sortedScrubbedConstNames) > 0 {
pairs = make([]*promproto.LabelPair, 0, len(sortedScrubbedConstNames))
for i := range sortedScrubbedConstNames {
pairs = append(pairs, &promproto.LabelPair{
Name: &sortedScrubbedConstNames[i],
Value: &sortedScrubbedConstVals[i],
})
}
}
scrubbedName := scrubName(o.Name)
return metadata{
Name: &scrubbedName,
Help: &o.Help,
Dims: makeDims(scrubbedName, sortedScrubbedConstNames, sortedScrubbedVarNames),
DisablePush: o.DisablePush,
constTagPairs: pairs,
varTagNames: o.VarTags, // preserve user-defined order
}, nil
}
// MergeTags merges variable and constant tags.
func (m metadata) MergeTags(variableTagPairs []string) []*promproto.LabelPair {
if len(variableTagPairs) == 0 {
return m.constTagPairs
}
n := len(m.constTagPairs) + len(m.varTagNames)
pairs := make([]*promproto.LabelPair, 0, n)
pairs = append(pairs, m.constTagPairs...)
for i := range m.varTagNames { // user-supplied order was preserved
name := scrubName(m.varTagNames[i])
val := scrubTagValue(variableTagPairs[i*2+1])
pairs = append(pairs, &promproto.LabelPair{
Name: &name,
Value: &val,
})
}
sort.Slice(pairs, func(i, j int) bool {
return pairs[i].GetName() < pairs[j].GetName()
})
return pairs
}
// ValidateVariableTags checks that the user-supplied variable tag names
// and values match the spec supplied at vector creation.
func (m metadata) ValidateVariableTags(variableTagPairs []string) error {
if len(variableTagPairs) != 2*len(m.varTagNames) {
return errInconsistentCardinality
}
for i, expected := range m.varTagNames { // user-supplied order was preserved
if expected != variableTagPairs[i*2] {
return fmt.Errorf(
"variable tag #%d doesn't match vector definition: expected %s, got %s",
i,
expected,
variableTagPairs[i*2],
)
}
}
return nil
}
// writeID writes the metric's ID to the supplied digester. Since we only use
// IDs as map keys, we can save an allocation on metric creation by not
// allocating a string for each ID.
func (m metadata) writeID(d *digester) {
d.add("", *m.Name)
for _, pair := range m.constTagPairs {
d.add("", pair.GetName())
d.add("", pair.GetValue())
}
}
// makeDims creates a string representation of the metric's dimensions (name,
// constant tag names, and variable tag names). It's used as a map value,
// so we can't avoid this allocation.
func makeDims(name string, constNames, sortedVarNames []string) string {
d := newDigester()
d.add("", name)
for _, n := range constNames {
d.add("", n)
}
for _, n := range sortedVarNames {
// To make sure that we can tell whether a given tag name is constant or
// variable, prefix variable tag names with a character that's otherwise
// forbidden.
d.add("$", n)
}
dims := string(d.digest())
d.free()
return dims
}