|
| 1 | +package telemetry |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + "go.opentelemetry.io/otel/attribute" |
| 10 | +) |
| 11 | + |
| 12 | +var attributeTestCases = []struct { |
| 13 | + Name string |
| 14 | + JSON string |
| 15 | + KVs []attribute.KeyValue |
| 16 | +}{ |
| 17 | + { |
| 18 | + Name: "bool", |
| 19 | + JSON: `{"enabled": true, "is_this_thing_on": false}`, |
| 20 | + KVs: []attribute.KeyValue{ |
| 21 | + attribute.Bool("enabled", true), |
| 22 | + attribute.Bool("is_this_thing_on", false), |
| 23 | + }, |
| 24 | + }, |
| 25 | + { |
| 26 | + Name: "int64", |
| 27 | + JSON: `{"age": 42, "zero": 0, "bigger_than_int32": 2147483650}`, |
| 28 | + KVs: []attribute.KeyValue{ |
| 29 | + attribute.Int("age", 42), |
| 30 | + attribute.Int("zero", 0), |
| 31 | + attribute.Int("bigger_than_int32", 2147483650), |
| 32 | + }, |
| 33 | + }, |
| 34 | + { |
| 35 | + Name: "float64", |
| 36 | + JSON: `{"pi": 3.141592653589793, "negative": -1.234567}`, |
| 37 | + KVs: []attribute.KeyValue{ |
| 38 | + attribute.Float64("pi", 3.141592653589793), |
| 39 | + attribute.Float64("negative", -1.234567), |
| 40 | + }, |
| 41 | + }, |
| 42 | + { |
| 43 | + Name: "string", |
| 44 | + JSON: `{"name": "Boz", "empty": ""}`, |
| 45 | + KVs: []attribute.KeyValue{ |
| 46 | + attribute.String("name", "Boz"), |
| 47 | + attribute.String("empty", ""), |
| 48 | + }, |
| 49 | + }, |
| 50 | + { |
| 51 | + Name: "bool slice", |
| 52 | + JSON: `{"flags": [true, false, false, true]}`, |
| 53 | + KVs: []attribute.KeyValue{ |
| 54 | + attribute.BoolSlice("flags", []bool{true, false, false, true}), |
| 55 | + }, |
| 56 | + }, |
| 57 | + { |
| 58 | + Name: "int64 slice", |
| 59 | + JSON: `{"lotto": [12, 17, 46]}`, |
| 60 | + KVs: []attribute.KeyValue{ |
| 61 | + attribute.IntSlice("lotto", []int{12, 17, 46}), |
| 62 | + }, |
| 63 | + }, |
| 64 | + { |
| 65 | + Name: "float64 slice", |
| 66 | + JSON: `{"coordinates": [51.477928, -0.001545], "mixed": [1, 2, 3, 4.5]}`, |
| 67 | + KVs: []attribute.KeyValue{ |
| 68 | + attribute.Float64Slice("coordinates", []float64{51.477928, -0.001545}), |
| 69 | + attribute.Float64Slice("mixed", []float64{1, 2, 3, 4.5}), |
| 70 | + }, |
| 71 | + }, |
| 72 | + { |
| 73 | + Name: "string slice", |
| 74 | + JSON: `{"hobbies": ["gardening", "fishing"], "empty": []}`, |
| 75 | + KVs: []attribute.KeyValue{ |
| 76 | + attribute.StringSlice("hobbies", []string{"gardening", "fishing"}), |
| 77 | + attribute.StringSlice("empty", []string{}), |
| 78 | + }, |
| 79 | + }, |
| 80 | +} |
| 81 | + |
| 82 | +func TestAttributesMarshalFunctional(t *testing.T) { |
| 83 | + x := struct { |
| 84 | + Attributes Attributes `json:"my_attrs"` |
| 85 | + }{ |
| 86 | + Attributes: Attributes([]attribute.KeyValue{ |
| 87 | + attribute.Bool("enabled", true), |
| 88 | + attribute.Int("age", 42), |
| 89 | + attribute.Float64("pi", 3.141592653589793), |
| 90 | + attribute.String("name", "Florp"), |
| 91 | + }), |
| 92 | + } |
| 93 | + |
| 94 | + out, err := json.Marshal(x) |
| 95 | + require.NoError(t, err) |
| 96 | + |
| 97 | + assert.JSONEq(t, `{ |
| 98 | + "my_attrs": { |
| 99 | + "enabled": true, |
| 100 | + "age": 42, |
| 101 | + "pi": 3.141592653589793, |
| 102 | + "name": "Florp" |
| 103 | + } |
| 104 | + }`, string(out)) |
| 105 | +} |
| 106 | + |
| 107 | +func TestAttributesMarshal(t *testing.T) { |
| 108 | + for _, tc := range attributeTestCases { |
| 109 | + t.Run(tc.Name, func(t *testing.T) { |
| 110 | + out, err := json.Marshal(Attributes(tc.KVs)) |
| 111 | + require.NoError(t, err) |
| 112 | + |
| 113 | + assert.JSONEq(t, tc.JSON, string(out)) |
| 114 | + }) |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +func TestAttributesUnmarshal(t *testing.T) { |
| 119 | + for _, tc := range attributeTestCases { |
| 120 | + t.Run(tc.Name, func(t *testing.T) { |
| 121 | + var attrs Attributes |
| 122 | + |
| 123 | + err := json.Unmarshal([]byte(tc.JSON), &attrs) |
| 124 | + require.NoError(t, err) |
| 125 | + |
| 126 | + assert.ElementsMatch(t, tc.KVs, attrs) |
| 127 | + }) |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +// For now, we want to ignore rather than choke on invalid types. |
| 132 | +func TestAttributesUnmarshalInvalidTypes(t *testing.T) { |
| 133 | + testCases := []struct { |
| 134 | + Name string |
| 135 | + JSON string |
| 136 | + }{ |
| 137 | + { |
| 138 | + Name: "null", |
| 139 | + JSON: `{"null": null}`, |
| 140 | + }, |
| 141 | + { |
| 142 | + Name: "empty map", |
| 143 | + JSON: `{"map": {}}`, |
| 144 | + }, |
| 145 | + { |
| 146 | + Name: "map with entries", |
| 147 | + JSON: `{"map": {"name": "Chigozie"}}`, |
| 148 | + }, |
| 149 | + { |
| 150 | + Name: "mixed type slice", |
| 151 | + JSON: `{"mixedup": [123, "eatmyshorts"]}`, |
| 152 | + }, |
| 153 | + { |
| 154 | + Name: "nested slice", |
| 155 | + JSON: `{"nested": [[1], [2], [3]]}`, |
| 156 | + }, |
| 157 | + } |
| 158 | + |
| 159 | + for _, tc := range testCases { |
| 160 | + t.Run(tc.Name, func(t *testing.T) { |
| 161 | + var attrs Attributes |
| 162 | + |
| 163 | + err := json.Unmarshal([]byte(tc.JSON), &attrs) |
| 164 | + require.NoError(t, err) |
| 165 | + |
| 166 | + assert.Empty(t, attrs) |
| 167 | + }) |
| 168 | + } |
| 169 | +} |
0 commit comments