-
Notifications
You must be signed in to change notification settings - Fork 962
Implement the keyed-attributes proposal #1631
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
Changes from all commits
53d396e
03db6d1
ef6ba96
e72fdb0
2b6a072
e572381
230de0a
fc11544
01c89f1
a7dbe16
55e22bf
269e259
1155c27
e1ab103
cac0ae5
d98eb2a
77f0fb5
8f1f871
dc86d82
529c15f
40f54ae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| /* | ||
| * Copyright 2020, 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 io.opentelemetry.common; | ||
|
|
||
| import javax.annotation.Nonnull; | ||
| import javax.annotation.Nullable; | ||
| import javax.annotation.concurrent.Immutable; | ||
|
|
||
| /** | ||
| * This interface provides a handle for setting the values of {@link Attributes}. The type of value | ||
|
jkwatson marked this conversation as resolved.
|
||
| * that can be set with an implementation of this key is denoted by the type parameter. | ||
| * | ||
| * <p>Implementations MUST be immutable, as these are used as the keys to Maps. | ||
| * | ||
| * @param <T> The type of value that can be set with the key. | ||
| */ | ||
| @SuppressWarnings("rawtypes") | ||
| @Immutable | ||
| public interface AttributeKey<T> extends Comparable<AttributeKey> { | ||
| /** Returns the underlying String representation of the key. */ | ||
| @Nullable | ||
| String getKey(); | ||
|
|
||
| /** Returns the type of attribute for this key. Useful for building switch statements. */ | ||
| @Nonnull | ||
| AttributeType getType(); | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,63 @@ | ||||||
| /* | ||||||
| * Copyright 2020, 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 io.opentelemetry.common; | ||||||
|
|
||||||
| import com.google.auto.value.AutoValue; | ||||||
|
|
||||||
| @SuppressWarnings("rawtypes") | ||||||
| @AutoValue | ||||||
| abstract class AttributeKeyImpl<T> implements AttributeKey<T> { | ||||||
|
|
||||||
| static <T> AttributeKeyImpl<T> create(String key, AttributeType type) { | ||||||
| return new AutoValue_AttributeKeyImpl<>(key, type); | ||||||
| } | ||||||
|
|
||||||
| ////////////////////////////////// | ||||||
| // IMPORTANT: the equals/hashcode/compareTo *only* include the key, and not the type, | ||||||
| // so that de-duping of attributes is based on the key, and not also based on the type. | ||||||
|
Contributor
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. Why? Can there be attributes with the same key but different types?
Contributor
Author
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. Well... what would that mean, exactly? Certainly with the previous implementation, you get only one value per key. Having multiple values per string-key would be very odd to have to deal with in an Exporter, I think.
Contributor
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. My though was that if there is NO two attributes with the same key but different type, why it is important to include only key, but not type, into equals/hashcode/compareTo?
Contributor
Author
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. Something has to make the replacement of one keyed-value with another (of a different type). This is the implementation that makes that happen. The de-duping relies on the equals/hashcode not including the type. |
||||||
| ////////////////////////////////// | ||||||
|
|
||||||
| @Override | ||||||
| public final boolean equals(Object o) { | ||||||
| if (this == o) { | ||||||
| return true; | ||||||
| } | ||||||
| if (!(o instanceof AttributeKeyImpl)) { | ||||||
| return false; | ||||||
| } | ||||||
|
|
||||||
| AttributeKeyImpl<?> that = (AttributeKeyImpl<?>) o; | ||||||
|
|
||||||
| return getKey() != null ? getKey().equals(that.getKey()) : that.getKey() == 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.
Suggested change
Contributor
Author
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. We can't use that in the API, due to java 7/android
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. This API is definititely in Java 7, check the link I posted 😃 |
||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public final int hashCode() { | ||||||
| return getKey() != null ? getKey().hashCode() : 0; | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public int compareTo(AttributeKey o) { | ||||||
| if (getKey() == null) { | ||||||
| return o.getKey() == null ? 0 : -1; | ||||||
| } | ||||||
| if (o.getKey() == null) { | ||||||
| return 1; | ||||||
| } | ||||||
| return getKey().compareTo(o.getKey()); | ||||||
|
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. Why no null check here? Use Objects.compare?
Contributor
Author
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. For some reason I thought we couldn't use that with java 7/android. Let me take a look.
Contributor
Author
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. updated to be null-safe |
||||||
| } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| /* | ||
| * Copyright 2020, 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 io.opentelemetry.common; | ||
|
|
||
| /** | ||
| * An enum that represents all the possible value types for an {@code AttributeKey} and hence the | ||
| * types of values that are allowed for {@link Attributes}. | ||
| * | ||
| * @since 0.1.0 | ||
| */ | ||
| public enum AttributeType { | ||
| STRING, | ||
| BOOLEAN, | ||
| LONG, | ||
| DOUBLE, | ||
| STRING_ARRAY, | ||
| BOOLEAN_ARRAY, | ||
| LONG_ARRAY, | ||
| DOUBLE_ARRAY | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.