-
Notifications
You must be signed in to change notification settings - Fork 2
/
attributes.go
65 lines (57 loc) · 1.25 KB
/
attributes.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
58
59
60
61
62
63
64
65
package generator
import (
commonpb "go.opentelemetry.io/proto/otlp/common/v1"
)
const unsupportedTypeValue = "unsupported_type"
func ToAttributes(attrs map[string]interface{}) []*commonpb.KeyValue {
attributes := make([]*commonpb.KeyValue, 0)
for k, v := range attrs {
attributes = append(attributes, &commonpb.KeyValue{
Key: k,
Value: ToAnyValue(v),
})
}
return attributes
}
func ToAnyValue(attr interface{}) *commonpb.AnyValue {
var value *commonpb.AnyValue
switch attrValue := attr.(type) {
case string:
value = &commonpb.AnyValue{
Value: &commonpb.AnyValue_StringValue{
StringValue: attrValue,
},
}
case int64:
value = &commonpb.AnyValue{
Value: &commonpb.AnyValue_IntValue{
IntValue: attrValue,
},
}
case int:
value = &commonpb.AnyValue{
Value: &commonpb.AnyValue_IntValue{
IntValue: int64(attrValue),
},
}
case float64:
value = &commonpb.AnyValue{
Value: &commonpb.AnyValue_DoubleValue{
DoubleValue: attrValue,
},
}
case bool:
value = &commonpb.AnyValue{
Value: &commonpb.AnyValue_BoolValue{
BoolValue: attrValue,
},
}
default:
value = &commonpb.AnyValue{
Value: &commonpb.AnyValue_StringValue{
StringValue: unsupportedTypeValue,
},
}
}
return value
}