File size: 7,155 Bytes
d6f631f | 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 | package meter_test
import (
"testing"
"time"
"github.com/oklog/ulid/v2"
"github.com/samber/lo"
"github.com/stretchr/testify/assert"
"github.com/openmeterio/openmeter/openmeter/meter"
"github.com/openmeterio/openmeter/pkg/models"
)
func TestParseEvent(t *testing.T) {
meterSum := meter.Meter{
ManagedResource: models.ManagedResource{
ID: ulid.Make().String(),
NamespacedModel: models.NamespacedModel{
Namespace: "default",
},
ManagedModel: models.ManagedModel{
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
},
Name: "Test meter",
},
Key: "m1",
Aggregation: meter.MeterAggregationSum,
EventType: "api-calls",
ValueProperty: lo.ToPtr("$.duration_ms"),
GroupBy: map[string]string{
"method": "$.method",
"path": "$.path",
},
}
meterCount := meter.Meter{
ManagedResource: models.ManagedResource{
ID: ulid.Make().String(),
NamespacedModel: models.NamespacedModel{
Namespace: "default",
},
ManagedModel: models.ManagedModel{
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
},
Name: "Test meter",
},
Key: "m2",
Aggregation: meter.MeterAggregationCount,
EventType: "api-calls",
}
meterUniqueCount := meter.Meter{
ManagedResource: models.ManagedResource{
ID: ulid.Make().String(),
NamespacedModel: models.NamespacedModel{
Namespace: "default",
},
ManagedModel: models.ManagedModel{
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
},
Name: "Test meter",
},
Key: "m3",
Aggregation: meter.MeterAggregationUniqueCount,
EventType: "spans",
ValueProperty: lo.ToPtr("$.trace_id"),
}
tests := []struct {
description string
meter meter.Meter
data []byte
err error
errString string
value *float64
valueStr *string
groupBy map[string]string
}{
{
description: "should parse event",
meter: meterSum,
data: []byte(`{"duration_ms": 100, "method": "GET", "path": "/api/v1"}`),
value: lo.ToPtr(100.0),
groupBy: map[string]string{
"method": "GET",
"path": "/api/v1",
},
},
{
description: "should parse event with numeric string value",
meter: meterSum,
data: []byte(`{"duration_ms": "100", "method": "GET", "path": "/api/v1"}`),
value: lo.ToPtr(100.0),
groupBy: map[string]string{
"method": "GET",
"path": "/api/v1",
},
},
{
description: "should parse count as value one",
meter: meterCount,
data: []byte(`{}`),
value: lo.ToPtr(1.0),
groupBy: map[string]string{},
},
{
description: "should parse unique count as string",
meter: meterUniqueCount,
data: []byte(`{"trace_id": "test_trace_id"}`),
valueStr: lo.ToPtr("test_trace_id"),
groupBy: map[string]string{},
},
{
description: "should parse event with missing group by properties",
meter: meterSum,
data: []byte(`{"duration_ms": 100}`),
value: lo.ToPtr(100.0),
groupBy: map[string]string{
"method": "",
"path": "",
},
},
{
description: "should return error with invalid json",
meter: meterSum,
data: []byte(`{`),
err: meter.ErrInvalidEvent{},
errString: "invalid event: failed to parse event data: unexpected end of JSON input",
groupBy: map[string]string{},
},
{
description: "should return error with data missing",
meter: meterSum,
data: []byte(`null`),
err: meter.ErrInvalidEvent{},
errString: "invalid event: null and missing value property",
groupBy: map[string]string{
"method": "",
"path": "",
},
},
{
description: "should return error with data null",
meter: meterSum,
data: []byte(`null`),
err: meter.ErrInvalidEvent{},
errString: "invalid event: null and missing value property",
groupBy: map[string]string{
"method": "",
"path": "",
},
},
{
description: "should return error with value property not found",
meter: meterSum,
data: []byte(`{"method": "GET", "path": "/api/v1"}`),
err: meter.ErrInvalidEvent{},
errString: `invalid event: missing value property: "$.duration_ms"`,
groupBy: map[string]string{
"method": "GET",
"path": "/api/v1",
},
},
{
description: "should return error when value property is null",
meter: meterSum,
data: []byte(`{"duration_ms": null, "method": "GET", "path": "/api/v1"}`),
err: meter.ErrInvalidEvent{},
errString: "invalid event: value cannot be null",
groupBy: map[string]string{
"method": "GET",
"path": "/api/v1",
},
},
{
description: "should return error when value property is NaN",
meter: meterSum,
data: []byte(`{"duration_ms": "NaN", "method": "GET", "path": "/api/v1"}`),
err: meter.ErrInvalidEvent{},
errString: "invalid event: value cannot be NaN",
groupBy: map[string]string{
"method": "GET",
"path": "/api/v1",
},
},
{
description: "should return error when value property is infinity",
meter: meterSum,
data: []byte(`{"duration_ms": "Inf", "method": "GET", "path": "/api/v1"}`),
err: meter.ErrInvalidEvent{},
errString: "invalid event: value cannot be infinity",
groupBy: map[string]string{
"method": "GET",
"path": "/api/v1",
},
},
{
description: "should return error when value property is postiive infinity",
meter: meterSum,
data: []byte(`{"duration_ms": "+Inf", "method": "GET", "path": "/api/v1"}`),
err: meter.ErrInvalidEvent{},
errString: "invalid event: value cannot be infinity",
groupBy: map[string]string{
"method": "GET",
"path": "/api/v1",
},
},
{
description: "should return error when value property is negative infinity",
meter: meterSum,
data: []byte(`{"duration_ms": "-Inf", "method": "GET", "path": "/api/v1"}`),
err: meter.ErrInvalidEvent{},
errString: "invalid event: value cannot be infinity",
groupBy: map[string]string{
"method": "GET",
"path": "/api/v1",
},
},
{
description: "should return error when value property cannot be parsed as number",
meter: meterSum,
data: []byte(`{"duration_ms": "not a number", "method": "GET", "path": "/api/v1"}`),
err: meter.ErrInvalidMeter{},
errString: "invalid event: value cannot be parsed as float64: not a number",
groupBy: map[string]string{
"method": "GET",
"path": "/api/v1",
},
},
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
parsedEvent, err := meter.ParseEvent(test.meter, test.data)
if test.err == nil {
assert.NoError(t, err)
} else {
assert.ErrorAs(t, err, &test.err)
assert.EqualError(t, err, test.errString)
}
assert.Equal(t, test.value, parsedEvent.Value)
assert.Equal(t, test.valueStr, parsedEvent.ValueString)
assert.Equal(t, test.groupBy, parsedEvent.GroupBy)
})
}
}
|