forked from agoda-com/opentelemetry-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conv.go
33 lines (30 loc) · 955 Bytes
/
conv.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
package otelzerolog
import (
"fmt"
"math"
"go.opentelemetry.io/otel/attribute"
)
func otelAttribute(key string, value interface{}) []attribute.KeyValue {
switch value := value.(type) {
case bool:
return []attribute.KeyValue{attribute.Bool(key, value)}
// Number information is lost when we're converting to byte to interface{}, let's recover it
case float64:
if _, frac := math.Modf(value); frac == 0.0 {
return []attribute.KeyValue{attribute.Int64(key, int64(value))}
} else {
return []attribute.KeyValue{attribute.Float64(key, value)}
}
case string:
return []attribute.KeyValue{attribute.String(key, value)}
case []interface{}:
var result []attribute.KeyValue
for _, v := range value {
// recursively call otelAttribute to handle nested arrays
result = append(result, otelAttribute(key, v)...)
}
return result
}
// Default case
return []attribute.KeyValue{attribute.String(key, fmt.Sprintf("%v", value))}
}