|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.vertx.v4_0.sql; |
| 7 | + |
| 8 | +import static io.opentelemetry.instrumentation.api.internal.SemconvStability.emitStableDatabaseSemconv; |
| 9 | +import static io.opentelemetry.instrumentation.testing.junit.db.SemconvStabilityUtil.maybeStable; |
| 10 | +import static io.opentelemetry.instrumentation.testing.junit.db.SemconvStabilityUtil.maybeStableDbSystemName; |
| 11 | +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; |
| 12 | +import static io.opentelemetry.semconv.DbAttributes.DB_QUERY_SUMMARY; |
| 13 | +import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_CONNECTION_STRING; |
| 14 | +import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_NAME; |
| 15 | +import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_OPERATION; |
| 16 | +import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_SQL_TABLE; |
| 17 | +import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_STATEMENT; |
| 18 | +import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_SYSTEM; |
| 19 | +import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_USER; |
| 20 | +import static java.util.concurrent.TimeUnit.SECONDS; |
| 21 | + |
| 22 | +import io.opentelemetry.api.trace.SpanKind; |
| 23 | +import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; |
| 24 | +import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; |
| 25 | +import io.vertx.core.Vertx; |
| 26 | +import io.vertx.jdbcclient.JDBCConnectOptions; |
| 27 | +import io.vertx.jdbcclient.JDBCPool; |
| 28 | +import io.vertx.sqlclient.PoolOptions; |
| 29 | +import org.junit.jupiter.api.AfterAll; |
| 30 | +import org.junit.jupiter.api.BeforeAll; |
| 31 | +import org.junit.jupiter.api.Test; |
| 32 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 33 | + |
| 34 | +/** |
| 35 | + * Tests that vertx-sql-client instrumentation is suppressed for JDBC-backed connections and JDBC |
| 36 | + * instrumentation handles them instead. |
| 37 | + */ |
| 38 | +@SuppressWarnings("deprecation") // using deprecated semconv |
| 39 | +class VertxJdbcClientTest { |
| 40 | + |
| 41 | + private static final String DB = "testdb"; |
| 42 | + |
| 43 | + @RegisterExtension |
| 44 | + private static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); |
| 45 | + |
| 46 | + private static Vertx vertx; |
| 47 | + private static io.vertx.sqlclient.Pool pool; |
| 48 | + |
| 49 | + @BeforeAll |
| 50 | + static void setUp() throws Exception { |
| 51 | + vertx = Vertx.vertx(); |
| 52 | + pool = |
| 53 | + JDBCPool.pool( |
| 54 | + vertx, |
| 55 | + new JDBCConnectOptions().setJdbcUrl("jdbc:hsqldb:mem:" + DB), |
| 56 | + new PoolOptions().setMaxSize(4)); |
| 57 | + pool.query("create table test(id int primary key, name varchar(255))") |
| 58 | + .execute() |
| 59 | + .compose(r -> pool.query("insert into test values (1, 'Hello'), (2, 'World')").execute()) |
| 60 | + .toCompletionStage() |
| 61 | + .toCompletableFuture() |
| 62 | + .get(30, SECONDS); |
| 63 | + } |
| 64 | + |
| 65 | + @AfterAll |
| 66 | + static void cleanUp() { |
| 67 | + pool.close(); |
| 68 | + vertx.close(); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + void testSimpleSelect() throws Exception { |
| 73 | + testing |
| 74 | + .runWithSpan("parent", () -> pool.query("select * from test").execute()) |
| 75 | + .toCompletionStage() |
| 76 | + .toCompletableFuture() |
| 77 | + .get(30, SECONDS); |
| 78 | + |
| 79 | + testing.waitAndAssertTraces( |
| 80 | + trace -> |
| 81 | + trace.hasSpansSatisfyingExactly( |
| 82 | + span -> span.hasName("parent").hasKind(SpanKind.INTERNAL), |
| 83 | + span -> |
| 84 | + span.hasName( |
| 85 | + emitStableDatabaseSemconv() ? "SELECT test" : "SELECT " + DB + ".test") |
| 86 | + .hasKind(SpanKind.CLIENT) |
| 87 | + .hasParent(trace.getSpan(0)) |
| 88 | + .hasAttributesSatisfyingExactly( |
| 89 | + equalTo(maybeStable(DB_SYSTEM), maybeStableDbSystemName("hsqldb")), |
| 90 | + equalTo(maybeStable(DB_NAME), DB), |
| 91 | + equalTo(DB_USER, emitStableDatabaseSemconv() ? null : "SA"), |
| 92 | + equalTo( |
| 93 | + DB_CONNECTION_STRING, |
| 94 | + emitStableDatabaseSemconv() ? null : "hsqldb:mem:"), |
| 95 | + equalTo(maybeStable(DB_STATEMENT), "select * from test"), |
| 96 | + equalTo( |
| 97 | + DB_QUERY_SUMMARY, |
| 98 | + emitStableDatabaseSemconv() ? "SELECT test" : null), |
| 99 | + equalTo( |
| 100 | + maybeStable(DB_OPERATION), |
| 101 | + emitStableDatabaseSemconv() ? null : "SELECT"), |
| 102 | + equalTo( |
| 103 | + maybeStable(DB_SQL_TABLE), |
| 104 | + emitStableDatabaseSemconv() ? null : "test")))); |
| 105 | + } |
| 106 | +} |
0 commit comments