-
Notifications
You must be signed in to change notification settings - Fork 1
/
metadata.go
49 lines (44 loc) · 858 Bytes
/
metadata.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
package totem
import (
"go.opentelemetry.io/otel/attribute"
"google.golang.org/grpc/metadata"
)
func (md *MD) ToMD() metadata.MD {
m := map[string][]string{}
for k, v := range md.GetData() {
m[k] = v.Items
}
return metadata.MD(m)
}
func FromMD(md metadata.MD) *MD {
if md == nil {
return nil
}
m := MD{
Data: map[string]*MDValues{},
}
for k, v := range md {
m.Data[k] = &MDValues{
Items: v,
}
}
return &m
}
func (md *MD) KV() []attribute.KeyValue {
kv := []attribute.KeyValue{}
for k, v := range md.GetData() {
if len(v.Items) == 1 {
kv = append(kv, attribute.String(k, v.Items[0]))
} else {
kv = append(kv, attribute.StringSlice(k, v.Items))
}
}
return kv
}
func (md *MD) Keys() []string {
keys := make([]string, 0, len(md.GetData()))
for k := range md.GetData() {
keys = append(keys, k)
}
return keys
}