Skip to content
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
7 changes: 6 additions & 1 deletion brave-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<groupId>io.zipkin.brave</groupId>
<artifactId>brave-bom</artifactId>
<version>6.2.1-SNAPSHOT</version>
<version>6.3.0-SNAPSHOT</version>
<name>Brave BOM</name>
<description>Bill Of Materials POM for all Brave artifacts</description>
<packaging>pom</packaging>
Expand Down Expand Up @@ -162,6 +162,11 @@
<artifactId>brave-instrumentation-jersey-server-jakarta</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>brave-instrumentation-jdbi3</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>brave-instrumentation-jms</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion brave-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave-parent</artifactId>
<version>6.2.1-SNAPSHOT</version>
<version>6.3.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion brave/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave-parent</artifactId>
<version>6.2.1-SNAPSHOT</version>
<version>6.3.0-SNAPSHOT</version>
</parent>

<artifactId>brave</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion context/jfr/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave-context-parent</artifactId>
<version>6.2.1-SNAPSHOT</version>
<version>6.3.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion context/log4j12/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave-context-parent</artifactId>
<version>6.2.1-SNAPSHOT</version>
<version>6.3.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion context/log4j2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave-context-parent</artifactId>
<version>6.2.1-SNAPSHOT</version>
<version>6.3.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion context/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave-parent</artifactId>
<version>6.2.1-SNAPSHOT</version>
<version>6.3.0-SNAPSHOT</version>
</parent>

<artifactId>brave-context-parent</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion context/slf4j/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave-context-parent</artifactId>
<version>6.2.1-SNAPSHOT</version>
<version>6.3.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion instrumentation/benchmarks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave-instrumentation-parent</artifactId>
<version>6.2.1-SNAPSHOT</version>
<version>6.3.0-SNAPSHOT</version>
</parent>

<artifactId>brave-instrumentation-benchmarks</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion instrumentation/dubbo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave-instrumentation-parent</artifactId>
<version>6.2.1-SNAPSHOT</version>
<version>6.3.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion instrumentation/grpc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave-instrumentation-parent</artifactId>
<version>6.2.1-SNAPSHOT</version>
<version>6.3.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion instrumentation/http-tests-jakarta/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave-instrumentation-parent</artifactId>
<version>6.2.1-SNAPSHOT</version>
<version>6.3.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion instrumentation/http-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave-instrumentation-parent</artifactId>
<version>6.2.1-SNAPSHOT</version>
<version>6.3.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion instrumentation/http/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave-instrumentation-parent</artifactId>
<version>6.2.1-SNAPSHOT</version>
<version>6.3.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion instrumentation/httpasyncclient/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave-instrumentation-parent</artifactId>
<version>6.2.1-SNAPSHOT</version>
<version>6.3.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion instrumentation/httpclient/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave-instrumentation-parent</artifactId>
<version>6.2.1-SNAPSHOT</version>
<version>6.3.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion instrumentation/httpclient5/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave-instrumentation-parent</artifactId>
<version>6.2.1-SNAPSHOT</version>
<version>6.3.0-SNAPSHOT</version>
</parent>

<artifactId>brave-instrumentation-httpclient5</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion instrumentation/jakarta-jms/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave-instrumentation-parent</artifactId>
<version>6.2.1-SNAPSHOT</version>
<version>6.3.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion instrumentation/jaxrs2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave-instrumentation-parent</artifactId>
<version>6.2.1-SNAPSHOT</version>
<version>6.3.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
19 changes: 19 additions & 0 deletions instrumentation/jdbi3/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# brave-instrumentation-jdbi3

This includes a JDBI 3 plugin that will report to Zipkin how long each
statement takes, along with relevant tags like the query.

To use it, call the `installPlugin` on the `Jdbi` instance you want to
instrument, or add the statement context listener manually like so:
```
jdbi.getConfig(SqlStatements.class)
.addContextListener(new BraveStatementContextListener(tracing));
```

The remote service name of the span is set to the hostname and port number of
the database server, if available, and the URL scheme if not. If the database
URL format allows it, you can add the `zipkinServiceName` query parameter to
override the remote service name.

Bind variable values are not included in the traces, only the SQL statement
with placeholders.
47 changes: 47 additions & 0 deletions instrumentation/jdbi3/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0"?>
<!--

Copyright The OpenZipkin Authors
SPDX-License-Identifier: Apache-2.0

-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave-instrumentation-parent</artifactId>
<version>6.3.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>brave-instrumentation-jdbi3</artifactId>
<name>Brave Instrumentation: JDBI3</name>

<properties>
<!-- Matches Export-Package in bnd.bnd -->
<module.name>brave.jdbi3</module.name>

<main.basedir>${project.basedir}/../..</main.basedir>

<jdbi3.version>3.49.1</jdbi3.version>
</properties>

<dependencies>
<dependency>
<groupId>org.jdbi</groupId>
<artifactId>jdbi3-core</artifactId>
<version>${jdbi3.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>brave-tests</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>${mysql-connector-j8.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright The OpenZipkin Authors
* SPDX-License-Identifier: Apache-2.0
*/
package brave.jdbi3;

import java.sql.Connection;
import java.sql.SQLException;
import java.time.Instant;

import org.jdbi.v3.core.statement.SqlStatements;
import org.jdbi.v3.core.statement.StatementContext;
import org.jdbi.v3.core.statement.StatementContextListener;

import brave.Span;
import brave.Tracing;

public class BraveStatementContextListener implements StatementContextListener {
private final Tracing tracing;
private final RemoteServiceNameResolver remoteServiceNameResolver;

public BraveStatementContextListener(Tracing tracing, RemoteServiceNameResolver remoteServiceNameResolver) { this.tracing = tracing;
this.remoteServiceNameResolver = remoteServiceNameResolver;
}

@Override
public void contextCreated(final StatementContext ctx) {
String spanName = ctx.describeJdbiStatementType();
Span span = tracing.tracer().nextSpan().name(spanName);

Instant start = ctx.getExecutionMoment();
if (start != null) {
span.start(start.toEpochMilli() * 1000);
} else {
span.start();
}

ctx.setTraceId(span.context().traceIdString());

ctx.addCleanable(() -> {
final SqlStatements stmtConfig = ctx.getConfig(SqlStatements.class);

String remoteServiceName = getRemoteServiceName(ctx);
if (remoteServiceName != null && !remoteServiceName.isEmpty()) {
span.remoteServiceName(remoteServiceName);
}

span.kind(Span.Kind.CLIENT);

final String renderedSql = ctx.getRenderedSql();
if (renderedSql != null) {
String truncated = renderedSql.substring(
0,
Math.min(renderedSql.length(), stmtConfig.getJfrSqlMaxLength())
);
span.tag("sql.query", truncated);
}

span.tag("sql.rows", Long.toString(ctx.getMappedRows()));

if (ctx.getCompletionMoment() == null) {
span.tag("error", "");
}

if (ctx.getCompletionMoment() != null) {
span.finish(ctx.getCompletionMoment().toEpochMilli() * 1000);
} else if (ctx.getExceptionMoment() != null) {
span.finish(ctx.getExceptionMoment().toEpochMilli() * 1000);
} else {
span.finish();
}
});
}

String getRemoteServiceName(StatementContext ctx) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

👍 thank you

Connection connection = ctx.getConnection();
if (connection == null) {
return null;
}
try {
String url = connection.getMetaData().getURL();
return remoteServiceNameResolver.resolve(url);
} catch (SQLException ignored) {
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright The OpenZipkin Authors
* SPDX-License-Identifier: Apache-2.0
*/
package brave.jdbi3;

import org.jdbi.v3.core.Jdbi;
import org.jdbi.v3.core.spi.JdbiPlugin;
import org.jdbi.v3.core.statement.SqlStatements;

import brave.Tracing;

/**
* A Jdbi plugin that will report to Zipkin how long each query takes.
* * <p>
* Install by calling
* {@code jdbi.installPlugin(Jdbi3BravePlugin.newBuilder(tracing).build());}
*/
public final class Jdbi3BravePlugin extends JdbiPlugin.Singleton {
private final Tracing tracing;

private Jdbi3BravePlugin(Tracing tracing) {
if (tracing == null) throw new NullPointerException("tracing == null");
this.tracing = tracing;
}

public static Builder newBuilder(Tracing tracing) {
return new Builder(tracing);
}

@Override
public void customizeJdbi(Jdbi jdbi) {
jdbi.getConfig(SqlStatements.class)
.addContextListener(new BraveStatementContextListener(tracing, new RemoteServiceNameResolver()));
}

public static final class Builder {
private final Tracing tracing;

public Builder(Tracing tracing) {
this.tracing = tracing;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
this.tracing = tracing;
if (tracing == null) throw new NullPointerException("tracing == null");
this.tracing = tracing;

}

public JdbiPlugin.Singleton build() {
return new Jdbi3BravePlugin(tracing);
}
}
}
Loading