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

Switch from opentracing to opentelemetry #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-java@v2
with:
java-version: '8'
java-version: '11'
distribution: 'adopt'
- uses: actions/cache@v2
with:
Expand All @@ -34,7 +34,7 @@ jobs:
path: '*/target/*.jar'
retention-days: 1
publish:
if: ${{ (github.event_name != 'pull_request') && (github.repository == 'lucidsoftware/opentracing-jdbc') }}
if: ${{ (github.event_name != 'pull_request') && (github.repository == 'lucidsoftware/otel-jdbc') }}
needs: build
runs-on: ubuntu-20.04
steps:
Expand All @@ -54,7 +54,7 @@ jobs:
SONATYPE_USERNAME: ${{ secrets.SONATYPE_USERNAME }}
SONATYPE_PASSWORD: ${{ secrets.SONATYPE_PASSWORD }}
release:
if: ${{ startsWith(github.ref, 'refs/tags') && (github.repository == 'lucidsoftware/opentracing-jdbc') }}
if: ${{ startsWith(github.ref, 'refs/tags') && (github.repository == 'lucidsoftware/otel-jdbc') }}
runs-on: ubuntu-latest
needs: build
steps:
Expand All @@ -70,4 +70,4 @@ jobs:
set -x
tag="${GITHUB_REF#refs/tags/}"
targets="jdbc/target/*.jar"
hub release create -a $targets -m "Release $tag" $tag
hub release create -a $targets -m "Release $tag" $tag
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@

target/
target-*/

.metals/
.bloop/
.bsp/
project/metals.sbt
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
# OpenTracing for JDBC
# OpenTelemetry for JDBC

[![Build Status](https://travis-ci.com/lucidsoftware/opentracing-jdbc.svg)](https://travis-ci.com/lucidsoftware/opentracing-jdbc)
![Maven Version](https://img.shields.io/maven-central/v/com.lucidchart/opentracing-jdbc.svg)
![Maven Version](https://img.shields.io/maven-central/v/com.lucidchart/otel-jdbc.svg)

## Install

Published as the `com.lucidchart:opentracing-jdbc` artifact.
Published as the `com.lucidchart:otel-jdbc` artifact.

## Example

```java
import io.opentracing.contrib.jdbc.*;
import com.lucidchart.otel.jdbc.*;
import java.net.InetAddress;
import java.nio.ByteBuffer;

Expand Down
19 changes: 11 additions & 8 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,21 @@ inScope(Global)(Seq(
sys.env.getOrElse("SONATYPE_PASSWORD", ""
)),
crossPaths := false,
developers += Developer("pauldraper", "Paul Draper", "[email protected]", url("https://github.com/pauldraper")),
homepage := Some(url("https://git.lucidchart.com/lucidsoftware/opentracing-playframework")),
developers ++= List(
Developer("lucidsoftware", "Lucid Software, Inc.", "[email protected]", url("https://lucid.co")),
Developer("tmccombs", "Thayne McCombs", "[email protected]", url("https://github.com/tmccombs")),
),
homepage := Some(url("https://git.lucidchart.com/lucidsoftware/otel-jdbc")),
licenses += "Apache 2.0 License" -> url("https://www.apache.org/licenses/LICENSE-2.0"),
organization := "com.lucidchart",
organizationHomepage := Some(url("http://opentracing.io/")),
organizationName := "OpenTracing",
organizationHomepage := Some(url("http://lucid.co/")),
organizationName := "Lucid Software, Inc.",
scmInfo := Some(ScmInfo(
url("https://github.com/lucidsoftware/opentracing-jdbc"),
"scm:git:[email protected]:lucidsoftware/opentracing-jdbc.git"
url("https://github.com/lucidsoftware/otel-jdbc"),
"scm:git:[email protected]:lucidsoftware/otel-jdbc.git"
)),
startYear := Some(2017),
startYear := Some(2024),
version := sys.props.getOrElse("build.version", "0-SNAPSHOT")
))

skip in publish := true
publish / skip := true
10 changes: 5 additions & 5 deletions jdbc/build.sbt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
libraryDependencies ++= Seq(
"com.lucidchart" % "opentracing-thread-context" % "0.5",
"io.opentracing" % "opentracing-api" % "0.31.0",
"io.opentracing" % "opentracing-util" % "0.31.0",
"p6spy" % "p6spy" % "3.9.1"
"io.opentelemetry" % "opentelemetry-bom" % "1.41.0",
"io.opentelemetry" % "opentelemetry-api" % "1.41.0",
"io.opentelemetry.semconv" % "opentelemetry-semconv" % "1.27.0-alpha",
"p6spy" % "p6spy" % "3.9.1",
)

moduleName := s"opentracing-${name.value}"
moduleName := s"otel-${name.value}"
publishTo := sonatypePublishToBundle.value
9 changes: 9 additions & 0 deletions jdbc/src/main/java/co/lucid/otel/jdbc/JdbcPeer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package co.lucid.otel.jdbc;

public class JdbcPeer {

public String name;
public String ip;
public Long port;

}
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
package io.opentracing.contrib.jdbc;
package co.lucid.otel.jdbc;

import com.p6spy.engine.common.ConnectionInformation;
import com.p6spy.engine.event.JdbcEventListener;
import com.p6spy.engine.wrapper.ConnectionWrapper;
import io.opentracing.Tracer;
import io.opentracing.util.GlobalTracer;
import io.opentracing.threadcontext.ContextSpan;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.Tracer;
import java.sql.Connection;

public class SpanConnectionWrapper {

public static SpanConnectionWrapper createDefault(JdbcPeer peer) {
return new SpanConnectionWrapper(GlobalTracer.get(), ContextSpan.DEFAULT, peer);
return new SpanConnectionWrapper(GlobalOpenTelemetry.getTracer("co.lucid.otel.jdbc"), peer);
}

private final JdbcEventListener eventListener;

public SpanConnectionWrapper(Tracer tracer, ContextSpan contextSpan, JdbcPeer peer) {
this.eventListener = new SpanEventListener(tracer, contextSpan, peer);
public SpanConnectionWrapper(Tracer tracer, JdbcPeer peer) {
this.eventListener = new SpanEventListener(tracer, peer);
}

public Connection wrap(Connection delegate) {
Expand All @@ -27,5 +27,4 @@ public Connection wrap(Connection delegate) {
ConnectionInformation.fromTestConnection(delegate)
);
}

}
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package io.opentracing.contrib.jdbc;
package co.lucid.otel.jdbc;

import com.p6spy.engine.common.PreparedStatementInformation;
import com.p6spy.engine.common.StatementInformation;
import com.p6spy.engine.event.JdbcEventListener;
import io.opentracing.Span;
import io.opentracing.Tracer;
import io.opentracing.noop.NoopSpan;
import io.opentracing.tag.Tags;
import io.opentracing.threadcontext.ContextSpan;
import java.io.PrintWriter;
import java.io.StringWriter;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.api.trace.StatusCode;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.semconv.SemanticAttributes;
import io.opentelemetry.semconv.NetworkAttributes;
import java.sql.Driver;
import java.sql.SQLException;
import java.time.Instant;
Expand All @@ -18,16 +17,15 @@

public class SpanEventListener extends JdbcEventListener {

private final ContextSpan spanContext;
private final Tracer tracer;
private final JdbcPeer peer;

public SpanEventListener(Tracer tracer, ContextSpan spanContext, JdbcPeer peer) {
this.spanContext = spanContext;
public SpanEventListener(Tracer tracer, JdbcPeer peer) {
this.tracer = tracer;
this.peer = peer;
}


@Override
public void onAfterExecute(StatementInformation statementInformation, long timeElapsedNanos, String sql, SQLException e) {
onAfterExecute("execute", statementInformation, timeElapsedNanos, e);
Expand Down Expand Up @@ -62,38 +60,30 @@ private void onAfterExecute(String type, StatementInformation statementInformati
Instant end = Instant.now();
Instant start = end.minusNanos(timeElapsedNanos);
String sql = statementInformation.getSql();
Span parentSpan = spanContext.get() != NoopSpan.INSTANCE ? spanContext.get() : null;
Span span = tracer.buildSpan(String.format("SQL %s %s", type, new Scanner(sql).next()))
.asChildOf(parentSpan)
.withStartTimestamp(TimeUnit.SECONDS.toMicros(start.getEpochSecond()) + TimeUnit.NANOSECONDS.toMicros(start.getNano()))
.start();
Tags.SPAN_KIND.set(span, Tags.SPAN_KIND_CLIENT);
var span = tracer.spanBuilder(String.format("SQL %s %s", type, new Scanner(sql).next()))
.setStartTimestamp(start)
.setAttribute(SemanticAttributes.DB_STATEMENT, sql)
.setSpanKind(SpanKind.CLIENT)
.startSpan();
if (peer.name != null) {
Tags.PEER_SERVICE.set(span, peer.name);
}
if (peer.ipv4 != null) {
Tags.PEER_HOST_IPV4.set(span, peer.ipv4);
span.setAttribute(SemanticAttributes.PEER_SERVICE, peer.name);
}
if (peer.ipv6 != null) {
Tags.PEER_HOST_IPV6.set(span, peer.ipv6);
if (peer.ip != null) {
span.setAttribute(NetworkAttributes.NETWORK_PEER_ADDRESS, peer.ip);
}
if (peer.port != null) {
Tags.PEER_PORT.set(span, peer.port);
span.setAttribute(NetworkAttributes.NETWORK_PEER_PORT, peer.port);
}
Driver driver = statementInformation.getConnectionInformation().getDriver();
if (driver != null) {
span.setTag("jdbc.driver", driver.getClass().getName());
span.setAttribute(SemanticAttributes.DB_JDBC_DRIVER_CLASSNAME, driver.getClass().getName());
}
span.setTag("jdbc.query", sql);

if (e != null) {
Tags.ERROR.set(span, true);
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
span.setTag("error.message", e.getMessage() + ". SqlState: " + e.getSQLState()+ ". ErrorCode: " +e.getErrorCode());
span.setTag("error.stack", sw.toString());
span.recordException(e);
span.setStatus(StatusCode.ERROR, e.getMessage() + ". SqlState: " + e.getSQLState() + ". ErrorCode: " + e.getErrorCode());
}
span.finish(TimeUnit.SECONDS.toMicros(end.getEpochSecond()) + TimeUnit.NANOSECONDS.toMicros(end.getNano()));
span.end(end);
}

}
10 changes: 0 additions & 10 deletions jdbc/src/main/java/io/opentracing/contrib/jdbc/JdbcPeer.java

This file was deleted.

2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.3.3
sbt.version=1.10.1
Loading