Skip to content

Commit ea91409

Browse files
committed
add validation/tests for SpanLimits
Signed-off-by: alex boten <[email protected]>
1 parent d93822d commit ea91409

File tree

4 files changed

+133
-0
lines changed

4 files changed

+133
-0
lines changed

otelconf/v1.0.0-rc.1/config_common.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,26 @@ func validateCardinalityLimits(plain *CardinalityLimits) error {
218218
}
219219
return nil
220220
}
221+
222+
// validateSpanLimits handles validation for SpanLimits.
223+
func validateSpanLimits(plain *SpanLimits) error {
224+
if plain.AttributeCountLimit != nil && 0 > *plain.AttributeCountLimit {
225+
return fmt.Errorf("field %s: must be >= %v", "attribute_count_limit", 0)
226+
}
227+
if plain.AttributeValueLengthLimit != nil && 0 > *plain.AttributeValueLengthLimit {
228+
return fmt.Errorf("field %s: must be >= %v", "attribute_value_length_limit", 0)
229+
}
230+
if plain.EventAttributeCountLimit != nil && 0 > *plain.EventAttributeCountLimit {
231+
return fmt.Errorf("field %s: must be >= %v", "event_attribute_count_limit", 0)
232+
}
233+
if plain.EventCountLimit != nil && 0 > *plain.EventCountLimit {
234+
return fmt.Errorf("field %s: must be >= %v", "event_count_limit", 0)
235+
}
236+
if plain.LinkAttributeCountLimit != nil && 0 > *plain.LinkAttributeCountLimit {
237+
return fmt.Errorf("field %s: must be >= %v", "link_attribute_count_limit", 0)
238+
}
239+
if plain.LinkCountLimit != nil && 0 > *plain.LinkCountLimit {
240+
return fmt.Errorf("field %s: must be >= %v", "link_count_limit", 0)
241+
}
242+
return nil
243+
}

otelconf/v1.0.0-rc.1/config_json.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,7 @@ func (j *TextMapPropagator) UnmarshalJSON(b []byte) error {
623623
return nil
624624
}
625625

626+
// UnmarshalJSON implements json.Unmarshaler.
626627
func (j *CardinalityLimits) UnmarshalJSON(value []byte) error {
627628
type Plain CardinalityLimits
628629
var plain Plain
@@ -635,3 +636,17 @@ func (j *CardinalityLimits) UnmarshalJSON(value []byte) error {
635636
*j = CardinalityLimits(plain)
636637
return nil
637638
}
639+
640+
// UnmarshalJSON implements json.Unmarshaler.
641+
func (j *SpanLimits) UnmarshalJSON(value []byte) error {
642+
type Plain SpanLimits
643+
var plain Plain
644+
if err := json.Unmarshal(value, &plain); err != nil {
645+
return err
646+
}
647+
if err := validateSpanLimits((*SpanLimits)(&plain)); err != nil {
648+
return err
649+
}
650+
*j = SpanLimits(plain)
651+
return nil
652+
}

otelconf/v1.0.0-rc.1/config_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,3 +1299,83 @@ func TestUnmarshalCardinalityLimits(t *testing.T) {
12991299
})
13001300
}
13011301
}
1302+
1303+
func TestUnmarshalSpanLimits(t *testing.T) {
1304+
for _, tt := range []struct {
1305+
name string
1306+
yamlConfig []byte
1307+
jsonConfig []byte
1308+
wantErr string
1309+
}{
1310+
{
1311+
name: "valid with all fields positive",
1312+
jsonConfig: []byte(`{"attribute_count_limit":100,"attribute_value_length_limit":200,"event_attribute_count_limit":300,"event_count_limit":400,"link_attribute_count_limit":500,"link_count_limit":600}`),
1313+
yamlConfig: []byte("attribute_count_limit: 100\nattribute_value_length_limit: 200\nevent_attribute_count_limit: 300\nevent_count_limit: 400\nlink_attribute_count_limit: 500\nlink_count_limit: 600"),
1314+
},
1315+
{
1316+
name: "valid with single field",
1317+
jsonConfig: []byte(`{"attribute_value_length_limit":2000}`),
1318+
yamlConfig: []byte("attribute_value_length_limit: 2000"),
1319+
},
1320+
{
1321+
name: "valid empty",
1322+
jsonConfig: []byte(`{}`),
1323+
yamlConfig: []byte("{}"),
1324+
},
1325+
{
1326+
name: "invalid attribute_count_limit negative",
1327+
jsonConfig: []byte(`{"attribute_count_limit":-1}`),
1328+
yamlConfig: []byte("attribute_count_limit: -1"),
1329+
wantErr: "field attribute_count_limit: must be >= 0",
1330+
},
1331+
{
1332+
name: "invalid attribute_value_length_limit negative",
1333+
jsonConfig: []byte(`{"attribute_value_length_limit":-1}`),
1334+
yamlConfig: []byte("attribute_value_length_limit: -1"),
1335+
wantErr: "field attribute_value_length_limit: must be >= 0",
1336+
},
1337+
{
1338+
name: "invalid event_attribute_count_limit negative",
1339+
jsonConfig: []byte(`{"event_attribute_count_limit":-1}`),
1340+
yamlConfig: []byte("event_attribute_count_limit: -1"),
1341+
wantErr: "field event_attribute_count_limit: must be >= 0",
1342+
},
1343+
{
1344+
name: "invalid event_count_limit negative",
1345+
jsonConfig: []byte(`{"event_count_limit":-1}`),
1346+
yamlConfig: []byte("event_count_limit: -1"),
1347+
wantErr: "field event_count_limit: must be >= 0",
1348+
},
1349+
{
1350+
name: "invalid link_attribute_count_limit negative",
1351+
jsonConfig: []byte(`{"link_attribute_count_limit":-1}`),
1352+
yamlConfig: []byte("link_attribute_count_limit: -1"),
1353+
wantErr: "field link_attribute_count_limit: must be >= 0",
1354+
},
1355+
{
1356+
name: "invalid link_count_limit negative",
1357+
jsonConfig: []byte(`{"link_count_limit":-1}`),
1358+
yamlConfig: []byte("link_count_limit: -1"),
1359+
wantErr: "field link_count_limit: must be >= 0",
1360+
},
1361+
} {
1362+
t.Run(tt.name, func(t *testing.T) {
1363+
cl := SpanLimits{}
1364+
err := cl.UnmarshalJSON(tt.jsonConfig)
1365+
if tt.wantErr != "" {
1366+
require.Error(t, err)
1367+
require.Contains(t, err.Error(), tt.wantErr)
1368+
} else {
1369+
require.NoError(t, err)
1370+
}
1371+
cl = SpanLimits{}
1372+
err = yaml.Unmarshal(tt.yamlConfig, &cl)
1373+
if tt.wantErr != "" {
1374+
require.Error(t, err)
1375+
require.Contains(t, err.Error(), tt.wantErr)
1376+
} else {
1377+
require.NoError(t, err)
1378+
}
1379+
})
1380+
}
1381+
}

otelconf/v1.0.0-rc.1/config_yaml.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ func (j *BatchSpanProcessor) UnmarshalYAML(node *yaml.Node) error {
326326
return nil
327327
}
328328

329+
// UnmarshalYAML implements yaml.Unmarshaler.
329330
func (j *CardinalityLimits) UnmarshalYAML(node *yaml.Node) error {
330331
type Plain CardinalityLimits
331332
var plain Plain
@@ -338,3 +339,17 @@ func (j *CardinalityLimits) UnmarshalYAML(node *yaml.Node) error {
338339
*j = CardinalityLimits(plain)
339340
return nil
340341
}
342+
343+
// UnmarshalYAML implements yaml.Unmarshaler.
344+
func (j *SpanLimits) UnmarshalYAML(node *yaml.Node) error {
345+
type Plain SpanLimits
346+
var plain Plain
347+
if err := node.Decode(&plain); err != nil {
348+
return err
349+
}
350+
if err := validateSpanLimits((*SpanLimits)(&plain)); err != nil {
351+
return err
352+
}
353+
*j = SpanLimits(plain)
354+
return nil
355+
}

0 commit comments

Comments
 (0)