|
2 | 2 |
|
3 | 3 | import static org.assertj.core.api.Assertions.assertThat; |
4 | 4 |
|
| 5 | +import com.datadoghq.trace.DDBaseSpan; |
| 6 | +import com.datadoghq.trace.DDTracer; |
| 7 | +import com.datadoghq.trace.writer.ListWriter; |
5 | 8 | import com.datastax.driver.core.Cluster; |
6 | 9 | import com.datastax.driver.core.Session; |
7 | | -import java.io.IOException; |
8 | | -import java.util.concurrent.ExecutionException; |
9 | | -import org.apache.cassandra.exceptions.ConfigurationException; |
10 | | -import org.apache.thrift.transport.TTransportException; |
| 10 | +import io.opentracing.Tracer; |
| 11 | +import io.opentracing.tag.Tags; |
11 | 12 | import org.cassandraunit.utils.EmbeddedCassandraServerHelper; |
12 | | -import org.junit.After; |
13 | | -import org.junit.Before; |
| 13 | +import org.junit.AfterClass; |
| 14 | +import org.junit.Assert; |
| 15 | +import org.junit.BeforeClass; |
14 | 16 | import org.junit.Test; |
15 | 17 | import org.junit.experimental.categories.Category; |
16 | 18 |
|
17 | | -/** Created by gpolaert on 6/2/17. */ |
18 | 19 | @Category(ExpensiveTest.class) |
19 | 20 | public class CassandraIntegrationTest { |
| 21 | + private static final ListWriter writer = new ListWriter(); |
| 22 | + private static final Tracer tracer = new DDTracer(writer); |
20 | 23 |
|
21 | | - @Before |
22 | | - public void start() |
23 | | - throws InterruptedException, TTransportException, ConfigurationException, IOException { |
| 24 | + @BeforeClass |
| 25 | + public static void start() throws Exception { |
24 | 26 | EmbeddedCassandraServerHelper.startEmbeddedCassandra(40000L); |
| 27 | + TestUtils.registerOrReplaceGlobalTracer(tracer); |
25 | 28 | } |
26 | 29 |
|
27 | | - @After |
28 | | - public void stop() { |
| 30 | + @AfterClass |
| 31 | + public static void stop() { |
29 | 32 | EmbeddedCassandraServerHelper.cleanEmbeddedCassandra(); |
30 | 33 | } |
31 | 34 |
|
32 | 35 | @Test |
33 | | - public void testNewSessionSync() throws ClassNotFoundException { |
| 36 | + public void testSync() throws ClassNotFoundException { |
34 | 37 | final Cluster cluster = EmbeddedCassandraServerHelper.getCluster(); |
35 | 38 | final Session session = cluster.newSession(); |
36 | 39 | assertThat(session.getClass().getName()).endsWith("contrib.cassandra.TracingSession"); |
| 40 | + final int origSize = writer.getList().size(); |
| 41 | + |
| 42 | + session.execute("DROP KEYSPACE IF EXISTS sync_test"); |
| 43 | + session.execute( |
| 44 | + "CREATE KEYSPACE sync_test WITH REPLICATION = {'class':'SimpleStrategy', 'replication_factor':3}"); |
| 45 | + session.execute("CREATE TABLE sync_test.users ( id UUID PRIMARY KEY, name text )"); |
| 46 | + session.execute("INSERT INTO sync_test.users (id, name) values (uuid(), 'alice')"); |
| 47 | + session.execute("SELECT * FROM sync_test.users where name = 'alice' ALLOW FILTERING"); |
| 48 | + |
| 49 | + assertThat(writer.getList().size()).isEqualTo(origSize + 5); |
| 50 | + DDBaseSpan<?> selectTrace = writer.get(writer.size() - 1).get(0); |
| 51 | + |
| 52 | + assertThat(selectTrace.getServiceName()).isEqualTo(DDTracer.UNASSIGNED_DEFAULT_SERVICE_NAME); |
| 53 | + assertThat(selectTrace.getOperationName()).isEqualTo("execute"); |
| 54 | + assertThat(selectTrace.getResourceName()).isEqualTo("execute"); |
| 55 | + |
| 56 | + assertThat(selectTrace.getTags().get(Tags.COMPONENT.getKey())).isEqualTo("java-cassandra"); |
| 57 | + assertThat(selectTrace.getTags().get(Tags.DB_STATEMENT.getKey())) |
| 58 | + .isEqualTo("SELECT * FROM sync_test.users where name = 'alice' ALLOW FILTERING"); |
| 59 | + assertThat(selectTrace.getTags().get(Tags.DB_TYPE.getKey())).isEqualTo("cassandra"); |
| 60 | + assertThat(selectTrace.getTags().get(Tags.PEER_HOSTNAME.getKey())).isEqualTo("localhost"); |
| 61 | + // More info about IPv4 tag: https://trello.com/c/2el2IwkF/174-mongodb-ot-contrib-provides-a-wrong-peeripv4 |
| 62 | + assertThat(selectTrace.getTags().get(Tags.PEER_HOST_IPV4.getKey())).isEqualTo(2130706433); |
| 63 | + assertThat(selectTrace.getTags().get(Tags.PEER_PORT.getKey())).isEqualTo(9142); |
| 64 | + assertThat(selectTrace.getTags().get(Tags.SPAN_KIND.getKey())).isEqualTo("client"); |
37 | 65 | } |
38 | 66 |
|
39 | 67 | @Test |
40 | | - public void testNewSessionAsync() |
41 | | - throws ClassNotFoundException, ExecutionException, InterruptedException { |
| 68 | + public void testAsync() throws Exception { |
42 | 69 | final Cluster cluster = EmbeddedCassandraServerHelper.getCluster(); |
43 | 70 | final Session session = cluster.connectAsync().get(); |
44 | 71 | assertThat(session.getClass().getName()).endsWith("contrib.cassandra.TracingSession"); |
| 72 | + final int origSize = writer.getList().size(); |
| 73 | + |
| 74 | + session.executeAsync("DROP KEYSPACE IF EXISTS async_test").get(); |
| 75 | + session |
| 76 | + .executeAsync( |
| 77 | + "CREATE KEYSPACE async_test WITH REPLICATION = {'class':'SimpleStrategy', 'replication_factor':3}") |
| 78 | + .get(); |
| 79 | + session.executeAsync("CREATE TABLE async_test.users ( id UUID PRIMARY KEY, name text )").get(); |
| 80 | + session.executeAsync("INSERT INTO async_test.users (id, name) values (uuid(), 'alice')").get(); |
| 81 | + session |
| 82 | + .executeAsync("SELECT * FROM async_test.users where name = 'alice' ALLOW FILTERING") |
| 83 | + .get(); |
| 84 | + |
| 85 | + // traces are finished on another thread, so we have some waiting logic |
| 86 | + for (int timeout = 0; writer.getList().size() < origSize + 5; timeout++) { |
| 87 | + if (timeout >= 10000) { |
| 88 | + Assert.fail("Cassandra async test timeout."); |
| 89 | + } |
| 90 | + Thread.sleep(1); |
| 91 | + } |
| 92 | + DDBaseSpan<?> selectTrace = writer.get(writer.size() - 1).get(0); |
| 93 | + |
| 94 | + assertThat(selectTrace.getServiceName()).isEqualTo(DDTracer.UNASSIGNED_DEFAULT_SERVICE_NAME); |
| 95 | + assertThat(selectTrace.getOperationName()).isEqualTo("execute"); |
| 96 | + assertThat(selectTrace.getResourceName()).isEqualTo("execute"); |
| 97 | + |
| 98 | + assertThat(selectTrace.getTags().get(Tags.COMPONENT.getKey())).isEqualTo("java-cassandra"); |
| 99 | + assertThat(selectTrace.getTags().get(Tags.DB_STATEMENT.getKey())) |
| 100 | + .isEqualTo("SELECT * FROM async_test.users where name = 'alice' ALLOW FILTERING"); |
| 101 | + assertThat(selectTrace.getTags().get(Tags.DB_TYPE.getKey())).isEqualTo("cassandra"); |
| 102 | + assertThat(selectTrace.getTags().get(Tags.PEER_HOSTNAME.getKey())).isEqualTo("localhost"); |
| 103 | + // More info about IPv4 tag: https://trello.com/c/2el2IwkF/174-mongodb-ot-contrib-provides-a-wrong-peeripv4 |
| 104 | + assertThat(selectTrace.getTags().get(Tags.PEER_HOST_IPV4.getKey())).isEqualTo(2130706433); |
| 105 | + assertThat(selectTrace.getTags().get(Tags.PEER_PORT.getKey())).isEqualTo(9142); |
| 106 | + assertThat(selectTrace.getTags().get(Tags.SPAN_KIND.getKey())).isEqualTo("client"); |
45 | 107 | } |
46 | 108 | } |
0 commit comments