|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.jdbc.test; |
| 7 | + |
| 8 | +import static io.opentelemetry.api.trace.SpanKind.INTERNAL; |
| 9 | +import static io.opentelemetry.instrumentation.api.internal.SemconvStability.emitStableDatabaseSemconv; |
| 10 | +import static io.opentelemetry.instrumentation.testing.junit.db.SemconvStabilityUtil.maybeStable; |
| 11 | +import static io.opentelemetry.instrumentation.testing.junit.db.SemconvStabilityUtil.maybeStableDbSystemName; |
| 12 | +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; |
| 13 | +import static io.opentelemetry.semconv.incubating.CodeIncubatingAttributes.CODE_FUNCTION; |
| 14 | +import static io.opentelemetry.semconv.incubating.CodeIncubatingAttributes.CODE_NAMESPACE; |
| 15 | +import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_CONNECTION_STRING; |
| 16 | +import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_NAME; |
| 17 | +import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_SYSTEM; |
| 18 | +import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_USER; |
| 19 | + |
| 20 | +import com.alibaba.druid.pool.DruidDataSource; |
| 21 | +import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; |
| 22 | +import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; |
| 23 | +import java.sql.Connection; |
| 24 | +import java.sql.SQLException; |
| 25 | +import javax.sql.DataSource; |
| 26 | +import org.junit.jupiter.api.AfterEach; |
| 27 | +import org.junit.jupiter.api.BeforeEach; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 30 | + |
| 31 | +@SuppressWarnings("deprecation") // using deprecated semconv |
| 32 | +class DruicDataSourceTest { |
| 33 | + |
| 34 | + @RegisterExtension |
| 35 | + static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); |
| 36 | + |
| 37 | + private DataSource dataSource; |
| 38 | + |
| 39 | + @BeforeEach |
| 40 | + void setUp() { |
| 41 | + DruidDataSource druidDataSource = new DruidDataSource(); |
| 42 | + druidDataSource.setUrl("jdbc:h2:mem:test"); |
| 43 | + druidDataSource.setDriverClassName("org.h2.Driver"); |
| 44 | + druidDataSource.setUsername("sa"); |
| 45 | + druidDataSource.setPassword(""); |
| 46 | + druidDataSource.setMaxActive(1); |
| 47 | + this.dataSource = druidDataSource; |
| 48 | + } |
| 49 | + |
| 50 | + @AfterEach |
| 51 | + void tearDown() { |
| 52 | + if (dataSource instanceof DruidDataSource) { |
| 53 | + ((DruidDataSource) dataSource).close(); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + void testGetConnection() throws SQLException { |
| 59 | + // In DruidDataSource we instrument both DruidPooledConnection getConnection() and the bridge |
| 60 | + // method Connection getConnection(). Here we call Connection getConnection() that delegates |
| 61 | + // to DruidPooledConnection getConnection(), and verify that only one span is created. |
| 62 | + testing.runWithSpan( |
| 63 | + "parent", |
| 64 | + () -> { |
| 65 | + try (Connection connection = dataSource.getConnection()) { |
| 66 | + return null; |
| 67 | + } |
| 68 | + }); |
| 69 | + |
| 70 | + testing.waitAndAssertTraces( |
| 71 | + trace -> |
| 72 | + trace.hasSpansSatisfyingExactly( |
| 73 | + span -> span.hasName("parent").hasKind(INTERNAL).hasNoParent(), |
| 74 | + span -> |
| 75 | + span.hasName("DruidDataSource.getConnection") |
| 76 | + .hasKind(INTERNAL) |
| 77 | + .hasParent(trace.getSpan(0)) |
| 78 | + .hasAttributesSatisfyingExactly( |
| 79 | + equalTo(CODE_NAMESPACE, "com.alibaba.druid.pool.DruidDataSource"), |
| 80 | + equalTo(CODE_FUNCTION, "getConnection"), |
| 81 | + equalTo( |
| 82 | + DB_CONNECTION_STRING, |
| 83 | + emitStableDatabaseSemconv() ? null : "h2:mem:"), |
| 84 | + equalTo(maybeStable(DB_NAME), "test"), |
| 85 | + equalTo(maybeStable(DB_SYSTEM), maybeStableDbSystemName("h2")), |
| 86 | + equalTo(DB_USER, emitStableDatabaseSemconv() ? null : "sa")))); |
| 87 | + } |
| 88 | +} |
0 commit comments