-
Notifications
You must be signed in to change notification settings - Fork 2
/
labels.go
47 lines (39 loc) · 1.04 KB
/
labels.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
package slogdriver
import "context"
// Label represents a key-value string pair.
type Label struct {
Key string
Value string
}
// NewLabel returns a new Label from a key and a value.
func NewLabel(key, value string) Label {
return Label{Key: key, Value: value}
}
// AddLabels returns a new Context with additional labels to be used in the log
// entries produced using that context.
//
// See https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#FIELDS.labels
func AddLabels(ctx context.Context, labels ...Label) context.Context {
return context.WithValue(ctx, labelsContextKeyT{}, &labelContainer{
Labels: labels,
Parent: labelsFromContext(ctx),
})
}
func labelsFromContext(ctx context.Context) *labelContainer {
v, _ := ctx.Value(labelsContextKeyT{}).(*labelContainer)
return v
}
type labelsContextKeyT struct{}
type labelContainer struct {
Labels []Label
Parent *labelContainer
}
func (l *labelContainer) Iterate(f func(Label)) {
if l == nil {
return
}
l.Parent.Iterate(f)
for _, label := range l.Labels {
f(label)
}
}