-
Notifications
You must be signed in to change notification settings - Fork 20
/
util.go
66 lines (59 loc) · 1.38 KB
/
util.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
66
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT license.
* SPDX-License-Identifier: MIT
*/
package observability
import (
"fmt"
"reflect"
"go.opentelemetry.io/otel/attribute"
)
func convertMapToAttributes(m map[string]any) []attribute.KeyValue {
attrs := make([]attribute.KeyValue, len(m))
i := 0
for k, v := range m {
attrs[i] = convertKey(k, v)
i++
}
return attrs
}
func convertKey(k string, v any) attribute.KeyValue {
r := reflect.TypeOf(v)
if r != nil && r.Kind() == reflect.Func {
res := reflect.ValueOf(v).Call(nil)[0]
return convertKey(k, res.Interface())
}
switch val := v.(type) {
case string:
return attribute.String(k, val)
case int:
return attribute.Int(k, val)
case int8:
return attribute.Int(k, int(val))
case int16:
return attribute.Int(k, int(val))
case int32:
return attribute.Int(k, int(val))
case int64:
return attribute.Int64(k, val)
case float32:
return attribute.Float64(k, float64(val))
case float64:
return attribute.Float64(k, val)
case bool:
return attribute.Bool(k, val)
case []string:
return attribute.StringSlice(k, val)
case []bool:
return attribute.BoolSlice(k, val)
case []float64:
return attribute.Float64Slice(k, val)
case []int64:
return attribute.Int64Slice(k, val)
case []int:
return attribute.IntSlice(k, val)
default:
return attribute.String(k, fmt.Sprintf("%v", val))
}
}