Skip to content
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

Implement the keyed-attributes proposal #1631

Merged
merged 21 commits into from
Sep 21, 2020
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
package io.opentelemetry.common;

/**
* Convenience interface for consuming {@link ReadableAttributes}.
* Used for iterating over all the key/value pairs in an {@link Attributes} instance.
*
* @since 0.9.0
jkwatson marked this conversation as resolved.
Show resolved Hide resolved
*/
public interface AttributeConsumer
extends ReadableKeyValuePairs.KeyValueConsumer<String, AttributeValue> {}
public interface AttributeConsumer {
<T> void consume(AttributeKey<T> key, T value);
}
41 changes: 41 additions & 0 deletions api/src/main/java/io/opentelemetry/common/AttributeKey.java
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.
Show resolved Hide 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();
}
63 changes: 63 additions & 0 deletions api/src/main/java/io/opentelemetry/common/AttributeKeyImpl.java
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? Can there be attributes with the same key but different types?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use https://docs.oracle.com/javase/7/docs/api/java/util/Objects.html#equals(java.lang.Object,%20java.lang.Object)?

Suggested change
return getKey() != null ? getKey().equals(that.getKey()) : that.getKey() == null;
return Objects.equals(getKey(), that.getKey());

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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

Copy link
Member

Choose a reason for hiding this comment

The 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());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why no null check here? Use Objects.compare?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated to be null-safe

}
}
34 changes: 34 additions & 0 deletions api/src/main/java/io/opentelemetry/common/AttributeType.java
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
}
Loading