Skip to content

Commit 848ea4c

Browse files
otelbot[bot]trask
andauthored
Code review sweep (run 24976350016) (open-telemetry#18334)
Co-authored-by: otelbot <197425009+otelbot@users.noreply.github.com> Co-authored-by: Trask Stalnaker <trask.stalnaker@gmail.com>
1 parent 8dfa63e commit 848ea4c

7 files changed

Lines changed: 35 additions & 60 deletions

File tree

instrumentation/jdbc/testing/src/main/java/io/opentelemetry/instrumentation/jdbc/testing/AbstractJdbcInstrumentationTest.java

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
import io.opentelemetry.sdk.testing.assertj.TraceAssert;
4848
import java.beans.PropertyVetoException;
4949
import java.io.Closeable;
50-
import java.io.IOException;
5150
import java.sql.CallableStatement;
5251
import java.sql.Connection;
5352
import java.sql.Driver;
@@ -69,7 +68,6 @@
6968
import org.assertj.core.api.ThrowingConsumer;
7069
import org.h2.jdbcx.JdbcDataSource;
7170
import org.hsqldb.jdbc.JDBCDriver;
72-
import org.junit.jupiter.api.AfterAll;
7371
import org.junit.jupiter.api.BeforeAll;
7472
import org.junit.jupiter.api.DisplayName;
7573
import org.junit.jupiter.api.Test;
@@ -129,32 +127,19 @@ static void setUp() {
129127
prepareConnectionPoolDatasources();
130128
}
131129

132-
@AfterAll
133-
static void tearDown() {
134-
cpDatasources
135-
.values()
136-
.forEach(
137-
k ->
138-
k.values()
139-
.forEach(
140-
dataSource -> {
141-
if (dataSource instanceof Closeable) {
142-
try {
143-
((Closeable) dataSource).close();
144-
} catch (IOException ignored) {
145-
// ignore exceptions during close
146-
}
147-
}
148-
}));
149-
}
150-
151130
static void prepareConnectionPoolDatasources() {
152131
List<String> connectionPoolNames = asList("tomcat", "hikari", "c3p0");
153132
connectionPoolNames.forEach(
154133
cpName -> {
155134
Map<String, DataSource> dbDsMapping = new HashMap<>();
156135
jdbcUrls.forEach(
157-
(dbType, jdbcUrl) -> dbDsMapping.put(dbType, createDs(cpName, dbType, jdbcUrl)));
136+
(dbType, jdbcUrl) -> {
137+
DataSource dataSource = createDs(cpName, dbType, jdbcUrl);
138+
if (dataSource instanceof Closeable) {
139+
cleanup.deferAfterAll((Closeable) dataSource);
140+
}
141+
dbDsMapping.put(dbType, dataSource);
142+
});
158143
cpDatasources.put(cpName, dbDsMapping);
159144
});
160145
}

instrumentation/jdbc/testing/src/main/java/io/opentelemetry/instrumentation/jdbc/testing/ProxyStatementFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414

1515
class ProxyStatementFactory {
1616

17-
static Statement proxyStatementWithCustomClassLoader(Statement statement) throws Exception {
17+
static Statement proxyStatementWithCustomClassLoader(Statement statement)
18+
throws ClassNotFoundException {
1819
TestClassLoader classLoader = new TestClassLoader(ProxyStatementFactory.class.getClassLoader());
1920
Class<?> testInterface = classLoader.loadClass(TestInterface.class.getName());
2021
if (testInterface.getClassLoader() != classLoader) {

instrumentation/jedis/jedis-1.4/testing/src/main/java/io/opentelemetry/javaagent/instrumentation/jedis/AbstractJedisTest.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@
2121
import static org.assertj.core.api.Assertions.assertThat;
2222

2323
import io.opentelemetry.api.trace.SpanKind;
24+
import io.opentelemetry.instrumentation.testing.internal.AutoCleanupExtension;
2425
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
2526
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
26-
import java.io.IOException;
27-
import org.junit.jupiter.api.AfterAll;
2827
import org.junit.jupiter.api.BeforeAll;
2928
import org.junit.jupiter.api.BeforeEach;
3029
import org.junit.jupiter.api.Test;
@@ -37,6 +36,9 @@ public abstract class AbstractJedisTest {
3736
@RegisterExtension
3837
private static final InstrumentationExtension testing = AgentInstrumentationExtension.create();
3938

39+
@RegisterExtension
40+
private static final AutoCleanupExtension cleanup = AutoCleanupExtension.create();
41+
4042
private static final GenericContainer<?> redisServer =
4143
new GenericContainer<>("redis:6.2.3-alpine").withExposedPorts(6379);
4244

@@ -49,15 +51,11 @@ public abstract class AbstractJedisTest {
4951
@BeforeAll
5052
static void setup() {
5153
redisServer.start();
54+
cleanup.deferAfterAll(redisServer::stop);
5255
host = redisServer.getHost();
5356
port = redisServer.getMappedPort(6379);
5457
jedis = new Jedis(host, port);
55-
}
56-
57-
@AfterAll
58-
static void cleanup() throws IOException {
59-
jedis.disconnect();
60-
redisServer.stop();
58+
cleanup.deferAfterAll(jedis::disconnect);
6159
}
6260

6361
@BeforeEach

instrumentation/jedis/jedis-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/jedis/v3_0/Jedis30ClientTest.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@
2727
import static org.assertj.core.api.Assertions.assertThat;
2828

2929
import io.opentelemetry.api.trace.SpanKind;
30+
import io.opentelemetry.instrumentation.testing.internal.AutoCleanupExtension;
3031
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
3132
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
3233
import java.net.InetAddress;
3334
import java.net.UnknownHostException;
3435
import org.assertj.core.api.AbstractLongAssert;
35-
import org.junit.jupiter.api.AfterAll;
3636
import org.junit.jupiter.api.BeforeAll;
3737
import org.junit.jupiter.api.BeforeEach;
3838
import org.junit.jupiter.api.Test;
@@ -45,6 +45,9 @@ class Jedis30ClientTest {
4545
@RegisterExtension
4646
private static final InstrumentationExtension testing = AgentInstrumentationExtension.create();
4747

48+
@RegisterExtension
49+
private static final AutoCleanupExtension cleanup = AutoCleanupExtension.create();
50+
4851
private static final GenericContainer<?> redisServer =
4952
new GenericContainer<>("redis:6.2.3-alpine").withExposedPorts(6379);
5053

@@ -59,16 +62,12 @@ class Jedis30ClientTest {
5962
@BeforeAll
6063
static void setup() throws UnknownHostException {
6164
redisServer.start();
65+
cleanup.deferAfterAll(redisServer::stop);
6266
host = redisServer.getHost();
6367
ip = InetAddress.getByName(host).getHostAddress();
6468
port = redisServer.getMappedPort(6379);
6569
jedis = new Jedis(host, port);
66-
}
67-
68-
@AfterAll
69-
static void cleanup() {
70-
redisServer.stop();
71-
jedis.close();
70+
cleanup.deferAfterAll(jedis);
7271
}
7372

7473
@BeforeEach

instrumentation/jedis/jedis-4.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/jedis/v4_0/Jedis40ClientTest.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222
import static org.assertj.core.api.Assertions.assertThat;
2323

2424
import io.opentelemetry.api.trace.SpanKind;
25+
import io.opentelemetry.instrumentation.testing.internal.AutoCleanupExtension;
2526
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
2627
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
2728
import java.net.InetAddress;
2829
import java.net.UnknownHostException;
29-
import org.junit.jupiter.api.AfterAll;
3030
import org.junit.jupiter.api.BeforeAll;
3131
import org.junit.jupiter.api.BeforeEach;
3232
import org.junit.jupiter.api.Test;
@@ -37,29 +37,28 @@
3737
@SuppressWarnings("deprecation") // using deprecated semconv
3838
class Jedis40ClientTest {
3939
@RegisterExtension
40-
static final InstrumentationExtension testing = AgentInstrumentationExtension.create();
40+
private static final InstrumentationExtension testing = AgentInstrumentationExtension.create();
4141

42-
static GenericContainer<?> redisServer =
42+
@RegisterExtension
43+
private static final AutoCleanupExtension cleanup = AutoCleanupExtension.create();
44+
45+
private static final GenericContainer<?> redisServer =
4346
new GenericContainer<>("redis:6.2.3-alpine").withExposedPorts(6379);
4447

45-
static String ip;
48+
private static String ip;
4649

47-
static int port;
50+
private static int port;
4851

49-
static Jedis jedis;
52+
private static Jedis jedis;
5053

5154
@BeforeAll
5255
static void setup() throws UnknownHostException {
5356
redisServer.start();
57+
cleanup.deferAfterAll(redisServer::stop);
5458
port = redisServer.getMappedPort(6379);
5559
ip = InetAddress.getByName(redisServer.getHost()).getHostAddress();
5660
jedis = new Jedis(redisServer.getHost(), port);
57-
}
58-
59-
@AfterAll
60-
static void cleanup() {
61-
redisServer.stop();
62-
jedis.close();
61+
cleanup.deferAfterAll(jedis);
6362
}
6463

6564
@BeforeEach

instrumentation/jetty-httpclient/jetty-httpclient-12.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jetty/httpclient/v12_0/JettyClient12ResponseListenersInstrumentation.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,7 @@ public static Scope onEnterNotify(@Advice.Argument(0) Response response) {
5959
}
6060

6161
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class, inline = false)
62-
public static void onExitNotify(
63-
@Advice.Argument(0) Response response,
64-
@Advice.Thrown Throwable throwable,
65-
@Advice.Enter @Nullable Scope scope) {
66-
62+
public static void onExitNotify(@Advice.Enter @Nullable Scope scope) {
6763
if (scope != null) {
6864
scope.close();
6965
}
@@ -82,10 +78,7 @@ public static Scope onEnterComplete(@Advice.Argument(0) Result result) {
8278
}
8379

8480
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class, inline = false)
85-
public static void onExitComplete(
86-
@Advice.Argument(0) Result result,
87-
@Advice.Thrown Throwable throwable,
88-
@Advice.Enter @Nullable Scope scope) {
81+
public static void onExitComplete(@Advice.Enter @Nullable Scope scope) {
8982
if (scope != null) {
9083
scope.close();
9184
}

instrumentation/jetty-httpclient/jetty-httpclient-12.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jetty/httpclient/v12_0/JettyHttpClient12Instrumentation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public static AdviceLocals onEnterSend(@Advice.This HttpRequest request) {
6363
if (context == null) {
6464
return null;
6565
}
66-
// set context for responseListeners
66+
// store the parent context for request/response listener callbacks
6767
request.attribute(JETTY_CLIENT_CONTEXT_KEY, parentContext);
6868

6969
return new AdviceLocals(context, context.makeCurrent());

0 commit comments

Comments
 (0)