Skip to content

Commit

Permalink
Fix ClickHouse tracing when database name not included in connection …
Browse files Browse the repository at this point in the history
…string (#11852)
  • Loading branch information
jaydeluca authored Jul 19, 2024
1 parent 145185e commit 57d9c49
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 57d9c49

Please sign in to comment.