Start plumbing for Dropwizard metrics and Zipkin tracing. Ensure proper bean value when working with enums.

This commit is contained in:
Greg Burd 2017-08-08 12:02:32 -04:00
parent f4dbf34920
commit 71e84da3bd
7 changed files with 120 additions and 14 deletions

View file

@ -17,7 +17,6 @@
<orderEntry type="library" name="Maven: io.netty:netty-common:4.0.47.Final" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-transport:4.0.47.Final" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-codec:4.0.47.Final" level="project" />
<orderEntry type="library" name="Maven: io.dropwizard.metrics:metrics-core:3.2.2" level="project" />
<orderEntry type="library" name="Maven: com.github.jnr:jnr-ffi:2.0.7" level="project" />
<orderEntry type="library" name="Maven: com.github.jnr:jffi:1.2.10" level="project" />
<orderEntry type="library" scope="RUNTIME" name="Maven: com.github.jnr:jffi:native:1.2.10" level="project" />
@ -36,6 +35,10 @@
<orderEntry type="library" name="Maven: org.springframework:spring-core:4.3.10.RELEASE" level="project" />
<orderEntry type="library" name="Maven: commons-logging:commons-logging:1.2" level="project" />
<orderEntry type="library" name="Maven: com.google.guava:guava:20.0" level="project" />
<orderEntry type="library" name="Maven: io.zipkin.java:zipkin:1.29.2" level="project" />
<orderEntry type="library" name="Maven: io.zipkin.brave:brave:4.0.6" level="project" />
<orderEntry type="library" name="Maven: io.zipkin.reporter:zipkin-reporter:0.6.12" level="project" />
<orderEntry type="library" name="Maven: io.dropwizard.metrics:metrics-core:3.2.2" level="project" />
<orderEntry type="library" name="Maven: javax.validation:validation-api:2.0.0.CR3" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.codehaus.jackson:jackson-mapper-asl:1.9.13" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: com.anthemengineering.mojo:infer-maven-plugin:0.1.0" level="project" />

19
pom.xml
View file

@ -148,6 +148,25 @@
<version>20.0</version>
</dependency>
<!-- Metrics and tracing -->
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin</artifactId>
<version>1.29.2</version>
</dependency>
<dependency>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave</artifactId>
<version>4.0.6</version>
</dependency>
<dependency>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-core</artifactId>
<version>3.2.2</version>
</dependency>
<!-- Validation -->
<dependency>
<groupId>javax.validation</groupId>

View file

@ -19,6 +19,8 @@ import java.io.PrintStream;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import brave.Tracer;
import com.codahale.metrics.MetricRegistry;
import com.datastax.driver.core.schemabuilder.SchemaStatement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -109,6 +111,10 @@ public abstract class AbstractSessionOperations {
}
}
public Tracer getZipkinTracer() { return null; }
public MetricRegistry getMetricRegistry() { return null; }
public void cache(String key, Object value) {
}

View file

@ -23,6 +23,8 @@ import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import brave.Tracer;
import com.codahale.metrics.MetricRegistry;
import com.datastax.driver.core.*;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
@ -48,6 +50,8 @@ public final class HelenusSession extends AbstractSessionOperations implements C
private volatile String usingKeyspace;
private volatile boolean showCql;
private final ConsistencyLevel defaultConsistencyLevel;
private final MetricRegistry metricRegistry;
private final Tracer zipkinTracer;
private final PrintStream printStream;
private final SessionRepository sessionRepository;
private final Executor executor;
@ -61,7 +65,8 @@ public final class HelenusSession extends AbstractSessionOperations implements C
HelenusSession(Session session, String usingKeyspace, CodecRegistry registry, boolean showCql,
PrintStream printStream, SessionRepositoryBuilder sessionRepositoryBuilder, Executor executor,
boolean dropSchemaOnClose, ConsistencyLevel consistencyLevel) {
boolean dropSchemaOnClose, ConsistencyLevel consistencyLevel, MetricRegistry metricRegistry,
Tracer tracer) {
this.session = session;
this.registry = registry == null ? CodecRegistry.DEFAULT_INSTANCE : registry;
this.usingKeyspace = Objects.requireNonNull(usingKeyspace,
@ -72,6 +77,8 @@ public final class HelenusSession extends AbstractSessionOperations implements C
this.executor = executor;
this.dropSchemaOnClose = dropSchemaOnClose;
this.defaultConsistencyLevel = consistencyLevel;
this.metricRegistry = metricRegistry;
this.zipkinTracer = tracer;
this.valueProvider = new RowColumnValueProvider(this.sessionRepository);
this.valuePreparer = new StatementColumnValuePreparer(this.sessionRepository);
@ -81,6 +88,7 @@ public final class HelenusSession extends AbstractSessionOperations implements C
this.currentUnitOfWork = null;
}
@Override
public Session currentSession() {
return session;
@ -137,6 +145,12 @@ public final class HelenusSession extends AbstractSessionOperations implements C
return valuePreparer;
}
@Override
public Tracer getZipkinTracer() { return zipkinTracer; }
@Override
public MetricRegistry getMetricRegistry() { return metricRegistry; }
public ConsistencyLevel getDefaultConsistencyLevel() {
return defaultConsistencyLevel;
}
@ -367,6 +381,10 @@ public final class HelenusSession extends AbstractSessionOperations implements C
return session;
}
public <E> E dsl(Class<E> iface) {
return Helenus.dsl(iface, getMetadata());
}
public void close() {
if (session.isClosed()) {

View file

@ -15,6 +15,7 @@
*/
package net.helenus.core;
import com.codahale.metrics.MetricRegistry;
import com.datastax.driver.core.*;
import com.google.common.util.concurrent.MoreExecutors;
import net.helenus.mapping.HelenusEntity;
@ -24,6 +25,8 @@ import net.helenus.mapping.value.ColumnValueProvider;
import net.helenus.support.HelenusException;
import net.helenus.support.PackageUtil;
import brave.Tracer;
import java.io.IOException;
import java.io.PrintStream;
import java.util.*;
@ -38,6 +41,8 @@ public final class SessionInitializer extends AbstractSessionOperations {
private String usingKeyspace;
private boolean showCql = false;
private ConsistencyLevel consistencyLevel;
private MetricRegistry metricRegistry;
private Tracer zipkinTracer;
private PrintStream printStream = System.out;
private Executor executor = MoreExecutors.directExecutor();
@ -97,6 +102,16 @@ public final class SessionInitializer extends AbstractSessionOperations {
return this;
}
public SessionInitializer metricRegistry(MetricRegistry metricRegistry) {
this.metricRegistry = metricRegistry;
return this;
}
public SessionInitializer zipkinTracer(Tracer tracer) {
this.zipkinTracer = tracer;
return this;
}
public SessionInitializer consistencyLevel(ConsistencyLevel consistencyLevel) {
this.consistencyLevel = consistencyLevel;
return this;
@ -213,7 +228,7 @@ public final class SessionInitializer extends AbstractSessionOperations {
public synchronized HelenusSession get() {
initialize();
return new HelenusSession(session, usingKeyspace, registry, showCql, printStream, sessionRepository, executor,
autoDdl == AutoDdl.CREATE_DROP, consistencyLevel);
autoDdl == AutoDdl.CREATE_DROP, consistencyLevel, metricRegistry, zipkinTracer);
}
private void initialize() {

View file

@ -15,20 +15,23 @@
*/
package net.helenus.core.operation;
import java.util.Optional;
import brave.Span;
import brave.Tracer;
import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.ResultSetFuture;
import com.google.common.base.Function;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import net.helenus.core.AbstractSessionOperations;
import java.util.Optional;
public abstract class AbstractOptionalOperation<E, O extends AbstractOptionalOperation<E, O>>
extends AbstractStatementOperation<E, O> {
Span span;
public AbstractOptionalOperation(AbstractSessionOperations sessionOperations) {
super(sessionOperations);
}
@ -50,17 +53,51 @@ public abstract class AbstractOptionalOperation<E, O extends AbstractOptionalOpe
});
}
public AbstractOptionalOperation<E, O> withinSpan(Span span) {
if (span != null) {
Tracer tracer = this.sessionOps.getZipkinTracer();
if (tracer != null) {
this.span = span;
}
}
return this;
}
public Optional<E> sync() {
Tracer tracer = this.sessionOps.getZipkinTracer();
final Span cassandraSpan = (tracer != null && span != null) ? tracer.newChild(span.context()) : null;
if (cassandraSpan != null) {
cassandraSpan.name("cassandra");
cassandraSpan.start();
}
ResultSet resultSet = sessionOps.executeAsync(options(buildStatement()), showValues).getUninterruptibly();
return transform(resultSet);
Optional<E> result = transform(resultSet);
if (cassandraSpan != null) {
cassandraSpan.finish();
}
return result;
}
public ListenableFuture<Optional<E>> async() {
final Tracer tracer = this.sessionOps.getZipkinTracer();
final Span cassandraSpan = (tracer != null && span != null) ? tracer.newChild(span.context()) : null;
if (cassandraSpan != null) {
cassandraSpan.name("cassandra");
cassandraSpan.start();
}
ResultSetFuture resultSetFuture = sessionOps.executeAsync(options(buildStatement()), showValues);
ListenableFuture<Optional<E>> future = Futures.transform(resultSetFuture,
new Function<ResultSet, Optional<E>>() {
@Override
public Optional<E> apply(ResultSet resultSet) {
if (cassandraSpan != null) {
cassandraSpan.finish();
}
return transform(resultSet);
}
}, sessionOps.getExecutor());

View file

@ -88,10 +88,10 @@ public class MapperInvocationHandler<E> implements InvocationHandler {
Object value = src.get(methodName);
if (value == null) {
Class<?> returnType = method.getReturnType();
if (value == null) {
if (returnType.isPrimitive()) {
DefaultPrimitiveTypes type = DefaultPrimitiveTypes.lookup(returnType);
@ -101,8 +101,16 @@ public class MapperInvocationHandler<E> implements InvocationHandler {
return type.getDefaultValue();
} else if (returnType.isEnum()) {
throw new HelenusException("missing default type for enum user type " + returnType);
}
} else if (returnType.isEnum() && (value.getClass() == Integer.class || value.getClass() == int.class)) {
try {
value = returnType.getClass().getEnumConstants()[(int) value];
} catch (ArrayIndexOutOfBoundsException e) {
throw new IllegalArgumentException("invalid ordinal " + value + " for enum type " + returnType.getSimpleName());
}
}
return value;