-
Notifications
You must be signed in to change notification settings - Fork 10
/
label.go
45 lines (35 loc) · 1.1 KB
/
label.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
package zerodriver
import (
"strings"
"github.com/rs/zerolog"
)
type LabelSource struct {
Key string
Value string
}
type labels struct {
store map[string]interface{}
}
func newLabels() *labels {
return &labels{store: map[string]interface{}{}}
}
// Label adds an optional label to the payload in the format of `key: value`.
// Key of label must start with `labels.`
func Label(key, value string) *LabelSource {
return &LabelSource{Key: "labels." + key, Value: value}
}
// Labels takes LabelSource structs, filters the ones that have their key start with the
// string `labels.` and their value type set to string type. It then wraps those
// key/value pairs in a top-level `labels` namespace.
func (e *Event) Labels(labels ...*LabelSource) *zerolog.Event {
lbls := newLabels()
for i := range labels {
if isLabelEvent(labels[i]) {
lbls.store[strings.Replace(labels[i].Key, "labels.", "", 1)] = labels[i].Value
}
}
return e.Event.Dict("logging.googleapis.com/labels", zerolog.Dict().Fields(lbls.store))
}
func isLabelEvent(label *LabelSource) bool {
return strings.HasPrefix(label.Key, "labels.")
}