-
Notifications
You must be signed in to change notification settings - Fork 920
Complex attributes incubating implementation #7814
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 all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
5e889a8
Prototype: Complex attributes (Option C - minimal)
trask 4a2c048
Remove String, Value<?> overload
trask 3b8e35f
Add javadoc explaining coersion
trask 04937c0
more javadoc
trask 73466c8
more javadoc
trask 1958768
more
trask e4308cf
Complex Attributes - incubating implementation
trask 5f342e2
Deprecation
trask 8c40374
More implementation
trask 7d49b5e
updates
trask d3f9dbe
javadoc
trask 08e6e0c
tests
trask c7824e1
javadoc
trask 26a3413
fix
trask d83ff5d
Add a missing test
trask 10d6e02
Apply suggestion from @trask
trask 198faa2
codecov
trask 4386c8c
Implement limits
trask 575535d
simplify putValue implementation
trask f1ffad8
initialize array lists with correct size
trask 6480d0e
Add test for non-existent array type to improve code coverage
trask 22e857d
spotless
trask b91d3bb
Move warning to where users will see it
trask 59ab595
Merge remote-tracking branch 'upstream/main' into complex-attrs-c-impl
trask d44e727
Don't allocate when already valid length
trask c6b9858
Remove unnecessary suppress warnings
trask 28fedf2
Remove test of deprecated feature
trask 587e7e3
spotless
trask 9b9ea16
Don't call deprecated method
trask 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,9 +8,13 @@ | |
| import io.opentelemetry.api.common.AttributeKey; | ||
| import io.opentelemetry.api.common.Attributes; | ||
| import io.opentelemetry.api.common.AttributesBuilder; | ||
| import io.opentelemetry.api.common.Value; | ||
| import io.opentelemetry.api.common.ValueType; | ||
| import io.opentelemetry.api.internal.ImmutableKeyValuePairs; | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.Comparator; | ||
| import java.util.List; | ||
| import javax.annotation.Nullable; | ||
| import javax.annotation.concurrent.Immutable; | ||
|
|
||
|
|
@@ -51,9 +55,121 @@ public ExtendedAttributesBuilder toBuilder() { | |
| @Override | ||
| @Nullable | ||
| public <T> T get(ExtendedAttributeKey<T> key) { | ||
| if (key == null) { | ||
| return null; | ||
| } | ||
| if (key.getType() == ExtendedAttributeType.VALUE) { | ||
| return (T) getAsValue(key.getKey()); | ||
| } | ||
| // Check if we're looking for an array type but have a VALUE with empty array | ||
| if (isArrayType(key.getType())) { | ||
| T value = (T) super.get(key); | ||
| if (value == null) { | ||
| // Check if there's a VALUE with the same key that contains an empty array | ||
| Value<?> valueAttr = getValueAttribute(key.getKey()); | ||
| if (valueAttr != null && isEmptyArray(valueAttr)) { | ||
| return (T) Collections.emptyList(); | ||
| } | ||
| } | ||
| return value; | ||
| } | ||
| return (T) super.get(key); | ||
| } | ||
|
|
||
| private static boolean isArrayType(ExtendedAttributeType type) { | ||
| return type == ExtendedAttributeType.STRING_ARRAY | ||
| || type == ExtendedAttributeType.LONG_ARRAY | ||
| || type == ExtendedAttributeType.DOUBLE_ARRAY | ||
| || type == ExtendedAttributeType.BOOLEAN_ARRAY; | ||
| } | ||
|
|
||
| @Nullable | ||
| private Value<?> getValueAttribute(String keyName) { | ||
| List<Object> data = data(); | ||
| for (int i = 0; i < data.size(); i += 2) { | ||
| ExtendedAttributeKey<?> currentKey = (ExtendedAttributeKey<?>) data.get(i); | ||
| if (currentKey.getKey().equals(keyName) | ||
| && currentKey.getType() == ExtendedAttributeType.VALUE) { | ||
| return (Value<?>) data.get(i + 1); | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| private static boolean isEmptyArray(Value<?> value) { | ||
| if (value.getType() != ValueType.ARRAY) { | ||
| return false; | ||
| } | ||
| @SuppressWarnings("unchecked") | ||
| List<Value<?>> arrayValues = (List<Value<?>>) value.getValue(); | ||
| return arrayValues.isEmpty(); | ||
| } | ||
|
|
||
| @Nullable | ||
| private Value<?> getAsValue(String keyName) { | ||
| // Find any attribute with the same key name and convert it to Value | ||
| List<Object> data = data(); | ||
| for (int i = 0; i < data.size(); i += 2) { | ||
| ExtendedAttributeKey<?> currentKey = (ExtendedAttributeKey<?>) data.get(i); | ||
| if (currentKey.getKey().equals(keyName)) { | ||
| Object value = data.get(i + 1); | ||
| return asValue(currentKey.getType(), value); | ||
| } | ||
| } | ||
| return null; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would this implementation benefit from #7076? |
||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| @Nullable | ||
| private static Value<?> asValue(ExtendedAttributeType type, Object value) { | ||
| switch (type) { | ||
| case STRING: | ||
| return Value.of((String) value); | ||
| case LONG: | ||
| return Value.of((Long) value); | ||
| case DOUBLE: | ||
| return Value.of((Double) value); | ||
| case BOOLEAN: | ||
| return Value.of((Boolean) value); | ||
| case STRING_ARRAY: | ||
| List<String> stringList = (List<String>) value; | ||
| Value<?>[] stringValues = new Value<?>[stringList.size()]; | ||
| for (int i = 0; i < stringList.size(); i++) { | ||
| stringValues[i] = Value.of(stringList.get(i)); | ||
| } | ||
| return Value.of(stringValues); | ||
| case LONG_ARRAY: | ||
| List<Long> longList = (List<Long>) value; | ||
| Value<?>[] longValues = new Value<?>[longList.size()]; | ||
| for (int i = 0; i < longList.size(); i++) { | ||
| longValues[i] = Value.of(longList.get(i)); | ||
| } | ||
| return Value.of(longValues); | ||
| case DOUBLE_ARRAY: | ||
| List<Double> doubleList = (List<Double>) value; | ||
| Value<?>[] doubleValues = new Value<?>[doubleList.size()]; | ||
| for (int i = 0; i < doubleList.size(); i++) { | ||
| doubleValues[i] = Value.of(doubleList.get(i)); | ||
| } | ||
| return Value.of(doubleValues); | ||
| case BOOLEAN_ARRAY: | ||
| List<Boolean> booleanList = (List<Boolean>) value; | ||
| Value<?>[] booleanValues = new Value<?>[booleanList.size()]; | ||
| for (int i = 0; i < booleanList.size(); i++) { | ||
| booleanValues[i] = Value.of(booleanList.get(i)); | ||
| } | ||
| return Value.of(booleanValues); | ||
| case VALUE: | ||
| // Already a Value | ||
| return (Value<?>) value; | ||
| case EXTENDED_ATTRIBUTES: | ||
| // Cannot convert EXTENDED_ATTRIBUTES to Value | ||
| return null; | ||
| } | ||
| // Should not reach here | ||
| return null; | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| @Override | ||
| public Attributes asAttributes() { | ||
|
|
||
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
Oops, something went wrong.
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.