diff --git a/instrumentation/clickhouse-client-0.5/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/clickhouse/ClickHouseClientInstrumentation.java b/instrumentation/clickhouse-client-0.5/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/clickhouse/ClickHouseClientInstrumentation.java index 886a364e9921..78055968a872 100644 --- a/instrumentation/clickhouse-client-0.5/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/clickhouse/ClickHouseClientInstrumentation.java +++ b/instrumentation/clickhouse-client-0.5/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/clickhouse/ClickHouseClientInstrumentation.java @@ -15,6 +15,7 @@ import com.clickhouse.client.ClickHouseClient; import com.clickhouse.client.ClickHouseRequest; +import com.clickhouse.client.config.ClickHouseDefaults; import io.opentelemetry.context.Context; import io.opentelemetry.context.Scope; import io.opentelemetry.javaagent.bootstrap.CallDepth; @@ -63,7 +64,10 @@ public static void onEnter( ClickHouseDbRequest.create( clickHouseRequest.getServer().getHost(), clickHouseRequest.getServer().getPort(), - clickHouseRequest.getServer().getDatabase().get(), + clickHouseRequest + .getServer() + .getDatabase() + .orElse(ClickHouseDefaults.DATABASE.getDefaultValue().toString()), clickHouseRequest.getPreparedQuery().getOriginalQuery()); if (!instrumenter().shouldStart(parentContext, request)) { diff --git a/instrumentation/clickhouse-client-0.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/clickhouse/ClickHouseClientTest.java b/instrumentation/clickhouse-client-0.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/clickhouse/ClickHouseClientTest.java index 472a00cb4319..745bed68392e 100644 --- a/instrumentation/clickhouse-client-0.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/clickhouse/ClickHouseClientTest.java +++ b/instrumentation/clickhouse-client-0.5/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/clickhouse/ClickHouseClientTest.java @@ -81,6 +81,31 @@ void cleanup() { clickhouseServer.stop(); } + @Test + void testConnectionStringWithoutDatabaseSpecifiedStillGeneratesSpans() + throws ClickHouseException { + ClickHouseNode server = ClickHouseNode.of("http://" + host + ":" + port + "?compress=0"); + ClickHouseClient client = ClickHouseClient.builder().build(); + + ClickHouseResponse response = + client + .read(server) + .format(ClickHouseFormat.RowBinaryWithNamesAndTypes) + .query("select * from " + tableName) + .executeAndWait(); + response.close(); + + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> + span.hasName("SELECT " + dbName) + .hasKind(SpanKind.CLIENT) + .hasNoParent() + .hasAttributesSatisfyingExactly( + attributeAssertions("select * from " + tableName, "SELECT")))); + } + @Test void testExecuteAndWaitWithStringQuery() throws ClickHouseException { testing.runWithSpan(