From 23f99798596ad77489e5cc0fde5446be45b48e4e Mon Sep 17 00:00:00 2001 From: Tobias Stadler Date: Mon, 12 Jul 2021 16:48:21 +0200 Subject: [PATCH 1/4] Use the resource auto-inference to set context.destination.service.resource for JDBC spans (#1913) --- .../apm/agent/jdbc/JDBCConfiguration.java | 41 +++++++++++++++++++ .../apm/agent/jdbc/helper/JdbcHelper.java | 13 +++++- ....configuration.ConfigurationOptionProvider | 1 + .../jdbc/AbstractJdbcInstrumentationTest.java | 16 ++++++-- .../elastic/apm/agent/jdbc/DataSourceIT.java | 2 +- .../co/elastic/apm/agent/jdbc/JdbcDbIT.java | 18 ++++---- .../agent/jdbc/JdbcInstrumentationTest.java | 2 +- .../JdbcServiceResourceAutoInferenceTest.java | 37 +++++++++++++++++ docs/configuration.asciidoc | 41 +++++++++++++++++++ 9 files changed, 155 insertions(+), 16 deletions(-) create mode 100644 apm-agent-plugins/apm-jdbc-plugin/src/main/java/co/elastic/apm/agent/jdbc/JDBCConfiguration.java create mode 100644 apm-agent-plugins/apm-jdbc-plugin/src/main/resources/META-INF/services/org.stagemonitor.configuration.ConfigurationOptionProvider create mode 100644 apm-agent-plugins/apm-jdbc-plugin/src/test/java/co/elastic/apm/agent/jdbc/JdbcServiceResourceAutoInferenceTest.java diff --git a/apm-agent-plugins/apm-jdbc-plugin/src/main/java/co/elastic/apm/agent/jdbc/JDBCConfiguration.java b/apm-agent-plugins/apm-jdbc-plugin/src/main/java/co/elastic/apm/agent/jdbc/JDBCConfiguration.java new file mode 100644 index 0000000000..85750609d7 --- /dev/null +++ b/apm-agent-plugins/apm-jdbc-plugin/src/main/java/co/elastic/apm/agent/jdbc/JDBCConfiguration.java @@ -0,0 +1,41 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package co.elastic.apm.agent.jdbc; + +import org.stagemonitor.configuration.ConfigurationOption; +import org.stagemonitor.configuration.ConfigurationOptionProvider; + +public class JDBCConfiguration extends ConfigurationOptionProvider { + + private static final String JDBC_CATEGORY = "JDBC"; + + private final ConfigurationOption use_service_resource_auto_inference = ConfigurationOption.booleanOption() + .key("use_jdbc_service_resource_auto_inference") + .configurationCategory(JDBC_CATEGORY) + .description("If set to `true`, the agent uses `type`, `subtype` and `db.instance` of a JDBC span to infer its `destination.service.resource`" + + " instead of relying on the JDBC instrumentation to set it.\n" + + "See for https://github.com/elastic/apm/blob/master/specs/agents/tracing-spans-destination.md#contextdestinationserviceresource for the inference algorithm.") + .dynamic(false) + .tags("added[1.25.0]") + .buildWithDefault(false); + + public boolean getUseJDBCServiceResourceAutoInference() { + return use_service_resource_auto_inference.get(); + } +} diff --git a/apm-agent-plugins/apm-jdbc-plugin/src/main/java/co/elastic/apm/agent/jdbc/helper/JdbcHelper.java b/apm-agent-plugins/apm-jdbc-plugin/src/main/java/co/elastic/apm/agent/jdbc/helper/JdbcHelper.java index 7158b367c5..0e6889d78a 100644 --- a/apm-agent-plugins/apm-jdbc-plugin/src/main/java/co/elastic/apm/agent/jdbc/helper/JdbcHelper.java +++ b/apm-agent-plugins/apm-jdbc-plugin/src/main/java/co/elastic/apm/agent/jdbc/helper/JdbcHelper.java @@ -20,9 +20,11 @@ import co.elastic.apm.agent.db.signature.Scanner; import co.elastic.apm.agent.db.signature.SignatureParser; +import co.elastic.apm.agent.impl.GlobalTracer; import co.elastic.apm.agent.impl.context.Destination; import co.elastic.apm.agent.impl.transaction.AbstractSpan; import co.elastic.apm.agent.impl.transaction.Span; +import co.elastic.apm.agent.jdbc.JDBCConfiguration; import co.elastic.apm.agent.jdbc.JdbcFilter; import com.blogspot.mydailyjava.weaklockfree.WeakConcurrentMap; import org.slf4j.Logger; @@ -44,6 +46,10 @@ public class JdbcHelper { public static final String DB_SPAN_TYPE = "db"; public static final String DB_SPAN_ACTION = "query"; + private static final boolean useJDBCServiceResourceAutoInference = GlobalTracer.getTracerImpl() + .getConfig(JDBCConfiguration.class) + .getUseJDBCServiceResourceAutoInference(); + private static final JdbcHelper INSTANCE = new JdbcHelper(); public static JdbcHelper get() { @@ -87,7 +93,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()) { @@ -121,8 +127,11 @@ public Span createJdbcSpan(@Nullable String sql, Object statement, @Nullable Abs .withPort(connectionMetaData.getPort()); destination.getService() .withName(vendor) - .withResource(vendor) .withType(DB_SPAN_TYPE); + + if(!useJDBCServiceResourceAutoInference) { + destination.getService().withResource(vendor); + } } span.withSubtype(vendor).withAction(DB_SPAN_ACTION); diff --git a/apm-agent-plugins/apm-jdbc-plugin/src/main/resources/META-INF/services/org.stagemonitor.configuration.ConfigurationOptionProvider b/apm-agent-plugins/apm-jdbc-plugin/src/main/resources/META-INF/services/org.stagemonitor.configuration.ConfigurationOptionProvider new file mode 100644 index 0000000000..210cb085a6 --- /dev/null +++ b/apm-agent-plugins/apm-jdbc-plugin/src/main/resources/META-INF/services/org.stagemonitor.configuration.ConfigurationOptionProvider @@ -0,0 +1 @@ +co.elastic.apm.agent.jdbc.JDBCConfiguration diff --git a/apm-agent-plugins/apm-jdbc-plugin/src/test/java/co/elastic/apm/agent/jdbc/AbstractJdbcInstrumentationTest.java b/apm-agent-plugins/apm-jdbc-plugin/src/test/java/co/elastic/apm/agent/jdbc/AbstractJdbcInstrumentationTest.java index 207456e0b3..1c3106e432 100644 --- a/apm-agent-plugins/apm-jdbc-plugin/src/test/java/co/elastic/apm/agent/jdbc/AbstractJdbcInstrumentationTest.java +++ b/apm-agent-plugins/apm-jdbc-plugin/src/test/java/co/elastic/apm/agent/jdbc/AbstractJdbcInstrumentationTest.java @@ -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; @@ -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"); @@ -78,6 +80,8 @@ public abstract class AbstractJdbcInstrumentationTest extends AbstractInstrument @Before public void setUp() throws Exception { + reporter.disableCheckDestinationAddress(); + preparedStatement = EXECUTOR_SERVICE.submit(new Callable() { public PreparedStatement call() throws Exception { return connection.prepareStatement(PREPARED_STATEMENT_SQL); @@ -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); @@ -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 (tracer.getConfig(JDBCConfiguration.class).getUseJDBCServiceResourceAutoInference() && 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") @@ -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"); @@ -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) { diff --git a/apm-agent-plugins/apm-jdbc-plugin/src/test/java/co/elastic/apm/agent/jdbc/DataSourceIT.java b/apm-agent-plugins/apm-jdbc-plugin/src/test/java/co/elastic/apm/agent/jdbc/DataSourceIT.java index b561ed3d45..5d03c9d935 100644 --- a/apm-agent-plugins/apm-jdbc-plugin/src/test/java/co/elastic/apm/agent/jdbc/DataSourceIT.java +++ b/apm-agent-plugins/apm-jdbc-plugin/src/test/java/co/elastic/apm/agent/jdbc/DataSourceIT.java @@ -39,7 +39,7 @@ public class DataSourceIT extends AbstractJdbcInstrumentationTest { private static final String URL = "jdbc:h2:mem:test"; public DataSourceIT(Supplier dataSourceSupplier) throws Exception { - super(dataSourceSupplier.get().getConnection(), "h2"); + super(dataSourceSupplier.get().getConnection(), "h2", "TEST"); } @Parameterized.Parameters(name = "{0}") diff --git a/apm-agent-plugins/apm-jdbc-plugin/src/test/java/co/elastic/apm/agent/jdbc/JdbcDbIT.java b/apm-agent-plugins/apm-jdbc-plugin/src/test/java/co/elastic/apm/agent/jdbc/JdbcDbIT.java index 5cdae8a581..6718e80345 100644 --- a/apm-agent-plugins/apm-jdbc-plugin/src/test/java/co/elastic/apm/agent/jdbc/JdbcDbIT.java +++ b/apm-agent-plugins/apm-jdbc-plugin/src/test/java/co/elastic/apm/agent/jdbc/JdbcDbIT.java @@ -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 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}, }); } diff --git a/apm-agent-plugins/apm-jdbc-plugin/src/test/java/co/elastic/apm/agent/jdbc/JdbcInstrumentationTest.java b/apm-agent-plugins/apm-jdbc-plugin/src/test/java/co/elastic/apm/agent/jdbc/JdbcInstrumentationTest.java index 929ec523d1..0103308d51 100644 --- a/apm-agent-plugins/apm-jdbc-plugin/src/test/java/co/elastic/apm/agent/jdbc/JdbcInstrumentationTest.java +++ b/apm-agent-plugins/apm-jdbc-plugin/src/test/java/co/elastic/apm/agent/jdbc/JdbcInstrumentationTest.java @@ -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"); } } diff --git a/apm-agent-plugins/apm-jdbc-plugin/src/test/java/co/elastic/apm/agent/jdbc/JdbcServiceResourceAutoInferenceTest.java b/apm-agent-plugins/apm-jdbc-plugin/src/test/java/co/elastic/apm/agent/jdbc/JdbcServiceResourceAutoInferenceTest.java new file mode 100644 index 0000000000..138cd523cc --- /dev/null +++ b/apm-agent-plugins/apm-jdbc-plugin/src/test/java/co/elastic/apm/agent/jdbc/JdbcServiceResourceAutoInferenceTest.java @@ -0,0 +1,37 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package co.elastic.apm.agent.jdbc; + +import org.junit.BeforeClass; + +import java.sql.DriverManager; + +import static org.mockito.Mockito.when; + +public class JdbcServiceResourceAutoInferenceTest extends AbstractJdbcInstrumentationTest { + + @BeforeClass + public static void setUseJDBCServiceResourceAutoInference() { + when(config.getConfig(JDBCConfiguration.class).getUseJDBCServiceResourceAutoInference()).thenReturn(true); + } + + public JdbcServiceResourceAutoInferenceTest() throws Exception { + super(DriverManager.getConnection("jdbc:h2:mem:test"), "h2", "TEST"); + } +} diff --git a/docs/configuration.asciidoc b/docs/configuration.asciidoc index d90d68d19c..f7c05b3502 100644 --- a/docs/configuration.asciidoc +++ b/docs/configuration.asciidoc @@ -136,6 +136,8 @@ Click on a key to get more information. * <> ** <> ** <> +* <> +** <> * <> ** <> * <> @@ -1504,6 +1506,32 @@ If you want to use the URI template from the `@Path` annotation, set the value t | `elastic.apm.use_jaxrs_path_as_transaction_name` | `use_jaxrs_path_as_transaction_name` | `ELASTIC_APM_USE_JAXRS_PATH_AS_TRANSACTION_NAME` |============ +[[config-jdbc]] +=== JDBC configuration options +// This file is auto generated. Please make your changes in *Configuration.java (for example CoreConfiguration.java) and execute ConfigurationExporter +[float] +[[config-use-jdbc-service-resource-auto-inference]] +==== `use_jdbc_service_resource_auto_inference` (added[1.25.0]) + +If set to `true`, the agent uses `type`, `subtype` and `db.instance` of a JDBC span to infer its `destination.service.resource` instead of relying on the JDBC instrumentation to set it. +See for https://github.com/elastic/apm/blob/master/specs/agents/tracing-spans-destination.md#contextdestinationserviceresource for the inference algorithm. + + + + +[options="header"] +|============ +| Default | Type | Dynamic +| `false` | Boolean | false +|============ + + +[options="header"] +|============ +| Java System Properties | Property file | Environment +| `elastic.apm.use_jdbc_service_resource_auto_inference` | `use_jdbc_service_resource_auto_inference` | `ELASTIC_APM_USE_JDBC_SERVICE_RESOURCE_AUTO_INFERENCE` +|============ + [[config-jmx]] === JMX configuration options // This file is auto generated. Please make your changes in *Configuration.java (for example CoreConfiguration.java) and execute ConfigurationExporter @@ -3306,6 +3334,19 @@ The default unit for this option is `ms`. # # use_jaxrs_path_as_transaction_name=false +############################################ +# JDBC # +############################################ + +# If set to `true`, the agent uses `type`, `subtype` and `db.instance` of a JDBC span to infer its `destination.service.resource` instead of relying on the JDBC instrumentation to set it. +# See for https://github.com/elastic/apm/blob/master/specs/agents/tracing-spans-destination.md#contextdestinationserviceresource for the inference algorithm. +# +# This setting can not be changed at runtime. Changes require a restart of the application. +# Type: Boolean +# Default value: false +# +# use_jdbc_service_resource_auto_inference=false + ############################################ # JMX # ############################################ From 825c4cc4bdf00742d97bbea9a21c8ad7ace0333e Mon Sep 17 00:00:00 2001 From: Tobias Stadler Date: Thu, 22 Jul 2021 09:16:34 +0200 Subject: [PATCH 2/4] Added #1928 to the changelog --- CHANGELOG.asciidoc | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc index c6bd54e726..983e937b1c 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -59,6 +59,7 @@ service map and downstream service in the dependencies table - {pull}1898[#1898] * Basic support for com.sun.net.httpserver.HttpServer - {pull}1854[#1854] * Update to async-profiler 1.8.6 {pull}1907[#1907] * Added support for setting the framework using the public api (#1908) - {pull}1909[#1909] +* Use the resource auto-inference to set `context.destination.service.resource` for JDBC spans - {pull}1928[#1928] [float] ===== Bug fixes From 6fbf2a3ff87c9078b12a85f3a16799bf0a359e9a Mon Sep 17 00:00:00 2001 From: Tobias Stadler Date: Mon, 23 Aug 2021 13:03:40 +0200 Subject: [PATCH 3/4] Renamed use_jdbc_service_resource_auto_inference to use_instance_for_db_resource and add the instance name during span creation. --- CHANGELOG.asciidoc | 2 +- .../apm/agent/jdbc/JDBCConfiguration.java | 14 +++---- .../apm/agent/jdbc/helper/JdbcHelper.java | 11 +++-- .../jdbc/AbstractJdbcInstrumentationTest.java | 2 +- .../JdbcServiceResourceAutoInferenceTest.java | 2 +- docs/configuration.asciidoc | 41 ------------------- 6 files changed, 16 insertions(+), 56 deletions(-) diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc index b265fc3c93..06508608e2 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -33,7 +33,7 @@ endif::[] * gRPC spans (client and server) can detect errors or cancellation through custom listeners - {pull}2067[#2067] * Add `-download-agent-version` to the agent <>, allowing the user to configure an arbitrary agent version that will be downloaded from maven and attached - {pull}1959[#1959] -* Use the resource auto-inference to set `context.destination.service.resource` for JDBC spans - {pull}1928[#1928] +* Add the instance name to `context.destination.service.resource` to JDBC spans, if use_jdbc_service_resource_auto_inference is set to true - {pull}1928[#1928] [float] ===== Bug fixes diff --git a/apm-agent-plugins/apm-jdbc-plugin/src/main/java/co/elastic/apm/agent/jdbc/JDBCConfiguration.java b/apm-agent-plugins/apm-jdbc-plugin/src/main/java/co/elastic/apm/agent/jdbc/JDBCConfiguration.java index e4cc080213..acb52875b5 100644 --- a/apm-agent-plugins/apm-jdbc-plugin/src/main/java/co/elastic/apm/agent/jdbc/JDBCConfiguration.java +++ b/apm-agent-plugins/apm-jdbc-plugin/src/main/java/co/elastic/apm/agent/jdbc/JDBCConfiguration.java @@ -25,17 +25,15 @@ public class JDBCConfiguration extends ConfigurationOptionProvider { private static final String JDBC_CATEGORY = "JDBC"; - private final ConfigurationOption use_service_resource_auto_inference = ConfigurationOption.booleanOption() - .key("use_jdbc_service_resource_auto_inference") + private final ConfigurationOption use_instance_for_db_resource = ConfigurationOption.booleanOption() + .key("use_instance_for_db_resource") .configurationCategory(JDBC_CATEGORY) - .description("If set to `true`, the agent uses `type`, `subtype` and `db.instance` of a JDBC span to infer its `destination.service.resource`" + - " instead of relying on the JDBC instrumentation to set it.\n" + - "See for https://github.com/elastic/apm/blob/master/specs/agents/tracing-spans-destination.md#contextdestinationserviceresource for the inference algorithm.") + .description("If set to `true`, the agent adds the db instance name to `destination.service.resource` of a JDBC span.") .dynamic(false) - .tags("added[1.26.0]") + .tags("added[1.26.0]", "internal") .buildWithDefault(false); - public boolean getUseJDBCServiceResourceAutoInference() { - return use_service_resource_auto_inference.get(); + public boolean getUseInstanceForDbResource() { + return use_instance_for_db_resource.get(); } } diff --git a/apm-agent-plugins/apm-jdbc-plugin/src/main/java/co/elastic/apm/agent/jdbc/helper/JdbcHelper.java b/apm-agent-plugins/apm-jdbc-plugin/src/main/java/co/elastic/apm/agent/jdbc/helper/JdbcHelper.java index 0e6889d78a..b867925322 100644 --- a/apm-agent-plugins/apm-jdbc-plugin/src/main/java/co/elastic/apm/agent/jdbc/helper/JdbcHelper.java +++ b/apm-agent-plugins/apm-jdbc-plugin/src/main/java/co/elastic/apm/agent/jdbc/helper/JdbcHelper.java @@ -46,9 +46,9 @@ public class JdbcHelper { public static final String DB_SPAN_TYPE = "db"; public static final String DB_SPAN_ACTION = "query"; - private static final boolean useJDBCServiceResourceAutoInference = GlobalTracer.getTracerImpl() + private static final boolean useInstanceForDbResource = GlobalTracer.getTracerImpl() .getConfig(JDBCConfiguration.class) - .getUseJDBCServiceResourceAutoInference(); + .getUseInstanceForDbResource(); private static final JdbcHelper INSTANCE = new JdbcHelper(); @@ -129,8 +129,11 @@ public Span createJdbcSpan(@Nullable String sql, Object statement, @Nullable Abs .withName(vendor) .withType(DB_SPAN_TYPE); - if(!useJDBCServiceResourceAutoInference) { - destination.getService().withResource(vendor); + destination.getService().withResource(vendor); + if (useInstanceForDbResource && connectionMetaData.getInstance() != null) { + destination.getService().getResource() + .append('/') + .append(connectionMetaData.getInstance()); } } span.withSubtype(vendor).withAction(DB_SPAN_ACTION); diff --git a/apm-agent-plugins/apm-jdbc-plugin/src/test/java/co/elastic/apm/agent/jdbc/AbstractJdbcInstrumentationTest.java b/apm-agent-plugins/apm-jdbc-plugin/src/test/java/co/elastic/apm/agent/jdbc/AbstractJdbcInstrumentationTest.java index 1c3106e432..b395ad6495 100644 --- a/apm-agent-plugins/apm-jdbc-plugin/src/test/java/co/elastic/apm/agent/jdbc/AbstractJdbcInstrumentationTest.java +++ b/apm-agent-plugins/apm-jdbc-plugin/src/test/java/co/elastic/apm/agent/jdbc/AbstractJdbcInstrumentationTest.java @@ -424,7 +424,7 @@ private Span assertSpanRecorded(String rawSql, boolean preparedStatement, long e } Destination.Service service = destination.getService(); - if (tracer.getConfig(JDBCConfiguration.class).getUseJDBCServiceResourceAutoInference() && expectedDbInstance != null) { + if (tracer.getConfig(JDBCConfiguration.class).getUseInstanceForDbResource() && expectedDbInstance != null) { assertThat(service.getResource().toString()).isEqualTo(expectedDbVendor + "/" + expectedDbInstance); } else { assertThat(service.getResource().toString()).isEqualTo(expectedDbVendor); diff --git a/apm-agent-plugins/apm-jdbc-plugin/src/test/java/co/elastic/apm/agent/jdbc/JdbcServiceResourceAutoInferenceTest.java b/apm-agent-plugins/apm-jdbc-plugin/src/test/java/co/elastic/apm/agent/jdbc/JdbcServiceResourceAutoInferenceTest.java index 138cd523cc..0737f86172 100644 --- a/apm-agent-plugins/apm-jdbc-plugin/src/test/java/co/elastic/apm/agent/jdbc/JdbcServiceResourceAutoInferenceTest.java +++ b/apm-agent-plugins/apm-jdbc-plugin/src/test/java/co/elastic/apm/agent/jdbc/JdbcServiceResourceAutoInferenceTest.java @@ -28,7 +28,7 @@ public class JdbcServiceResourceAutoInferenceTest extends AbstractJdbcInstrument @BeforeClass public static void setUseJDBCServiceResourceAutoInference() { - when(config.getConfig(JDBCConfiguration.class).getUseJDBCServiceResourceAutoInference()).thenReturn(true); + when(config.getConfig(JDBCConfiguration.class).getUseInstanceForDbResource()).thenReturn(true); } public JdbcServiceResourceAutoInferenceTest() throws Exception { diff --git a/docs/configuration.asciidoc b/docs/configuration.asciidoc index dad4928cca..d70015393d 100644 --- a/docs/configuration.asciidoc +++ b/docs/configuration.asciidoc @@ -136,8 +136,6 @@ Click on a key to get more information. * <> ** <> ** <> -* <> -** <> * <> ** <> * <> @@ -1507,32 +1505,6 @@ If you want to use the URI template from the `@Path` annotation, set the value t | `elastic.apm.use_jaxrs_path_as_transaction_name` | `use_jaxrs_path_as_transaction_name` | `ELASTIC_APM_USE_JAXRS_PATH_AS_TRANSACTION_NAME` |============ -[[config-jdbc]] -=== JDBC configuration options -// This file is auto generated. Please make your changes in *Configuration.java (for example CoreConfiguration.java) and execute ConfigurationExporter -[float] -[[config-use-jdbc-service-resource-auto-inference]] -==== `use_jdbc_service_resource_auto_inference` (added[1.25.0]) - -If set to `true`, the agent uses `type`, `subtype` and `db.instance` of a JDBC span to infer its `destination.service.resource` instead of relying on the JDBC instrumentation to set it. -See for https://github.com/elastic/apm/blob/master/specs/agents/tracing-spans-destination.md#contextdestinationserviceresource for the inference algorithm. - - - - -[options="header"] -|============ -| Default | Type | Dynamic -| `false` | Boolean | false -|============ - - -[options="header"] -|============ -| Java System Properties | Property file | Environment -| `elastic.apm.use_jdbc_service_resource_auto_inference` | `use_jdbc_service_resource_auto_inference` | `ELASTIC_APM_USE_JDBC_SERVICE_RESOURCE_AUTO_INFERENCE` -|============ - [[config-jmx]] === JMX configuration options // This file is auto generated. Please make your changes in *Configuration.java (for example CoreConfiguration.java) and execute ConfigurationExporter @@ -3361,19 +3333,6 @@ The default unit for this option is `ms`. # # use_jaxrs_path_as_transaction_name=false -############################################ -# JDBC # -############################################ - -# If set to `true`, the agent uses `type`, `subtype` and `db.instance` of a JDBC span to infer its `destination.service.resource` instead of relying on the JDBC instrumentation to set it. -# See for https://github.com/elastic/apm/blob/master/specs/agents/tracing-spans-destination.md#contextdestinationserviceresource for the inference algorithm. -# -# This setting can not be changed at runtime. Changes require a restart of the application. -# Type: Boolean -# Default value: false -# -# use_jdbc_service_resource_auto_inference=false - ############################################ # JMX # ############################################ From c351d3954b8cb99b44650763e8da546ad5f3c214 Mon Sep 17 00:00:00 2001 From: Tobias Stadler Date: Wed, 24 Nov 2021 18:46:36 +0100 Subject: [PATCH 4/4] Fixed merge --- CHANGELOG.asciidoc | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc index 542bfb7676..041057a9bc 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -20,6 +20,7 @@ endif::[] === Unreleased +[[release-notes-1.27.1]] ==== 1.27.1 - YYYY/MM/DD [float]