-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add cardinality limiting to the metric SDK as an experimental feature #4457
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
Merged
Changes from 5 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
3bc9dcc
Add agg limiting func
MrAlias c8ce214
Add unit test for limitAttr
MrAlias c79f14e
Add limiting to aggregate types
MrAlias 6aa1640
Add internal x pkg for experimental feature-flagging
MrAlias cc11cb0
Connect cardinality limit to metric SDK
MrAlias 07dc9b9
Merge branch 'main' into cardinality-limit-x
MrAlias 62311eb
Replace limitAttr fn with limiter type
MrAlias b53c71c
Use x.CardinalityLimit directly
MrAlias 92efe65
Simplify limiter test
MrAlias 27df16c
Add limiter benchmark
MrAlias c2d5246
Document the AggregationLimit field
MrAlias 426b29d
Test sum limits
MrAlias 3a59317
Test limit for last value
MrAlias 0354e68
Test histogram limit
MrAlias f1f3be7
Refactor expo hist test to use existing fixtures
MrAlias 4874fcb
Test the ExponentialHistogram limit
MrAlias 3fa01f1
Fix lint
MrAlias ea4fe06
Add docs
MrAlias b022999
Merge branch 'main' into cardinality-limit-x
MrAlias 5bc739e
Rename aggregation field to aggLimit
MrAlias 320fee0
Merge branch 'main' into cardinality-limit-x
MrAlias a8fbbf7
Merge branch 'main' into cardinality-limit-x
MrAlias 41f950b
Merge branch 'main' into cardinality-limit-x
pellared 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
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
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
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 | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package aggregate // import "go.opentelemetry.io/otel/sdk/metric/internal/aggregate" | ||
|
|
||
| import "go.opentelemetry.io/otel/attribute" | ||
|
|
||
| // overflowSet is the attribute set used to record a measurement when adding | ||
| // another distinct attribute set to the aggregate would exceed the aggregate | ||
| // limit. | ||
| var overflowSet = attribute.NewSet(attribute.Bool("otel.metric.overflow", true)) | ||
|
|
||
| // limtAttr checks if adding a measurement for a will exceed the limit of the | ||
| // already measured values in m. If it will, overflowSet is returned. | ||
| // Otherwise, if it will not exceed the limit, or the limit is not set (limit | ||
| // <= 0), a is returned. | ||
| func limitAttr[V any](a attribute.Set, m map[attribute.Set]V, limit int) attribute.Set { | ||
| if limit > 0 { | ||
| _, exists := m[a] | ||
| if !exists && len(m) >= limit-1 { | ||
| return overflowSet | ||
| } | ||
| } | ||
|
|
||
| return a | ||
| } |
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,47 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package aggregate // import "go.opentelemetry.io/otel/sdk/metric/internal/aggregate" | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "go.opentelemetry.io/otel/attribute" | ||
| ) | ||
|
|
||
| func TestLimitAttr(t *testing.T) { | ||
| m := map[attribute.Set]struct{}{alice: {}} | ||
|
|
||
| t.Run("NoLimit", func(t *testing.T) { | ||
| assert.Equal(t, alice, limitAttr(alice, m, 0)) | ||
| assert.Equal(t, bob, limitAttr(bob, m, 0)) | ||
| }) | ||
|
|
||
| t.Run("NotAtLimit/Exists", func(t *testing.T) { | ||
| assert.Equal(t, alice, limitAttr(alice, m, 3)) | ||
| }) | ||
|
|
||
| t.Run("NotAtLimit/DoesNotExist", func(t *testing.T) { | ||
| assert.Equal(t, bob, limitAttr(bob, m, 3)) | ||
| }) | ||
|
|
||
| t.Run("AtLimit/Exists", func(t *testing.T) { | ||
| assert.Equal(t, alice, limitAttr(alice, m, 2)) | ||
| }) | ||
|
|
||
| t.Run("AtLimit/DoesNotExist", func(t *testing.T) { | ||
| assert.Equal(t, overflowSet, limitAttr(bob, m, 2)) | ||
| }) | ||
| } |
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,59 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| // Package x contains support for OTel metric SDK experimental features. | ||
| package x // import "go.opentelemetry.io/otel/sdk/metric/internal/x" | ||
|
|
||
| import ( | ||
| "os" | ||
| "strings" | ||
| ) | ||
|
|
||
| const EnvKeyRoot = "OTEL_GO_X_" | ||
|
|
||
| var ( | ||
| CardinalityLimit = Feature{ | ||
| EnvKeySuffix: "CARDINALITY_LIMIT", | ||
| // TODO: support accepting number values here to set the cardinality | ||
| // limit. | ||
| EnablementVals: []string{"true"}, | ||
| } | ||
| ) | ||
|
|
||
| type Feature struct { | ||
| // EnvKeySuffix is the environment variable key suffix the xFeature is | ||
| // stored at. It is assumed EnvKeyRoot is the base of the environment | ||
| // variable key. | ||
| EnvKeySuffix string | ||
| // EnablementVals are the case-insensitive comparison values that indicate | ||
| // the Feature is enabled. | ||
| EnablementVals []string | ||
| } | ||
|
|
||
| // Enabled returns if the Feature is enabled. | ||
| func Enabled(f Feature) bool { | ||
| key := EnvKeyRoot + f.EnvKeySuffix | ||
| vRaw, present := os.LookupEnv(key) | ||
| if !present { | ||
| return false | ||
| } | ||
|
|
||
| v := strings.ToLower(vRaw) | ||
| for _, allowed := range f.EnablementVals { | ||
| if v == strings.ToLower(allowed) { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
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.