-
Notifications
You must be signed in to change notification settings - Fork 1
/
attr.go
265 lines (254 loc) · 8.01 KB
/
attr.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package otelattr
import (
"encoding/json"
"fmt"
"reflect"
"go.opentelemetry.io/otel/attribute"
)
type Marshaler interface {
MarshalOtelAttributes() ([]attribute.KeyValue, error)
}
func MarshalOtelAttributes(v interface{}) ([]attribute.KeyValue, error) {
if m, ok := v.(Marshaler); ok {
return m.MarshalOtelAttributes()
}
rv := reflect.ValueOf(v)
return marshalOtelAttributes(rv)
}
func marshalOtelAttributes(rv reflect.Value) ([]attribute.KeyValue, error) {
switch rv.Kind() {
case reflect.Struct:
return marshalStruct(rv)
case reflect.Ptr:
return marshalOtelAttributes(rv.Elem())
case reflect.Interface:
return marshalOtelAttributes(rv.Elem())
case reflect.Map:
return marshalMap(rv)
default:
return nil, fmt.Errorf("unsupported type %s", rv.Type())
}
}
func marshalMap(rv reflect.Value) ([]attribute.KeyValue, error) {
keys := rv.MapKeys()
if len(keys) == 0 {
return []attribute.KeyValue{}, nil
}
if keys[0].Kind() != reflect.String {
return nil, fmt.Errorf("unsupport map key type %s", keys[0].Type())
}
attrs := make([]attribute.KeyValue, 0, len(keys))
for index, key := range keys {
mv := rv.MapIndex(key)
keyString := key.String()
switch value := mv.Interface().(type) {
case bool:
attrs = append(attrs, attribute.Bool(keyString, value))
case int:
attrs = append(attrs, attribute.Int64(keyString, (int64)(value)))
case int8:
attrs = append(attrs, attribute.Int64(keyString, (int64)(value)))
case int16:
attrs = append(attrs, attribute.Int64(keyString, (int64)(value)))
case int32:
attrs = append(attrs, attribute.Int64(keyString, (int64)(value)))
case int64:
attrs = append(attrs, attribute.Int64(keyString, value))
case uint:
attrs = append(attrs, attribute.Int64(keyString, (int64)(value)))
case uint8:
attrs = append(attrs, attribute.Int64(keyString, (int64)(value)))
case uint16:
attrs = append(attrs, attribute.Int64(keyString, (int64)(value)))
case uint32:
attrs = append(attrs, attribute.Int64(keyString, (int64)(value)))
case uint64:
attrs = append(attrs, attribute.Int64(keyString, (int64)(value)))
case float32:
attrs = append(attrs, attribute.Float64(keyString, (float64)(value)))
case float64:
attrs = append(attrs, attribute.Float64(keyString, value))
case string:
attrs = append(attrs, attribute.String(keyString, value))
case []bool:
attrs = append(attrs, attribute.BoolSlice(keyString, value))
case []int:
s := make([]int64, len(value))
for i, v := range value {
s[i] = int64(v)
}
attrs = append(attrs, attribute.Int64Slice(keyString, s))
case []int8:
s := make([]int64, len(value))
for i, v := range value {
s[i] = int64(v)
}
attrs = append(attrs, attribute.Int64Slice(keyString, s))
case []int16:
s := make([]int64, len(value))
for i, v := range value {
s[i] = int64(v)
}
attrs = append(attrs, attribute.Int64Slice(keyString, s))
case []int32:
s := make([]int64, len(value))
for i, v := range value {
s[i] = int64(v)
}
attrs = append(attrs, attribute.Int64Slice(keyString, s))
case []int64:
attrs = append(attrs, attribute.Int64Slice(keyString, value))
case []uint:
s := make([]int64, len(value))
for i, v := range value {
s[i] = int64(v)
}
attrs = append(attrs, attribute.Int64Slice(keyString, s))
case []uint8:
s := make([]int64, len(value))
for i, v := range value {
s[i] = int64(v)
}
attrs = append(attrs, attribute.Int64Slice(keyString, s))
case []uint16:
s := make([]int64, len(value))
for i, v := range value {
s[i] = int64(v)
}
attrs = append(attrs, attribute.Int64Slice(keyString, s))
case []uint32:
s := make([]int64, len(value))
for i, v := range value {
s[i] = int64(v)
}
attrs = append(attrs, attribute.Int64Slice(keyString, s))
case []uint64:
s := make([]int64, len(value))
for i, v := range value {
s[i] = int64(v)
}
attrs = append(attrs, attribute.Int64Slice(keyString, s))
case []float32:
s := make([]float64, len(value))
for i, v := range value {
s[i] = float64(v)
}
attrs = append(attrs, attribute.Float64Slice(keyString, s))
case []float64:
attrs = append(attrs, attribute.Float64Slice(keyString, value))
case []string:
attrs = append(attrs, attribute.StringSlice(keyString, value))
default:
kvs, err := marshalField(structFiled{
attributeName: keyString,
filedIndex: index,
attributePrefix: keyString + ".",
}, reflect.ValueOf(value))
if err != nil {
return nil, err
}
attrs = append(attrs, kvs...)
}
}
return attrs, nil
}
func marshalStruct(rv reflect.Value) ([]attribute.KeyValue, error) {
t := rv.Type()
fields := getStructFields(t)
kvs := make([]attribute.KeyValue, 0, len(fields))
for _, f := range fields {
fv := rv.Field(f.filedIndex)
if f.omitEmpty && isEmptyValue(fv) {
continue
}
filedValue, err := marshalField(f, fv)
if err != nil {
return nil, err
}
kvs = append(kvs, filedValue...)
}
return kvs, nil
}
func marshalField(f structFiled, fv reflect.Value) ([]attribute.KeyValue, error) {
switch fv.Kind() {
case reflect.Bool:
return []attribute.KeyValue{attribute.Bool(f.attributeName, fv.Bool())}, nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return []attribute.KeyValue{attribute.Int64(f.attributeName, fv.Int())}, nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return []attribute.KeyValue{attribute.Int64(f.attributeName, int64(fv.Uint()))}, nil
case reflect.Float32, reflect.Float64:
return []attribute.KeyValue{attribute.Float64(f.attributeName, fv.Float())}, nil
case reflect.String:
return []attribute.KeyValue{attribute.String(f.attributeName, fv.String())}, nil
case reflect.Slice, reflect.Array:
return marshalSlice(f, fv)
case reflect.Struct:
kvs, err := MarshalOtelAttributes(fv.Interface())
if err != nil {
return nil, err
}
for i := range kvs {
kvs[i].Key = attribute.Key(f.attributePrefix) + kvs[i].Key
}
return kvs, nil
case reflect.Ptr:
if fv.IsNil() {
return nil, nil
}
return marshalField(f, fv.Elem())
default:
bs, err := json.Marshal(fv.Interface())
if err != nil {
return []attribute.KeyValue{}, err
}
return []attribute.KeyValue{attribute.String(f.attributeName, string(bs))}, nil
}
}
func marshalSlice(f structFiled, fv reflect.Value) ([]attribute.KeyValue, error) {
switch fv.Type().Elem().Kind() {
case reflect.Bool:
return []attribute.KeyValue{attribute.BoolSlice(f.attributeName, reflectValueToSlice[bool](fv))}, nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return []attribute.KeyValue{attribute.Int64Slice(f.attributeName, reflectValueToSlice[int64](fv))}, nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return []attribute.KeyValue{attribute.Int64Slice(f.attributeName, reflectValueToSlice[int64](fv))}, nil
case reflect.Float32, reflect.Float64:
return []attribute.KeyValue{attribute.Float64Slice(f.attributeName, reflectValueToSlice[float64](fv))}, nil
case reflect.String:
return []attribute.KeyValue{attribute.StringSlice(f.attributeName, reflectValueToSlice[string](fv))}, nil
default:
bs, err := json.Marshal(fv.Interface())
if err != nil {
return []attribute.KeyValue{}, err
}
return []attribute.KeyValue{attribute.String(f.attributeName, string(bs))}, nil
}
}
func reflectValueToSlice[T any](v reflect.Value) []T {
slice := make([]T, v.Len())
var zero T
rt := reflect.TypeOf(zero)
for i := 0; i < v.Len(); i++ {
slice[i] = v.Index(i).Convert(rt).Interface().(T)
}
return slice
}
func isEmptyValue(v reflect.Value) bool {
switch v.Kind() {
case reflect.Bool:
return !v.Bool()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return v.Int() == 0
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return v.Uint() == 0
case reflect.Float32, reflect.Float64:
return v.Float() == 0
case reflect.String:
return v.String() == ""
case reflect.Slice:
return v.Len() == 0
default:
return false
}
}