Skip to content
Draft
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
@@ -0,0 +1,37 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.clickhouse.common;

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
import org.jspecify.annotations.Nullable;

class ClickHouseAttributesExtractor implements AttributesExtractor<ClickHouseDbRequest, Void> {

private static final AttributeKey<String> QUERY_ID =
AttributeKey.stringKey("clickhouse.query_id");

@Override
public void onStart(
AttributesBuilder attributes,
Context parentContext,
ClickHouseDbRequest clickHouseDbRequest) {
String queryId = clickHouseDbRequest.getQueryId();
if (queryId != null) {
attributes.put(QUERY_ID, clickHouseDbRequest.getQueryId());
}
}

@Override
public void onEnd(
AttributesBuilder attributes,
Context context,
ClickHouseDbRequest clickHouseDbRequest,
@Nullable Void unused,
@Nullable Throwable error) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ public abstract class ClickHouseDbRequest {
SqlStatementSanitizer.create(AgentCommonConfig.get().isStatementSanitizationEnabled());

public static ClickHouseDbRequest create(
@Nullable String host, @Nullable Integer port, @Nullable String dbName, String sql) {
return new AutoValue_ClickHouseDbRequest(host, port, dbName, sanitizer.sanitize(sql));
@Nullable String host,
@Nullable Integer port,
@Nullable String dbName,
@Nullable String queryId,
String sql) {
return new AutoValue_ClickHouseDbRequest(host, port, dbName, queryId, sanitizer.sanitize(sql));
}

@Nullable
Expand All @@ -31,5 +35,8 @@ public static ClickHouseDbRequest create(
@Nullable
public abstract String getDbName();

@Nullable
public abstract String getQueryId();

public abstract SqlStatementInfo getSqlStatementInfo();
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public static Instrumenter<ClickHouseDbRequest, Void> createInstrumenter(
.addAttributesExtractor(DbClientAttributesExtractor.create(dbAttributesGetter))
.addAttributesExtractor(
ServerAttributesExtractor.create(new ClickHouseNetworkAttributesGetter()))
.addAttributesExtractor(new ClickHouseAttributesExtractor())
.addOperationMetrics(DbClientMetrics.get())
.buildInstrumenter(SpanKindExtractor.alwaysClient());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public static ClickHouseScope onEnter(
.getServer()
.getDatabase()
.orElse(ClickHouseDefaults.DATABASE.getDefaultValue().toString()),
clickHouseRequest.getQueryId().orElse(null),
ClickHouseRequestAccess.getQuery(clickHouseRequest));

return ClickHouseScope.start(instrumenter(), currentContext(), request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_STATEMENT;
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_SYSTEM;
import static java.util.Arrays.asList;
import static java.util.stream.Collectors.toList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;

Expand All @@ -32,6 +33,7 @@
import com.clickhouse.client.ClickHouseResponseSummary;
import com.clickhouse.data.ClickHouseFormat;
import com.google.common.collect.ImmutableMap;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.instrumentation.api.internal.SemconvStability;
Expand All @@ -44,6 +46,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Stream;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
Expand All @@ -65,6 +68,9 @@ class ClickHouseClientV1Test {
private static ClickHouseNode server;
private static ClickHouseClient client;

private static final AttributeKey<String> queryIdKey =
AttributeKey.stringKey("clickhouse.query_id");

@BeforeAll
static void setup() throws ClickHouseException {
clickhouseServer.start();
Expand Down Expand Up @@ -182,6 +188,11 @@ void testExecuteAndWaitWithStringQueryAndId() throws ClickHouseException {
response.close();
});

List<AttributeAssertion> attributeAssertions =
Stream.concat(
attributeAssertions("select * from " + tableName, "SELECT").stream(),
Stream.of(equalTo(queryIdKey, "test_query_id")))
.collect(toList());
testing.waitAndAssertTraces(
trace ->
trace.hasSpansSatisfyingExactly(
Expand All @@ -190,8 +201,7 @@ void testExecuteAndWaitWithStringQueryAndId() throws ClickHouseException {
span.hasName("SELECT " + dbName)
.hasKind(SpanKind.CLIENT)
.hasParent(trace.getSpan(0))
.hasAttributesSatisfyingExactly(
attributeAssertions("select * from " + tableName, "SELECT"))));
.hasAttributesSatisfyingExactly(attributeAssertions)));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,15 @@ public static ClickHouseScope onEnter(
}

String database = client.getConfiguration().get("database");
String queryId = null;
if (querySettings != null) {
queryId = querySettings.getQueryId();
}

Context parentContext = currentContext();
ClickHouseDbRequest request =
ClickHouseDbRequest.create(
addressAndPort.getAddress(), addressAndPort.getPort(), database, sqlQuery);
addressAndPort.getAddress(), addressAndPort.getPort(), database, queryId, sqlQuery);

return ClickHouseScope.start(instrumenter(), parentContext, request);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_STATEMENT;
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_SYSTEM;
import static java.util.Arrays.asList;
import static java.util.stream.Collectors.toList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;

Expand All @@ -31,6 +32,7 @@
import com.clickhouse.client.api.query.QueryResponse;
import com.clickhouse.client.api.query.QuerySettings;
import com.clickhouse.client.api.query.Records;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.instrumentation.api.internal.SemconvStability;
Expand All @@ -45,6 +47,7 @@
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Stream;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
Expand All @@ -67,6 +70,9 @@ class ClickHouseClientV2Test {
private static final String username = "default";
private static final String password = "";

private static final AttributeKey<String> queryIdKey =
AttributeKey.stringKey("clickhouse.query_id");

@BeforeAll
static void setup() throws Exception {
clickhouseServer.start();
Expand Down Expand Up @@ -178,6 +184,11 @@ void testQueryWithStringQueryAndId() throws Exception {
response.close();
});

List<AttributeAssertion> attributeAssertions =
Stream.concat(
attributeAssertions("select * from " + tableName, "SELECT").stream(),
Stream.of(equalTo(queryIdKey, "test_query_id")))
.collect(toList());
testing.waitAndAssertTraces(
trace ->
trace.hasSpansSatisfyingExactly(
Expand All @@ -186,8 +197,7 @@ void testQueryWithStringQueryAndId() throws Exception {
span.hasName("SELECT " + dbName)
.hasKind(SpanKind.CLIENT)
.hasParent(trace.getSpan(0))
.hasAttributesSatisfyingExactly(
attributeAssertions("select * from " + tableName, "SELECT"))));
.hasAttributesSatisfyingExactly(attributeAssertions)));
}

@Test
Expand Down
Loading