Skip to content

Commit

Permalink
Merge f2aae96 into 33e7333
Browse files Browse the repository at this point in the history
  • Loading branch information
sjoerdtalsma committed Nov 9, 2018
2 parents 33e7333 + f2aae96 commit 1b1dea7
Show file tree
Hide file tree
Showing 17 changed files with 821 additions and 327 deletions.
65 changes: 3 additions & 62 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,69 +3,10 @@
# OpenTracing JDBI Instrumentation
OpenTracing instrumentation for JDBI.

## Installation
## Installation and Usage

pom.xml
```xml
<dependency>
<groupId>io.opentracing.contrib</groupId>
<artifactId>jdbi-opentracing</artifactId>
<version>VERSION</version>
</dependency>
```

## Usage
### Jdbi v2
```java
// Instantiate tracer
Tracer tracer = ...;

// Instatiate DBI
DBI dbi = ...;

// One time only: bind OpenTracing to the DBI instance as a TimingCollector.
// OpenTracingCollector is a Jdbi SqlLogger that creates OpenTracing Spans for each Jdbi SqlStatement.
dbi.setTimingCollector(new OpenTracingCollector(tracer)); //io.opentracing.contrib.**jdbi**.OpenTracingCollector

// Elsewhere, anywhere a `Handle` is available:
Handle handle = ...;
Span parentSpan = ...; // optional

// Create statements as usual with your `handle` instance.
Query<Map<String, Object>> statement = handle.createQuery("SELECT COUNT(*) FROM accounts");

// If a parent Span is available, establish the relationship via setParent.
OpenTracingCollector.setParent(statement, parent);

// Use JDBI as per usual, and Spans will be created for every SQLStatement automatically.
List<Map<String, Object>> results = statement.list();
```

### Jdbi3
```java
// Instantiate tracer
Tracer tracer = ...;

// Instatiate Jdbi
Jdbi dbi = Jdbi.create...;

// One time only: bind OpenTracing to the Jdbi instance as a SqlLogger.
// OpenTracingCollector is a Jdbi SqlLogger that creates OpenTracing Spans for each Jdbi SqlStatement.
dbi.setSqlLogger(new OpenTracingCollector(tracer)); //io.opentracing.contrib.**jdbi3**.OpenTracingCollector

// Elsewhere, anywhere a `Handle` is available:
Handle handle = ...;
Span parentSpan = ...; // optional

// Create statements as usual with your `handle` instance.
Query statement = handle.createQuery("SELECT COUNT(*) FROM accounts");

// If a parent Span is available, establish the relationship via setParent.
OpenTracingCollector.setParent(statement, parent);

// Use Jdbi as per usual, and Spans will be created for every SqlStatement automatically.
List<Map<String, Object>> results = statement.mapToMap().list();
```
- [Using OpenTracing with Jdbi v2](opentracing-jdbi/README.md)
- [Using OpenTracing with Jdbi3](opentracing-jdbi3/README.md)

## License

Expand Down
53 changes: 53 additions & 0 deletions opentracing-jdbi/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# OpenTracing JDBI Instrumentation
OpenTracing instrumentation for JDBI v2.

## Installation

[![Released Version][maven-img]][maven]

pom.xml
```xml
<dependency>
<groupId>io.opentracing.contrib</groupId>
<artifactId>opentracing-jdbi</artifactId>
<version>VERSION</version>
</dependency>
```

## Usage

```java
// Instantiate tracer
Tracer tracer = ...;

// Instatiate DBI
DBI dbi = ...;

// One time only: bind OpenTracing to the DBI instance as a TimingCollector.
// OpenTracingCollector is a Jdbi SqlLogger that creates OpenTracing Spans for each Jdbi SqlStatement.
dbi.setTimingCollector(new OpenTracingCollector(tracer)); //io.opentracing.contrib.**jdbi**.OpenTracingCollector

// Elsewhere, anywhere a `Handle` is available:
Handle handle = ...;
Span parentSpan = ...; // optional

// Create statements as usual with your `handle` instance.
Query<Map<String, Object>> statement = handle.createQuery("SELECT COUNT(*) FROM accounts");

// If a parent Span is available, establish the relationship via setParent.
OpenTracingCollector.setParent(statement, parent);

// Use JDBI as per usual, and Spans will be created for every SQLStatement automatically.
List<Map<String, Object>> results = statement.list();
```

## License

[Apache 2.0 License](./LICENSE).

[ci-img]: https://travis-ci.org/opentracing-contrib/java-jdbi.svg?branch=master
[ci]: https://travis-ci.org/opentracing-contrib/java-jdbi
[cov-img]: https://coveralls.io/repos/github/opentracing-contrib/java-jdbi/badge.svg?branch=master
[cov]: https://coveralls.io/github/opentracing-contrib/java-jdbi?branch=master
[maven-img]: https://img.shields.io/maven-central/v/io.opentracing.contrib/jdbi-opentracing.svg
[maven]: http://search.maven.org/#search%7Cga%7C1%7Cjdbi-opentracing
46 changes: 46 additions & 0 deletions opentracing-jdbi/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2016-2018 The OpenTracing Authors
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License
is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the License for the specific language governing permissions and limitations under
the License.
-->
<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">

<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.opentracing.contrib</groupId>
<artifactId>jdbi-opentracing</artifactId>
<version>0.5.0-SNAPSHOT</version>
</parent>

<artifactId>opentracing-jdbi</artifactId>
<description>OpenTracing Instrumentation for JDBI</description>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>

<jdbi.version>2.78</jdbi.version>
</properties>

<dependencies>
<dependency>
<groupId>org.jdbi</groupId>
<artifactId>jdbi</artifactId>
<version>${jdbi.version}</version>
</dependency>
</dependencies>

</project>
65 changes: 65 additions & 0 deletions opentracing-jdbi3/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# OpenTracing JDBI Instrumentation
OpenTracing instrumentation for JDBI.

## Installation

[![Released Version][maven-img]][maven]

pom.xml
```xml
<dependency>
<groupId>io.opentracing.contrib</groupId>
<artifactId>opentracing-jdbi3</artifactId>
<version>VERSION</version>
</dependency>
```

## Usage

```java
// Instantiate tracer
Tracer tracer = ...;

// Instatiate Jdbi
Jdbi dbi = Jdbi.create...;

// One time only: bind OpenTracing to the Jdbi instance as a SqlLogger.
// OpentracingSqlLogger creates OpenTracing Spans for each Jdbi SqlStatement.
dbi.setSqlLogger(new OpentracingSqlLogger(tracer));

// Elsewhere, anywhere a `Handle` is available:
Handle handle = ...;
Span parentSpan = ...; // optional

// Create statements as usual with your `handle` instance.
Query statement = handle.createQuery("SELECT COUNT(*) FROM accounts");

// If a parent Span is available, establish the relationship via setParent.
OpentracingSqlLogger.setParent(statement, parent);

// Use Jdbi as per usual, and Spans will be created for every SqlStatement automatically.
List<Map<String, Object>> results = statement.mapToMap().list();
```

### Older Jdbi3 versions

The `OpentracingSqlLogger` obviously implements `SqlLogger`.
This interface was introduced in JDBI version `3.2`.
In case you have an older version of Jdbi3, you should use the now-deprecated
`OpentracingTimingCollector` instead.
Configuration and behaviour is the same as described above,
except setting the SqlLogger changes from `dbi.setSqlLogger(..)` to:
```java
dbi.setTimingCollector(new OpentracingTimingCollector(tracer));
```

## License

[Apache 2.0 License](./LICENSE).

[ci-img]: https://travis-ci.org/opentracing-contrib/java-jdbi.svg?branch=master
[ci]: https://travis-ci.org/opentracing-contrib/java-jdbi
[cov-img]: https://coveralls.io/repos/github/opentracing-contrib/java-jdbi/badge.svg?branch=master
[cov]: https://coveralls.io/github/opentracing-contrib/java-jdbi?branch=master
[maven-img]: https://img.shields.io/maven-central/v/io.opentracing.contrib/jdbi-opentracing.svg
[maven]: http://search.maven.org/#search%7Cga%7C1%7Cjdbi-opentracing
57 changes: 57 additions & 0 deletions opentracing-jdbi3/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2016-2018 The OpenTracing Authors
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License
is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the License for the specific language governing permissions and limitations under
the License.
-->
<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">

<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.opentracing.contrib</groupId>
<artifactId>jdbi-opentracing</artifactId>
<version>0.5.0-SNAPSHOT</version>
</parent>

<artifactId>opentracing-jdbi3</artifactId>
<description>OpenTracing Instrumentation for JDBI 3</description>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>

<jdbi3.version>3.5.1</jdbi3.version>
</properties>

<dependencies>
<dependency>
<groupId>org.jdbi</groupId>
<artifactId>jdbi3-core</artifactId>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jdbi</groupId>
<artifactId>jdbi3-bom</artifactId>
<version>${jdbi3.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import org.jdbi.v3.core.statement.StatementContext;

/**
* An abstract API that allows the OpenTracingCollector to customize how parent Spans are
* An abstract API that allows the OpentracingSqlLogger to customize how parent Spans are
* discovered.
* <p>
* For instance, if Spans are stored in a thread-local variable, an ActiveSpanSource could
Expand All @@ -41,7 +41,7 @@
* return activeSpan.get();
* }
* };
* OpenTracingCollector otColl = new OpenTracingCollector(tracer, spanSource);
* OpentracingSqlLogger otColl = new OpentracingSqlLogger(tracer, spanSource);
* ...
* }</pre>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
import org.jdbi.v3.core.statement.StatementContext;

/**
* OpenTracingCollector is a Jdbi SqlLogger that creates OpenTracing Spans for each Jdbi SQLStatement.
* OpentracingSqlLogger is a Jdbi SqlLogger that creates OpenTracing Spans for each Jdbi SQLStatement.
*
* <p>Example usage:
* <pre>{@code
* io.opentracing.Tracer tracer = ...;
* Jdbi dbi = ...;
*
* // One time only: bind OpenTracing to the Jdbi instance as a SqlLogger.
* dbi.setSqlLogger(new OpenTracingCollector(tracer));
* dbi.setSqlLogger(new OpentracingSqlLogger(tracer));
*
* // Elsewhere, anywhere a `Handle` is available:
* Handle handle = ...;
Expand All @@ -40,14 +40,14 @@
* Query statement = handle.createQuery("SELECT COUNT(*) FROM accounts");
*
* // If a parent Span is available, establish the relationship via setParent.
* OpenTracingCollector.setParent(statement, parent);
* OpentracingSqlLogger.setParent(statement, parent);
*
* // Use Jdbi as per usual, and Spans will be created for every SQLStatement automatically.
* List<Map<String, Object>> results = statement.mapToMap().list();
* }</pre>
*/
@SuppressWarnings("WeakerAccess")
public class OpenTracingCollector implements SqlLogger {
public class OpentracingSqlLogger implements SqlLogger {
public final static String PARENT_SPAN_ATTRIBUTE_KEY = "io.opentracing.parent";
static final String COMPONENT_NAME = "java-jdbi";

Expand All @@ -56,7 +56,7 @@ public class OpenTracingCollector implements SqlLogger {
private final ActiveSpanSource activeSpanSource;
private final SqlLogger next;

public OpenTracingCollector(Tracer tracer) {
public OpentracingSqlLogger(Tracer tracer) {
this(tracer, SpanDecorator.DEFAULT);
}

Expand All @@ -65,20 +65,20 @@ public OpenTracingCollector(Tracer tracer) {
* @param next a timing collector to "chain" to. When collect is called on
* this SqlLogger, collect will also be called on 'next'
*/
public OpenTracingCollector(Tracer tracer, SqlLogger next) {
public OpentracingSqlLogger(Tracer tracer, SqlLogger next) {
this(tracer, SpanDecorator.DEFAULT, null, next);
}

public OpenTracingCollector(Tracer tracer, SpanDecorator spanDecorator) {
public OpentracingSqlLogger(Tracer tracer, SpanDecorator spanDecorator) {
this(tracer, spanDecorator, null);
}

public OpenTracingCollector(Tracer tracer, ActiveSpanSource spanSource) {
public OpentracingSqlLogger(Tracer tracer, ActiveSpanSource spanSource) {
this(tracer, SpanDecorator.DEFAULT, spanSource);
}

public OpenTracingCollector(Tracer tracer, SpanDecorator spanDecorator,
ActiveSpanSource activeSpanSource) {
public OpentracingSqlLogger(Tracer tracer, SpanDecorator spanDecorator,
ActiveSpanSource activeSpanSource) {
this(tracer, spanDecorator, activeSpanSource, null);
}

Expand All @@ -91,8 +91,8 @@ public OpenTracingCollector(Tracer tracer, SpanDecorator spanDecorator,
* this SqlLogger, logAfterExecution will also be called on 'next'
* @see ActiveSpanSource
*/
public OpenTracingCollector(Tracer tracer, SpanDecorator spanDecorator,
ActiveSpanSource activeSpanSource, SqlLogger next) {
public OpentracingSqlLogger(Tracer tracer, SpanDecorator spanDecorator,
ActiveSpanSource activeSpanSource, SqlLogger next) {
this.tracer = tracer;
this.spanDecorator = spanDecorator;
this.activeSpanSource = activeSpanSource;
Expand Down
Loading

0 comments on commit 1b1dea7

Please sign in to comment.