-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmeta.go
57 lines (43 loc) · 1.22 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
56
57
package otel
import (
"context"
"github.com/alexfalkowski/go-service/transport/grpc/meta"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"
"google.golang.org/grpc/metadata"
)
func inject(ctx context.Context) context.Context {
md := meta.ExtractOutgoing(ctx)
prop := otel.GetTextMapPropagator()
prop.Inject(ctx, &metadataCarrier{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, &metadataCarrier{metadata: &md})
}
type metadataCarrier struct {
metadata *metadata.MD
}
var _ propagation.TextMapCarrier = &metadataCarrier{}
// Get returns the value associated with the passed key.
func (s *metadataCarrier) 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 *metadataCarrier) Set(key string, value string) {
s.metadata.Set(key, value)
}
// Keys lists the keys stored in this carrier.
func (s *metadataCarrier) Keys() []string {
out := make([]string, 0, len(*s.metadata))
for key := range *s.metadata {
out = append(out, key)
}
return out
}