Skip to content

Commit ba798db

Browse files
authored
Statically import semconv constants in tests (#13026)
1 parent be85182 commit ba798db

File tree

13 files changed

+268
-226
lines changed

13 files changed

+268
-226
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/bash -e
2+
3+
# shellcheck disable=SC2044
4+
for file in $(find instrumentation instrumentation-api -name '*Test.java'); do
5+
echo "Processing $file"
6+
7+
# stable semconv
8+
9+
negative_lookbehind='(?<!import static io.opentelemetry.semconv.)'
10+
11+
for class in "ClientAttributes" "ErrorAttributes" "ExceptionAttributes" "HttpAttributes" "JvmAttributes" "NetworkAttributes" "OtelAttributes" "ServerAttributes" "ServiceAttributes" "TelemetryAttributes" "UrlAttributes" "UserAgentAttributes"; do
12+
for attr in $(perl -ne "print \"\$1\n\" if /$negative_lookbehind$class\.([A-Z][A-Z][A-Z_]*)/" "$file" | sort -u); do
13+
perl -i -pe "s/$negative_lookbehind$class\.$attr/$attr/" "$file"
14+
sed -i "0,/^import/{s/^import/import static io.opentelemetry.semconv.$class.$attr;\nimport/}" "$file"
15+
done
16+
done
17+
18+
# incubating semconv
19+
20+
negative_lookbehind='(?<!import static io.opentelemetry.semconv.incubating.)'
21+
22+
for class in "AwsIncubatingAttributes" "DbIncubatingAttributes" "HttpIncubatingAttributes" "MessagingIncubatingAttributes"; do
23+
for attr in $(perl -ne "print \"\$1\n\" if /$negative_lookbehind$class\.([A-Z][A-Z][A-Z_]*)/" "$file" | sort -u); do
24+
perl -i -pe "s/$negative_lookbehind$class\.$attr/$attr/" "$file"
25+
sed -i "0,/^import/{s/^import/import static io.opentelemetry.semconv.incubating.$class.$attr;\nimport/}" "$file"
26+
done
27+
done
28+
done
29+
30+
./gradlew spotlessApply

instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/semconv/http/HttpClientAttributesExtractorTest.java

+45-44
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@
66
package io.opentelemetry.instrumentation.api.semconv.http;
77

88
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
9+
import static io.opentelemetry.semconv.ErrorAttributes.ERROR_TYPE;
10+
import static io.opentelemetry.semconv.HttpAttributes.HTTP_REQUEST_METHOD;
11+
import static io.opentelemetry.semconv.HttpAttributes.HTTP_REQUEST_METHOD_ORIGINAL;
12+
import static io.opentelemetry.semconv.HttpAttributes.HTTP_REQUEST_RESEND_COUNT;
13+
import static io.opentelemetry.semconv.HttpAttributes.HTTP_RESPONSE_STATUS_CODE;
14+
import static io.opentelemetry.semconv.NetworkAttributes.NETWORK_PEER_ADDRESS;
15+
import static io.opentelemetry.semconv.NetworkAttributes.NETWORK_PEER_PORT;
16+
import static io.opentelemetry.semconv.NetworkAttributes.NETWORK_PROTOCOL_NAME;
17+
import static io.opentelemetry.semconv.NetworkAttributes.NETWORK_PROTOCOL_VERSION;
18+
import static io.opentelemetry.semconv.ServerAttributes.SERVER_ADDRESS;
19+
import static io.opentelemetry.semconv.ServerAttributes.SERVER_PORT;
20+
import static io.opentelemetry.semconv.UrlAttributes.URL_FULL;
921
import static java.util.Arrays.asList;
1022
import static java.util.Collections.emptyList;
1123
import static java.util.Collections.emptyMap;
@@ -18,11 +30,6 @@
1830
import io.opentelemetry.context.Context;
1931
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
2032
import io.opentelemetry.instrumentation.api.internal.HttpConstants;
21-
import io.opentelemetry.semconv.ErrorAttributes;
22-
import io.opentelemetry.semconv.HttpAttributes;
23-
import io.opentelemetry.semconv.NetworkAttributes;
24-
import io.opentelemetry.semconv.ServerAttributes;
25-
import io.opentelemetry.semconv.UrlAttributes;
2633
import java.net.ConnectException;
2734
import java.util.HashMap;
2835
import java.util.HashSet;
@@ -171,26 +178,26 @@ void normal() {
171178
extractor.onStart(startAttributes, Context.root(), request);
172179
assertThat(startAttributes.build())
173180
.containsOnly(
174-
entry(HttpAttributes.HTTP_REQUEST_METHOD, "POST"),
175-
entry(UrlAttributes.URL_FULL, "http://github.com"),
181+
entry(HTTP_REQUEST_METHOD, "POST"),
182+
entry(URL_FULL, "http://github.com"),
176183
entry(
177184
AttributeKey.stringArrayKey("http.request.header.custom-request-header"),
178185
asList("123", "456")),
179-
entry(ServerAttributes.SERVER_ADDRESS, "github.com"),
180-
entry(ServerAttributes.SERVER_PORT, 80L),
181-
entry(HttpAttributes.HTTP_REQUEST_RESEND_COUNT, 2L));
186+
entry(SERVER_ADDRESS, "github.com"),
187+
entry(SERVER_PORT, 80L),
188+
entry(HTTP_REQUEST_RESEND_COUNT, 2L));
182189

183190
AttributesBuilder endAttributes = Attributes.builder();
184191
extractor.onEnd(endAttributes, Context.root(), request, response, null);
185192
assertThat(endAttributes.build())
186193
.containsOnly(
187-
entry(HttpAttributes.HTTP_RESPONSE_STATUS_CODE, 202L),
194+
entry(HTTP_RESPONSE_STATUS_CODE, 202L),
188195
entry(
189196
AttributeKey.stringArrayKey("http.response.header.custom-response-header"),
190197
asList("654", "321")),
191-
entry(NetworkAttributes.NETWORK_PROTOCOL_VERSION, "1.1"),
192-
entry(NetworkAttributes.NETWORK_PEER_ADDRESS, "4.3.2.1"),
193-
entry(NetworkAttributes.NETWORK_PEER_PORT, 456L));
198+
entry(NETWORK_PROTOCOL_VERSION, "1.1"),
199+
entry(NETWORK_PEER_ADDRESS, "4.3.2.1"),
200+
entry(NETWORK_PEER_PORT, 456L));
194201
}
195202

196203
@ParameterizedTest
@@ -207,8 +214,8 @@ void shouldExtractKnownMethods(String requestMethod) {
207214
extractor.onEnd(attributes, Context.root(), request, emptyMap(), null);
208215

209216
assertThat(attributes.build())
210-
.containsEntry(HttpAttributes.HTTP_REQUEST_METHOD, requestMethod)
211-
.doesNotContainKey(HttpAttributes.HTTP_REQUEST_METHOD_ORIGINAL);
217+
.containsEntry(HTTP_REQUEST_METHOD, requestMethod)
218+
.doesNotContainKey(HTTP_REQUEST_METHOD_ORIGINAL);
212219
}
213220

214221
@ParameterizedTest
@@ -225,8 +232,8 @@ void shouldTreatMethodsAsCaseSensitive(String requestMethod) {
225232
extractor.onEnd(attributes, Context.root(), request, emptyMap(), null);
226233

227234
assertThat(attributes.build())
228-
.containsEntry(HttpAttributes.HTTP_REQUEST_METHOD, HttpConstants._OTHER)
229-
.containsEntry(HttpAttributes.HTTP_REQUEST_METHOD_ORIGINAL, requestMethod);
235+
.containsEntry(HTTP_REQUEST_METHOD, HttpConstants._OTHER)
236+
.containsEntry(HTTP_REQUEST_METHOD_ORIGINAL, requestMethod);
230237
}
231238

232239
@ParameterizedTest
@@ -243,8 +250,8 @@ void shouldUseOtherForUnknownMethods(String requestMethod) {
243250
extractor.onEnd(attributes, Context.root(), request, emptyMap(), null);
244251

245252
assertThat(attributes.build())
246-
.containsEntry(HttpAttributes.HTTP_REQUEST_METHOD, HttpConstants._OTHER)
247-
.containsEntry(HttpAttributes.HTTP_REQUEST_METHOD_ORIGINAL, requestMethod);
253+
.containsEntry(HTTP_REQUEST_METHOD, HttpConstants._OTHER)
254+
.containsEntry(HTTP_REQUEST_METHOD_ORIGINAL, requestMethod);
248255
}
249256

250257
@ParameterizedTest
@@ -263,8 +270,8 @@ void shouldExtractKnownMethods_override(String requestMethod) {
263270
extractor.onEnd(attributes, Context.root(), request, emptyMap(), null);
264271

265272
assertThat(attributes.build())
266-
.containsEntry(HttpAttributes.HTTP_REQUEST_METHOD, requestMethod)
267-
.doesNotContainKey(HttpAttributes.HTTP_REQUEST_METHOD_ORIGINAL);
273+
.containsEntry(HTTP_REQUEST_METHOD, requestMethod)
274+
.doesNotContainKey(HTTP_REQUEST_METHOD_ORIGINAL);
268275
}
269276

270277
@ParameterizedTest
@@ -283,8 +290,8 @@ void shouldUseOtherForUnknownMethods_override(String requestMethod) {
283290
extractor.onEnd(attributes, Context.root(), request, emptyMap(), null);
284291

285292
assertThat(attributes.build())
286-
.containsEntry(HttpAttributes.HTTP_REQUEST_METHOD, HttpConstants._OTHER)
287-
.containsEntry(HttpAttributes.HTTP_REQUEST_METHOD_ORIGINAL, requestMethod);
293+
.containsEntry(HTTP_REQUEST_METHOD, HttpConstants._OTHER)
294+
.containsEntry(HTTP_REQUEST_METHOD_ORIGINAL, requestMethod);
288295
}
289296

290297
@Test
@@ -300,8 +307,8 @@ void shouldExtractErrorType_httpStatusCode() {
300307
extractor.onEnd(attributes, Context.root(), emptyMap(), response, null);
301308

302309
assertThat(attributes.build())
303-
.containsEntry(HttpAttributes.HTTP_RESPONSE_STATUS_CODE, 400)
304-
.containsEntry(ErrorAttributes.ERROR_TYPE, "400");
310+
.containsEntry(HTTP_RESPONSE_STATUS_CODE, 400)
311+
.containsEntry(ERROR_TYPE, "400");
305312
}
306313

307314
@Test
@@ -317,7 +324,7 @@ void shouldExtractErrorType_getter() {
317324
extractor.onStart(attributes, Context.root(), emptyMap());
318325
extractor.onEnd(attributes, Context.root(), request, emptyMap(), null);
319326

320-
assertThat(attributes.build()).containsEntry(ErrorAttributes.ERROR_TYPE, "custom error type");
327+
assertThat(attributes.build()).containsEntry(ERROR_TYPE, "custom error type");
321328
}
322329

323330
@Test
@@ -329,8 +336,7 @@ void shouldExtractErrorType_exceptionClassName() {
329336
extractor.onStart(attributes, Context.root(), emptyMap());
330337
extractor.onEnd(attributes, Context.root(), emptyMap(), emptyMap(), new ConnectException());
331338

332-
assertThat(attributes.build())
333-
.containsEntry(ErrorAttributes.ERROR_TYPE, "java.net.ConnectException");
339+
assertThat(attributes.build()).containsEntry(ERROR_TYPE, "java.net.ConnectException");
334340
}
335341

336342
@Test
@@ -342,7 +348,7 @@ void shouldExtractErrorType_other() {
342348
extractor.onStart(attributes, Context.root(), emptyMap());
343349
extractor.onEnd(attributes, Context.root(), emptyMap(), emptyMap(), null);
344350

345-
assertThat(attributes.build()).containsEntry(ErrorAttributes.ERROR_TYPE, HttpConstants._OTHER);
351+
assertThat(attributes.build()).containsEntry(ERROR_TYPE, HttpConstants._OTHER);
346352
}
347353

348354
@Test
@@ -359,14 +365,11 @@ void shouldExtractServerAddressAndPortFromHostHeader() {
359365
AttributesBuilder startAttributes = Attributes.builder();
360366
extractor.onStart(startAttributes, Context.root(), request);
361367
assertThat(startAttributes.build())
362-
.containsOnly(
363-
entry(ServerAttributes.SERVER_ADDRESS, "github.com"),
364-
entry(ServerAttributes.SERVER_PORT, 123L));
368+
.containsOnly(entry(SERVER_ADDRESS, "github.com"), entry(SERVER_PORT, 123L));
365369

366370
AttributesBuilder endAttributes = Attributes.builder();
367371
extractor.onEnd(endAttributes, Context.root(), request, response, null);
368-
assertThat(endAttributes.build())
369-
.containsOnly(entry(HttpAttributes.HTTP_RESPONSE_STATUS_CODE, 200L));
372+
assertThat(endAttributes.build()).containsOnly(entry(HTTP_RESPONSE_STATUS_CODE, 200L));
370373
}
371374

372375
@Test
@@ -386,17 +389,15 @@ void shouldExtractPeerAddressEvenIfItDuplicatesServerAddress() {
386389
AttributesBuilder startAttributes = Attributes.builder();
387390
extractor.onStart(startAttributes, Context.root(), request);
388391
assertThat(startAttributes.build())
389-
.containsOnly(
390-
entry(ServerAttributes.SERVER_ADDRESS, "1.2.3.4"),
391-
entry(ServerAttributes.SERVER_PORT, 123L));
392+
.containsOnly(entry(SERVER_ADDRESS, "1.2.3.4"), entry(SERVER_PORT, 123L));
392393

393394
AttributesBuilder endAttributes = Attributes.builder();
394395
extractor.onEnd(endAttributes, Context.root(), request, response, null);
395396
assertThat(endAttributes.build())
396397
.containsOnly(
397-
entry(HttpAttributes.HTTP_RESPONSE_STATUS_CODE, 200L),
398-
entry(NetworkAttributes.NETWORK_PEER_ADDRESS, "1.2.3.4"),
399-
entry(NetworkAttributes.NETWORK_PEER_PORT, 456L));
398+
entry(HTTP_RESPONSE_STATUS_CODE, 200L),
399+
entry(NETWORK_PEER_ADDRESS, "1.2.3.4"),
400+
entry(NETWORK_PEER_PORT, 456L));
400401
}
401402

402403
@Test
@@ -419,8 +420,8 @@ void shouldExtractProtocolNameDifferentFromHttp() {
419420
extractor.onEnd(endAttributes, Context.root(), request, response, null);
420421
assertThat(endAttributes.build())
421422
.containsOnly(
422-
entry(HttpAttributes.HTTP_RESPONSE_STATUS_CODE, 200L),
423-
entry(NetworkAttributes.NETWORK_PROTOCOL_NAME, "spdy"),
424-
entry(NetworkAttributes.NETWORK_PROTOCOL_VERSION, "3.1"));
423+
entry(HTTP_RESPONSE_STATUS_CODE, 200L),
424+
entry(NETWORK_PROTOCOL_NAME, "spdy"),
425+
entry(NETWORK_PROTOCOL_VERSION, "3.1"));
425426
}
426427
}

instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/semconv/http/HttpClientMetricsTest.java

+35-29
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@
77

88
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
99
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
10+
import static io.opentelemetry.semconv.ErrorAttributes.ERROR_TYPE;
11+
import static io.opentelemetry.semconv.HttpAttributes.HTTP_REQUEST_METHOD;
12+
import static io.opentelemetry.semconv.HttpAttributes.HTTP_RESPONSE_STATUS_CODE;
13+
import static io.opentelemetry.semconv.NetworkAttributes.NETWORK_PEER_ADDRESS;
14+
import static io.opentelemetry.semconv.NetworkAttributes.NETWORK_PEER_PORT;
15+
import static io.opentelemetry.semconv.NetworkAttributes.NETWORK_PROTOCOL_NAME;
16+
import static io.opentelemetry.semconv.NetworkAttributes.NETWORK_PROTOCOL_VERSION;
17+
import static io.opentelemetry.semconv.ServerAttributes.SERVER_ADDRESS;
18+
import static io.opentelemetry.semconv.ServerAttributes.SERVER_PORT;
19+
import static io.opentelemetry.semconv.UrlAttributes.URL_FULL;
20+
import static io.opentelemetry.semconv.UrlAttributes.URL_PATH;
21+
import static io.opentelemetry.semconv.UrlAttributes.URL_QUERY;
22+
import static io.opentelemetry.semconv.incubating.HttpIncubatingAttributes.HTTP_REQUEST_BODY_SIZE;
23+
import static io.opentelemetry.semconv.incubating.HttpIncubatingAttributes.HTTP_RESPONSE_BODY_SIZE;
1024

1125
import io.opentelemetry.api.common.Attributes;
1226
import io.opentelemetry.api.trace.Span;
@@ -17,12 +31,6 @@
1731
import io.opentelemetry.instrumentation.api.instrumenter.OperationListener;
1832
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
1933
import io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader;
20-
import io.opentelemetry.semconv.ErrorAttributes;
21-
import io.opentelemetry.semconv.HttpAttributes;
22-
import io.opentelemetry.semconv.NetworkAttributes;
23-
import io.opentelemetry.semconv.ServerAttributes;
24-
import io.opentelemetry.semconv.UrlAttributes;
25-
import io.opentelemetry.semconv.incubating.HttpIncubatingAttributes;
2634
import java.util.concurrent.TimeUnit;
2735
import org.junit.jupiter.api.Test;
2836

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

4250
Attributes requestAttributes =
4351
Attributes.builder()
44-
.put(HttpAttributes.HTTP_REQUEST_METHOD, "GET")
45-
.put(UrlAttributes.URL_FULL, "https://localhost:1234/")
46-
.put(UrlAttributes.URL_PATH, "/")
47-
.put(UrlAttributes.URL_QUERY, "q=a")
48-
.put(ServerAttributes.SERVER_ADDRESS, "localhost")
49-
.put(ServerAttributes.SERVER_PORT, 1234)
52+
.put(HTTP_REQUEST_METHOD, "GET")
53+
.put(URL_FULL, "https://localhost:1234/")
54+
.put(URL_PATH, "/")
55+
.put(URL_QUERY, "q=a")
56+
.put(SERVER_ADDRESS, "localhost")
57+
.put(SERVER_PORT, 1234)
5058
.build();
5159

5260
Attributes responseAttributes =
5361
Attributes.builder()
54-
.put(HttpAttributes.HTTP_RESPONSE_STATUS_CODE, 200)
55-
.put(ErrorAttributes.ERROR_TYPE, "400")
56-
.put(HttpIncubatingAttributes.HTTP_REQUEST_BODY_SIZE, 100)
57-
.put(HttpIncubatingAttributes.HTTP_RESPONSE_BODY_SIZE, 200)
58-
.put(NetworkAttributes.NETWORK_PROTOCOL_NAME, "http")
59-
.put(NetworkAttributes.NETWORK_PROTOCOL_VERSION, "2.0")
60-
.put(NetworkAttributes.NETWORK_PEER_ADDRESS, "1.2.3.4")
61-
.put(NetworkAttributes.NETWORK_PEER_PORT, 8080)
62+
.put(HTTP_RESPONSE_STATUS_CODE, 200)
63+
.put(ERROR_TYPE, "400")
64+
.put(HTTP_REQUEST_BODY_SIZE, 100)
65+
.put(HTTP_RESPONSE_BODY_SIZE, 200)
66+
.put(NETWORK_PROTOCOL_NAME, "http")
67+
.put(NETWORK_PROTOCOL_VERSION, "2.0")
68+
.put(NETWORK_PEER_ADDRESS, "1.2.3.4")
69+
.put(NETWORK_PEER_PORT, 8080)
6270
.build();
6371

6472
Context parent =
@@ -95,15 +103,13 @@ void collectsMetrics() {
95103
point
96104
.hasSum(0.15 /* seconds */)
97105
.hasAttributesSatisfying(
98-
equalTo(HttpAttributes.HTTP_REQUEST_METHOD, "GET"),
99-
equalTo(HttpAttributes.HTTP_RESPONSE_STATUS_CODE, 200),
100-
equalTo(ErrorAttributes.ERROR_TYPE, "400"),
101-
equalTo(
102-
NetworkAttributes.NETWORK_PROTOCOL_NAME, "http"),
103-
equalTo(
104-
NetworkAttributes.NETWORK_PROTOCOL_VERSION, "2.0"),
105-
equalTo(ServerAttributes.SERVER_ADDRESS, "localhost"),
106-
equalTo(ServerAttributes.SERVER_PORT, 1234))
106+
equalTo(HTTP_REQUEST_METHOD, "GET"),
107+
equalTo(HTTP_RESPONSE_STATUS_CODE, 200),
108+
equalTo(ERROR_TYPE, "400"),
109+
equalTo(NETWORK_PROTOCOL_NAME, "http"),
110+
equalTo(NETWORK_PROTOCOL_VERSION, "2.0"),
111+
equalTo(SERVER_ADDRESS, "localhost"),
112+
equalTo(SERVER_PORT, 1234))
107113
.hasExemplarsSatisfying(
108114
exemplar ->
109115
exemplar

0 commit comments

Comments
 (0)