From 6dafbc6f5593df10520f3b84590878e53251158a Mon Sep 17 00:00:00 2001 From: Lance Linder Date: Mon, 24 Apr 2017 11:59:48 -0500 Subject: [PATCH] Add Cassandra tracing module --- build.gradle | 2 -- ratpack-cassandra-tracing/build.gradle | 12 +++++++ .../zipkin/CassandraTracingModule.java | 33 +++++++++++++++++++ .../cassandra/zipkin/TracedSession.java | 31 +++++++++++++++++ .../ratpack/cassandra/AbstractSession.java | 13 ++++---- settings.gradle | 1 + 6 files changed, 83 insertions(+), 9 deletions(-) create mode 100644 ratpack-cassandra-tracing/build.gradle create mode 100644 ratpack-cassandra-tracing/src/main/java/smartthings/ratpack/cassandra/zipkin/CassandraTracingModule.java create mode 100644 ratpack-cassandra-tracing/src/main/java/smartthings/ratpack/cassandra/zipkin/TracedSession.java diff --git a/build.gradle b/build.gradle index 6d7b242..1a9417b 100644 --- a/build.gradle +++ b/build.gradle @@ -25,7 +25,6 @@ task wrapper(type: Wrapper) { subprojects { - group = "smartthings" version = rootProject.file('version.txt').text.trim() @@ -37,7 +36,6 @@ subprojects { apply plugin: 'com.github.ben-manes.versions' apply from: rootProject.file('gradle/publishing.gradle') - sourceCompatibility = "1.8" targetCompatibility = "1.8" diff --git a/ratpack-cassandra-tracing/build.gradle b/ratpack-cassandra-tracing/build.gradle new file mode 100644 index 0000000..1039f75 --- /dev/null +++ b/ratpack-cassandra-tracing/build.gradle @@ -0,0 +1,12 @@ +dependencies { + compile "io.ratpack:ratpack-core:${ratpackVersion}" + compile "io.ratpack:ratpack-guice:${ratpackVersion}" + + compile 'com.datastax.cassandra:cassandra-driver-core:3.1.0' + compile 'smartthings.brave:smartthings-brave-cassandra-latencytracker:0.1.14' + compile project(':ratpack-cassandra') + + testCompile "io.ratpack:ratpack-groovy-test:${ratpackVersion}" + //Note cassandra unit pulls in netty-all which may need changed if the version conflicts with Ratpacks + testCompile 'org.cassandraunit:cassandra-unit:3.0.0.1' +} diff --git a/ratpack-cassandra-tracing/src/main/java/smartthings/ratpack/cassandra/zipkin/CassandraTracingModule.java b/ratpack-cassandra-tracing/src/main/java/smartthings/ratpack/cassandra/zipkin/CassandraTracingModule.java new file mode 100644 index 0000000..5690ebe --- /dev/null +++ b/ratpack-cassandra-tracing/src/main/java/smartthings/ratpack/cassandra/zipkin/CassandraTracingModule.java @@ -0,0 +1,33 @@ +package smartthings.ratpack.cassandra.zipkin; + +import com.datastax.driver.core.Session; +import com.google.inject.Scopes; +import smartthings.ratpack.cassandra.CassandraHealthCheck; +import smartthings.ratpack.cassandra.CassandraModule; +import smartthings.ratpack.cassandra.CassandraService; +import smartthings.ratpack.cassandra.RatpackSession; + +public class CassandraTracingModule extends CassandraModule { + + @Override + protected void configure() { + bind(TracedSession.class).in(Scopes.SINGLETON); + bind(Session.class).to(TracedSession.class); + bind(RatpackSession.class).to(TracedSession.class); + bind(CassandraService.class).in(Scopes.SINGLETON); + bind(CassandraHealthCheck.class).in(Scopes.SINGLETON); + } + + public static class Config extends CassandraModule.Config { + String serviceName; + + public String getServiceName() { + return serviceName; + } + + public void setServiceName(String serviceName) { + this.serviceName = serviceName; + } + } + +} diff --git a/ratpack-cassandra-tracing/src/main/java/smartthings/ratpack/cassandra/zipkin/TracedSession.java b/ratpack-cassandra-tracing/src/main/java/smartthings/ratpack/cassandra/zipkin/TracedSession.java new file mode 100644 index 0000000..851580f --- /dev/null +++ b/ratpack-cassandra-tracing/src/main/java/smartthings/ratpack/cassandra/zipkin/TracedSession.java @@ -0,0 +1,31 @@ +package smartthings.ratpack.cassandra.zipkin; + +import com.datastax.driver.core.Cluster; +import com.datastax.driver.core.Session; +import com.github.kristofa.brave.Brave; +import com.google.inject.Inject; +import smartthings.ratpack.cassandra.AbstractSession; + +public final class TracedSession extends AbstractSession { + + private final Brave brave; + private final Cluster cluster; + private final String keyspace; + private final CassandraTracingModule.Config config; + + @Inject + public TracedSession(Cluster cluster, Brave brave, CassandraTracingModule.Config config) { + super(cluster, config.getKeyspace()); + this.brave = brave; + this.cluster = cluster; + this.keyspace = config.getKeyspace(); + this.config = config; + } + + protected Session createDelegate() { + Session session = (keyspace != null && !keyspace.equals("")) ? cluster.connect(keyspace) : cluster.connect(); + + return smartthings.brave.cassandra.TracedSession.create(session, brave, config.getServiceName()); + } + +} diff --git a/ratpack-cassandra/src/main/java/smartthings/ratpack/cassandra/AbstractSession.java b/ratpack-cassandra/src/main/java/smartthings/ratpack/cassandra/AbstractSession.java index 6223150..dc10359 100644 --- a/ratpack-cassandra/src/main/java/smartthings/ratpack/cassandra/AbstractSession.java +++ b/ratpack-cassandra/src/main/java/smartthings/ratpack/cassandra/AbstractSession.java @@ -10,7 +10,6 @@ import com.datastax.driver.core.Statement; import com.google.common.util.concurrent.ListenableFuture; import java.util.Map; -import java.util.Optional; import ratpack.exec.Promise; import ratpack.service.Service; import ratpack.service.StartEvent; @@ -26,17 +25,17 @@ public abstract class AbstractSession implements RatpackSession, Session, Service { private final Cluster cluster; - private final Optional keyspace; + private final String keyspace; private Session delegate; - AbstractSession(Cluster cluster) { + public AbstractSession(Cluster cluster) { this.cluster = cluster; - this.keyspace = Optional.empty(); + this.keyspace = null; } - AbstractSession(Cluster cluster, String keyspace) { + public AbstractSession(Cluster cluster, String keyspace) { this.cluster = cluster; - this.keyspace = Optional.of(keyspace); + this.keyspace = keyspace; } @Override @@ -212,6 +211,6 @@ protected final Session getDelegate() { } protected Session createDelegate() { - return (keyspace.isPresent()) ? cluster.connect(keyspace.get()) : cluster.connect(); + return (keyspace != null && !keyspace.equals("")) ? cluster.connect(keyspace) : cluster.connect(); } } diff --git a/settings.gradle b/settings.gradle index 6faf914..12a2dbe 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,6 +1,7 @@ include 'ratpack-cassandra' include 'ratpack-cassandra-rx' include 'ratpack-cassandra-migrate' +include 'ratpack-cassandra-tracing' rootProject.name='ratpack-cassandra-parent'