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

Support tracing the ClickHouse Java HTTP client #11660

Merged
merged 8 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions docs/supported-libraries.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ These are the supported libraries and frameworks:
| [AWS SDK](https://aws.amazon.com/sdk-for-java/) | 1.11.x and 2.2+ | [opentelemetry-aws-sdk-1.11](../instrumentation/aws-sdk/aws-sdk-1.11/library),<br>[opentelemetry-aws-sdk-1.11-autoconfigure](../instrumentation/aws-sdk/aws-sdk-1.11/library-autoconfigure),<br>[opentelemetry-aws-sdk-2.2](../instrumentation/aws-sdk/aws-sdk-2.2/library),<br>[opentelemetry-aws-sdk-2.2-autoconfigure](../instrumentation/aws-sdk/aws-sdk-2.2/library-autoconfigure) | [Messaging Spans], [Database Client Spans], [HTTP Client Spans] |
| [Azure Core](https://docs.microsoft.com/en-us/java/api/overview/azure/core-readme) | 1.14+ | N/A | Context propagation |
| [Cassandra Driver](https://github.com/datastax/java-driver) | 3.0+ | [opentelemetry-cassandra-4.4](../instrumentation/cassandra/cassandra-4.4/library) | [Database Client Spans] |
| [Clickhouse Client](https://github.com/ClickHouse/clickhouse-java) | 0.5+ | N/A | [Database Client Spans] |
| [Couchbase Client](https://github.com/couchbase/couchbase-java-client) | 2.0+ and 3.1+ | N/A | [Database Client Spans] |
| [c3p0](https://github.com/swaldman/c3p0) | 0.9.2+ | [opentelemetry-c3p0-0.9](../instrumentation/c3p0-0.9/library) | [Database Pool Metrics] |
| [Dropwizard Metrics](https://metrics.dropwizard.io/) | 4.0+ (disabled by default) | N/A | none |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
plugins {
id("otel.javaagent-instrumentation")
}

muzzle {
pass {
group.set("com.clickhouse.client")
module.set("clickhouse-client")
versions.set("[0.5.0,)")
assertInverse.set(true)
}
}

dependencies {
compileOnly("com.clickhouse:clickhouse-client:0.5.0")
compileOnly("com.google.auto.value:auto-value-annotations")
annotationProcessor("com.google.auto.value:auto-value")

testLibrary("com.clickhouse:clickhouse-client:0.5.0")
testLibrary("com.clickhouse:clickhouse-http-client:0.5.0")
testLibrary("org.apache.httpcomponents.client5:httpclient5:5.2.3")
}
jaydeluca marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.clickhouse;

import io.opentelemetry.instrumentation.api.incubator.semconv.db.DbClientAttributesGetter;
import io.opentelemetry.semconv.incubating.DbIncubatingAttributes;
import javax.annotation.Nullable;

final class ClickHouseAttributesGetter implements DbClientAttributesGetter<ClickHouseDbRequest> {
@Nullable
@Override
public String getStatement(ClickHouseDbRequest request) {
if (request.getSqlStatementInfo() == null) {
return null;
}
return request.getSqlStatementInfo().getFullStatement();
}

@Nullable
@Override
public String getOperation(ClickHouseDbRequest request) {
if (request.getSqlStatementInfo() == null) {
return null;
}
return request.getSqlStatementInfo().getOperation();
}

@Nullable
@Override
public String getSystem(ClickHouseDbRequest request) {
return DbIncubatingAttributes.DbSystemValues.CLICKHOUSE;
}

@Nullable
@Override
public String getUser(ClickHouseDbRequest request) {
return null;
}

@Nullable
@Override
public String getName(ClickHouseDbRequest request) {
String dbName = request.getDbName();
if (dbName == null || dbName.isEmpty()) {
return null;
}
return dbName;
}

@Nullable
@Override
public String getConnectionString(ClickHouseDbRequest request) {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.clickhouse;

import static io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge.currentContext;
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface;
import static io.opentelemetry.javaagent.instrumentation.clickhouse.ClickHouseSingletons.instrumenter;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.namedOneOf;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;

import com.clickhouse.client.ClickHouseClient;
import com.clickhouse.client.ClickHouseRequest;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.javaagent.bootstrap.CallDepth;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;

public class ClickHouseClientInstrumentation implements TypeInstrumentation {
@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return implementsInterface(named("com.clickhouse.client.ClickHouseClient"));
}

@Override
public void transform(TypeTransformer transformer) {
transformer.applyAdviceToMethod(
isMethod()
.and(namedOneOf("executeAndWait", "execute"))
.and(takesArgument(0, named("com.clickhouse.client.ClickHouseRequest"))),
this.getClass().getName() + "$ClickHouseExecuteAndWaitAdvice");
}

@SuppressWarnings("unused")
public static class ClickHouseExecuteAndWaitAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void onEnter(
@Advice.Argument(0) ClickHouseRequest<?> clickHouseRequest,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope,
@Advice.Local("otelCallDepth") CallDepth callDepth) {
callDepth = CallDepth.forClass(ClickHouseClient.class);
if (callDepth.decrementAndGet() > 0) {
return;
}
jaydeluca marked this conversation as resolved.
Show resolved Hide resolved

if (clickHouseRequest == null) {
jaydeluca marked this conversation as resolved.
Show resolved Hide resolved
return;
}

Context parentContext = currentContext();

ClickHouseDbRequest request =
ClickHouseDbRequest.create(
clickHouseRequest.getServer().getHost(),
clickHouseRequest.getServer().getPort(),
clickHouseRequest.getServer().getDatabase().get(),
clickHouseRequest.getPreparedQuery().getOriginalQuery());

if (!instrumenter().shouldStart(parentContext, request)) {
return;
}

context = instrumenter().start(parentContext, request);
scope = context.makeCurrent();
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void onExit(
@Advice.Thrown Throwable throwable,
@Advice.Local("otelRequest") ClickHouseDbRequest clickHouseRequest,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
if (scope == null) {
return;
}

scope.close();
instrumenter().end(context, clickHouseRequest, null, throwable);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.clickhouse;

import com.google.auto.value.AutoValue;
import io.opentelemetry.instrumentation.api.incubator.semconv.db.SqlStatementInfo;
import io.opentelemetry.instrumentation.api.incubator.semconv.db.SqlStatementSanitizer;
import io.opentelemetry.javaagent.bootstrap.internal.CommonConfig;

@AutoValue
public abstract class ClickHouseDbRequest {

private static final SqlStatementSanitizer sanitizer =
SqlStatementSanitizer.create(CommonConfig.get().isStatementSanitizationEnabled());

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

public abstract String getHost();

public abstract Integer getPort();

public abstract String getDbName();

public abstract SqlStatementInfo getSqlStatementInfo();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.clickhouse;

import com.google.auto.service.AutoService;
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import java.util.Collections;
import java.util.List;

@AutoService(InstrumentationModule.class)
public class ClickHouseInstrumentationModule extends InstrumentationModule {

public ClickHouseInstrumentationModule() {
super("clickhouse", "clickhouse-client");
Copy link
Contributor

@steverao steverao Jul 1, 2024

Choose a reason for hiding this comment

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

According to previous convention, the module should be named to clickhouse-client-0.5. Please pay attention to modify other places together.

}

@Override
public List<TypeInstrumentation> typeInstrumentations() {
return Collections.singletonList(new ClickHouseClientInstrumentation());
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggest to use static import here.

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.clickhouse;

import io.opentelemetry.instrumentation.api.semconv.network.ServerAttributesGetter;

final class ClickHouseNetworkAttributesGetter
implements ServerAttributesGetter<ClickHouseDbRequest> {

@Override
public String getServerAddress(ClickHouseDbRequest request) {
return request.getHost();
}

@Override
public Integer getServerPort(ClickHouseDbRequest request) {
return request.getPort();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.clickhouse;

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.api.incubator.semconv.db.DbClientAttributesExtractor;
import io.opentelemetry.instrumentation.api.incubator.semconv.db.DbClientSpanNameExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.api.instrumenter.SpanKindExtractor;
import io.opentelemetry.instrumentation.api.semconv.network.ServerAttributesExtractor;

public final class ClickHouseSingletons {

private static final Instrumenter<ClickHouseDbRequest, Void> INSTRUMENTER;

static {
ClickHouseAttributesGetter dbAttributesGetter = new ClickHouseAttributesGetter();

INSTRUMENTER =
Instrumenter.<ClickHouseDbRequest, Void>builder(
GlobalOpenTelemetry.get(),
"io.opentelemetry.clickhouse-client",
DbClientSpanNameExtractor.create(dbAttributesGetter))
.addAttributesExtractor(DbClientAttributesExtractor.create(dbAttributesGetter))
.addAttributesExtractor(
ServerAttributesExtractor.create(new ClickHouseNetworkAttributesGetter()))
.buildInstrumenter(SpanKindExtractor.alwaysClient());
}

public static Instrumenter<ClickHouseDbRequest, Void> instrumenter() {
return INSTRUMENTER;
}

private ClickHouseSingletons() {}
}
Loading
Loading