-
Notifications
You must be signed in to change notification settings - Fork 5k
Expand file tree
/
Copy pathotelmap.go
More file actions
144 lines (135 loc) · 4.26 KB
/
Copy pathotelmap.go
File metadata and controls
144 lines (135 loc) · 4.26 KB
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
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
// Package otelmap provides utilities for converting between beats and otel map types.
package otelmap
import (
"encoding"
"encoding/json"
"fmt"
"reflect"
"time"
"github.com/elastic/beats/v7/libbeat/common"
"github.com/elastic/elastic-agent-libs/mapstr"
"go.opentelemetry.io/collector/pdata/pcommon"
)
// Allow ConvertNonPrimitive to be called recursively to handle nested maps of either type.
type mapstrOrMap interface {
mapstr.M | map[string]any
}
// ToMapstr converts a [pcommon.Map] to a [mapstr.M].
func ToMapstr(m pcommon.Map) mapstr.M {
return m.AsRaw()
}
// ConvertNonPrimitive handles the conversion of non-primitive data types to pcommon-primitive types.
// The conversion is performed in place.
// Notes:
// 1. Slices require special handling when converting a map[string]any to pcommon.Map.
// The pcommon.Map expects all slices to be of type []any.
// If you attempt to use other slice types (e.g., []string or []int),
// pcommon.Map.FromRaw(...) will return an "invalid type" error.
// To overcome this, we use "reflect" to transform []T into []any.
func ConvertNonPrimitive[T mapstrOrMap](m T) {
for key, val := range m {
switch x := val.(type) {
case mapstr.M:
ConvertNonPrimitive(x)
m[key] = map[string]any(x)
case []mapstr.M:
s := make([]any, len(x))
for i, val := range x {
ConvertNonPrimitive(val)
s[i] = map[string]any(val)
}
m[key] = s
case map[string]any:
ConvertNonPrimitive(x)
m[key] = x
case []map[string]any:
s := make([]any, len(x))
for i := range x {
ConvertNonPrimitive(x[i])
s[i] = x[i]
}
m[key] = s
case time.Time:
m[key] = x.UTC().Format("2006-01-02T15:04:05.000Z")
case common.Time:
m[key] = time.Time(x).UTC().Format("2006-01-02T15:04:05.000Z")
case []time.Time:
s := make([]any, 0, len(x))
for _, i := range x {
s = append(s, i.UTC().Format("2006-01-02T15:04:05.000Z"))
}
m[key] = s
case []common.Time:
s := make([]any, 0, len(x))
for _, i := range x {
s = append(s, time.Time(i).UTC().Format("2006-01-02T15:04:05.000Z"))
}
m[key] = s
case encoding.TextMarshaler:
text, err := x.MarshalText()
if err != nil {
m[key] = fmt.Sprintf("error converting %T to string: %s", x, err)
continue
}
m[key] = string(text)
case []bool, []string, []float32, []float64, []int, []int8, []int16, []int32, []int64,
[]uint, []uint8, []uint16, []uint32, []uint64:
ref := reflect.ValueOf(x)
s := make([]any, ref.Len())
for i := 0; i < ref.Len(); i++ {
s[i] = ref.Index(i).Interface()
}
m[key] = s
case nil, string, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, bool:
default:
ref := reflect.ValueOf(x)
if ref.Kind() == reflect.Struct {
var im map[string]any
err := marshalUnmarshal(x, &im)
if err != nil {
m[key] = fmt.Sprintf("error encoding struct to map: %s", err)
continue
}
ConvertNonPrimitive(im)
m[key] = im
break
}
if ref.Kind() == reflect.Slice || ref.Kind() == reflect.Array {
s := make([]any, ref.Len())
for i := 0; i < ref.Len(); i++ {
elem := ref.Index(i).Interface()
if mi, ok := elem.(map[string]any); ok {
ConvertNonPrimitive(mi)
s[i] = mi
} else if mi, ok := elem.(mapstr.M); ok {
ConvertNonPrimitive(mi)
s[i] = map[string]any(mi)
} else {
s[i] = elem
}
}
m[key] = s
break // we figured out the type, so we don't need the unknown type case
}
m[key] = fmt.Sprintf("unknown type: %T", x)
}
}
}
// marshalUnmarshal converts an interface to a mapstr.M by marshalling to JSON
// then unmarshalling the JSON object into a mapstr.M.
// Copied from libbeat/common/event.go
func marshalUnmarshal(in interface{}, out interface{}) error {
// Decode and encode as JSON to normalize the types.
marshaled, err := json.Marshal(in)
if err != nil {
return fmt.Errorf("error marshalling to JSON: %w", err)
}
err = json.Unmarshal(marshaled, out)
if err != nil {
return fmt.Errorf("error unmarshalling from JSON: %w", err)
}
return nil
}