-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmeta.go
55 lines (42 loc) · 1.1 KB
/
meta.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
package tracer
import (
"context"
"github.com/alexfalkowski/go-service/transport/grpc/meta"
"go.opentelemetry.io/otel"
"google.golang.org/grpc/metadata"
)
// Carrier for tracer.
type Carrier struct {
Metadata metadata.MD
}
// Get returns the value associated with the passed key.
func (s *Carrier) Get(key string) string {
values := s.Metadata.Get(key)
if len(values) == 0 {
return ""
}
return values[0]
}
// Set stores the key-value pair.
func (s *Carrier) Set(key string, value string) {
s.Metadata.Set(key, value)
}
// Keys lists the keys stored in this carrier.
func (s *Carrier) Keys() []string {
out := make([]string, 0, len(s.Metadata))
for key := range s.Metadata {
out = append(out, key)
}
return out
}
func inject(ctx context.Context) context.Context {
md := meta.ExtractOutgoing(ctx)
prop := otel.GetTextMapPropagator()
prop.Inject(ctx, &Carrier{Metadata: md})
return metadata.NewOutgoingContext(ctx, md)
}
func extract(ctx context.Context) context.Context {
md := meta.ExtractIncoming(ctx)
prop := otel.GetTextMapPropagator()
return prop.Extract(ctx, &Carrier{Metadata: md})
}