-
Notifications
You must be signed in to change notification settings - Fork 1k
fix: datasource instrumentation to support custom connection #14602
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f737744
fix: DataSource instrumentation to support custom connection
wl2027 2c304a5
fix covariant return type
wl2027 8092e60
fix ci test
wl2027 20b1364
fix test
wl2027 6ca07be
Merge branch 'open-telemetry:main' into jdbc-ds
wl2027 6a9f18d
review
laurit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
...a/io/opentelemetry/javaagent/instrumentation/jdbc/test/DataSourceInstrumentationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.javaagent.instrumentation.jdbc.test; | ||
|
|
||
| import static io.opentelemetry.api.trace.SpanKind.INTERNAL; | ||
| import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; | ||
| import static io.opentelemetry.semconv.incubating.CodeIncubatingAttributes.CODE_FUNCTION; | ||
| import static io.opentelemetry.semconv.incubating.CodeIncubatingAttributes.CODE_NAMESPACE; | ||
| import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_NAME; | ||
| import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_SYSTEM; | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import com.alibaba.druid.pool.DruidDataSource; | ||
| import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; | ||
| import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; | ||
| import io.opentelemetry.sdk.trace.data.SpanData; | ||
| import java.sql.Connection; | ||
| import java.sql.SQLException; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
| import javax.sql.DataSource; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
|
||
| @SuppressWarnings("deprecation") // using deprecated semconv | ||
| class DataSourceInstrumentationTest { | ||
|
|
||
| @RegisterExtension | ||
| static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); | ||
|
|
||
| private DataSource dataSource; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| DruidDataSource druidDataSource = new DruidDataSource(); | ||
| druidDataSource.setUrl("jdbc:h2:mem:test"); | ||
| druidDataSource.setDriverClassName("org.h2.Driver"); | ||
| druidDataSource.setUsername("sa"); | ||
| druidDataSource.setPassword(""); | ||
| druidDataSource.setMaxActive(1); | ||
| this.dataSource = druidDataSource; | ||
| } | ||
|
|
||
| @AfterEach | ||
| void tearDown() { | ||
| if (dataSource instanceof DruidDataSource) { | ||
| ((DruidDataSource) dataSource).close(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void testDruidDataSourceGetConnection() throws SQLException { | ||
| testing.runWithSpan( | ||
| "parent", | ||
| () -> { | ||
| try (Connection connection = dataSource.getConnection()) { | ||
| return null; | ||
| } catch (SQLException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| }); | ||
|
|
||
| testing.waitAndAssertTraces( | ||
| trace -> | ||
| trace.hasSpansSatisfyingExactly( | ||
| span -> span.hasName("parent").hasKind(INTERNAL).hasNoParent(), | ||
| span -> | ||
| span.hasName("DruidDataSource.getConnection") | ||
| .hasKind(INTERNAL) | ||
| .hasParent(trace.getSpan(0)) | ||
| .hasAttributesSatisfying( | ||
wl2027 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| equalTo(CODE_NAMESPACE, "com.alibaba.druid.pool.DruidDataSource"), | ||
| equalTo(CODE_FUNCTION, "getConnection"), | ||
| equalTo(DB_SYSTEM, "h2"), | ||
wl2027 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| equalTo(DB_NAME, "test")))); | ||
|
|
||
| List<SpanData> spans = testing.spans(); | ||
| List<SpanData> dataSourceSpans = | ||
| spans.stream() | ||
| .filter(span -> span.getName().equals("DruidDataSource.getConnection")) | ||
| .collect(Collectors.toList()); | ||
|
|
||
| assertThat(dataSourceSpans) | ||
| .as("Should have exactly one DruidDataSource.getConnection span, not duplicates") | ||
| .hasSize(1); | ||
wl2027 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.