Skip to content
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

Use the resource auto-inference to set context.destination.service.resource for JDBC spans (#1913) #1914

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ service map and downstream service in the dependencies table - {pull}1898[#1898]
* Fix `ClassCastException` with `ConnnectionMetaData` and multiple classloaders - {pull}1864[#1864]
* Fix NPE in `co.elastic.apm.agent.servlet.helper.ServletTransactionCreationHelper.getClassloader` - {pull}1861[#1861]
* Fix for Jboss JMX unexpected notifications - {pull}1895[#1895]
* Use the resource auto-inference to set `context.destination.service.resource` for JDBC spans - {pull}1914[#1914]

[[release-notes-1.x]]
=== Java Agent version 1.x
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public Span createJdbcSpan(@Nullable String sql, Object statement, @Nullable Abs
return null;
}

Span span = parent.createSpan().activate();
Span span = parent.createExitSpan().activate();
if (sql.isEmpty()) {
span.withName("empty query");
} else if (span.isSampled()) {
Expand Down Expand Up @@ -121,7 +121,6 @@ public Span createJdbcSpan(@Nullable String sql, Object statement, @Nullable Abs
.withPort(connectionMetaData.getPort());
destination.getService()
.withName(vendor)
.withResource(vendor)
.withType(DB_SPAN_TYPE);
}
span.withSubtype(vendor).withAction(DB_SPAN_ACTION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public abstract class AbstractJdbcInstrumentationTest extends AbstractInstrument
private static final long PREPARED_STMT_TIMEOUT = 10000;

private final String expectedDbVendor;
private final String expectedDbInstance;
private Connection connection;
@Nullable
private PreparedStatement preparedStatement;
Expand All @@ -67,9 +68,10 @@ public abstract class AbstractJdbcInstrumentationTest extends AbstractInstrument
private final Transaction transaction;
private final SignatureParser signatureParser;

AbstractJdbcInstrumentationTest(Connection connection, String expectedDbVendor) throws Exception {
AbstractJdbcInstrumentationTest(Connection connection, String expectedDbVendor, String expectedDbInstance) throws Exception {
this.connection = connection;
this.expectedDbVendor = expectedDbVendor;
this.expectedDbInstance= expectedDbInstance;
connection.createStatement().execute("CREATE TABLE ELASTIC_APM (FOO INT NOT NULL, BAR VARCHAR(255))");
connection.createStatement().execute("ALTER TABLE ELASTIC_APM ADD PRIMARY KEY (FOO)");
transaction = startTestRootTransaction("jdbc-test");
Expand All @@ -78,6 +80,8 @@ public abstract class AbstractJdbcInstrumentationTest extends AbstractInstrument

@Before
public void setUp() throws Exception {
reporter.disableCheckDestinationAddress();

preparedStatement = EXECUTOR_SERVICE.submit(new Callable<PreparedStatement>() {
public PreparedStatement call() throws Exception {
return connection.prepareStatement(PREPARED_STATEMENT_SQL);
Expand Down Expand Up @@ -394,6 +398,7 @@ private Span assertSpanRecorded(String rawSql, boolean preparedStatement, long e
Span span = reporter.getFirstSpan();
StringBuilder processedSql = new StringBuilder();
signatureParser.querySignature(rawSql, processedSql, preparedStatement);
assertThat(span.isExit()).isTrue();
assertThat(span.getNameAsString()).isEqualTo(processedSql.toString());
assertThat(span.getType()).isEqualTo(DB_SPAN_TYPE);
assertThat(span.getSubtype()).isEqualTo(expectedDbVendor);
Expand All @@ -419,7 +424,11 @@ private Span assertSpanRecorded(String rawSql, boolean preparedStatement, long e
}

Destination.Service service = destination.getService();
assertThat(service.getResource().toString()).isEqualTo(expectedDbVendor);
if (expectedDbInstance != null) {
assertThat(service.getResource().toString()).isEqualTo(expectedDbVendor + "/" + expectedDbInstance);
} else {
assertThat(service.getResource().toString()).isEqualTo(expectedDbVendor);
}

assertThat(span.getOutcome())
.describedAs("span outcome should be explicitly set to either failure or success")
Expand All @@ -435,6 +444,7 @@ private void assertSpanRecordedWithoutConnection(String rawSql, boolean prepared
Span jdbcSpan = reporter.getFirstSpan();
StringBuilder processedSql = new StringBuilder();
signatureParser.querySignature(rawSql, processedSql, preparedStatement);
assertThat(jdbcSpan.isExit()).isTrue();
assertThat(jdbcSpan.getNameAsString()).isEqualTo(processedSql.toString());
assertThat(jdbcSpan.getType()).isEqualTo(DB_SPAN_TYPE);
assertThat(jdbcSpan.getSubtype()).isEqualTo("unknown");
Expand All @@ -455,7 +465,7 @@ private void assertSpanRecordedWithoutConnection(String rawSql, boolean prepared
assertThat(destination.getPort()).isLessThanOrEqualTo(0);

Destination.Service service = destination.getService();
assertThat(service.getResource()).isNullOrEmpty();
assertThat(service.getResource().toString()).isEqualTo("unknown");
}

private static long[] toLongArray(int[] a) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class DataSourceIT extends AbstractJdbcInstrumentationTest {
private static final String URL = "jdbc:h2:mem:test";

public DataSourceIT(Supplier<DataSource> dataSourceSupplier) throws Exception {
super(dataSourceSupplier.get().getConnection(), "h2");
super(dataSourceSupplier.get().getConnection(), "h2", "TEST");
}

@Parameterized.Parameters(name = "{0}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,20 @@ public class JdbcDbIT extends AbstractJdbcInstrumentationTest {
System.setProperty("oracle.jdbc.timezoneAsRegion", "false");
}

public JdbcDbIT(String url, String expectedDbVendor) throws Exception {
super(DriverManager.getConnection(url), expectedDbVendor);
public JdbcDbIT(String url, String expectedDbVendor, String expectedDbInstance) throws Exception {
super(DriverManager.getConnection(url), expectedDbVendor, expectedDbInstance);
}

@Parameterized.Parameters(name = "{1} {0}")
public static Iterable<Object[]> data() {
return Arrays.asList(new Object[][]{
{"jdbc:tc:mysql:5://hostname/databasename", "mysql"},
{"jdbc:tc:postgresql:9://hostname/databasename", "postgresql"},
{"jdbc:tc:postgresql:10://hostname/databasename", "postgresql"},
{"jdbc:tc:mariadb:10://hostname/databasename", "mariadb"},
{"jdbc:tc:sqlserver:2017-CU12://hostname/databasename", "sqlserver"},
{"jdbc:tc:db2:11.5.0.0a://hostname/databasename", "db2"},
{"jdbc:tc:oracle://hostname/databasename", "oracle"},
{"jdbc:tc:mysql:5://hostname/databasename", "mysql", "databasename"},
{"jdbc:tc:postgresql:9://hostname/databasename", "postgresql", "databasename"},
{"jdbc:tc:postgresql:10://hostname/databasename", "postgresql", "databasename"},
{"jdbc:tc:mariadb:10://hostname/databasename", "mariadb", "databasename"},
{"jdbc:tc:sqlserver:2017-CU12://hostname/databasename", "sqlserver", "master"},
{"jdbc:tc:db2:11.5.0.0a://hostname/databasename", "db2", null},
{"jdbc:tc:oracle://hostname/databasename", "oracle", null},
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
public class JdbcInstrumentationTest extends AbstractJdbcInstrumentationTest {

public JdbcInstrumentationTest() throws Exception {
super(DriverManager.getConnection("jdbc:h2:mem:test"), "h2");
super(DriverManager.getConnection("jdbc:h2:mem:test"), "h2", "TEST");
}

}