-
Notifications
You must be signed in to change notification settings - Fork 716
Add JDBI 3 plugin #1460
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
Merged
codefromthecrypt
merged 2 commits into
openzipkin:master
from
reftel:feature/jdbi_instrumentation
May 27, 2025
Merged
Add JDBI 3 plugin #1460
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
87 changes: 87 additions & 0 deletions
87
instrumentation/jdbi3/src/main/java/brave/jdbi3/BraveStatementContextListener.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) { | ||
| Connection connection = ctx.getConnection(); | ||
| if (connection == null) { | ||
| return null; | ||
| } | ||
| try { | ||
| String url = connection.getMetaData().getURL(); | ||
| return remoteServiceNameResolver.resolve(url); | ||
| } catch (SQLException ignored) { | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
48 changes: 48 additions & 0 deletions
48
instrumentation/jdbi3/src/main/java/brave/jdbi3/Jdbi3BravePlugin.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| } | ||||||||
|
|
||||||||
| public JdbiPlugin.Singleton build() { | ||||||||
| return new Jdbi3BravePlugin(tracing); | ||||||||
| } | ||||||||
| } | ||||||||
| } | ||||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 thank you