diff --git a/api/src/main/java/io/opentelemetry/common/BooleanValuedKey.java b/api/src/main/java/io/opentelemetry/common/BooleanValuedKey.java new file mode 100644 index 00000000000..d05f9e56ab5 --- /dev/null +++ b/api/src/main/java/io/opentelemetry/common/BooleanValuedKey.java @@ -0,0 +1,21 @@ +/* + * 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; + +public interface BooleanValuedKey { + String key(); +} diff --git a/api/src/main/java/io/opentelemetry/common/DoubleValuedKey.java b/api/src/main/java/io/opentelemetry/common/DoubleValuedKey.java new file mode 100644 index 00000000000..a39da3d13e7 --- /dev/null +++ b/api/src/main/java/io/opentelemetry/common/DoubleValuedKey.java @@ -0,0 +1,21 @@ +/* + * 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; + +public interface DoubleValuedKey { + String key(); +} diff --git a/api/src/main/java/io/opentelemetry/common/LongValuedKey.java b/api/src/main/java/io/opentelemetry/common/LongValuedKey.java new file mode 100644 index 00000000000..d66b9814d99 --- /dev/null +++ b/api/src/main/java/io/opentelemetry/common/LongValuedKey.java @@ -0,0 +1,21 @@ +/* + * 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; + +public interface LongValuedKey { + String key(); +} diff --git a/api/src/main/java/io/opentelemetry/common/StringValuedKey.java b/api/src/main/java/io/opentelemetry/common/StringValuedKey.java new file mode 100644 index 00000000000..d629bb8b566 --- /dev/null +++ b/api/src/main/java/io/opentelemetry/common/StringValuedKey.java @@ -0,0 +1,21 @@ +/* + * 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; + +public interface StringValuedKey { + String key(); +} diff --git a/api/src/main/java/io/opentelemetry/trace/DefaultSpan.java b/api/src/main/java/io/opentelemetry/trace/DefaultSpan.java index cfdd4dc720a..be04fb2f481 100644 --- a/api/src/main/java/io/opentelemetry/trace/DefaultSpan.java +++ b/api/src/main/java/io/opentelemetry/trace/DefaultSpan.java @@ -17,6 +17,10 @@ package io.opentelemetry.trace; import io.opentelemetry.common.AttributeValue; +import io.opentelemetry.common.BooleanValuedKey; +import io.opentelemetry.common.DoubleValuedKey; +import io.opentelemetry.common.LongValuedKey; +import io.opentelemetry.common.StringValuedKey; import io.opentelemetry.internal.Utils; import java.util.Map; import java.util.Random; @@ -73,29 +77,29 @@ static DefaultSpan createRandom() { } @Override - public void setAttribute(String key, String value) { + public void setAttribute(String key, AttributeValue value) { Utils.checkNotNull(key, "key"); + Utils.checkNotNull(value, "value"); } @Override - public void setAttribute(String key, long value) { + public void setAttribute(StringValuedKey key, String value) { Utils.checkNotNull(key, "key"); } @Override - public void setAttribute(String key, double value) { + public void setAttribute(DoubleValuedKey key, double value) { Utils.checkNotNull(key, "key"); } @Override - public void setAttribute(String key, boolean value) { + public void setAttribute(BooleanValuedKey key, boolean value) { Utils.checkNotNull(key, "key"); } @Override - public void setAttribute(String key, AttributeValue value) { + public void setAttribute(LongValuedKey key, long value) { Utils.checkNotNull(key, "key"); - Utils.checkNotNull(value, "value"); } @Override diff --git a/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java b/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java index 8b2d94df6d6..52642424654 100644 --- a/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java +++ b/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java @@ -17,6 +17,10 @@ package io.opentelemetry.trace; import io.opentelemetry.common.AttributeValue; +import io.opentelemetry.common.BooleanValuedKey; +import io.opentelemetry.common.DoubleValuedKey; +import io.opentelemetry.common.LongValuedKey; +import io.opentelemetry.common.StringValuedKey; import io.opentelemetry.context.Scope; import io.opentelemetry.internal.Utils; import java.util.Map; @@ -116,33 +120,33 @@ public NoopSpanBuilder addLink(Link link) { } @Override - public NoopSpanBuilder setAttribute(String key, String value) { + public NoopSpanBuilder setAttribute(String key, AttributeValue value) { Utils.checkNotNull(key, "key"); + Utils.checkNotNull(value, "value"); return this; } @Override - public NoopSpanBuilder setAttribute(String key, long value) { + public NoopSpanBuilder setAttribute(StringValuedKey key, String value) { Utils.checkNotNull(key, "key"); return this; } @Override - public NoopSpanBuilder setAttribute(String key, double value) { + public NoopSpanBuilder setAttribute(DoubleValuedKey key, double value) { Utils.checkNotNull(key, "key"); return this; } @Override - public NoopSpanBuilder setAttribute(String key, boolean value) { + public NoopSpanBuilder setAttribute(BooleanValuedKey key, boolean value) { Utils.checkNotNull(key, "key"); return this; } @Override - public NoopSpanBuilder setAttribute(String key, AttributeValue value) { + public NoopSpanBuilder setAttribute(LongValuedKey key, long value) { Utils.checkNotNull(key, "key"); - Utils.checkNotNull(value, "value"); return this; } diff --git a/api/src/main/java/io/opentelemetry/trace/Span.java b/api/src/main/java/io/opentelemetry/trace/Span.java index b18949eb197..a526332bef8 100644 --- a/api/src/main/java/io/opentelemetry/trace/Span.java +++ b/api/src/main/java/io/opentelemetry/trace/Span.java @@ -17,6 +17,10 @@ package io.opentelemetry.trace; import io.opentelemetry.common.AttributeValue; +import io.opentelemetry.common.BooleanValuedKey; +import io.opentelemetry.common.DoubleValuedKey; +import io.opentelemetry.common.LongValuedKey; +import io.opentelemetry.common.StringValuedKey; import java.util.Map; import javax.annotation.Nullable; import javax.annotation.concurrent.ThreadSafe; @@ -84,24 +88,24 @@ enum Kind { * Sets an attribute to the {@code Span}. If the {@code Span} previously contained a mapping for * the key, the old value is replaced by the specified value. * - *
If a null or empty String {@code value} is passed in, the attribute will be silently - * dropped. Note: this behavior could change in the future. - * * @param key the key for this attribute. * @param value the value for this attribute. * @since 0.1.0 */ - void setAttribute(String key, @Nullable String value); + void setAttribute(String key, AttributeValue value); /** * Sets an attribute to the {@code Span}. If the {@code Span} previously contained a mapping for * the key, the old value is replaced by the specified value. * + *
If a null or empty String {@code value} is passed in, the attribute will be silently + * dropped. Note: this behavior could change in the future. + * * @param key the key for this attribute. * @param value the value for this attribute. * @since 0.1.0 */ - void setAttribute(String key, long value); + void setAttribute(StringValuedKey key, @Nullable String value); /** * Sets an attribute to the {@code Span}. If the {@code Span} previously contained a mapping for @@ -111,7 +115,7 @@ enum Kind { * @param value the value for this attribute. * @since 0.1.0 */ - void setAttribute(String key, double value); + void setAttribute(DoubleValuedKey key, double value); /** * Sets an attribute to the {@code Span}. If the {@code Span} previously contained a mapping for @@ -121,7 +125,7 @@ enum Kind { * @param value the value for this attribute. * @since 0.1.0 */ - void setAttribute(String key, boolean value); + void setAttribute(BooleanValuedKey key, boolean value); /** * Sets an attribute to the {@code Span}. If the {@code Span} previously contained a mapping for @@ -131,7 +135,7 @@ enum Kind { * @param value the value for this attribute. * @since 0.1.0 */ - void setAttribute(String key, AttributeValue value); + void setAttribute(LongValuedKey key, long value); /** * Adds an event to the {@code Span}. @@ -263,7 +267,7 @@ enum Kind { /** * Returns {@code true} if this {@code Span} records tracing events (e.g. {@link - * #addEvent(String)}, {@link #setAttribute(String, long)}). + * #addEvent(String)}, {@link #setAttribute(LongValuedKey, long)}). * * @return {@code true} if this {@code Span} records tracing events. * @since 0.1.0 @@ -459,28 +463,29 @@ interface Builder { * Sets an attribute to the newly created {@code Span}. If {@code Span.Builder} previously * contained a mapping for the key, the old value is replaced by the specified value. * - *
If a null or empty String {@code value} is passed in, the attribute will be silently - * dropped. Note: this behavior could change in the future. - * * @param key the key for this attribute. * @param value the value for this attribute. * @return this. * @throws NullPointerException if {@code key} is {@code null}. + * @throws NullPointerException if {@code value} is {@code null}. * @since 0.3.0 */ - Builder setAttribute(String key, @Nullable String value); + Builder setAttribute(String key, AttributeValue value); /** * Sets an attribute to the newly created {@code Span}. If {@code Span.Builder} previously * contained a mapping for the key, the old value is replaced by the specified value. * + *
If a null or empty String {@code value} is passed in, the attribute will be silently
+ * dropped. Note: this behavior could change in the future.
+ *
* @param key the key for this attribute.
* @param value the value for this attribute.
* @return this.
* @throws NullPointerException if {@code key} is {@code null}.
* @since 0.3.0
*/
- Builder setAttribute(String key, long value);
+ Builder setAttribute(StringValuedKey key, @Nullable String value);
/**
* Sets an attribute to the newly created {@code Span}. If {@code Span.Builder} previously
@@ -492,7 +497,7 @@ interface Builder {
* @throws NullPointerException if {@code key} is {@code null}.
* @since 0.3.0
*/
- Builder setAttribute(String key, double value);
+ Builder setAttribute(DoubleValuedKey key, double value);
/**
* Sets an attribute to the newly created {@code Span}. If {@code Span.Builder} previously
@@ -504,7 +509,7 @@ interface Builder {
* @throws NullPointerException if {@code key} is {@code null}.
* @since 0.3.0
*/
- Builder setAttribute(String key, boolean value);
+ Builder setAttribute(BooleanValuedKey key, boolean value);
/**
* Sets an attribute to the newly created {@code Span}. If {@code Span.Builder} previously
@@ -514,10 +519,9 @@ interface Builder {
* @param value the value for this attribute.
* @return this.
* @throws NullPointerException if {@code key} is {@code null}.
- * @throws NullPointerException if {@code value} is {@code null}.
* @since 0.3.0
*/
- Builder setAttribute(String key, AttributeValue value);
+ Builder setAttribute(LongValuedKey key, long value);
/**
* Sets the {@link Span.Kind} for the newly created {@code Span}. If not called, the
diff --git a/api/src/main/java/io/opentelemetry/trace/attributes/BooleanAttributeSetter.java b/api/src/main/java/io/opentelemetry/trace/attributes/BooleanAttributeSetter.java
index 08e9eb624cc..de398011ff4 100644
--- a/api/src/main/java/io/opentelemetry/trace/attributes/BooleanAttributeSetter.java
+++ b/api/src/main/java/io/opentelemetry/trace/attributes/BooleanAttributeSetter.java
@@ -16,12 +16,13 @@
package io.opentelemetry.trace.attributes;
+import io.opentelemetry.common.BooleanValuedKey;
import io.opentelemetry.trace.Span;
import javax.annotation.concurrent.Immutable;
/** Defines the behavior for a span attribute with boolean values. */
@Immutable
-public final class BooleanAttributeSetter {
+public final class BooleanAttributeSetter implements BooleanValuedKey {
/**
* Returns a new attribute setter.
@@ -29,7 +30,7 @@ public final class BooleanAttributeSetter {
* @param attributeKey the attribute name
* @return the setter object
*/
- public static BooleanAttributeSetter create(String attributeKey) {
+ public static BooleanAttributeSetter booleanKey(String attributeKey) {
return new BooleanAttributeSetter(attributeKey);
}
@@ -47,6 +48,7 @@ private BooleanAttributeSetter(String attributeKey) {
*
* @return the attribute map key
*/
+ @Override
public String key() {
return attributeKey;
}
@@ -58,6 +60,6 @@ public String key() {
* @param value the value for this attribute
*/
public void set(Span span, boolean value) {
- span.setAttribute(key(), value);
+ span.setAttribute(booleanKey(attributeKey), value);
}
}
diff --git a/api/src/main/java/io/opentelemetry/trace/attributes/DoubleAttributeSetter.java b/api/src/main/java/io/opentelemetry/trace/attributes/DoubleAttributeSetter.java
index a36c2f03a6f..bb63c955852 100644
--- a/api/src/main/java/io/opentelemetry/trace/attributes/DoubleAttributeSetter.java
+++ b/api/src/main/java/io/opentelemetry/trace/attributes/DoubleAttributeSetter.java
@@ -16,12 +16,13 @@
package io.opentelemetry.trace.attributes;
+import io.opentelemetry.common.DoubleValuedKey;
import io.opentelemetry.trace.Span;
import javax.annotation.concurrent.Immutable;
/** Defines the behavior for a span attribute with double values. */
@Immutable
-public final class DoubleAttributeSetter {
+public final class DoubleAttributeSetter implements DoubleValuedKey {
/**
* Returns a new attribute setter.
@@ -29,7 +30,7 @@ public final class DoubleAttributeSetter {
* @param attributeKey the attribute name
* @return the setter object
*/
- public static DoubleAttributeSetter create(String attributeKey) {
+ public static DoubleAttributeSetter doubleKey(String attributeKey) {
return new DoubleAttributeSetter(attributeKey);
}
@@ -47,6 +48,7 @@ private DoubleAttributeSetter(String attributeKey) {
*
* @return the attribute map key
*/
+ @Override
public String key() {
return attributeKey;
}
@@ -58,6 +60,6 @@ public String key() {
* @param value the value for this attribute
*/
public void set(Span span, double value) {
- span.setAttribute(key(), value);
+ span.setAttribute(doubleKey(attributeKey), value);
}
}
diff --git a/api/src/main/java/io/opentelemetry/trace/attributes/LongAttributeSetter.java b/api/src/main/java/io/opentelemetry/trace/attributes/LongAttributeSetter.java
index 7208c9c1aec..610a4b96df6 100644
--- a/api/src/main/java/io/opentelemetry/trace/attributes/LongAttributeSetter.java
+++ b/api/src/main/java/io/opentelemetry/trace/attributes/LongAttributeSetter.java
@@ -16,12 +16,13 @@
package io.opentelemetry.trace.attributes;
+import io.opentelemetry.common.LongValuedKey;
import io.opentelemetry.trace.Span;
import javax.annotation.concurrent.Immutable;
/** Defines the behavior for a span attribute with long values. */
@Immutable
-public final class LongAttributeSetter {
+public final class LongAttributeSetter implements LongValuedKey {
/**
* Returns a new attribute setter.
@@ -29,7 +30,7 @@ public final class LongAttributeSetter {
* @param attributeKey the attribute name
* @return the setter object
*/
- public static LongAttributeSetter create(String attributeKey) {
+ public static LongAttributeSetter longKey(String attributeKey) {
return new LongAttributeSetter(attributeKey);
}
@@ -47,6 +48,7 @@ private LongAttributeSetter(String attributeKey) {
*
* @return the attribute map key
*/
+ @Override
public String key() {
return attributeKey;
}
@@ -58,6 +60,6 @@ public String key() {
* @param value the value for this attribute
*/
public void set(Span span, long value) {
- span.setAttribute(key(), value);
+ span.setAttribute(longKey(attributeKey), value);
}
}
diff --git a/api/src/main/java/io/opentelemetry/trace/attributes/SemanticAttributes.java b/api/src/main/java/io/opentelemetry/trace/attributes/SemanticAttributes.java
index 42244550d3d..cc4ef3319fb 100644
--- a/api/src/main/java/io/opentelemetry/trace/attributes/SemanticAttributes.java
+++ b/api/src/main/java/io/opentelemetry/trace/attributes/SemanticAttributes.java
@@ -16,6 +16,9 @@
package io.opentelemetry.trace.attributes;
+import static io.opentelemetry.trace.attributes.LongAttributeSetter.longKey;
+import static io.opentelemetry.trace.attributes.StringAttributeSetter.stringKey;
+
/**
* Defines constants for all attribute names defined in the OpenTelemetry Semantic Conventions
* specifications.
@@ -27,103 +30,81 @@
public final class SemanticAttributes {
/** Transport protocol used. */
- public static final StringAttributeSetter NET_TRANSPORT =
- StringAttributeSetter.create("net.transport");
+ public static final StringAttributeSetter NET_TRANSPORT = stringKey("net.transport");
/** Remote address of the peer (dotted decimal for IPv4 or RFC5952 for IPv6). */
- public static final StringAttributeSetter NET_PEER_IP =
- StringAttributeSetter.create("net.peer.ip");
+ public static final StringAttributeSetter NET_PEER_IP = stringKey("net.peer.ip");
/** Remote port number as an integer. E.g., 80. */
- public static final LongAttributeSetter NET_PEER_PORT =
- LongAttributeSetter.create("net.peer.port");
+ public static final LongAttributeSetter NET_PEER_PORT = longKey("net.peer.port");
/** Remote hostname or similar. */
- public static final StringAttributeSetter NET_PEER_NAME =
- StringAttributeSetter.create("net.peer.name");
+ public static final StringAttributeSetter NET_PEER_NAME = stringKey("net.peer.name");
/** Like net.peer.ip but for the host IP. Useful in case of a multi-IP host. */
- public static final StringAttributeSetter NET_HOST_IP =
- StringAttributeSetter.create("net.host.ip");
+ public static final StringAttributeSetter NET_HOST_IP = stringKey("net.host.ip");
/** Like net.peer.port but for the host port. */
- public static final LongAttributeSetter NET_HOST_PORT =
- LongAttributeSetter.create("net.host.port");
+ public static final LongAttributeSetter NET_HOST_PORT = longKey("net.host.port");
/** Local hostname or similar. */
- public static final StringAttributeSetter NET_HOST_NAME =
- StringAttributeSetter.create("net.host.name");
+ public static final StringAttributeSetter NET_HOST_NAME = stringKey("net.host.name");
/**
* Username or client_id extracted from the access token or Authorization header in the inbound
* request from outside the system.
*/
- public static final StringAttributeSetter ENDUSER_ID = StringAttributeSetter.create("enduser.id");
+ public static final StringAttributeSetter ENDUSER_ID = stringKey("enduser.id");
/**
* Actual/assumed role the client is making the request under extracted from token or application
* security context.
*/
- public static final StringAttributeSetter ENDUSER_ROLE =
- StringAttributeSetter.create("enduser.role");
+ public static final StringAttributeSetter ENDUSER_ROLE = stringKey("enduser.role");
/**
* Scopes or granted authorities the client currently possesses extracted from token or
* application security context. The value would come from the scope associated with an OAuth 2.0
* Access Token or an attribute value in a SAML 2.0 Assertion.
*/
- public static final StringAttributeSetter ENDUSER_SCOPE =
- StringAttributeSetter.create("enduser.scope");
+ public static final StringAttributeSetter ENDUSER_SCOPE = stringKey("enduser.scope");
/** HTTP request method. E.g. "GET". */
- public static final StringAttributeSetter HTTP_METHOD =
- StringAttributeSetter.create("http.method");
+ public static final StringAttributeSetter HTTP_METHOD = stringKey("http.method");
/** Full HTTP request URL in the form scheme://host[:port]/path?query[#fragment]. */
- public static final StringAttributeSetter HTTP_URL = StringAttributeSetter.create("http.url");
+ public static final StringAttributeSetter HTTP_URL = stringKey("http.url");
/** The full request target as passed in a HTTP request line or equivalent. */
- public static final StringAttributeSetter HTTP_TARGET =
- StringAttributeSetter.create("http.target");
+ public static final StringAttributeSetter HTTP_TARGET = stringKey("http.target");
/** The value of the HTTP host header. */
- public static final StringAttributeSetter HTTP_HOST = StringAttributeSetter.create("http.host");
+ public static final StringAttributeSetter HTTP_HOST = stringKey("http.host");
/** The URI scheme identifying the used protocol: "http" or "https". */
- public static final StringAttributeSetter HTTP_SCHEME =
- StringAttributeSetter.create("http.scheme");
+ public static final StringAttributeSetter HTTP_SCHEME = stringKey("http.scheme");
/** HTTP response status code. E.g. 200 (integer) If and only if one was received/sent. */
- public static final LongAttributeSetter HTTP_STATUS_CODE =
- LongAttributeSetter.create("http.status_code");
+ public static final LongAttributeSetter HTTP_STATUS_CODE = longKey("http.status_code");
/** HTTP reason phrase. E.g. "OK" */
- public static final StringAttributeSetter HTTP_STATUS_TEXT =
- StringAttributeSetter.create("http.status_text");
+ public static final StringAttributeSetter HTTP_STATUS_TEXT = stringKey("http.status_text");
/** Kind of HTTP protocol used: "1.0", "1.1", "2", "SPDY" or "QUIC". */
- public static final StringAttributeSetter HTTP_FLAVOR =
- StringAttributeSetter.create("http.flavor");
+ public static final StringAttributeSetter HTTP_FLAVOR = stringKey("http.flavor");
/** Value of the HTTP "User-Agent" header sent by the client. */
- public static final StringAttributeSetter HTTP_USER_AGENT =
- StringAttributeSetter.create("http.user_agent");
+ public static final StringAttributeSetter HTTP_USER_AGENT = stringKey("http.user_agent");
/** The primary server name of the matched virtual host. Usually obtained via configuration. */
- public static final StringAttributeSetter HTTP_SERVER_NAME =
- StringAttributeSetter.create("http.server_name");
+ public static final StringAttributeSetter HTTP_SERVER_NAME = stringKey("http.server_name");
/** The matched route (path template). */
- public static final StringAttributeSetter HTTP_ROUTE = StringAttributeSetter.create("http.route");
+ public static final StringAttributeSetter HTTP_ROUTE = stringKey("http.route");
/** The IP address of the original client behind all proxies, if known. */
- public static final StringAttributeSetter HTTP_CLIENT_IP =
- StringAttributeSetter.create("http.client_ip");
+ public static final StringAttributeSetter HTTP_CLIENT_IP = stringKey("http.client_ip");
/** The service name, must be equal to the $service part in the span name. */
- public static final StringAttributeSetter RPC_SERVICE =
- StringAttributeSetter.create("rpc.service");
+ public static final StringAttributeSetter RPC_SERVICE = stringKey("rpc.service");
/** RPC span event attribute with value "SENT" or "RECEIVED". */
- public static final StringAttributeSetter MESSAGE_TYPE =
- StringAttributeSetter.create("message.type");
+ public static final StringAttributeSetter MESSAGE_TYPE = stringKey("message.type");
/** RPC span event attribute starting from 1 for each of sent messages and received messages. */
- public static final LongAttributeSetter MESSAGE_ID = LongAttributeSetter.create("message.id");
+ public static final LongAttributeSetter MESSAGE_ID = longKey("message.id");
/** RPC span event attribute for compressed size. */
public static final LongAttributeSetter MESSAGE_COMPRESSED_SIZE =
- LongAttributeSetter.create("message.compressed_size");
+ longKey("message.compressed_size");
/** RPC span event attribute for uncompressed size. */
public static final LongAttributeSetter MESSAGE_UNCOMPRESSED_SIZE =
- LongAttributeSetter.create("message.uncompressed_size");
+ longKey("message.uncompressed_size");
/** Database type. For any SQL database, "sql". For others, the lower-case database category. */
- public static final StringAttributeSetter DB_TYPE = StringAttributeSetter.create("db.type");
+ public static final StringAttributeSetter DB_TYPE = stringKey("db.type");
/** Database instance name. */
- public static final StringAttributeSetter DB_INSTANCE =
- StringAttributeSetter.create("db.instance");
+ public static final StringAttributeSetter DB_INSTANCE = stringKey("db.instance");
/** Database statement for the given database type. */
- public static final StringAttributeSetter DB_STATEMENT =
- StringAttributeSetter.create("db.statement");
+ public static final StringAttributeSetter DB_STATEMENT = stringKey("db.statement");
/** Username for accessing database. */
- public static final StringAttributeSetter DB_USER = StringAttributeSetter.create("db.user");
+ public static final StringAttributeSetter DB_USER = stringKey("db.user");
/** JDBC substring like "mysql://db.example.com:3306" */
- public static final StringAttributeSetter DB_URL = StringAttributeSetter.create("db.url");
+ public static final StringAttributeSetter DB_URL = stringKey("db.url");
private SemanticAttributes() {}
}
diff --git a/api/src/main/java/io/opentelemetry/trace/attributes/StringAttributeSetter.java b/api/src/main/java/io/opentelemetry/trace/attributes/StringAttributeSetter.java
index 145b5f0fc46..1878e1c5d26 100644
--- a/api/src/main/java/io/opentelemetry/trace/attributes/StringAttributeSetter.java
+++ b/api/src/main/java/io/opentelemetry/trace/attributes/StringAttributeSetter.java
@@ -16,13 +16,14 @@
package io.opentelemetry.trace.attributes;
+import io.opentelemetry.common.StringValuedKey;
import io.opentelemetry.trace.Span;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
/** Defines the behavior for a span attribute with string values. */
@Immutable
-public final class StringAttributeSetter {
+public final class StringAttributeSetter implements StringValuedKey {
/**
* Returns a new attribute setter.
@@ -30,7 +31,7 @@ public final class StringAttributeSetter {
* @param attributeKey the attribute name
* @return the setter object
*/
- public static StringAttributeSetter create(String attributeKey) {
+ public static StringAttributeSetter stringKey(String attributeKey) {
return new StringAttributeSetter(attributeKey);
}
@@ -48,6 +49,7 @@ private StringAttributeSetter(String attributeKey) {
*
* @return the attribute map key
*/
+ @Override
public String key() {
return attributeKey;
}
@@ -59,6 +61,6 @@ public String key() {
* @param value the value for this attribute
*/
public void set(Span span, @Nullable String value) {
- span.setAttribute(key(), value);
+ span.setAttribute(stringKey(attributeKey), value);
}
}
diff --git a/api/src/test/java/io/opentelemetry/trace/DefaultSpanTest.java b/api/src/test/java/io/opentelemetry/trace/DefaultSpanTest.java
index 0a930e0a342..7a3dafd4824 100644
--- a/api/src/test/java/io/opentelemetry/trace/DefaultSpanTest.java
+++ b/api/src/test/java/io/opentelemetry/trace/DefaultSpanTest.java
@@ -17,6 +17,7 @@
package io.opentelemetry.trace;
import static com.google.common.truth.Truth.assertThat;
+import static io.opentelemetry.trace.attributes.StringAttributeSetter.stringKey;
import io.opentelemetry.common.AttributeValue;
import java.util.Collections;
@@ -54,8 +55,8 @@ public void doNotCrash() {
"MyStringAttributeKey", AttributeValue.stringAttributeValue("MyStringAttributeValue"));
span.setAttribute("MyBooleanAttributeKey", AttributeValue.booleanAttributeValue(true));
span.setAttribute("MyLongAttributeKey", AttributeValue.longAttributeValue(123));
- span.setAttribute("NullString", (String) null);
- span.setAttribute("EmptyString", "");
+ span.setAttribute(stringKey("NullString"), null);
+ span.setAttribute(stringKey("EmptyString"), "");
span.addEvent("event");
span.addEvent("event", 0);
span.addEvent(
diff --git a/api/src/test/java/io/opentelemetry/trace/SpanBuilderTest.java b/api/src/test/java/io/opentelemetry/trace/SpanBuilderTest.java
index 27094c41c5f..baf46efbdd6 100644
--- a/api/src/test/java/io/opentelemetry/trace/SpanBuilderTest.java
+++ b/api/src/test/java/io/opentelemetry/trace/SpanBuilderTest.java
@@ -17,6 +17,10 @@
package io.opentelemetry.trace;
import static com.google.common.truth.Truth.assertThat;
+import static io.opentelemetry.trace.attributes.BooleanAttributeSetter.booleanKey;
+import static io.opentelemetry.trace.attributes.DoubleAttributeSetter.doubleKey;
+import static io.opentelemetry.trace.attributes.LongAttributeSetter.longKey;
+import static io.opentelemetry.trace.attributes.StringAttributeSetter.stringKey;
import io.opentelemetry.common.AttributeValue;
import io.opentelemetry.trace.Span.Kind;
@@ -57,10 +61,10 @@ public Map