Skip to content

Commit 5f1fc9e

Browse files
committed
Add Cassandra batch current behavior tests
1 parent 8e25e07 commit 5f1fc9e

2 files changed

Lines changed: 382 additions & 146 deletions

File tree

  • instrumentation/cassandra

instrumentation/cassandra/cassandra-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/cassandra/v3_0/CassandraClientTest.java

Lines changed: 191 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import java.time.Duration;
4444
import java.util.concurrent.ExecutorService;
4545
import java.util.concurrent.Executors;
46+
import java.util.function.Function;
4647
import java.util.stream.Stream;
4748
import org.junit.jupiter.api.BeforeAll;
4849
import org.junit.jupiter.api.Test;
@@ -295,34 +296,27 @@ void testMetrics() {
295296
SERVER_PORT);
296297
}
297298

298-
@Test
299-
void batchStatementWithSameQuery() {
299+
@ParameterizedTest
300+
@MethodSource("batchScenarios")
301+
void batchStatement(BatchScenario scenario) {
300302
Session session = cluster.connect();
301303
cleanup.deferCleanup(session);
302304

303-
session.execute("DROP KEYSPACE IF EXISTS batch_same_test");
305+
session.execute("DROP KEYSPACE IF EXISTS batch_test");
304306
session.execute(
305-
"CREATE KEYSPACE batch_same_test WITH REPLICATION = {'class':'SimpleStrategy', 'replication_factor':1}");
306-
session.execute("CREATE TABLE batch_same_test.users ( name text PRIMARY KEY, age int )");
307-
PreparedStatement preparedStatement =
308-
session.prepare("INSERT INTO batch_same_test.users (name, age) values (?, ?)");
307+
"CREATE KEYSPACE batch_test WITH REPLICATION = {'class':'SimpleStrategy', 'replication_factor':1}");
308+
session.execute("CREATE TABLE batch_test.records ( id int PRIMARY KEY, num int )");
309309
testing.waitForTraces(3);
310310
testing.clearData();
311311

312-
BatchStatement batchStatement =
313-
new BatchStatement()
314-
.add(preparedStatement.bind("alice", 1))
315-
.add(preparedStatement.bind("bob", 2));
316-
session.execute(batchStatement);
312+
session.execute(scenario.buildBatch.apply(session));
317313

318314
testing.waitAndAssertTraces(
319315
trace ->
320316
trace.hasSpansSatisfyingExactly(
321317
span ->
322318
span.hasName(
323-
emitStableDatabaseSemconv()
324-
? "BATCH INSERT batch_same_test.users"
325-
: "DB Query")
319+
emitStableDatabaseSemconv() ? scenario.spanName : scenario.oldSpanName)
326320
.hasKind(SpanKind.CLIENT)
327321
.hasNoParent()
328322
.hasAttributesSatisfyingExactly(
@@ -335,62 +329,89 @@ void batchStatementWithSameQuery() {
335329
equalTo(
336330
maybeStable(DB_STATEMENT),
337331
emitStableDatabaseSemconv()
338-
? "INSERT INTO batch_same_test.users (name, age) values (?, ?)"
339-
: null),
332+
? scenario.queryText
333+
: scenario.oldStatement),
340334
equalTo(
341-
DB_OPERATION_BATCH_SIZE, emitStableDatabaseSemconv() ? 2L : null),
335+
DB_OPERATION_BATCH_SIZE,
336+
emitStableDatabaseSemconv() ? scenario.batchSize : null),
342337
equalTo(
343338
DB_QUERY_SUMMARY,
344-
emitStableDatabaseSemconv()
345-
? "BATCH INSERT batch_same_test.users"
346-
: null))));
347-
}
348-
349-
@Test
350-
void batchStatementWithDifferentQueries() {
351-
Session session = cluster.connect();
352-
cleanup.deferCleanup(session);
353-
354-
session.execute("DROP KEYSPACE IF EXISTS batch_mixed_test");
355-
session.execute(
356-
"CREATE KEYSPACE batch_mixed_test WITH REPLICATION = {'class':'SimpleStrategy', 'replication_factor':1}");
357-
session.execute("CREATE TABLE batch_mixed_test.users ( name text PRIMARY KEY, age int )");
358-
PreparedStatement insertStatement =
359-
session.prepare("INSERT INTO batch_mixed_test.users (name, age) values ('alice', ?)");
360-
testing.waitForTraces(3);
361-
testing.clearData();
362-
363-
BatchStatement batchStatement =
364-
new BatchStatement()
365-
.add(insertStatement.bind(1))
366-
.add(
367-
new SimpleStatement(
368-
"UPDATE batch_mixed_test.users SET age = 2 WHERE name = 'alice'"));
369-
session.execute(batchStatement);
370-
371-
testing.waitAndAssertTraces(
372-
trace ->
373-
trace.hasSpansSatisfyingExactly(
374-
span ->
375-
span.hasName(emitStableDatabaseSemconv() ? "BATCH" : "DB Query")
376-
.hasKind(SpanKind.CLIENT)
377-
.hasNoParent()
378-
.hasAttributesSatisfyingExactly(
379-
equalTo(NETWORK_TYPE, emitStableDatabaseSemconv() ? null : "ipv4"),
380-
equalTo(SERVER_ADDRESS, cassandraHost),
381-
equalTo(SERVER_PORT, cassandraPort),
382-
equalTo(NETWORK_PEER_ADDRESS, cassandraIp),
383-
equalTo(NETWORK_PEER_PORT, cassandraPort),
384-
equalTo(maybeStable(DB_SYSTEM), CASSANDRA),
339+
emitStableDatabaseSemconv() ? scenario.querySummary : null),
385340
equalTo(
386-
maybeStable(DB_STATEMENT),
341+
maybeStable(DB_OPERATION),
387342
emitStableDatabaseSemconv()
388-
? "INSERT INTO batch_mixed_test.users (name, age) values ('alice', ?); UPDATE batch_mixed_test.users SET age = ? WHERE name = ?"
389-
: null),
390-
equalTo(
391-
DB_OPERATION_BATCH_SIZE, emitStableDatabaseSemconv() ? 2L : null),
343+
? scenario.operationName
344+
: scenario.oldOperation),
392345
equalTo(
393-
DB_QUERY_SUMMARY, emitStableDatabaseSemconv() ? "BATCH" : null))));
346+
maybeStable(DB_CASSANDRA_TABLE),
347+
emitStableDatabaseSemconv()
348+
? scenario.collectionName
349+
: scenario.oldTable))));
350+
}
351+
352+
private static Stream<Arguments> batchScenarios() {
353+
return Stream.of(
354+
Arguments.argumentSet(
355+
"empty",
356+
BatchScenario.builder()
357+
.buildBatch(session -> new BatchStatement())
358+
.spanName("cassandra")
359+
.oldSpanName("DB Query")
360+
.build()),
361+
Arguments.argumentSet(
362+
"single",
363+
BatchScenario.builder()
364+
.buildBatch(
365+
session -> {
366+
PreparedStatement insert =
367+
session.prepare("INSERT INTO batch_test.records (id, num) values (?, ?)");
368+
return new BatchStatement().add(insert.bind(1, 1));
369+
})
370+
.spanName("INSERT batch_test.records")
371+
.oldSpanName("INSERT batch_test.records")
372+
.queryText("INSERT INTO batch_test.records (id, num) values (?, ?)")
373+
.oldStatement("INSERT INTO batch_test.records (id, num) values (?, ?)")
374+
.querySummary("INSERT batch_test.records")
375+
.operationName("INSERT")
376+
.oldOperation("INSERT")
377+
.collectionName("batch_test.records")
378+
.oldTable("batch_test.records")
379+
.build()),
380+
Arguments.argumentSet(
381+
"twoSameOperation",
382+
BatchScenario.builder()
383+
.buildBatch(
384+
session -> {
385+
PreparedStatement insert =
386+
session.prepare("INSERT INTO batch_test.records (id, num) values (?, ?)");
387+
return new BatchStatement().add(insert.bind(1, 1)).add(insert.bind(2, 2));
388+
})
389+
.spanName("BATCH INSERT batch_test.records")
390+
.oldSpanName("DB Query")
391+
.queryText("INSERT INTO batch_test.records (id, num) values (?, ?)")
392+
.querySummary("BATCH INSERT batch_test.records")
393+
.batchSize(2)
394+
.build()),
395+
Arguments.argumentSet(
396+
"twoDifferentOperations",
397+
BatchScenario.builder()
398+
.buildBatch(
399+
session -> {
400+
PreparedStatement insert =
401+
session.prepare("INSERT INTO batch_test.records (id, num) values (4, ?)");
402+
return new BatchStatement()
403+
.add(insert.bind(4))
404+
.add(
405+
new SimpleStatement(
406+
"UPDATE batch_test.records SET num = 5 WHERE id = 4"));
407+
})
408+
.spanName("BATCH")
409+
.oldSpanName("DB Query")
410+
.queryText(
411+
"INSERT INTO batch_test.records (id, num) values (4, ?); UPDATE batch_test.records SET num = ? WHERE id = ?")
412+
.querySummary("BATCH")
413+
.batchSize(2)
414+
.build()));
394415
}
395416

396417
private static Stream<Arguments> provideSyncParameters() {
@@ -524,4 +545,109 @@ private static class Parameter {
524545
this.table = table;
525546
}
526547
}
548+
549+
private static final class BatchScenario {
550+
final Function<Session, BatchStatement> buildBatch;
551+
final String spanName;
552+
final String oldSpanName;
553+
final String queryText;
554+
final String oldStatement;
555+
final String querySummary;
556+
final Long batchSize;
557+
final String operationName;
558+
final String oldOperation;
559+
final String collectionName;
560+
final String oldTable;
561+
562+
BatchScenario(Builder builder) {
563+
this.buildBatch = builder.buildBatch;
564+
this.spanName = builder.spanName;
565+
this.oldSpanName = builder.oldSpanName;
566+
this.queryText = builder.queryText;
567+
this.oldStatement = builder.oldStatement;
568+
this.querySummary = builder.querySummary;
569+
this.batchSize = builder.batchSize;
570+
this.operationName = builder.operationName;
571+
this.oldOperation = builder.oldOperation;
572+
this.collectionName = builder.collectionName;
573+
this.oldTable = builder.oldTable;
574+
}
575+
576+
static Builder builder() {
577+
return new Builder();
578+
}
579+
580+
static final class Builder {
581+
private Function<Session, BatchStatement> buildBatch;
582+
private String spanName;
583+
private String oldSpanName;
584+
private String queryText;
585+
private String oldStatement;
586+
private String querySummary;
587+
private Long batchSize;
588+
private String operationName;
589+
private String oldOperation;
590+
private String collectionName;
591+
private String oldTable;
592+
593+
Builder buildBatch(Function<Session, BatchStatement> buildBatch) {
594+
this.buildBatch = buildBatch;
595+
return this;
596+
}
597+
598+
Builder spanName(String spanName) {
599+
this.spanName = spanName;
600+
return this;
601+
}
602+
603+
Builder oldSpanName(String oldSpanName) {
604+
this.oldSpanName = oldSpanName;
605+
return this;
606+
}
607+
608+
Builder queryText(String queryText) {
609+
this.queryText = queryText;
610+
return this;
611+
}
612+
613+
Builder oldStatement(String oldStatement) {
614+
this.oldStatement = oldStatement;
615+
return this;
616+
}
617+
618+
Builder querySummary(String querySummary) {
619+
this.querySummary = querySummary;
620+
return this;
621+
}
622+
623+
Builder batchSize(long batchSize) {
624+
this.batchSize = batchSize;
625+
return this;
626+
}
627+
628+
Builder operationName(String operationName) {
629+
this.operationName = operationName;
630+
return this;
631+
}
632+
633+
Builder oldOperation(String oldOperation) {
634+
this.oldOperation = oldOperation;
635+
return this;
636+
}
637+
638+
Builder collectionName(String collectionName) {
639+
this.collectionName = collectionName;
640+
return this;
641+
}
642+
643+
Builder oldTable(String oldTable) {
644+
this.oldTable = oldTable;
645+
return this;
646+
}
647+
648+
BatchScenario build() {
649+
return new BatchScenario(this);
650+
}
651+
}
652+
}
527653
}

0 commit comments

Comments
 (0)