Skip to content

Commit

Permalink
Statically import semconv constants in tests (#13026)
Browse files Browse the repository at this point in the history
  • Loading branch information
trask authored Jan 10, 2025
1 parent be85182 commit ba798db
Show file tree
Hide file tree
Showing 13 changed files with 268 additions and 226 deletions.
30 changes: 30 additions & 0 deletions .github/scripts/static-import-semconv-constants.sh
Original file line number Diff line number Diff line change
@@ -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='(?<!import static io.opentelemetry.semconv.)'

for class in "ClientAttributes" "ErrorAttributes" "ExceptionAttributes" "HttpAttributes" "JvmAttributes" "NetworkAttributes" "OtelAttributes" "ServerAttributes" "ServiceAttributes" "TelemetryAttributes" "UrlAttributes" "UserAgentAttributes"; do
for attr in $(perl -ne "print \"\$1\n\" if /$negative_lookbehind$class\.([A-Z][A-Z][A-Z_]*)/" "$file" | sort -u); do
perl -i -pe "s/$negative_lookbehind$class\.$attr/$attr/" "$file"
sed -i "0,/^import/{s/^import/import static io.opentelemetry.semconv.$class.$attr;\nimport/}" "$file"
done
done

# incubating semconv

negative_lookbehind='(?<!import static io.opentelemetry.semconv.incubating.)'

for class in "AwsIncubatingAttributes" "DbIncubatingAttributes" "HttpIncubatingAttributes" "MessagingIncubatingAttributes"; do
for attr in $(perl -ne "print \"\$1\n\" if /$negative_lookbehind$class\.([A-Z][A-Z][A-Z_]*)/" "$file" | sort -u); do
perl -i -pe "s/$negative_lookbehind$class\.$attr/$attr/" "$file"
sed -i "0,/^import/{s/^import/import static io.opentelemetry.semconv.incubating.$class.$attr;\nimport/}" "$file"
done
done
done

./gradlew spotlessApply
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@
package io.opentelemetry.instrumentation.api.semconv.http;

import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
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_REQUEST_RESEND_COUNT;
import static io.opentelemetry.semconv.HttpAttributes.HTTP_RESPONSE_STATUS_CODE;
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_FULL;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;
Expand All @@ -18,11 +30,6 @@
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
import io.opentelemetry.instrumentation.api.internal.HttpConstants;
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 java.net.ConnectException;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -171,26 +178,26 @@ void normal() {
extractor.onStart(startAttributes, Context.root(), request);
assertThat(startAttributes.build())
.containsOnly(
entry(HttpAttributes.HTTP_REQUEST_METHOD, "POST"),
entry(UrlAttributes.URL_FULL, "http://github.com"),
entry(HTTP_REQUEST_METHOD, "POST"),
entry(URL_FULL, "http://github.com"),
entry(
AttributeKey.stringArrayKey("http.request.header.custom-request-header"),
asList("123", "456")),
entry(ServerAttributes.SERVER_ADDRESS, "github.com"),
entry(ServerAttributes.SERVER_PORT, 80L),
entry(HttpAttributes.HTTP_REQUEST_RESEND_COUNT, 2L));
entry(SERVER_ADDRESS, "github.com"),
entry(SERVER_PORT, 80L),
entry(HTTP_REQUEST_RESEND_COUNT, 2L));

AttributesBuilder endAttributes = Attributes.builder();
extractor.onEnd(endAttributes, Context.root(), request, response, null);
assertThat(endAttributes.build())
.containsOnly(
entry(HttpAttributes.HTTP_RESPONSE_STATUS_CODE, 202L),
entry(HTTP_RESPONSE_STATUS_CODE, 202L),
entry(
AttributeKey.stringArrayKey("http.response.header.custom-response-header"),
asList("654", "321")),
entry(NetworkAttributes.NETWORK_PROTOCOL_VERSION, "1.1"),
entry(NetworkAttributes.NETWORK_PEER_ADDRESS, "4.3.2.1"),
entry(NetworkAttributes.NETWORK_PEER_PORT, 456L));
entry(NETWORK_PROTOCOL_VERSION, "1.1"),
entry(NETWORK_PEER_ADDRESS, "4.3.2.1"),
entry(NETWORK_PEER_PORT, 456L));
}

@ParameterizedTest
Expand All @@ -207,8 +214,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
Expand All @@ -225,8 +232,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
Expand All @@ -243,8 +250,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
Expand All @@ -263,8 +270,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
Expand All @@ -283,8 +290,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
Expand All @@ -300,8 +307,8 @@ void shouldExtractErrorType_httpStatusCode() {
extractor.onEnd(attributes, Context.root(), emptyMap(), response, null);

assertThat(attributes.build())
.containsEntry(HttpAttributes.HTTP_RESPONSE_STATUS_CODE, 400)
.containsEntry(ErrorAttributes.ERROR_TYPE, "400");
.containsEntry(HTTP_RESPONSE_STATUS_CODE, 400)
.containsEntry(ERROR_TYPE, "400");
}

@Test
Expand All @@ -317,7 +324,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
Expand All @@ -329,8 +336,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
Expand All @@ -342,7 +348,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
Expand All @@ -359,14 +365,11 @@ void shouldExtractServerAddressAndPortFromHostHeader() {
AttributesBuilder startAttributes = Attributes.builder();
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
Expand All @@ -386,17 +389,15 @@ void shouldExtractPeerAddressEvenIfItDuplicatesServerAddress() {
AttributesBuilder startAttributes = Attributes.builder();
extractor.onStart(startAttributes, Context.root(), request);
assertThat(startAttributes.build())
.containsOnly(
entry(ServerAttributes.SERVER_ADDRESS, "1.2.3.4"),
entry(ServerAttributes.SERVER_PORT, 123L));
.containsOnly(entry(SERVER_ADDRESS, "1.2.3.4"), 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),
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
Expand All @@ -419,8 +420,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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@

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.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_FULL;
import static io.opentelemetry.semconv.UrlAttributes.URL_PATH;
import static io.opentelemetry.semconv.UrlAttributes.URL_QUERY;
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;
Expand All @@ -17,12 +31,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;

Expand All @@ -41,24 +49,24 @@ void collectsMetrics() {

Attributes requestAttributes =
Attributes.builder()
.put(HttpAttributes.HTTP_REQUEST_METHOD, "GET")
.put(UrlAttributes.URL_FULL, "https://localhost:1234/")
.put(UrlAttributes.URL_PATH, "/")
.put(UrlAttributes.URL_QUERY, "q=a")
.put(ServerAttributes.SERVER_ADDRESS, "localhost")
.put(ServerAttributes.SERVER_PORT, 1234)
.put(HTTP_REQUEST_METHOD, "GET")
.put(URL_FULL, "https://localhost:1234/")
.put(URL_PATH, "/")
.put(URL_QUERY, "q=a")
.put(SERVER_ADDRESS, "localhost")
.put(SERVER_PORT, 1234)
.build();

Attributes responseAttributes =
Attributes.builder()
.put(HttpAttributes.HTTP_RESPONSE_STATUS_CODE, 200)
.put(ErrorAttributes.ERROR_TYPE, "400")
.put(HttpIncubatingAttributes.HTTP_REQUEST_BODY_SIZE, 100)
.put(HttpIncubatingAttributes.HTTP_RESPONSE_BODY_SIZE, 200)
.put(NetworkAttributes.NETWORK_PROTOCOL_NAME, "http")
.put(NetworkAttributes.NETWORK_PROTOCOL_VERSION, "2.0")
.put(NetworkAttributes.NETWORK_PEER_ADDRESS, "1.2.3.4")
.put(NetworkAttributes.NETWORK_PEER_PORT, 8080)
.put(HTTP_RESPONSE_STATUS_CODE, 200)
.put(ERROR_TYPE, "400")
.put(HTTP_REQUEST_BODY_SIZE, 100)
.put(HTTP_RESPONSE_BODY_SIZE, 200)
.put(NETWORK_PROTOCOL_NAME, "http")
.put(NETWORK_PROTOCOL_VERSION, "2.0")
.put(NETWORK_PEER_ADDRESS, "1.2.3.4")
.put(NETWORK_PEER_PORT, 8080)
.build();

Context parent =
Expand Down Expand Up @@ -95,15 +103,13 @@ 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, "400"),
equalTo(
NetworkAttributes.NETWORK_PROTOCOL_NAME, "http"),
equalTo(
NetworkAttributes.NETWORK_PROTOCOL_VERSION, "2.0"),
equalTo(ServerAttributes.SERVER_ADDRESS, "localhost"),
equalTo(ServerAttributes.SERVER_PORT, 1234))
equalTo(HTTP_REQUEST_METHOD, "GET"),
equalTo(HTTP_RESPONSE_STATUS_CODE, 200),
equalTo(ERROR_TYPE, "400"),
equalTo(NETWORK_PROTOCOL_NAME, "http"),
equalTo(NETWORK_PROTOCOL_VERSION, "2.0"),
equalTo(SERVER_ADDRESS, "localhost"),
equalTo(SERVER_PORT, 1234))
.hasExemplarsSatisfying(
exemplar ->
exemplar
Expand Down
Loading

0 comments on commit ba798db

Please sign in to comment.