Skip to content

Commit 9403e22

Browse files
otelbot[bot]trask
andauthored
Code review sweep (run 24968439270) (#18325)
Co-authored-by: otelbot <197425009+otelbot@users.noreply.github.com> Co-authored-by: Trask Stalnaker <trask.stalnaker@gmail.com>
1 parent d780054 commit 9403e22

11 files changed

Lines changed: 66 additions & 44 deletions

File tree

instrumentation/java-util-logging/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jul/JavaUtilLoggingHelper.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
public class JavaUtilLoggingHelper {
3131

32-
private static final Formatter FORMATTER = new AccessibleFormatter();
32+
private static final Formatter formatter = new AccessibleFormatter();
3333

3434
private static final boolean captureExperimentalAttributes =
3535
DeclarativeConfigUtil.getInstrumentationConfig(GlobalOpenTelemetry.get(), "java_util_logging")
@@ -68,7 +68,7 @@ public static void capture(application.java.util.logging.Logger logger, LogRecor
6868
*/
6969
private static void mapLogRecord(LogRecordBuilder builder, LogRecord logRecord) {
7070
// message
71-
String message = FORMATTER.formatMessage(logRecord);
71+
String message = formatter.formatMessage(logRecord);
7272
if (message != null) {
7373
builder.setBody(message);
7474
}
@@ -82,7 +82,7 @@ private static void mapLogRecord(LogRecordBuilder builder, LogRecord logRecord)
8282
Level level = logRecord.getLevel();
8383
if (level != null) {
8484
builder.setSeverity(levelToSeverity(level));
85-
builder.setSeverityText(logRecord.getLevel().getName());
85+
builder.setSeverityText(level.getName());
8686
}
8787

8888
AttributesBuilder attributes = Attributes.builder();

instrumentation/java-util-logging/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/jul/JavaUtilLoggingTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,18 +161,18 @@ private static void performLogging(
161161
}
162162

163163
@FunctionalInterface
164-
interface LoggerMethod {
164+
private interface LoggerMethod {
165165
void call(Logger logger, String msg);
166166
}
167167

168-
static String experimental(String value) {
168+
private static String experimental(String value) {
169169
if (isExperimentalAttributesEnabled) {
170170
return value;
171171
}
172172
return null;
173173
}
174174

175-
static Long experimental(long value) {
175+
private static Long experimental(long value) {
176176
if (isExperimentalAttributesEnabled) {
177177
return value;
178178
}

instrumentation/javalin/javalin-5.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/javalin/v5_0/JavalinInstrumentationModule.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ public List<TypeInstrumentation> typeInstrumentations() {
3030

3131
@Override
3232
public ElementMatcher.Junction<ClassLoader> classLoaderMatcher() {
33-
return hasClassesNamed("io.javalin.http.Handler")
33+
// added in 5.0.0
34+
return hasClassesNamed("io.javalin.config.JavalinConfig")
3435
// added in 7.0.0
3536
.and(not(hasClassesNamed("io.javalin.config.JavalinState")));
3637
}

instrumentation/javalin/testing/src/main/java/io/opentelemetry/instrumentation/javalin/AbstractJavalinTest.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,37 +26,39 @@
2626
import io.javalin.Javalin;
2727
import io.opentelemetry.api.trace.SpanKind;
2828
import io.opentelemetry.instrumentation.test.utils.PortUtils;
29-
import io.opentelemetry.instrumentation.testing.internal.AutoCleanupExtension;
3029
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
3130
import io.opentelemetry.testing.internal.armeria.client.WebClient;
3231
import io.opentelemetry.testing.internal.armeria.common.AggregatedHttpResponse;
32+
import org.junit.jupiter.api.AfterAll;
3333
import org.junit.jupiter.api.BeforeAll;
3434
import org.junit.jupiter.api.Test;
3535
import org.junit.jupiter.api.TestInstance;
36-
import org.junit.jupiter.api.extension.RegisterExtension;
3736

3837
@TestInstance(PER_CLASS)
3938
public abstract class AbstractJavalinTest {
4039

41-
@RegisterExtension static final AutoCleanupExtension cleanup = AutoCleanupExtension.create();
42-
4340
protected abstract InstrumentationExtension testing();
4441

4542
protected abstract Javalin setupJavalin(int port);
4643

4744
protected abstract String getJettyInstrumentation();
4845

4946
private int port;
47+
private Javalin app;
5048
private WebClient client;
5149

5250
@BeforeAll
5351
void setup() {
5452
port = PortUtils.findOpenPort();
55-
Javalin app = setupJavalin(port);
56-
cleanup.deferAfterAll(app::stop);
53+
app = setupJavalin(port);
5754
client = WebClient.of("http://localhost:" + port);
5855
}
5956

57+
@AfterAll
58+
void stopJavalin() {
59+
app.stop();
60+
}
61+
6062
@Test
6163
void testSpanNameAndHttpRouteSpanWithPathParamResponseSuccessful() {
6264
String id = "123";

instrumentation/jaxrs-client/jaxrs-client-1.1-testing/src/test/java/io/opentelemetry/javaagent/instrumentation/jaxrsclient/JaxRsClientV1Test.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ private static Client buildClient(boolean readTimeout) {
4444
client.addFilter(new LoggingFilter());
4545
client.addFilter(new GZIPContentEncodingFilter());
4646

47+
// shared across tests via @TestInstance(PER_CLASS), destroy after the class finishes
4748
cleanup.deferAfterAll(client::destroy);
4849
return client;
4950
}

instrumentation/jaxrs-client/jaxrs-client-2.0-testing/src/test/java/io/opentelemetry/javaagent/instrumentation/jaxrsclient/AbstractJaxRsClientTest.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,13 @@ public int sendRequest(
6060
try {
6161
Entity<String> body = BODY_METHODS.contains(method) ? Entity.text("") : null;
6262
Response response = request.build(method, body).invoke();
63-
// read response body to avoid broken pipe errors on the server side
64-
response.readEntity(String.class);
65-
response.close();
66-
return response.getStatus();
63+
try {
64+
// read response body to avoid broken pipe errors on the server side
65+
response.readEntity(String.class);
66+
return response.getStatus();
67+
} finally {
68+
response.close();
69+
}
6770
} catch (ProcessingException e) {
6871
if (e.getCause() instanceof Exception) {
6972
throw (Exception) e.getCause();
@@ -89,9 +92,13 @@ public void sendRequestWithCallback(
8992
new InvocationCallback<Response>() {
9093
@Override
9194
public void completed(Response response) {
92-
// read response body
93-
response.readEntity(String.class);
94-
requestResult.complete(response.getStatus());
95+
try {
96+
// read response body
97+
response.readEntity(String.class);
98+
requestResult.complete(response.getStatus());
99+
} finally {
100+
response.close();
101+
}
95102
}
96103

97104
@Override

instrumentation/jaxrs-client/jaxrs-client-2.0-testing/src/test/java/io/opentelemetry/javaagent/instrumentation/jaxrsclient/JaxMultithreadedClientTest.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import io.opentelemetry.testing.internal.armeria.testing.junit5.server.ServerExtension;
1818
import java.net.URI;
1919
import java.util.concurrent.CountDownLatch;
20+
import java.util.concurrent.atomic.AtomicBoolean;
2021
import javax.ws.rs.client.Client;
2122
import org.glassfish.jersey.client.JerseyClientBuilder;
2223
import org.junit.jupiter.api.DisplayName;
@@ -41,20 +42,23 @@ protected void configure(ServerBuilder sb) {
4142

4243
@SuppressWarnings("CatchingUnchecked")
4344
boolean checkUri(JerseyClientBuilder builder, URI uri) {
45+
Client client = builder.build();
4446
try {
45-
Client client = builder.build();
46-
client.target(uri).request().get();
47+
client.target(uri).request().get().close();
48+
return false;
4749
} catch (Exception e) {
4850
return true;
51+
} finally {
52+
client.close();
4953
}
50-
return false;
5154
}
5255

5356
@DisplayName("multiple threads using the same builder works")
5457
@Test
5558
void testMultipleThreads() throws InterruptedException {
5659
URI uri = server.httpUri().resolve("/success");
5760
JerseyClientBuilder builder = new JerseyClientBuilder();
61+
AtomicBoolean hadErrors = new AtomicBoolean();
5862

5963
// Start 10 threads and do 50 requests each
6064
CountDownLatch latch = new CountDownLatch(10);
@@ -63,17 +67,22 @@ void testMultipleThreads() throws InterruptedException {
6367
new Runnable() {
6468
@Override
6569
public void run() {
66-
boolean hadErrors = false;
67-
for (int j = 0; j < 50; j++) {
68-
hadErrors = hadErrors || checkUri(builder, uri);
70+
try {
71+
for (int j = 0; j < 50; j++) {
72+
if (checkUri(builder, uri)) {
73+
hadErrors.set(true);
74+
return;
75+
}
76+
}
77+
} finally {
78+
latch.countDown();
6979
}
70-
assertThat(hadErrors).isFalse();
71-
latch.countDown();
7280
}
7381
})
7482
.start();
7583
}
7684

7785
assertThat(latch.await(10, SECONDS)).isTrue();
86+
assertThat(hadErrors).isFalse();
7887
}
7988
}

instrumentation/jaxrs-client/jaxrs-client-2.0-testing/src/test/java/io/opentelemetry/javaagent/instrumentation/jaxrsclient/ResteasySingleConnection.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,16 @@ public int doRequest(String path, Map<String, String> headers) throws ExecutionE
5454
}
5555

5656
Response response = requestBuilder.buildGet().invoke();
57-
response.close();
57+
try {
58+
String responseId = response.getHeaderString(REQUEST_ID_HEADER);
59+
if (!Objects.equals(requestId, responseId)) {
60+
throw new IllegalStateException(
61+
String.format("Received response with id %s, expected %s", responseId, requestId));
62+
}
5863

59-
String responseId = response.getHeaderString(REQUEST_ID_HEADER);
60-
if (!Objects.equals(requestId, responseId)) {
61-
throw new IllegalStateException(
62-
String.format("Received response with id %s, expected %s", responseId, requestId));
64+
return response.getStatus();
65+
} finally {
66+
response.close();
6367
}
64-
65-
return response.getStatus();
6668
}
6769
}

instrumentation/jaxrs/jaxrs-1.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jaxrs/v1_0/HandlerData.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import javax.ws.rs.HttpMethod;
1414
import javax.ws.rs.Path;
1515

16-
public class HandlerData {
16+
class HandlerData {
1717

1818
private static final ClassValue<Map<Method, String>> serverSpanNames =
1919
new ClassValue<Map<Method, String>>() {
@@ -26,16 +26,16 @@ protected Map<Method, String> computeValue(Class<?> type) {
2626
private final Class<?> target;
2727
private final Method method;
2828

29-
public HandlerData(Class<?> target, Method method) {
29+
HandlerData(Class<?> target, Method method) {
3030
this.target = target;
3131
this.method = method;
3232
}
3333

34-
public Class<?> codeClass() {
34+
Class<?> codeClass() {
3535
return target;
3636
}
3737

38-
public String methodName() {
38+
String methodName() {
3939
return method.getName();
4040
}
4141

instrumentation/jaxrs/jaxrs-1.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jaxrs/v1_0/JaxrsServerSpanNaming.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
import io.opentelemetry.javaagent.bootstrap.jaxrs.JaxrsContextPath;
1010
import io.opentelemetry.javaagent.bootstrap.servlet.ServletContextPath;
1111

12-
public class JaxrsServerSpanNaming {
12+
class JaxrsServerSpanNaming {
1313

1414
private static final HttpServerRouteGetter<HandlerData> serverSpanName =
1515
(context, handlerData) -> {
1616
String pathBasedSpanName = handlerData.getServerSpanName();
1717
// If path based name is empty skip prepending context path so that path based name would
18-
// remain as an empty string for which we skip updating span name. Path base span name is
18+
// remain as an empty string for which we skip updating span name. Path based span name is
1919
// empty when method and class don't have a jax-rs path annotation, this can happen when
2020
// creating an "abort" span, see RequestContextHelper.
2121
if (!pathBasedSpanName.isEmpty()) {
@@ -25,7 +25,7 @@ public class JaxrsServerSpanNaming {
2525
return pathBasedSpanName;
2626
};
2727

28-
public static HttpServerRouteGetter<HandlerData> serverSpanName() {
28+
static HttpServerRouteGetter<HandlerData> serverSpanName() {
2929
return serverSpanName;
3030
}
3131

0 commit comments

Comments
 (0)