Skip to content

Commit

Permalink
Merge c97dcf0 into 33e7333
Browse files Browse the repository at this point in the history
  • Loading branch information
sjoerdtalsma authored Nov 9, 2018
2 parents 33e7333 + c97dcf0 commit dc635eb
Show file tree
Hide file tree
Showing 14 changed files with 700 additions and 265 deletions.
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>
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
* 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.
*/
package io.opentracing.contrib.jdbi3;

import io.opentracing.Span;
import io.opentracing.Tracer;
import io.opentracing.tag.Tags;
import org.jdbi.v3.core.statement.SqlStatement;
import org.jdbi.v3.core.statement.StatementContext;
import org.jdbi.v3.core.statement.TimingCollector;

/**
* OpentracingTimingCollector is a JDBI TimingCollector that creates OpenTracing Spans for each JDBI
* SQLStatement.
* <p>
* <p>Example usage:
* <pre>{@code
* io.opentracing.Tracer tracer = ...;
* DBI dbi = ...;
*
* // One time only: bind OpenTracing to the DBI instance as a TimingCollector.
* dbi.setTimingCollector(new OpentracingTimingCollector(tracer));
*
* // 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.
* OpentracingTimingCollector.setParent(statement, parent);
*
* // Use JDBI as per usual, and Spans will be created for every SQLStatement automatically.
* List<Map<String, Object>> results = statement.list();
* }</pre>
*
* @see OpentracingSqlLogger
* @deprecated Please use the {@code OpentracingSqlLogger} instead if you have JDBI version 3.2 or greater.
*/
@Deprecated
@SuppressWarnings("WeakerAccess")
public class OpentracingTimingCollector implements TimingCollector {
public final static String PARENT_SPAN_ATTRIBUTE_KEY = "io.opentracing.parent";

private final Tracer tracer;
private final SpanDecorator spanDecorator;
private final ActiveSpanSource activeSpanSource;
private final TimingCollector next;

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

/**
* @param tracer the OpenTracing tracer to trace JDBI calls.
* @param next a timing collector to "chain" to. When collect is called on
* this TimingCollector, collect will also be called on 'next'
*/
public OpentracingTimingCollector(Tracer tracer, TimingCollector next) {
this(tracer, SpanDecorator.DEFAULT, null, next);
}

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

public OpentracingTimingCollector(Tracer tracer, ActiveSpanSource activeSpanSource) {
this(tracer, SpanDecorator.DEFAULT, activeSpanSource);
}

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

/**
* @param tracer the OpenTracing tracer to trace JDBI calls.
* @param spanDecorator the SpanDecorator used to name and decorate spans.
* @param activeSpanSource a source that can provide the currently active
* span when creating a child span.
* @param next a timing collector to "chain" to. When collect is called on
* this TimingCollector, collect will also be called on 'next'
* @see SpanDecorator
* @see ActiveSpanSource
*/
public OpentracingTimingCollector(Tracer tracer, SpanDecorator spanDecorator,
ActiveSpanSource activeSpanSource, TimingCollector next) {
this.tracer = tracer;
this.spanDecorator = spanDecorator;
this.activeSpanSource = activeSpanSource;
this.next = next;
}

public void collect(long elapsedNanos, StatementContext statementContext) {
long nowMicros = System.currentTimeMillis() * 1000;
Tracer.SpanBuilder builder = tracer
.buildSpan(spanDecorator.generateOperationName(statementContext))
.withTag(Tags.COMPONENT.getKey(), "java-jdbi")
.withTag(Tags.DB_STATEMENT.getKey(), statementContext.getRawSql())
.withStartTimestamp(nowMicros - (elapsedNanos / 1000));
Span parent = (Span) statementContext.getAttribute(PARENT_SPAN_ATTRIBUTE_KEY);
if (parent == null && this.activeSpanSource != null) {
parent = this.activeSpanSource.activeSpan(statementContext);
}
if (parent != null) {
builder = builder.asChildOf(parent);
}
Span collectSpan = builder.start();
spanDecorator.decorateSpan(collectSpan, statementContext);
collectSpan.finish(nowMicros);

if (next != null) {
next.collect(elapsedNanos, statementContext);
}
}

/**
* Establish an explicit parent relationship for the (child) Span associated with a
* SQLStatement.
*
* @param statement the JDBI SQLStatement which will act as the child of `parent`
* @param parent the parent Span for `statement`
*/
public static void setParent(SqlStatement<?> statement, Span parent) {
statement.getContext().define(PARENT_SPAN_ATTRIBUTE_KEY, parent);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
import org.jdbi.v3.core.statement.StatementContext;

/**
* SpanDecorator allows the OpenTracingCollector user to control the precise naming and
* SpanDecorator allows the OpentracingSqlLogger user to control the precise naming and
* decoration of OpenTracing
* Spans emitted by the collector.
*
* @see OpenTracingCollector#OpenTracingCollector(Tracer, SpanDecorator)
* @see OpentracingSqlLogger#OpentracingSqlLogger(Tracer, SpanDecorator)
*/
public interface SpanDecorator {

Expand Down
Loading

0 comments on commit dc635eb

Please sign in to comment.