From ba798db943d6178b72b1c930158abf5f09bf59e0 Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Fri, 10 Jan 2025 13:30:32 -0800 Subject: [PATCH] Statically import semconv constants in tests (#13026) --- .../static-import-semconv-constants.sh | 30 ++++ .../HttpClientAttributesExtractorTest.java | 89 ++++++------ .../semconv/http/HttpClientMetricsTest.java | 64 +++++---- .../HttpServerAttributesExtractorTest.java | 130 +++++++++--------- .../semconv/http/HttpServerMetricsTest.java | 87 ++++++------ .../ClientAttributesExtractorTest.java | 7 +- ...ributesExtractorInetSocketAddressTest.java | 16 ++- .../NetworkAttributesExtractorTest.java | 28 ++-- .../ServerAttributesExtractorTest.java | 10 +- .../url/UrlAttributesExtractorTest.java | 8 +- .../apacheshenyu/v2_4/ShenYuRouteTest.java | 4 +- .../apiclient/ElasticsearchClientTest.java | 11 +- .../v4_0/redis/VertxRedisClientTest.java | 10 +- 13 files changed, 268 insertions(+), 226 deletions(-) create mode 100644 .github/scripts/static-import-semconv-constants.sh diff --git a/.github/scripts/static-import-semconv-constants.sh b/.github/scripts/static-import-semconv-constants.sh new file mode 100644 index 000000000000..776b68b51fb5 --- /dev/null +++ b/.github/scripts/static-import-semconv-constants.sh @@ -0,0 +1,30 @@ +#!/bin/bash -e + +# shellcheck disable=SC2044 +for file in $(find instrumentation instrumentation-api -name '*Test.java'); do + echo "Processing $file" + + # stable semconv + + negative_lookbehind='(? exemplar diff --git a/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/semconv/http/HttpServerAttributesExtractorTest.java b/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/semconv/http/HttpServerAttributesExtractorTest.java index 8526d9e04f29..ff0d2565bb2d 100644 --- a/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/semconv/http/HttpServerAttributesExtractorTest.java +++ b/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/semconv/http/HttpServerAttributesExtractorTest.java @@ -6,6 +6,22 @@ package io.opentelemetry.instrumentation.api.semconv.http; import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; +import static io.opentelemetry.semconv.ClientAttributes.CLIENT_ADDRESS; +import static io.opentelemetry.semconv.ErrorAttributes.ERROR_TYPE; +import static io.opentelemetry.semconv.HttpAttributes.HTTP_REQUEST_METHOD; +import static io.opentelemetry.semconv.HttpAttributes.HTTP_REQUEST_METHOD_ORIGINAL; +import static io.opentelemetry.semconv.HttpAttributes.HTTP_RESPONSE_STATUS_CODE; +import static io.opentelemetry.semconv.HttpAttributes.HTTP_ROUTE; +import static io.opentelemetry.semconv.NetworkAttributes.NETWORK_PEER_ADDRESS; +import static io.opentelemetry.semconv.NetworkAttributes.NETWORK_PEER_PORT; +import static io.opentelemetry.semconv.NetworkAttributes.NETWORK_PROTOCOL_NAME; +import static io.opentelemetry.semconv.NetworkAttributes.NETWORK_PROTOCOL_VERSION; +import static io.opentelemetry.semconv.ServerAttributes.SERVER_ADDRESS; +import static io.opentelemetry.semconv.ServerAttributes.SERVER_PORT; +import static io.opentelemetry.semconv.UrlAttributes.URL_PATH; +import static io.opentelemetry.semconv.UrlAttributes.URL_QUERY; +import static io.opentelemetry.semconv.UrlAttributes.URL_SCHEME; +import static io.opentelemetry.semconv.UserAgentAttributes.USER_AGENT_ORIGINAL; import static java.util.Arrays.asList; import static java.util.Collections.emptyList; import static java.util.Collections.emptyMap; @@ -18,13 +34,6 @@ import io.opentelemetry.context.Context; import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; import io.opentelemetry.instrumentation.api.internal.HttpConstants; -import io.opentelemetry.semconv.ClientAttributes; -import io.opentelemetry.semconv.ErrorAttributes; -import io.opentelemetry.semconv.HttpAttributes; -import io.opentelemetry.semconv.NetworkAttributes; -import io.opentelemetry.semconv.ServerAttributes; -import io.opentelemetry.semconv.UrlAttributes; -import io.opentelemetry.semconv.UserAgentAttributes; import java.net.ConnectException; import java.util.HashMap; import java.util.HashSet; @@ -198,15 +207,15 @@ void normal() { extractor.onStart(startAttributes, Context.root(), request); assertThat(startAttributes.build()) .containsOnly( - entry(ServerAttributes.SERVER_ADDRESS, "github.com"), - entry(ServerAttributes.SERVER_PORT, 443L), - entry(HttpAttributes.HTTP_REQUEST_METHOD, "POST"), - entry(UrlAttributes.URL_SCHEME, "https"), - entry(UrlAttributes.URL_PATH, "/repositories/1"), - entry(UrlAttributes.URL_QUERY, "details=true"), - entry(UserAgentAttributes.USER_AGENT_ORIGINAL, "okhttp 3.x"), - entry(HttpAttributes.HTTP_ROUTE, "/repositories/{id}"), - entry(ClientAttributes.CLIENT_ADDRESS, "1.1.1.1"), + entry(SERVER_ADDRESS, "github.com"), + entry(SERVER_PORT, 443L), + entry(HTTP_REQUEST_METHOD, "POST"), + entry(URL_SCHEME, "https"), + entry(URL_PATH, "/repositories/1"), + entry(URL_QUERY, "details=true"), + entry(USER_AGENT_ORIGINAL, "okhttp 3.x"), + entry(HTTP_ROUTE, "/repositories/{id}"), + entry(CLIENT_ADDRESS, "1.1.1.1"), entry( AttributeKey.stringArrayKey("http.request.header.custom-request-header"), asList("123", "456"))); @@ -215,11 +224,11 @@ void normal() { extractor.onEnd(endAttributes, Context.root(), request, response, null); assertThat(endAttributes.build()) .containsOnly( - entry(NetworkAttributes.NETWORK_PROTOCOL_VERSION, "2.0"), - entry(NetworkAttributes.NETWORK_PEER_ADDRESS, "4.3.2.1"), - entry(NetworkAttributes.NETWORK_PEER_PORT, 456L), - entry(HttpAttributes.HTTP_ROUTE, "/repositories/{repoId}"), - entry(HttpAttributes.HTTP_RESPONSE_STATUS_CODE, 202L), + entry(NETWORK_PROTOCOL_VERSION, "2.0"), + entry(NETWORK_PEER_ADDRESS, "4.3.2.1"), + entry(NETWORK_PEER_PORT, 456L), + entry(HTTP_ROUTE, "/repositories/{repoId}"), + entry(HTTP_RESPONSE_STATUS_CODE, 202L), entry( AttributeKey.stringArrayKey("http.response.header.custom-response-header"), asList("654", "321"))); @@ -239,8 +248,8 @@ void shouldExtractKnownMethods(String requestMethod) { extractor.onEnd(attributes, Context.root(), request, emptyMap(), null); assertThat(attributes.build()) - .containsEntry(HttpAttributes.HTTP_REQUEST_METHOD, requestMethod) - .doesNotContainKey(HttpAttributes.HTTP_REQUEST_METHOD_ORIGINAL); + .containsEntry(HTTP_REQUEST_METHOD, requestMethod) + .doesNotContainKey(HTTP_REQUEST_METHOD_ORIGINAL); } @ParameterizedTest @@ -257,8 +266,8 @@ void shouldTreatMethodsAsCaseSensitive(String requestMethod) { extractor.onEnd(attributes, Context.root(), request, emptyMap(), null); assertThat(attributes.build()) - .containsEntry(HttpAttributes.HTTP_REQUEST_METHOD, HttpConstants._OTHER) - .containsEntry(HttpAttributes.HTTP_REQUEST_METHOD_ORIGINAL, requestMethod); + .containsEntry(HTTP_REQUEST_METHOD, HttpConstants._OTHER) + .containsEntry(HTTP_REQUEST_METHOD_ORIGINAL, requestMethod); } @ParameterizedTest @@ -275,8 +284,8 @@ void shouldUseOtherForUnknownMethods(String requestMethod) { extractor.onEnd(attributes, Context.root(), request, emptyMap(), null); assertThat(attributes.build()) - .containsEntry(HttpAttributes.HTTP_REQUEST_METHOD, HttpConstants._OTHER) - .containsEntry(HttpAttributes.HTTP_REQUEST_METHOD_ORIGINAL, requestMethod); + .containsEntry(HTTP_REQUEST_METHOD, HttpConstants._OTHER) + .containsEntry(HTTP_REQUEST_METHOD_ORIGINAL, requestMethod); } @ParameterizedTest @@ -295,8 +304,8 @@ void shouldExtractKnownMethods_override(String requestMethod) { extractor.onEnd(attributes, Context.root(), request, emptyMap(), null); assertThat(attributes.build()) - .containsEntry(HttpAttributes.HTTP_REQUEST_METHOD, requestMethod) - .doesNotContainKey(HttpAttributes.HTTP_REQUEST_METHOD_ORIGINAL); + .containsEntry(HTTP_REQUEST_METHOD, requestMethod) + .doesNotContainKey(HTTP_REQUEST_METHOD_ORIGINAL); } @ParameterizedTest @@ -315,8 +324,8 @@ void shouldUseOtherForUnknownMethods_override(String requestMethod) { extractor.onEnd(attributes, Context.root(), request, emptyMap(), null); assertThat(attributes.build()) - .containsEntry(HttpAttributes.HTTP_REQUEST_METHOD, HttpConstants._OTHER) - .containsEntry(HttpAttributes.HTTP_REQUEST_METHOD_ORIGINAL, requestMethod); + .containsEntry(HTTP_REQUEST_METHOD, HttpConstants._OTHER) + .containsEntry(HTTP_REQUEST_METHOD_ORIGINAL, requestMethod); } @Test @@ -332,8 +341,8 @@ void shouldExtractErrorType_httpStatusCode() { extractor.onEnd(attributes, Context.root(), emptyMap(), response, null); assertThat(attributes.build()) - .containsEntry(HttpAttributes.HTTP_RESPONSE_STATUS_CODE, 500) - .containsEntry(ErrorAttributes.ERROR_TYPE, "500"); + .containsEntry(HTTP_RESPONSE_STATUS_CODE, 500) + .containsEntry(ERROR_TYPE, "500"); } @Test @@ -349,7 +358,7 @@ void shouldExtractErrorType_getter() { extractor.onStart(attributes, Context.root(), emptyMap()); extractor.onEnd(attributes, Context.root(), request, emptyMap(), null); - assertThat(attributes.build()).containsEntry(ErrorAttributes.ERROR_TYPE, "custom error type"); + assertThat(attributes.build()).containsEntry(ERROR_TYPE, "custom error type"); } @Test @@ -361,8 +370,7 @@ void shouldExtractErrorType_exceptionClassName() { extractor.onStart(attributes, Context.root(), emptyMap()); extractor.onEnd(attributes, Context.root(), emptyMap(), emptyMap(), new ConnectException()); - assertThat(attributes.build()) - .containsEntry(ErrorAttributes.ERROR_TYPE, "java.net.ConnectException"); + assertThat(attributes.build()).containsEntry(ERROR_TYPE, "java.net.ConnectException"); } @Test @@ -374,7 +382,7 @@ void shouldExtractErrorType_other() { extractor.onStart(attributes, Context.root(), emptyMap()); extractor.onEnd(attributes, Context.root(), emptyMap(), emptyMap(), null); - assertThat(attributes.build()).containsEntry(ErrorAttributes.ERROR_TYPE, HttpConstants._OTHER); + assertThat(attributes.build()).containsEntry(ERROR_TYPE, HttpConstants._OTHER); } @Test @@ -391,12 +399,11 @@ void shouldPreferUrlSchemeFromForwardedHeader() { AttributesBuilder startAttributes = Attributes.builder(); extractor.onStart(startAttributes, Context.root(), request); - assertThat(startAttributes.build()).containsOnly(entry(UrlAttributes.URL_SCHEME, "https")); + assertThat(startAttributes.build()).containsOnly(entry(URL_SCHEME, "https")); AttributesBuilder endAttributes = Attributes.builder(); extractor.onEnd(endAttributes, Context.root(), request, response, null); - assertThat(endAttributes.build()) - .containsOnly(entry(HttpAttributes.HTTP_RESPONSE_STATUS_CODE, 202L)); + assertThat(endAttributes.build()).containsOnly(entry(HTTP_RESPONSE_STATUS_CODE, 202L)); } @Test @@ -417,14 +424,11 @@ void shouldExtractServerAddressAndPortFromForwardedHeader() { extractor.onStart(startAttributes, Context.root(), request); assertThat(startAttributes.build()) - .containsOnly( - entry(ServerAttributes.SERVER_ADDRESS, "example.com"), - entry(ServerAttributes.SERVER_PORT, 42L)); + .containsOnly(entry(SERVER_ADDRESS, "example.com"), entry(SERVER_PORT, 42L)); AttributesBuilder endAttributes = Attributes.builder(); extractor.onEnd(endAttributes, Context.root(), request, response, null); - assertThat(endAttributes.build()) - .containsOnly(entry(HttpAttributes.HTTP_RESPONSE_STATUS_CODE, 200L)); + assertThat(endAttributes.build()).containsOnly(entry(HTTP_RESPONSE_STATUS_CODE, 200L)); } @Test @@ -444,14 +448,11 @@ void shouldExtractServerAddressAndPortFromForwardedHostHeader() { extractor.onStart(startAttributes, Context.root(), request); assertThat(startAttributes.build()) - .containsOnly( - entry(ServerAttributes.SERVER_ADDRESS, "opentelemetry.io"), - entry(ServerAttributes.SERVER_PORT, 987L)); + .containsOnly(entry(SERVER_ADDRESS, "opentelemetry.io"), entry(SERVER_PORT, 987L)); AttributesBuilder endAttributes = Attributes.builder(); extractor.onEnd(endAttributes, Context.root(), request, response, null); - assertThat(endAttributes.build()) - .containsOnly(entry(HttpAttributes.HTTP_RESPONSE_STATUS_CODE, 200L)); + assertThat(endAttributes.build()).containsOnly(entry(HTTP_RESPONSE_STATUS_CODE, 200L)); } @Test @@ -470,14 +471,11 @@ void shouldExtractServerAddressAndPortFromAuthorityPseudoHeader() { extractor.onStart(startAttributes, Context.root(), request); assertThat(startAttributes.build()) - .containsOnly( - entry(ServerAttributes.SERVER_ADDRESS, "opentelemetry.io"), - entry(ServerAttributes.SERVER_PORT, 42L)); + .containsOnly(entry(SERVER_ADDRESS, "opentelemetry.io"), entry(SERVER_PORT, 42L)); AttributesBuilder endAttributes = Attributes.builder(); extractor.onEnd(endAttributes, Context.root(), request, response, null); - assertThat(endAttributes.build()) - .containsOnly(entry(HttpAttributes.HTTP_RESPONSE_STATUS_CODE, 200L)); + assertThat(endAttributes.build()).containsOnly(entry(HTTP_RESPONSE_STATUS_CODE, 200L)); } @Test @@ -495,14 +493,11 @@ void shouldExtractServerAddressAndPortFromHostHeader() { extractor.onStart(startAttributes, Context.root(), request); assertThat(startAttributes.build()) - .containsOnly( - entry(ServerAttributes.SERVER_ADDRESS, "github.com"), - entry(ServerAttributes.SERVER_PORT, 123L)); + .containsOnly(entry(SERVER_ADDRESS, "github.com"), entry(SERVER_PORT, 123L)); AttributesBuilder endAttributes = Attributes.builder(); extractor.onEnd(endAttributes, Context.root(), request, response, null); - assertThat(endAttributes.build()) - .containsOnly(entry(HttpAttributes.HTTP_RESPONSE_STATUS_CODE, 200L)); + assertThat(endAttributes.build()).containsOnly(entry(HTTP_RESPONSE_STATUS_CODE, 200L)); } @Test @@ -520,16 +515,15 @@ void shouldExtractPeerAddressEvenIfItDuplicatesClientAddress() { AttributesBuilder startAttributes = Attributes.builder(); extractor.onStart(startAttributes, Context.root(), request); - assertThat(startAttributes.build()) - .containsOnly(entry(ClientAttributes.CLIENT_ADDRESS, "1.2.3.4")); + assertThat(startAttributes.build()).containsOnly(entry(CLIENT_ADDRESS, "1.2.3.4")); AttributesBuilder endAttributes = Attributes.builder(); extractor.onEnd(endAttributes, Context.root(), request, response, null); assertThat(endAttributes.build()) .containsOnly( - entry(HttpAttributes.HTTP_RESPONSE_STATUS_CODE, 200L), - entry(NetworkAttributes.NETWORK_PEER_ADDRESS, "1.2.3.4"), - entry(NetworkAttributes.NETWORK_PEER_PORT, 456L)); + entry(HTTP_RESPONSE_STATUS_CODE, 200L), + entry(NETWORK_PEER_ADDRESS, "1.2.3.4"), + entry(NETWORK_PEER_PORT, 456L)); } @Test @@ -552,8 +546,8 @@ void shouldExtractProtocolNameDifferentFromHttp() { extractor.onEnd(endAttributes, Context.root(), request, response, null); assertThat(endAttributes.build()) .containsOnly( - entry(HttpAttributes.HTTP_RESPONSE_STATUS_CODE, 200L), - entry(NetworkAttributes.NETWORK_PROTOCOL_NAME, "spdy"), - entry(NetworkAttributes.NETWORK_PROTOCOL_VERSION, "3.1")); + entry(HTTP_RESPONSE_STATUS_CODE, 200L), + entry(NETWORK_PROTOCOL_NAME, "spdy"), + entry(NETWORK_PROTOCOL_VERSION, "3.1")); } } diff --git a/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/semconv/http/HttpServerMetricsTest.java b/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/semconv/http/HttpServerMetricsTest.java index 9a9da78627cd..57ea66921eed 100644 --- a/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/semconv/http/HttpServerMetricsTest.java +++ b/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/semconv/http/HttpServerMetricsTest.java @@ -7,6 +7,25 @@ import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; +import static io.opentelemetry.semconv.ErrorAttributes.ERROR_TYPE; +import static io.opentelemetry.semconv.HttpAttributes.HTTP_REQUEST_METHOD; +import static io.opentelemetry.semconv.HttpAttributes.HTTP_RESPONSE_STATUS_CODE; +import static io.opentelemetry.semconv.HttpAttributes.HTTP_ROUTE; +import static io.opentelemetry.semconv.NetworkAttributes.NETWORK_LOCAL_ADDRESS; +import static io.opentelemetry.semconv.NetworkAttributes.NETWORK_LOCAL_PORT; +import static io.opentelemetry.semconv.NetworkAttributes.NETWORK_PEER_ADDRESS; +import static io.opentelemetry.semconv.NetworkAttributes.NETWORK_PEER_PORT; +import static io.opentelemetry.semconv.NetworkAttributes.NETWORK_PROTOCOL_NAME; +import static io.opentelemetry.semconv.NetworkAttributes.NETWORK_PROTOCOL_VERSION; +import static io.opentelemetry.semconv.NetworkAttributes.NETWORK_TRANSPORT; +import static io.opentelemetry.semconv.NetworkAttributes.NETWORK_TYPE; +import static io.opentelemetry.semconv.ServerAttributes.SERVER_ADDRESS; +import static io.opentelemetry.semconv.ServerAttributes.SERVER_PORT; +import static io.opentelemetry.semconv.UrlAttributes.URL_PATH; +import static io.opentelemetry.semconv.UrlAttributes.URL_QUERY; +import static io.opentelemetry.semconv.UrlAttributes.URL_SCHEME; +import static io.opentelemetry.semconv.incubating.HttpIncubatingAttributes.HTTP_REQUEST_BODY_SIZE; +import static io.opentelemetry.semconv.incubating.HttpIncubatingAttributes.HTTP_RESPONSE_BODY_SIZE; import io.opentelemetry.api.common.Attributes; import io.opentelemetry.api.trace.Span; @@ -17,12 +36,6 @@ import io.opentelemetry.instrumentation.api.instrumenter.OperationListener; import io.opentelemetry.sdk.metrics.SdkMeterProvider; import io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader; -import io.opentelemetry.semconv.ErrorAttributes; -import io.opentelemetry.semconv.HttpAttributes; -import io.opentelemetry.semconv.NetworkAttributes; -import io.opentelemetry.semconv.ServerAttributes; -import io.opentelemetry.semconv.UrlAttributes; -import io.opentelemetry.semconv.incubating.HttpIncubatingAttributes; import java.util.concurrent.TimeUnit; import org.junit.jupiter.api.Test; @@ -41,28 +54,28 @@ void collectsMetrics() { Attributes requestAttributes = Attributes.builder() - .put(HttpAttributes.HTTP_REQUEST_METHOD, "GET") - .put(UrlAttributes.URL_SCHEME, "https") - .put(UrlAttributes.URL_PATH, "/") - .put(UrlAttributes.URL_QUERY, "q=a") - .put(NetworkAttributes.NETWORK_TRANSPORT, "tcp") - .put(NetworkAttributes.NETWORK_TYPE, "ipv4") - .put(NetworkAttributes.NETWORK_PROTOCOL_NAME, "http") - .put(NetworkAttributes.NETWORK_PROTOCOL_VERSION, "2.0") - .put(ServerAttributes.SERVER_ADDRESS, "localhost") - .put(ServerAttributes.SERVER_PORT, 1234) + .put(HTTP_REQUEST_METHOD, "GET") + .put(URL_SCHEME, "https") + .put(URL_PATH, "/") + .put(URL_QUERY, "q=a") + .put(NETWORK_TRANSPORT, "tcp") + .put(NETWORK_TYPE, "ipv4") + .put(NETWORK_PROTOCOL_NAME, "http") + .put(NETWORK_PROTOCOL_VERSION, "2.0") + .put(SERVER_ADDRESS, "localhost") + .put(SERVER_PORT, 1234) .build(); Attributes responseAttributes = Attributes.builder() - .put(HttpAttributes.HTTP_RESPONSE_STATUS_CODE, 200) - .put(ErrorAttributes.ERROR_TYPE, "500") - .put(HttpIncubatingAttributes.HTTP_REQUEST_BODY_SIZE, 100) - .put(HttpIncubatingAttributes.HTTP_RESPONSE_BODY_SIZE, 200) - .put(NetworkAttributes.NETWORK_PEER_ADDRESS, "1.2.3.4") - .put(NetworkAttributes.NETWORK_PEER_PORT, 8080) - .put(NetworkAttributes.NETWORK_LOCAL_ADDRESS, "4.3.2.1") - .put(NetworkAttributes.NETWORK_LOCAL_PORT, 9090) + .put(HTTP_RESPONSE_STATUS_CODE, 200) + .put(ERROR_TYPE, "500") + .put(HTTP_REQUEST_BODY_SIZE, 100) + .put(HTTP_RESPONSE_BODY_SIZE, 200) + .put(NETWORK_PEER_ADDRESS, "1.2.3.4") + .put(NETWORK_PEER_PORT, 8080) + .put(NETWORK_LOCAL_ADDRESS, "4.3.2.1") + .put(NETWORK_LOCAL_PORT, 9090) .build(); SpanContext spanContext1 = @@ -100,14 +113,12 @@ void collectsMetrics() { point .hasSum(0.15 /* seconds */) .hasAttributesSatisfying( - equalTo(HttpAttributes.HTTP_REQUEST_METHOD, "GET"), - equalTo(HttpAttributes.HTTP_RESPONSE_STATUS_CODE, 200), - equalTo(ErrorAttributes.ERROR_TYPE, "500"), - equalTo( - NetworkAttributes.NETWORK_PROTOCOL_NAME, "http"), - equalTo( - NetworkAttributes.NETWORK_PROTOCOL_VERSION, "2.0"), - equalTo(UrlAttributes.URL_SCHEME, "https")) + equalTo(HTTP_REQUEST_METHOD, "GET"), + equalTo(HTTP_RESPONSE_STATUS_CODE, 200), + equalTo(ERROR_TYPE, "500"), + equalTo(NETWORK_PROTOCOL_NAME, "http"), + equalTo(NETWORK_PROTOCOL_VERSION, "2.0"), + equalTo(URL_SCHEME, "https")) .hasExemplarsSatisfying( exemplar -> exemplar @@ -145,13 +156,9 @@ void collectsHttpRouteFromEndAttributes() { OperationListener listener = HttpServerMetrics.get().create(meterProvider.get("test")); Attributes requestAttributes = - Attributes.builder() - .put(ServerAttributes.SERVER_ADDRESS, "host") - .put(UrlAttributes.URL_SCHEME, "https") - .build(); + Attributes.builder().put(SERVER_ADDRESS, "host").put(URL_SCHEME, "https").build(); - Attributes responseAttributes = - Attributes.builder().put(HttpAttributes.HTTP_ROUTE, "/test/{id}").build(); + Attributes responseAttributes = Attributes.builder().put(HTTP_ROUTE, "/test/{id}").build(); Context parentContext = Context.root(); @@ -173,8 +180,8 @@ void collectsHttpRouteFromEndAttributes() { point .hasSum(0.100 /* seconds */) .hasAttributesSatisfying( - equalTo(UrlAttributes.URL_SCHEME, "https"), - equalTo(HttpAttributes.HTTP_ROUTE, "/test/{id}"))))); + equalTo(URL_SCHEME, "https"), + equalTo(HTTP_ROUTE, "/test/{id}"))))); } private static long nanos(int millis) { diff --git a/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/semconv/network/ClientAttributesExtractorTest.java b/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/semconv/network/ClientAttributesExtractorTest.java index 889568efc26c..30448b241227 100644 --- a/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/semconv/network/ClientAttributesExtractorTest.java +++ b/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/semconv/network/ClientAttributesExtractorTest.java @@ -6,6 +6,8 @@ package io.opentelemetry.instrumentation.api.semconv.network; import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; +import static io.opentelemetry.semconv.ClientAttributes.CLIENT_ADDRESS; +import static io.opentelemetry.semconv.ClientAttributes.CLIENT_PORT; import static java.util.Collections.emptyMap; import static org.assertj.core.api.Assertions.entry; @@ -13,7 +15,6 @@ import io.opentelemetry.api.common.AttributesBuilder; import io.opentelemetry.context.Context; import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; -import io.opentelemetry.semconv.ClientAttributes; import java.util.HashMap; import java.util.Map; import javax.annotation.Nullable; @@ -49,9 +50,7 @@ void allAttributes() { AttributesBuilder startAttributes = Attributes.builder(); extractor.onStart(startAttributes, Context.root(), request); assertThat(startAttributes.build()) - .containsOnly( - entry(ClientAttributes.CLIENT_ADDRESS, "opentelemetry.io"), - entry(ClientAttributes.CLIENT_PORT, 80L)); + .containsOnly(entry(CLIENT_ADDRESS, "opentelemetry.io"), entry(CLIENT_PORT, 80L)); AttributesBuilder endAttributes = Attributes.builder(); extractor.onEnd(endAttributes, Context.root(), request, null, null); diff --git a/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/semconv/network/NetworkAttributesExtractorInetSocketAddressTest.java b/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/semconv/network/NetworkAttributesExtractorInetSocketAddressTest.java index 9a96690cbabd..8d080c47477e 100644 --- a/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/semconv/network/NetworkAttributesExtractorInetSocketAddressTest.java +++ b/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/semconv/network/NetworkAttributesExtractorInetSocketAddressTest.java @@ -6,13 +6,17 @@ package io.opentelemetry.instrumentation.api.semconv.network; import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; +import static io.opentelemetry.semconv.NetworkAttributes.NETWORK_LOCAL_ADDRESS; +import static io.opentelemetry.semconv.NetworkAttributes.NETWORK_LOCAL_PORT; +import static io.opentelemetry.semconv.NetworkAttributes.NETWORK_PEER_ADDRESS; +import static io.opentelemetry.semconv.NetworkAttributes.NETWORK_PEER_PORT; +import static io.opentelemetry.semconv.NetworkAttributes.NETWORK_TYPE; import static org.assertj.core.api.Assertions.entry; import io.opentelemetry.api.common.Attributes; import io.opentelemetry.api.common.AttributesBuilder; import io.opentelemetry.context.Context; import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; -import io.opentelemetry.semconv.NetworkAttributes; import java.net.InetSocketAddress; import javax.annotation.Nullable; import org.junit.jupiter.api.Test; @@ -53,11 +57,11 @@ void fullAddress() { extractor.onEnd(endAttributes, Context.root(), local, peer, null); assertThat(endAttributes.build()) .containsOnly( - entry(NetworkAttributes.NETWORK_TYPE, "ipv4"), - entry(NetworkAttributes.NETWORK_LOCAL_ADDRESS, "1.2.3.4"), - entry(NetworkAttributes.NETWORK_LOCAL_PORT, 8080L), - entry(NetworkAttributes.NETWORK_PEER_ADDRESS, "4.3.2.1"), - entry(NetworkAttributes.NETWORK_PEER_PORT, 9090L)); + entry(NETWORK_TYPE, "ipv4"), + entry(NETWORK_LOCAL_ADDRESS, "1.2.3.4"), + entry(NETWORK_LOCAL_PORT, 8080L), + entry(NETWORK_PEER_ADDRESS, "4.3.2.1"), + entry(NETWORK_PEER_PORT, 9090L)); } @Test diff --git a/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/semconv/network/NetworkAttributesExtractorTest.java b/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/semconv/network/NetworkAttributesExtractorTest.java index 5371b2aa8b20..d277f4014177 100644 --- a/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/semconv/network/NetworkAttributesExtractorTest.java +++ b/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/semconv/network/NetworkAttributesExtractorTest.java @@ -6,6 +6,14 @@ package io.opentelemetry.instrumentation.api.semconv.network; import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; +import static io.opentelemetry.semconv.NetworkAttributes.NETWORK_LOCAL_ADDRESS; +import static io.opentelemetry.semconv.NetworkAttributes.NETWORK_LOCAL_PORT; +import static io.opentelemetry.semconv.NetworkAttributes.NETWORK_PEER_ADDRESS; +import static io.opentelemetry.semconv.NetworkAttributes.NETWORK_PEER_PORT; +import static io.opentelemetry.semconv.NetworkAttributes.NETWORK_PROTOCOL_NAME; +import static io.opentelemetry.semconv.NetworkAttributes.NETWORK_PROTOCOL_VERSION; +import static io.opentelemetry.semconv.NetworkAttributes.NETWORK_TRANSPORT; +import static io.opentelemetry.semconv.NetworkAttributes.NETWORK_TYPE; import static java.util.Collections.emptyMap; import static org.assertj.core.api.Assertions.entry; @@ -13,7 +21,6 @@ import io.opentelemetry.api.common.AttributesBuilder; import io.opentelemetry.context.Context; import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; -import io.opentelemetry.semconv.NetworkAttributes; import java.util.HashMap; import java.util.Map; import javax.annotation.Nullable; @@ -98,14 +105,14 @@ void allAttributes() { extractor.onEnd(endAttributes, Context.root(), request, null, null); assertThat(endAttributes.build()) .containsOnly( - entry(NetworkAttributes.NETWORK_TRANSPORT, "tcp"), - entry(NetworkAttributes.NETWORK_TYPE, "ipv4"), - entry(NetworkAttributes.NETWORK_PROTOCOL_NAME, "http"), - entry(NetworkAttributes.NETWORK_PROTOCOL_VERSION, "1.1"), - entry(NetworkAttributes.NETWORK_LOCAL_ADDRESS, "1.2.3.4"), - entry(NetworkAttributes.NETWORK_LOCAL_PORT, 8080L), - entry(NetworkAttributes.NETWORK_PEER_ADDRESS, "4.3.2.1"), - entry(NetworkAttributes.NETWORK_PEER_PORT, 9090L)); + entry(NETWORK_TRANSPORT, "tcp"), + entry(NETWORK_TYPE, "ipv4"), + entry(NETWORK_PROTOCOL_NAME, "http"), + entry(NETWORK_PROTOCOL_VERSION, "1.1"), + entry(NETWORK_LOCAL_ADDRESS, "1.2.3.4"), + entry(NETWORK_LOCAL_PORT, 8080L), + entry(NETWORK_PEER_ADDRESS, "4.3.2.1"), + entry(NETWORK_PEER_PORT, 9090L)); } @Test @@ -141,7 +148,6 @@ void doesNotSetNegativePortValues() { extractor.onEnd(endAttributes, Context.root(), request, null, null); assertThat(endAttributes.build()) .containsOnly( - entry(NetworkAttributes.NETWORK_LOCAL_ADDRESS, "1.2.3.4"), - entry(NetworkAttributes.NETWORK_PEER_ADDRESS, "4.3.2.1")); + entry(NETWORK_LOCAL_ADDRESS, "1.2.3.4"), entry(NETWORK_PEER_ADDRESS, "4.3.2.1")); } } diff --git a/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/semconv/network/ServerAttributesExtractorTest.java b/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/semconv/network/ServerAttributesExtractorTest.java index 9cea5e06e361..d01d180847ef 100644 --- a/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/semconv/network/ServerAttributesExtractorTest.java +++ b/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/semconv/network/ServerAttributesExtractorTest.java @@ -6,6 +6,8 @@ package io.opentelemetry.instrumentation.api.semconv.network; import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; +import static io.opentelemetry.semconv.ServerAttributes.SERVER_ADDRESS; +import static io.opentelemetry.semconv.ServerAttributes.SERVER_PORT; import static java.util.Collections.emptyMap; import static org.assertj.core.api.Assertions.entry; @@ -13,7 +15,6 @@ import io.opentelemetry.api.common.AttributesBuilder; import io.opentelemetry.context.Context; import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; -import io.opentelemetry.semconv.ServerAttributes; import java.util.HashMap; import java.util.Map; import javax.annotation.Nullable; @@ -49,9 +50,7 @@ void allAttributes() { AttributesBuilder startAttributes = Attributes.builder(); extractor.onStart(startAttributes, Context.root(), request); assertThat(startAttributes.build()) - .containsOnly( - entry(ServerAttributes.SERVER_ADDRESS, "opentelemetry.io"), - entry(ServerAttributes.SERVER_PORT, 80L)); + .containsOnly(entry(SERVER_ADDRESS, "opentelemetry.io"), entry(SERVER_PORT, 80L)); AttributesBuilder endAttributes = Attributes.builder(); extractor.onEnd(endAttributes, Context.root(), request, null, null); @@ -83,8 +82,7 @@ void doesNotSetNegativePortValue() { AttributesBuilder startAttributes = Attributes.builder(); extractor.onStart(startAttributes, Context.root(), request); - assertThat(startAttributes.build()) - .containsOnly(entry(ServerAttributes.SERVER_ADDRESS, "opentelemetry.io")); + assertThat(startAttributes.build()).containsOnly(entry(SERVER_ADDRESS, "opentelemetry.io")); } @Test diff --git a/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/semconv/url/UrlAttributesExtractorTest.java b/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/semconv/url/UrlAttributesExtractorTest.java index 41b3d8c9371d..8c8930835921 100644 --- a/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/semconv/url/UrlAttributesExtractorTest.java +++ b/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/semconv/url/UrlAttributesExtractorTest.java @@ -6,6 +6,9 @@ package io.opentelemetry.instrumentation.api.semconv.url; import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; +import static io.opentelemetry.semconv.UrlAttributes.URL_PATH; +import static io.opentelemetry.semconv.UrlAttributes.URL_QUERY; +import static io.opentelemetry.semconv.UrlAttributes.URL_SCHEME; import static java.util.Collections.emptyMap; import static org.assertj.core.api.Assertions.entry; @@ -13,7 +16,6 @@ import io.opentelemetry.api.common.AttributesBuilder; import io.opentelemetry.context.Context; import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; -import io.opentelemetry.semconv.UrlAttributes; import java.util.HashMap; import java.util.Map; import javax.annotation.Nullable; @@ -56,9 +58,7 @@ void allAttributes() { extractor.onStart(startAttributes, Context.root(), request); assertThat(startAttributes.build()) .containsOnly( - entry(UrlAttributes.URL_SCHEME, "https"), - entry(UrlAttributes.URL_PATH, "/test"), - entry(UrlAttributes.URL_QUERY, "q=Java")); + entry(URL_SCHEME, "https"), entry(URL_PATH, "/test"), entry(URL_QUERY, "q=Java")); AttributesBuilder endAttributes = Attributes.builder(); extractor.onEnd(endAttributes, Context.root(), request, null, null); diff --git a/instrumentation/apache-shenyu-2.4/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/apacheshenyu/v2_4/ShenYuRouteTest.java b/instrumentation/apache-shenyu-2.4/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/apacheshenyu/v2_4/ShenYuRouteTest.java index af4fc5be6d82..0dd60b248b56 100644 --- a/instrumentation/apache-shenyu-2.4/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/apacheshenyu/v2_4/ShenYuRouteTest.java +++ b/instrumentation/apache-shenyu-2.4/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/apacheshenyu/v2_4/ShenYuRouteTest.java @@ -6,12 +6,12 @@ package io.opentelemetry.javaagent.instrumentation.apacheshenyu.v2_4; import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; +import static io.opentelemetry.semconv.HttpAttributes.HTTP_ROUTE; 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.HttpAttributes; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import org.apache.shenyu.common.dto.MetaData; @@ -112,7 +112,7 @@ void testUpdateRouter() { span.hasName("GET /a/b/c") .hasKind(SpanKind.SERVER) .hasAttributesSatisfying( - equalTo(HttpAttributes.HTTP_ROUTE, "/a/b/c"), + equalTo(HTTP_ROUTE, "/a/b/c"), equalTo(META_ID_ATTRIBUTE, "123"), equalTo(META_ENABLED_ATTRIBUTE, true), equalTo(METHOD_NAME_ATTRIBUTE, "hello"), diff --git a/instrumentation/elasticsearch/elasticsearch-api-client-7.16/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/elasticsearch/apiclient/ElasticsearchClientTest.java b/instrumentation/elasticsearch/elasticsearch-api-client-7.16/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/elasticsearch/apiclient/ElasticsearchClientTest.java index 7e2298ac2c5f..44cf25a0f8c2 100644 --- a/instrumentation/elasticsearch/elasticsearch-api-client-7.16/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/elasticsearch/apiclient/ElasticsearchClientTest.java +++ b/instrumentation/elasticsearch/elasticsearch-api-client-7.16/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/elasticsearch/apiclient/ElasticsearchClientTest.java @@ -14,6 +14,7 @@ import static io.opentelemetry.semconv.ServerAttributes.SERVER_ADDRESS; import static io.opentelemetry.semconv.ServerAttributes.SERVER_PORT; import static io.opentelemetry.semconv.UrlAttributes.URL_FULL; +import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_ELASTICSEARCH_PATH_PARTS; import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_OPERATION; import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_SYSTEM; @@ -26,7 +27,6 @@ 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; @@ -139,13 +139,8 @@ public void elasticsearchIndex() throws IOException { URL_FULL, httpHost.toURI() + "/test-index/_doc/test-id?timeout=10s"), equalTo( - DbIncubatingAttributes.DB_ELASTICSEARCH_PATH_PARTS.getAttributeKey( - "index"), - "test-index"), - equalTo( - DbIncubatingAttributes.DB_ELASTICSEARCH_PATH_PARTS.getAttributeKey( - "id"), - "test-id")), + DB_ELASTICSEARCH_PATH_PARTS.getAttributeKey("index"), "test-index"), + equalTo(DB_ELASTICSEARCH_PATH_PARTS.getAttributeKey("id"), "test-id")), span -> span.hasName("PUT") .hasKind(SpanKind.CLIENT) diff --git a/instrumentation/vertx/vertx-redis-client-4.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/vertx/v4_0/redis/VertxRedisClientTest.java b/instrumentation/vertx/vertx-redis-client-4.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/vertx/v4_0/redis/VertxRedisClientTest.java index 116e62b1efe3..d7113fa066e4 100644 --- a/instrumentation/vertx/vertx-redis-client-4.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/vertx/v4_0/redis/VertxRedisClientTest.java +++ b/instrumentation/vertx/vertx-redis-client-4.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/vertx/v4_0/redis/VertxRedisClientTest.java @@ -11,7 +11,10 @@ import static io.opentelemetry.semconv.NetworkAttributes.NETWORK_PEER_PORT; import static io.opentelemetry.semconv.ServerAttributes.SERVER_ADDRESS; import static io.opentelemetry.semconv.ServerAttributes.SERVER_PORT; +import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_NAMESPACE; import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_OPERATION; +import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_OPERATION_NAME; +import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_QUERY_TEXT; import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_REDIS_DATABASE_INDEX; import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_STATEMENT; import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_SYSTEM; @@ -21,7 +24,6 @@ import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; import io.opentelemetry.sdk.testing.assertj.AttributeAssertion; -import io.opentelemetry.semconv.incubating.DbIncubatingAttributes; import io.vertx.core.Vertx; import io.vertx.redis.client.Redis; import io.vertx.redis.client.RedisAPI; @@ -212,9 +214,9 @@ private static AttributeAssertion[] redisSpanAttributes(String operation, String if (emitStableDatabaseSemconv()) { return new AttributeAssertion[] { equalTo(DB_SYSTEM, "redis"), - equalTo(DbIncubatingAttributes.DB_QUERY_TEXT, statement), - equalTo(DbIncubatingAttributes.DB_OPERATION_NAME, operation), - equalTo(DbIncubatingAttributes.DB_NAMESPACE, "1"), + equalTo(DB_QUERY_TEXT, statement), + equalTo(DB_OPERATION_NAME, operation), + equalTo(DB_NAMESPACE, "1"), equalTo(SERVER_ADDRESS, host), equalTo(SERVER_PORT, port), equalTo(NETWORK_PEER_PORT, port),