-
Notifications
You must be signed in to change notification settings - Fork 12
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
Prepare statements support #9
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,16 +13,7 @@ | |
*/ | ||
package io.opentracing.contrib.cassandra; | ||
|
||
import com.datastax.driver.core.BoundStatement; | ||
import com.datastax.driver.core.CloseFuture; | ||
import com.datastax.driver.core.Cluster; | ||
import com.datastax.driver.core.Host; | ||
import com.datastax.driver.core.PreparedStatement; | ||
import com.datastax.driver.core.RegularStatement; | ||
import com.datastax.driver.core.ResultSet; | ||
import com.datastax.driver.core.ResultSetFuture; | ||
import com.datastax.driver.core.Session; | ||
import com.datastax.driver.core.Statement; | ||
import com.datastax.driver.core.*; | ||
import com.google.common.base.Function; | ||
import com.google.common.util.concurrent.Futures; | ||
import com.google.common.util.concurrent.ListenableFuture; | ||
|
@@ -31,6 +22,7 @@ | |
import io.opentracing.contrib.cassandra.QuerySpanNameProvider.CustomStringSpanName; | ||
import io.opentracing.contrib.cassandra.QuerySpanNameProvider.QuerySpanNameProvider; | ||
import io.opentracing.tag.Tags; | ||
|
||
import java.io.PrintWriter; | ||
import java.io.StringWriter; | ||
import java.net.Inet4Address; | ||
|
@@ -219,31 +211,56 @@ public ResultSetFuture executeAsync(Statement statement) { | |
*/ | ||
@Override | ||
public PreparedStatement prepare(String query) { | ||
return session.prepare(query); | ||
Span span = buildSpan(query); | ||
try { | ||
PreparedStatement resultSet = session.prepare(query); | ||
finishSpan(span); | ||
return resultSet; | ||
} catch (Exception e) { | ||
finishSpan(span, e); | ||
throw e; | ||
} | ||
} | ||
|
||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public PreparedStatement prepare(RegularStatement statement) { | ||
return session.prepare(statement); | ||
String query = getQuery(statement); | ||
Span span = buildSpan(query); | ||
try { | ||
PreparedStatement resultSet = session.prepare(statement); | ||
finishSpan(span); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't span created (started) when prepared statement started to execute. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ha! you're right ;) I will 👀 into it |
||
return resultSet; | ||
} catch (Exception e) { | ||
finishSpan(span, e); | ||
throw e; | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public ListenableFuture<PreparedStatement> prepareAsync(String query) { | ||
return session.prepareAsync(query); | ||
final Span span = buildSpan(query); | ||
ListenableFuture<PreparedStatement> future = session.prepareAsync(query); | ||
future.addListener(createListener(span, future), executorService); | ||
return future; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public ListenableFuture<PreparedStatement> prepareAsync(RegularStatement statement) { | ||
return session.prepareAsync(statement); | ||
String query = getQuery(statement); | ||
final Span span = buildSpan(query); | ||
ListenableFuture<PreparedStatement> future = session.prepareAsync(statement); | ||
future.addListener(createListener(span, future), executorService); | ||
return future; | ||
} | ||
|
||
/** | ||
|
@@ -307,6 +324,23 @@ public void run() { | |
}; | ||
} | ||
|
||
private Runnable createListener( | ||
final Span span, | ||
final ListenableFuture<PreparedStatement> future | ||
) { | ||
return new Runnable() { | ||
@Override | ||
public void run() { | ||
try { | ||
future.get(); | ||
finishSpan(span); | ||
} catch (InterruptedException | ExecutionException e) { | ||
finishSpan(span, e); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
private Span buildSpan(String query) { | ||
String querySpanName = querySpanNameProvider.querySpanName(query); | ||
Tracer.SpanBuilder spanBuilder = tracer.buildSpan(querySpanName) | ||
|
@@ -345,6 +379,10 @@ private static void finishSpan(Span span, ResultSet resultSet) { | |
span.finish(); | ||
} | ||
|
||
private static void finishSpan(Span span) { | ||
span.finish(); | ||
} | ||
|
||
private static void finishSpan(Span span, Exception e) { | ||
Tags.ERROR.set(span, Boolean.TRUE); | ||
span.log(errorLogs(e)); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package io.opentracing.contrib.cassandra; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import com.datastax.driver.core.*; | ||
import com.datastax.driver.mapping.MappingManager; | ||
import com.datastax.driver.mapping.annotations.Column; | ||
import com.datastax.driver.mapping.annotations.Table; | ||
import io.opentracing.Scope; | ||
import io.opentracing.contrib.cassandra.QuerySpanNameProvider.PrefixedFullQuerySpanName; | ||
import io.opentracing.mock.MockSpan; | ||
import io.opentracing.mock.MockTracer; | ||
import io.opentracing.util.ThreadLocalScopeManager; | ||
import org.cassandraunit.utils.EmbeddedCassandraServerHelper; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.util.UUID; | ||
|
||
public class CassandraMappingTest { | ||
|
||
private static final MockTracer mockTracer = new MockTracer( | ||
new ThreadLocalScopeManager(), | ||
MockTracer.Propagator.TEXT_MAP | ||
); | ||
|
||
@Before | ||
public void before() throws Exception { | ||
System.setProperty("java.library.path", "src/test/resources/libs"); | ||
EmbeddedCassandraServerHelper.startEmbeddedCassandra(); | ||
Session session = createSession(); | ||
createKeyspace(session); | ||
createTable(session); | ||
session.close(); | ||
mockTracer.reset(); | ||
} | ||
|
||
@After | ||
public void after() { | ||
EmbeddedCassandraServerHelper.cleanEmbeddedCassandra(); | ||
} | ||
|
||
@Test | ||
public void mapperOperationShouldSetParentCorrectly() { | ||
Session session = createSession(); | ||
//when | ||
try (Scope ignored = mockTracer.buildSpan("test operation").startActive(true)) { | ||
MappingManager mappingManager = new MappingManager(session); | ||
mappingManager.mapper(Book.class).save(new Book(UUID.randomUUID(), "random book")); | ||
session.close(); | ||
} | ||
|
||
//then | ||
MockSpan testOperation = findSpanWithNameStartingWith("test operation"); | ||
MockSpan testedOperation = findSpanWithNameStartingWith("tested operation"); | ||
|
||
assertNotNull(testOperation); | ||
assertNotNull(testedOperation); | ||
assertEquals( | ||
"'tested operation' span should be a child of wrapping 'test operation' span", | ||
testOperation.context().spanId(), | ||
testedOperation.parentId() | ||
); | ||
} | ||
|
||
private MockSpan findSpanWithNameStartingWith(String operationNamePrefix) { | ||
for (MockSpan mockSpan : mockTracer.finishedSpans()) { | ||
if(mockSpan.operationName().startsWith(operationNamePrefix)) { | ||
return mockSpan; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
private Session createSession() { | ||
Cluster.Builder builder = Cluster.builder().addContactPoints("127.0.0.1").withPort(9142); | ||
Cluster cluster = new TracingCluster( | ||
builder, | ||
mockTracer, | ||
PrefixedFullQuerySpanName.newBuilder().build("tested operation") | ||
); | ||
Session session = cluster.connect(); | ||
assertFalse(session.isClosed()); | ||
assertNotNull(session.getState()); | ||
assertNotNull(session.getCluster()); | ||
assertNull(session.getLoggedKeyspace()); | ||
return cluster.connect(); | ||
} | ||
|
||
private void createKeyspace(Session session) { | ||
PreparedStatement prepared = session.prepare( | ||
"create keyspace test WITH replication = {'class':'SimpleStrategy', 'replication_factor' : 1};"); | ||
BoundStatement bound = prepared.bind(); | ||
session.execute(bound); | ||
} | ||
|
||
private void createTable(Session session) { | ||
session.execute("CREATE TABLE IF NOT EXISTS test.book (id uuid PRIMARY KEY, title text)"); | ||
} | ||
|
||
@Table(name = "book", keyspace = "test") | ||
public static class Book { | ||
|
||
public Book(UUID id, String title) { | ||
this.id = id; | ||
this.title = title; | ||
} | ||
|
||
@Column | ||
private UUID id; | ||
|
||
@Column | ||
private String title; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please don't use stars in imports