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

Fix ClickHouse tracing when database name not included in connection string #11852

Merged
merged 1 commit into from
Jul 19, 2024
Merged
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
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(),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would leaving out the port number from the connection string cause a similar problem?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, this was only applicable to the getDatabase() method because it's the only one thats Optional. The other methods like port and host will use the defaults and should never not have a value

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
Loading