...
1
2
3
4 package otelhttp
5
6 import (
7 "context"
8 "sync"
9
10 "go.opentelemetry.io/otel/attribute"
11 )
12
13
14
15 type Labeler struct {
16 mu sync.Mutex
17 attributes []attribute.KeyValue
18 }
19
20
21 func (l *Labeler) Add(ls ...attribute.KeyValue) {
22 l.mu.Lock()
23 defer l.mu.Unlock()
24 l.attributes = append(l.attributes, ls...)
25 }
26
27
28 func (l *Labeler) Get() []attribute.KeyValue {
29 l.mu.Lock()
30 defer l.mu.Unlock()
31 ret := make([]attribute.KeyValue, len(l.attributes))
32 copy(ret, l.attributes)
33 return ret
34 }
35
36 type labelerContextKeyType int
37
38 const lablelerContextKey labelerContextKeyType = 0
39
40 func injectLabeler(ctx context.Context, l *Labeler) context.Context {
41 return context.WithValue(ctx, lablelerContextKey, l)
42 }
43
44
45
46
47
48 func LabelerFromContext(ctx context.Context) (*Labeler, bool) {
49 l, ok := ctx.Value(lablelerContextKey).(*Labeler)
50 if !ok {
51 l = &Labeler{}
52 }
53 return l, ok
54 }
55
View as plain text