This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
/
tag.go
152 lines (122 loc) · 3.01 KB
/
tag.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
package tagquery
import (
"encoding/json"
"io"
"sort"
"strings"
"github.com/grafana/metrictank/errors"
"github.com/grafana/metrictank/schema"
)
//go:generate msgp
type Tags []Tag
//ParseTags parses a list of key value pairs into a list of Tags.
//ParseTags([]string{"key=value", "name=whatever"}) -> Tags{{Key: "key", Value: "value"}, {Key: "name", Value: "whatever"}}}
func ParseTags(tags []string) (Tags, error) {
res := make(Tags, len(tags))
for i := range tags {
tag, err := ParseTag(tags[i])
if err != nil {
return nil, err
}
res[i] = tag
}
sort.Sort(res)
return res, nil
}
func ParseTagsFromMetricName(name string) (Tags, error) {
elements := strings.Split(name, ";")
nameValue := schema.SanitizeNameAsTagValue(elements[0])
if !schema.ValidateTagValue(nameValue) {
return nil, errors.NewBadRequestf("Metric name is invalid as tag value \"%s\"", nameValue)
}
if len(elements) < 2 {
return []Tag{{Key: "name", Value: nameValue}}, nil
}
res, err := ParseTags(elements[1:])
if err != nil {
return nil, err
}
res = append(res, Tag{Key: "name", Value: nameValue})
sort.Sort(res)
return res, nil
}
func (t Tags) Len() int { return len(t) }
func (t Tags) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
func (t Tags) Less(i, j int) bool {
if t[i].Key == t[j].Key {
return t[i].Value < t[j].Value
}
return t[i].Key < t[j].Key
}
func (t Tags) Strings() []string {
builder := strings.Builder{}
res := make([]string, len(t))
for i := range t {
t[i].StringIntoWriter(&builder)
res[i] = builder.String()
builder.Reset()
}
return res
}
func (t Tags) Sort() {
sort.Slice(t, func(i, j int) bool {
if t[i].Key == t[j].Key {
return t[i].Value < t[j].Value
}
return t[i].Key < t[j].Key
})
}
func (t Tags) Equal(other Tags) bool {
if len(t) != len(other) {
return false
}
for i := range t {
if t[i] != other[i] {
return false
}
}
return true
}
// MarshalJSON satisfies the json.Marshaler interface
// it is used by the api endpoint /metaTags to list the meta tag records
func (t Tags) MarshalJSON() ([]byte, error) {
return json.Marshal(t.Strings())
}
func (t *Tags) UnmarshalJSON(data []byte) error {
var tagStrings []string
err := json.Unmarshal(data, &tagStrings)
if err != nil {
return err
}
parsed, err := ParseTags(tagStrings)
if err != nil {
return err
}
*t = parsed
return nil
}
type Tag struct {
Key string
Value string
}
func ParseTag(tag string) (Tag, error) {
res := Tag{}
equalPos := strings.Index(tag, "=")
if equalPos < 0 {
return res, errors.NewBadRequestf("Missing equal sign in tag: %s", tag)
}
res.Key = tag[:equalPos]
if !schema.ValidateTagKey(res.Key) {
return res, errors.NewBadRequestf("Invalid tag key \"%s\"", res.Key)
}
res.Value = tag[equalPos+1:]
if !schema.ValidateTagValue(res.Value) {
return res, errors.NewBadRequestf("Invalid tag value \"%s\"", res.Value)
}
return res, nil
}
func (t *Tag) StringIntoWriter(writer io.Writer) {
writer.Write([]byte(t.Key))
writer.Write([]byte("="))
writer.Write([]byte(t.Value))
}