-
Notifications
You must be signed in to change notification settings - Fork 779
otelconf: add unmarshaling and validation for span & cardinality limits #8043
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
MrAlias
merged 11 commits into
open-telemetry:main
from
codeboten:codeboten/otelconf-v1-limits
Oct 23, 2025
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
71e0d46
otelconf: add unmarshaling and validation for span & cardinality limits
codeboten 33f60b1
fix license headers
codeboten 75a73e8
cover case with invalid json/yaml
codeboten 0202729
add changelog
codeboten bbe296b
Merge branch 'main' into codeboten/otelconf-v1-limits
codeboten 5716b00
Merge branch 'main' into codeboten/otelconf-v1-limits
codeboten e609d21
apply review feedback
codeboten af6ca9c
for now bound is always zero, change if necessary in the future
codeboten 7bfa0ba
Merge branch 'main' into codeboten/otelconf-v1-limits
codeboten 0df5fc8
Update otelconf/config_common.go
codeboten fee3bbb
Merge branch 'main' into codeboten/otelconf-v1-limits
codeboten File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package otelconf // import "go.opentelemetry.io/contrib/otelconf" | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| ) | ||
|
|
||
| var ( | ||
| errUnmarshalingCardinalityLimits = errors.New("unmarshaling cardinality_limit") | ||
| errUnmarshalingSpanLimits = errors.New("unmarshaling span_limit") | ||
| ) | ||
|
|
||
| type errBound struct { | ||
| Field string | ||
| Bound int | ||
| Op string | ||
| } | ||
|
|
||
| func (e *errBound) Error() string { | ||
| return fmt.Sprintf("field %s: must be %s %d", e.Field, e.Op, e.Bound) | ||
| } | ||
|
|
||
| func (e *errBound) Is(target error) bool { | ||
| t, ok := target.(*errBound) | ||
| if !ok { | ||
| return false | ||
| } | ||
| return e.Field == t.Field && e.Bound == t.Bound && e.Op == t.Op | ||
| } | ||
|
|
||
| // newErrGreaterOrEqualZero creates a new error indicating that the field must be greater than | ||
| // or equal to zero. | ||
| func newErrGreaterOrEqualZero(field string) error { | ||
| return &errBound{Field: field, Bound: 0, Op: ">="} | ||
| } | ||
|
|
||
| // newErrGreaterThanZero creates a new error indicating that the field must be greater | ||
| // than zero. | ||
| func newErrGreaterThanZero(field string) error { | ||
| return &errBound{Field: field, Bound: 0, Op: ">"} | ||
| } | ||
|
|
||
| // validateCardinalityLimits handles validation for CardinalityLimits. | ||
| func validateCardinalityLimits(plain *CardinalityLimits) error { | ||
| if plain.Counter != nil && 0 >= *plain.Counter { | ||
| return newErrGreaterThanZero("counter") | ||
| } | ||
| if plain.Default != nil && 0 >= *plain.Default { | ||
| return newErrGreaterThanZero("default") | ||
| } | ||
| if plain.Gauge != nil && 0 >= *plain.Gauge { | ||
| return newErrGreaterThanZero("gauge") | ||
| } | ||
| if plain.Histogram != nil && 0 >= *plain.Histogram { | ||
| return newErrGreaterThanZero("histogram") | ||
| } | ||
| if plain.ObservableCounter != nil && 0 >= *plain.ObservableCounter { | ||
| return newErrGreaterThanZero("observable_counter") | ||
| } | ||
| if plain.ObservableGauge != nil && 0 >= *plain.ObservableGauge { | ||
| return newErrGreaterThanZero("observable_gauge") | ||
| } | ||
| if plain.ObservableUpDownCounter != nil && 0 >= *plain.ObservableUpDownCounter { | ||
| return newErrGreaterThanZero("observable_up_down_counter") | ||
| } | ||
| if plain.UpDownCounter != nil && 0 >= *plain.UpDownCounter { | ||
| return newErrGreaterThanZero("up_down_counter") | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // validateSpanLimits handles validation for SpanLimits. | ||
| func validateSpanLimits(plain *SpanLimits) error { | ||
| if plain.AttributeCountLimit != nil && 0 > *plain.AttributeCountLimit { | ||
| return newErrGreaterOrEqualZero("attribute_count_limit") | ||
| } | ||
| if plain.AttributeValueLengthLimit != nil && 0 > *plain.AttributeValueLengthLimit { | ||
| return newErrGreaterOrEqualZero("attribute_value_length_limit") | ||
| } | ||
| if plain.EventAttributeCountLimit != nil && 0 > *plain.EventAttributeCountLimit { | ||
| return newErrGreaterOrEqualZero("event_attribute_count_limit") | ||
| } | ||
| if plain.EventCountLimit != nil && 0 > *plain.EventCountLimit { | ||
| return newErrGreaterOrEqualZero("event_count_limit") | ||
| } | ||
| if plain.LinkAttributeCountLimit != nil && 0 > *plain.LinkAttributeCountLimit { | ||
| return newErrGreaterOrEqualZero("link_attribute_count_limit") | ||
| } | ||
| if plain.LinkCountLimit != nil && 0 > *plain.LinkCountLimit { | ||
| return newErrGreaterOrEqualZero("link_count_limit") | ||
| } | ||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package otelconf // import "go.opentelemetry.io/contrib/otelconf" | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "errors" | ||
| ) | ||
|
|
||
| // UnmarshalJSON implements json.Unmarshaler. | ||
| func (j *CardinalityLimits) UnmarshalJSON(value []byte) error { | ||
| type Plain CardinalityLimits | ||
| var plain Plain | ||
| if err := json.Unmarshal(value, &plain); err != nil { | ||
| return errors.Join(errUnmarshalingCardinalityLimits, err) | ||
| } | ||
| if err := validateCardinalityLimits((*CardinalityLimits)(&plain)); err != nil { | ||
| return err | ||
| } | ||
| *j = CardinalityLimits(plain) | ||
| return nil | ||
| } | ||
|
|
||
| // UnmarshalJSON implements json.Unmarshaler. | ||
| func (j *SpanLimits) UnmarshalJSON(value []byte) error { | ||
| type Plain SpanLimits | ||
| var plain Plain | ||
| if err := json.Unmarshal(value, &plain); err != nil { | ||
| return errors.Join(errUnmarshalingSpanLimits, err) | ||
| } | ||
| if err := validateSpanLimits((*SpanLimits)(&plain)); err != nil { | ||
| return err | ||
| } | ||
| *j = SpanLimits(plain) | ||
| return nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,225 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package otelconf | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "go.yaml.in/yaml/v3" | ||
| ) | ||
|
|
||
| func TestUnmarshalCardinalityLimits(t *testing.T) { | ||
| for _, tt := range []struct { | ||
| name string | ||
| yamlConfig []byte | ||
| jsonConfig []byte | ||
| wantErrT error | ||
| }{ | ||
| { | ||
| name: "valid with all fields positive", | ||
| jsonConfig: []byte(`{"counter":100,"default":200,"gauge":300,"histogram":400,"observable_counter":500,"observable_gauge":600,"observable_up_down_counter":700,"up_down_counter":800}`), | ||
| yamlConfig: []byte("counter: 100\ndefault: 200\ngauge: 300\nhistogram: 400\nobservable_counter: 500\nobservable_gauge: 600\nobservable_up_down_counter: 700\nup_down_counter: 800"), | ||
| }, | ||
| { | ||
| name: "valid with single field", | ||
| jsonConfig: []byte(`{"default":2000}`), | ||
| yamlConfig: []byte("default: 2000"), | ||
| }, | ||
| { | ||
| name: "valid empty", | ||
| jsonConfig: []byte(`{}`), | ||
| yamlConfig: []byte("{}"), | ||
| }, | ||
| { | ||
| name: "invalid data", | ||
| jsonConfig: []byte(`{:2000}`), | ||
| yamlConfig: []byte("counter: !!str 2000"), | ||
| wantErrT: errUnmarshalingCardinalityLimits, | ||
| }, | ||
| { | ||
| name: "invalid counter zero", | ||
| jsonConfig: []byte(`{"counter":0}`), | ||
| yamlConfig: []byte("counter: 0"), | ||
| wantErrT: newErrGreaterThanZero("counter"), | ||
| }, | ||
| { | ||
| name: "invalid counter negative", | ||
| jsonConfig: []byte(`{"counter":-1}`), | ||
| yamlConfig: []byte("counter: -1"), | ||
| wantErrT: newErrGreaterThanZero("counter"), | ||
| }, | ||
| { | ||
| name: "invalid default zero", | ||
| jsonConfig: []byte(`{"default":0}`), | ||
| yamlConfig: []byte("default: 0"), | ||
| wantErrT: newErrGreaterThanZero("default"), | ||
| }, | ||
| { | ||
| name: "invalid default negative", | ||
| jsonConfig: []byte(`{"default":-1}`), | ||
| yamlConfig: []byte("default: -1"), | ||
| wantErrT: newErrGreaterThanZero("default"), | ||
| }, | ||
| { | ||
| name: "invalid gauge zero", | ||
| jsonConfig: []byte(`{"gauge":0}`), | ||
| yamlConfig: []byte("gauge: 0"), | ||
| wantErrT: newErrGreaterThanZero("gauge"), | ||
| }, | ||
| { | ||
| name: "invalid gauge negative", | ||
| jsonConfig: []byte(`{"gauge":-1}`), | ||
| yamlConfig: []byte("gauge: -1"), | ||
| wantErrT: newErrGreaterThanZero("gauge"), | ||
| }, | ||
| { | ||
| name: "invalid histogram zero", | ||
| jsonConfig: []byte(`{"histogram":0}`), | ||
| yamlConfig: []byte("histogram: 0"), | ||
| wantErrT: newErrGreaterThanZero("histogram"), | ||
| }, | ||
| { | ||
| name: "invalid histogram negative", | ||
| jsonConfig: []byte(`{"histogram":-1}`), | ||
| yamlConfig: []byte("histogram: -1"), | ||
| wantErrT: newErrGreaterThanZero("histogram"), | ||
| }, | ||
| { | ||
| name: "invalid observable_counter zero", | ||
| jsonConfig: []byte(`{"observable_counter":0}`), | ||
| yamlConfig: []byte("observable_counter: 0"), | ||
| wantErrT: newErrGreaterThanZero("observable_counter"), | ||
| }, | ||
| { | ||
| name: "invalid observable_counter negative", | ||
| jsonConfig: []byte(`{"observable_counter":-1}`), | ||
| yamlConfig: []byte("observable_counter: -1"), | ||
| wantErrT: newErrGreaterThanZero("observable_counter"), | ||
| }, | ||
| { | ||
| name: "invalid observable_gauge zero", | ||
| jsonConfig: []byte(`{"observable_gauge":0}`), | ||
| yamlConfig: []byte("observable_gauge: 0"), | ||
| wantErrT: newErrGreaterThanZero("observable_gauge"), | ||
| }, | ||
| { | ||
| name: "invalid observable_gauge negative", | ||
| jsonConfig: []byte(`{"observable_gauge":-1}`), | ||
| yamlConfig: []byte("observable_gauge: -1"), | ||
| wantErrT: newErrGreaterThanZero("observable_gauge"), | ||
| }, | ||
| { | ||
| name: "invalid observable_up_down_counter zero", | ||
| jsonConfig: []byte(`{"observable_up_down_counter":0}`), | ||
| yamlConfig: []byte("observable_up_down_counter: 0"), | ||
| wantErrT: newErrGreaterThanZero("observable_up_down_counter"), | ||
| }, | ||
| { | ||
| name: "invalid observable_up_down_counter negative", | ||
| jsonConfig: []byte(`{"observable_up_down_counter":-1}`), | ||
| yamlConfig: []byte("observable_up_down_counter: -1"), | ||
| wantErrT: newErrGreaterThanZero("observable_up_down_counter"), | ||
| }, | ||
| { | ||
| name: "invalid up_down_counter zero", | ||
| jsonConfig: []byte(`{"up_down_counter":0}`), | ||
| yamlConfig: []byte("up_down_counter: 0"), | ||
| wantErrT: newErrGreaterThanZero("up_down_counter"), | ||
| }, | ||
| { | ||
| name: "invalid up_down_counter negative", | ||
| jsonConfig: []byte(`{"up_down_counter":-1}`), | ||
| yamlConfig: []byte("up_down_counter: -1"), | ||
| wantErrT: newErrGreaterThanZero("up_down_counter"), | ||
| }, | ||
| } { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| cl := CardinalityLimits{} | ||
| err := cl.UnmarshalJSON(tt.jsonConfig) | ||
| assert.ErrorIs(t, err, tt.wantErrT) | ||
|
|
||
| cl = CardinalityLimits{} | ||
| err = yaml.Unmarshal(tt.yamlConfig, &cl) | ||
| assert.ErrorIs(t, err, tt.wantErrT) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestUnmarshalSpanLimits(t *testing.T) { | ||
| for _, tt := range []struct { | ||
| name string | ||
| yamlConfig []byte | ||
| jsonConfig []byte | ||
| wantErrT error | ||
| }{ | ||
| { | ||
| name: "valid with all fields positive", | ||
| 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}`), | ||
| 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"), | ||
| }, | ||
| { | ||
| name: "valid with single field", | ||
| jsonConfig: []byte(`{"attribute_value_length_limit":2000}`), | ||
| yamlConfig: []byte("attribute_value_length_limit: 2000"), | ||
| }, | ||
| { | ||
| name: "valid empty", | ||
| jsonConfig: []byte(`{}`), | ||
| yamlConfig: []byte("{}"), | ||
| }, | ||
| { | ||
| name: "invalid data", | ||
| jsonConfig: []byte(`{:2000}`), | ||
| yamlConfig: []byte("attribute_count_limit: !!str 2000"), | ||
| wantErrT: errUnmarshalingSpanLimits, | ||
| }, | ||
| { | ||
| name: "invalid attribute_count_limit negative", | ||
| jsonConfig: []byte(`{"attribute_count_limit":-1}`), | ||
| yamlConfig: []byte("attribute_count_limit: -1"), | ||
| wantErrT: newErrGreaterOrEqualZero("attribute_count_limit"), | ||
| }, | ||
| { | ||
| name: "invalid attribute_value_length_limit negative", | ||
| jsonConfig: []byte(`{"attribute_value_length_limit":-1}`), | ||
| yamlConfig: []byte("attribute_value_length_limit: -1"), | ||
| wantErrT: newErrGreaterOrEqualZero("attribute_value_length_limit"), | ||
| }, | ||
| { | ||
| name: "invalid event_attribute_count_limit negative", | ||
| jsonConfig: []byte(`{"event_attribute_count_limit":-1}`), | ||
| yamlConfig: []byte("event_attribute_count_limit: -1"), | ||
| wantErrT: newErrGreaterOrEqualZero("event_attribute_count_limit"), | ||
| }, | ||
| { | ||
| name: "invalid event_count_limit negative", | ||
| jsonConfig: []byte(`{"event_count_limit":-1}`), | ||
| yamlConfig: []byte("event_count_limit: -1"), | ||
| wantErrT: newErrGreaterOrEqualZero("event_count_limit"), | ||
| }, | ||
| { | ||
| name: "invalid link_attribute_count_limit negative", | ||
| jsonConfig: []byte(`{"link_attribute_count_limit":-1}`), | ||
| yamlConfig: []byte("link_attribute_count_limit: -1"), | ||
| wantErrT: newErrGreaterOrEqualZero("link_attribute_count_limit"), | ||
| }, | ||
| { | ||
| name: "invalid link_count_limit negative", | ||
| jsonConfig: []byte(`{"link_count_limit":-1}`), | ||
| yamlConfig: []byte("link_count_limit: -1"), | ||
| wantErrT: newErrGreaterOrEqualZero("link_count_limit"), | ||
| }, | ||
| } { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| cl := SpanLimits{} | ||
| err := cl.UnmarshalJSON(tt.jsonConfig) | ||
| assert.ErrorIs(t, err, tt.wantErrT) | ||
|
|
||
| cl = SpanLimits{} | ||
| err = yaml.Unmarshal(tt.yamlConfig, &cl) | ||
| assert.ErrorIs(t, err, tt.wantErrT) | ||
| }) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.