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

reuse semconv incubating attributes in test code #12992

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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 @@ -8,7 +8,6 @@
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
import static org.assertj.core.api.Assertions.entry;

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.context.Context;
Expand Down Expand Up @@ -90,9 +89,9 @@ void shouldExtractAllAvailableAttributes() {
entry(DbIncubatingAttributes.DB_CONNECTION_STRING, "mydb:///potatoes"),
entry(DbIncubatingAttributes.DB_STATEMENT, "SELECT * FROM potato"),
entry(DbIncubatingAttributes.DB_OPERATION, "SELECT"),
entry(AttributeKey.stringKey("db.namespace"), "potatoes"),
entry(AttributeKey.stringKey("db.query.text"), "SELECT * FROM potato"),
entry(AttributeKey.stringKey("db.operation.name"), "SELECT"));
entry(DbIncubatingAttributes.DB_NAMESPACE, "potatoes"),
Copy link
Member

Choose a reason for hiding this comment

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

is there any reason not to use static imports for all of these?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is only to minimize the diff and keep things consistent, do you think we should migrate and use static imports like in some other files ?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we could skip static imports in this file to reduce diff size. In #12920 @trask used a script to automatically add the static imports, but that script only ran under instrumentation. We could run that script in instrumentation-api-incubator in a separate PR. Use static imports in the changes that are under instrumentation, all (or at least most) of the usages should already use static imports there.

entry(DbIncubatingAttributes.DB_QUERY_TEXT, "SELECT * FROM potato"),
entry(DbIncubatingAttributes.DB_OPERATION_NAME, "SELECT"));
} else if (SemconvStability.emitOldDatabaseSemconv()) {
assertThat(startAttributes.build())
.containsOnly(
Expand All @@ -106,9 +105,9 @@ void shouldExtractAllAvailableAttributes() {
assertThat(startAttributes.build())
.containsOnly(
entry(DbIncubatingAttributes.DB_SYSTEM, "myDb"),
entry(AttributeKey.stringKey("db.namespace"), "potatoes"),
entry(AttributeKey.stringKey("db.query.text"), "SELECT * FROM potato"),
entry(AttributeKey.stringKey("db.operation.name"), "SELECT"));
entry(DbIncubatingAttributes.DB_NAMESPACE, "potatoes"),
entry(DbIncubatingAttributes.DB_QUERY_TEXT, "SELECT * FROM potato"),
entry(DbIncubatingAttributes.DB_OPERATION_NAME, "SELECT"));
}

assertThat(endAttributes.build().isEmpty()).isTrue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

package io.opentelemetry.instrumentation.api.incubator.semconv.db;

import static io.opentelemetry.api.common.AttributeKey.stringKey;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
import static org.assertj.core.api.Assertions.entry;

Expand Down Expand Up @@ -88,10 +87,10 @@ void shouldExtractAllAttributes() {
entry(DbIncubatingAttributes.DB_STATEMENT, "SELECT * FROM potato WHERE id=?"),
entry(DbIncubatingAttributes.DB_OPERATION, "SELECT"),
entry(DbIncubatingAttributes.DB_SQL_TABLE, "potato"),
entry(stringKey("db.namespace"), "potatoes"),
entry(stringKey("db.query.text"), "SELECT * FROM potato WHERE id=?"),
entry(stringKey("db.operation.name"), "SELECT"),
entry(stringKey("db.collection.name"), "potato"));
entry(DbIncubatingAttributes.DB_NAMESPACE, "potatoes"),
entry(DbIncubatingAttributes.DB_QUERY_TEXT, "SELECT * FROM potato WHERE id=?"),
entry(DbIncubatingAttributes.DB_OPERATION_NAME, "SELECT"),
entry(DbIncubatingAttributes.DB_COLLECTION_NAME, "potato"));
} else if (SemconvStability.emitOldDatabaseSemconv()) {
assertThat(startAttributes.build())
.containsOnly(
Expand All @@ -106,10 +105,10 @@ void shouldExtractAllAttributes() {
assertThat(startAttributes.build())
.containsOnly(
entry(DbIncubatingAttributes.DB_SYSTEM, "myDb"),
entry(stringKey("db.namespace"), "potatoes"),
entry(stringKey("db.query.text"), "SELECT * FROM potato WHERE id=?"),
entry(stringKey("db.operation.name"), "SELECT"),
entry(stringKey("db.collection.name"), "potato"));
entry(DbIncubatingAttributes.DB_NAMESPACE, "potatoes"),
entry(DbIncubatingAttributes.DB_QUERY_TEXT, "SELECT * FROM potato WHERE id=?"),
entry(DbIncubatingAttributes.DB_OPERATION_NAME, "SELECT"),
entry(DbIncubatingAttributes.DB_COLLECTION_NAME, "potato"));
}

assertThat(endAttributes.build().isEmpty()).isTrue();
Expand All @@ -136,8 +135,8 @@ void shouldNotExtractTableIfAttributeIsNotSet() {
.containsOnly(
entry(DbIncubatingAttributes.DB_STATEMENT, "SELECT *"),
entry(DbIncubatingAttributes.DB_OPERATION, "SELECT"),
entry(stringKey("db.query.text"), "SELECT *"),
entry(stringKey("db.operation.name"), "SELECT"));
entry(DbIncubatingAttributes.DB_QUERY_TEXT, "SELECT *"),
entry(DbIncubatingAttributes.DB_OPERATION_NAME, "SELECT"));
} else if (SemconvStability.emitOldDatabaseSemconv()) {
assertThat(attributes.build())
.containsOnly(
Expand All @@ -146,8 +145,8 @@ void shouldNotExtractTableIfAttributeIsNotSet() {
} else if (SemconvStability.emitStableDatabaseSemconv()) {
assertThat(attributes.build())
.containsOnly(
entry(stringKey("db.query.text"), "SELECT *"),
entry(stringKey("db.operation.name"), "SELECT"));
entry(DbIncubatingAttributes.DB_QUERY_TEXT, "SELECT *"),
entry(DbIncubatingAttributes.DB_OPERATION_NAME, "SELECT"));
}
}

Expand Down Expand Up @@ -176,9 +175,9 @@ void shouldExtractTableToSpecifiedKey() {
entry(DbIncubatingAttributes.DB_STATEMENT, "SELECT * FROM table"),
entry(DbIncubatingAttributes.DB_OPERATION, "SELECT"),
entry(DbIncubatingAttributes.DB_CASSANDRA_TABLE, "table"),
entry(stringKey("db.query.text"), "SELECT * FROM table"),
entry(stringKey("db.operation.name"), "SELECT"),
entry(stringKey("db.collection.name"), "table"));
entry(DbIncubatingAttributes.DB_QUERY_TEXT, "SELECT * FROM table"),
entry(DbIncubatingAttributes.DB_OPERATION_NAME, "SELECT"),
entry(DbIncubatingAttributes.DB_COLLECTION_NAME, "table"));
} else if (SemconvStability.emitOldDatabaseSemconv()) {
assertThat(attributes.build())
.containsOnly(
Expand All @@ -188,9 +187,9 @@ void shouldExtractTableToSpecifiedKey() {
} else if (SemconvStability.emitStableDatabaseSemconv()) {
assertThat(attributes.build())
.containsOnly(
entry(stringKey("db.query.text"), "SELECT * FROM table"),
entry(stringKey("db.operation.name"), "SELECT"),
entry(stringKey("db.collection.name"), "table"));
entry(DbIncubatingAttributes.DB_QUERY_TEXT, "SELECT * FROM table"),
entry(DbIncubatingAttributes.DB_OPERATION_NAME, "SELECT"),
entry(DbIncubatingAttributes.DB_COLLECTION_NAME, "table"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import io.opentelemetry.semconv.HttpAttributes;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.apache.shenyu.common.dto.MetaData;
Expand Down Expand Up @@ -111,7 +112,7 @@ void testUpdateRouter() {
span.hasName("GET /a/b/c")
.hasKind(SpanKind.SERVER)
.hasAttributesSatisfying(
equalTo(AttributeKey.stringKey("http.route"), "/a/b/c"),
equalTo(HttpAttributes.HTTP_ROUTE, "/a/b/c"),
equalTo(META_ID_ATTRIBUTE, "123"),
equalTo(META_ENABLED_ATTRIBUTE, true),
equalTo(METHOD_NAME_ATTRIBUTE, "hello"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import io.opentelemetry.semconv.incubating.DbIncubatingAttributes;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -139,10 +139,12 @@ public void elasticsearchIndex() throws IOException {
URL_FULL,
httpHost.toURI() + "/test-index/_doc/test-id?timeout=10s"),
equalTo(
AttributeKey.stringKey("db.elasticsearch.path_parts.index"),
DbIncubatingAttributes.DB_ELASTICSEARCH_PATH_PARTS.getAttributeKey(
"index"),
"test-index"),
equalTo(
AttributeKey.stringKey("db.elasticsearch.path_parts.id"),
DbIncubatingAttributes.DB_ELASTICSEARCH_PATH_PARTS.getAttributeKey(
"id"),
"test-id")),
span ->
span.hasName("PUT")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static io.opentelemetry.semconv.incubating.MessagingIncubatingAttributes.MESSAGING_MESSAGE_BODY_SIZE;
import static io.opentelemetry.semconv.incubating.MessagingIncubatingAttributes.MESSAGING_OPERATION;
import static io.opentelemetry.semconv.incubating.MessagingIncubatingAttributes.MESSAGING_SYSTEM;
import static io.opentelemetry.semconv.incubating.MessagingIncubatingAttributes.MessagingSystemIncubatingValues.KAFKA;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;

Expand All @@ -28,7 +29,6 @@
import io.opentelemetry.sdk.testing.assertj.AttributeAssertion;
import io.opentelemetry.sdk.trace.data.LinkData;
import io.opentelemetry.sdk.trace.data.SpanData;
import io.opentelemetry.semconv.incubating.MessagingIncubatingAttributes;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -111,9 +111,7 @@ void testKafkaProduceAndConsumeWithStreamsInBetween() throws Exception {
.hasKind(SpanKind.PRODUCER)
.hasNoParent()
.hasAttributesSatisfyingExactly(
equalTo(
MESSAGING_SYSTEM,
MessagingIncubatingAttributes.MessagingSystemIncubatingValues.KAFKA),
equalTo(MESSAGING_SYSTEM, KAFKA),
equalTo(MESSAGING_DESTINATION_NAME, STREAM_PENDING),
equalTo(MESSAGING_OPERATION, "publish"),
satisfies(MESSAGING_CLIENT_ID, k -> k.startsWith("producer")),
Expand All @@ -131,10 +129,7 @@ void testKafkaProduceAndConsumeWithStreamsInBetween() throws Exception {
List<AttributeAssertion> assertions =
new ArrayList<>(
asList(
equalTo(
MESSAGING_SYSTEM,
MessagingIncubatingAttributes.MessagingSystemIncubatingValues
.KAFKA),
equalTo(MESSAGING_SYSTEM, KAFKA),
equalTo(MESSAGING_DESTINATION_NAME, STREAM_PENDING),
equalTo(MESSAGING_OPERATION, "receive"),
satisfies(MESSAGING_CLIENT_ID, k -> k.endsWith("consumer")),
Expand All @@ -152,10 +147,7 @@ void testKafkaProduceAndConsumeWithStreamsInBetween() throws Exception {
List<AttributeAssertion> assertions =
new ArrayList<>(
asList(
equalTo(
MESSAGING_SYSTEM,
MessagingIncubatingAttributes.MessagingSystemIncubatingValues
.KAFKA),
equalTo(MESSAGING_SYSTEM, KAFKA),
equalTo(MESSAGING_DESTINATION_NAME, STREAM_PENDING),
equalTo(MESSAGING_OPERATION, "process"),
satisfies(MESSAGING_CLIENT_ID, k -> k.endsWith("consumer")),
Expand Down Expand Up @@ -186,9 +178,7 @@ void testKafkaProduceAndConsumeWithStreamsInBetween() throws Exception {
.hasTraceId(receivedContext.getTraceId())
.hasSpanId(receivedContext.getSpanId())
.hasAttributesSatisfyingExactly(
equalTo(
MESSAGING_SYSTEM,
MessagingIncubatingAttributes.MessagingSystemIncubatingValues.KAFKA),
equalTo(MESSAGING_SYSTEM, KAFKA),
equalTo(MESSAGING_DESTINATION_NAME, STREAM_PROCESSED),
equalTo(MESSAGING_OPERATION, "publish"),
satisfies(MESSAGING_CLIENT_ID, k -> k.endsWith("producer")),
Expand All @@ -206,10 +196,7 @@ void testKafkaProduceAndConsumeWithStreamsInBetween() throws Exception {
List<AttributeAssertion> assertions =
new ArrayList<>(
asList(
equalTo(
MESSAGING_SYSTEM,
MessagingIncubatingAttributes.MessagingSystemIncubatingValues
.KAFKA),
equalTo(MESSAGING_SYSTEM, KAFKA),
equalTo(MESSAGING_DESTINATION_NAME, STREAM_PROCESSED),
equalTo(MESSAGING_OPERATION, "receive"),
satisfies(MESSAGING_CLIENT_ID, k -> k.startsWith("consumer")),
Expand All @@ -227,10 +214,7 @@ void testKafkaProduceAndConsumeWithStreamsInBetween() throws Exception {
List<AttributeAssertion> assertions =
new ArrayList<>(
asList(
equalTo(
MESSAGING_SYSTEM,
MessagingIncubatingAttributes.MessagingSystemIncubatingValues
.KAFKA),
equalTo(MESSAGING_SYSTEM, KAFKA),
equalTo(MESSAGING_DESTINATION_NAME, STREAM_PROCESSED),
equalTo(MESSAGING_OPERATION, "process"),
satisfies(MESSAGING_CLIENT_ID, k -> k.startsWith("consumer")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static io.opentelemetry.semconv.incubating.MessagingIncubatingAttributes.MESSAGING_MESSAGE_BODY_SIZE;
import static io.opentelemetry.semconv.incubating.MessagingIncubatingAttributes.MESSAGING_OPERATION;
import static io.opentelemetry.semconv.incubating.MessagingIncubatingAttributes.MESSAGING_SYSTEM;
import static io.opentelemetry.semconv.incubating.MessagingIncubatingAttributes.MessagingSystemIncubatingValues.KAFKA;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;

Expand All @@ -25,7 +26,6 @@
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.sdk.testing.assertj.AttributeAssertion;
import io.opentelemetry.sdk.trace.data.SpanData;
import io.opentelemetry.semconv.incubating.MessagingIncubatingAttributes;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -101,10 +101,7 @@ void testKafkaProduceAndConsumeWithStreamsInBetween() throws Exception {
.hasKind(SpanKind.PRODUCER)
.hasNoParent()
.hasAttributesSatisfyingExactly(
equalTo(
MESSAGING_SYSTEM,
MessagingIncubatingAttributes.MessagingSystemIncubatingValues
.KAFKA),
equalTo(MESSAGING_SYSTEM, KAFKA),
equalTo(MESSAGING_DESTINATION_NAME, STREAM_PENDING),
equalTo(MESSAGING_OPERATION, "publish"),
equalTo(MESSAGING_CLIENT_ID, "producer-1"),
Expand All @@ -118,10 +115,7 @@ void testKafkaProduceAndConsumeWithStreamsInBetween() throws Exception {
List<AttributeAssertion> assertions =
new ArrayList<>(
asList(
equalTo(
MESSAGING_SYSTEM,
MessagingIncubatingAttributes.MessagingSystemIncubatingValues
.KAFKA),
equalTo(MESSAGING_SYSTEM, KAFKA),
equalTo(MESSAGING_DESTINATION_NAME, STREAM_PENDING),
equalTo(MESSAGING_OPERATION, "process"),
satisfies(MESSAGING_CLIENT_ID, k -> k.endsWith("consumer")),
Expand Down Expand Up @@ -153,9 +147,7 @@ void testKafkaProduceAndConsumeWithStreamsInBetween() throws Exception {
.hasTraceId(receivedContext.getTraceId())
.hasSpanId(receivedContext.getSpanId())
.hasAttributesSatisfyingExactly(
equalTo(
MESSAGING_SYSTEM,
MessagingIncubatingAttributes.MessagingSystemIncubatingValues.KAFKA),
equalTo(MESSAGING_SYSTEM, KAFKA),
equalTo(MESSAGING_DESTINATION_NAME, STREAM_PROCESSED),
equalTo(MESSAGING_OPERATION, "publish"),
satisfies(MESSAGING_CLIENT_ID, k -> k.isInstanceOf(String.class)),
Expand All @@ -169,10 +161,7 @@ void testKafkaProduceAndConsumeWithStreamsInBetween() throws Exception {
List<AttributeAssertion> assertions =
new ArrayList<>(
asList(
equalTo(
MESSAGING_SYSTEM,
MessagingIncubatingAttributes.MessagingSystemIncubatingValues
.KAFKA),
equalTo(MESSAGING_SYSTEM, KAFKA),
equalTo(MESSAGING_DESTINATION_NAME, STREAM_PROCESSED),
equalTo(MESSAGING_OPERATION, "process"),
satisfies(MESSAGING_CLIENT_ID, k -> k.startsWith("consumer")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import io.opentelemetry.sdk.testing.assertj.AttributeAssertion;
import io.opentelemetry.sdk.trace.data.StatusData;
import io.opentelemetry.semconv.incubating.CodeIncubatingAttributes;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
Expand Down Expand Up @@ -342,8 +343,8 @@ private static List<AttributeAssertion> attributeAssertions(
List<AttributeAssertion> attributeAssertions =
new ArrayList<>(
asList(
equalTo(AttributeKey.stringKey("code.namespace"), codeNamespace),
equalTo(AttributeKey.stringKey("code.function"), "process"),
equalTo(CodeIncubatingAttributes.CODE_NAMESPACE, codeNamespace),
equalTo(CodeIncubatingAttributes.CODE_FUNCTION, "process"),
equalTo(AttributeKey.stringKey("job.system"), "powerjob"),
equalTo(AttributeKey.longKey("scheduling.powerjob.job.id"), jobId),
equalTo(AttributeKey.stringKey("scheduling.powerjob.job.type"), jobType)));
Expand Down
Loading
Loading