Skip to content

Commit 79153fd

Browse files
committed
Add Cassandra batch current behavior tests
1 parent 8e25e07 commit 79153fd

2 files changed

Lines changed: 341 additions & 149 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: 170 additions & 66 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,83 @@ 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),
387-
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),
341+
maybeStable(DB_OPERATION),
342+
scenario.operationName),
392343
equalTo(
393-
DB_QUERY_SUMMARY, emitStableDatabaseSemconv() ? "BATCH" : null))));
344+
maybeStable(DB_CASSANDRA_TABLE),
345+
scenario.collectionName))));
346+
}
347+
348+
private static Stream<Arguments> batchScenarios() {
349+
return Stream.of(
350+
Arguments.argumentSet(
351+
"empty",
352+
BatchScenario.builder()
353+
.buildBatch(session -> new BatchStatement())
354+
.spanName("cassandra")
355+
.oldSpanName("DB Query")
356+
.build()),
357+
Arguments.argumentSet(
358+
"single",
359+
BatchScenario.builder()
360+
.buildBatch(
361+
session -> {
362+
PreparedStatement insert =
363+
session.prepare("INSERT INTO batch_test.records (id, num) values (?, ?)");
364+
return new BatchStatement().add(insert.bind(1, 1));
365+
})
366+
.spanName("INSERT batch_test.records")
367+
.oldSpanName("INSERT batch_test.records")
368+
.queryText("INSERT INTO batch_test.records (id, num) values (?, ?)")
369+
.oldStatement("INSERT INTO batch_test.records (id, num) values (?, ?)")
370+
.querySummary("INSERT batch_test.records")
371+
.operationName("INSERT")
372+
.collectionName("batch_test.records")
373+
.build()),
374+
Arguments.argumentSet(
375+
"twoSameOperation",
376+
BatchScenario.builder()
377+
.buildBatch(
378+
session -> {
379+
PreparedStatement insert =
380+
session.prepare("INSERT INTO batch_test.records (id, num) values (?, ?)");
381+
return new BatchStatement().add(insert.bind(1, 1)).add(insert.bind(2, 2));
382+
})
383+
.spanName("BATCH INSERT batch_test.records")
384+
.oldSpanName("DB Query")
385+
.queryText("INSERT INTO batch_test.records (id, num) values (?, ?)")
386+
.querySummary("BATCH INSERT batch_test.records")
387+
.batchSize(2)
388+
.build()),
389+
Arguments.argumentSet(
390+
"twoDifferentOperations",
391+
BatchScenario.builder()
392+
.buildBatch(
393+
session -> {
394+
PreparedStatement insert =
395+
session.prepare("INSERT INTO batch_test.records (id, num) values (4, ?)");
396+
return new BatchStatement()
397+
.add(insert.bind(4))
398+
.add(
399+
new SimpleStatement(
400+
"UPDATE batch_test.records SET num = 5 WHERE id = 4"));
401+
})
402+
.spanName("BATCH")
403+
.oldSpanName("DB Query")
404+
.queryText(
405+
"INSERT INTO batch_test.records (id, num) values (4, ?); UPDATE batch_test.records SET num = ? WHERE id = ?")
406+
.querySummary("BATCH")
407+
.batchSize(2)
408+
.build()));
394409
}
395410

396411
private static Stream<Arguments> provideSyncParameters() {
@@ -524,4 +539,93 @@ private static class Parameter {
524539
this.table = table;
525540
}
526541
}
542+
543+
private static final class BatchScenario {
544+
final Function<Session, BatchStatement> buildBatch;
545+
final String spanName;
546+
final String oldSpanName;
547+
final String queryText;
548+
final String oldStatement;
549+
final String querySummary;
550+
final Long batchSize;
551+
final String operationName;
552+
final String collectionName;
553+
554+
BatchScenario(Builder builder) {
555+
this.buildBatch = builder.buildBatch;
556+
this.spanName = builder.spanName;
557+
this.oldSpanName = builder.oldSpanName;
558+
this.queryText = builder.queryText;
559+
this.oldStatement = builder.oldStatement;
560+
this.querySummary = builder.querySummary;
561+
this.batchSize = builder.batchSize;
562+
this.operationName = builder.operationName;
563+
this.collectionName = builder.collectionName;
564+
}
565+
566+
static Builder builder() {
567+
return new Builder();
568+
}
569+
570+
static final class Builder {
571+
private Function<Session, BatchStatement> buildBatch;
572+
private String spanName;
573+
private String oldSpanName;
574+
private String queryText;
575+
private String oldStatement;
576+
private String querySummary;
577+
private Long batchSize;
578+
private String operationName;
579+
private String collectionName;
580+
581+
Builder buildBatch(Function<Session, BatchStatement> buildBatch) {
582+
this.buildBatch = buildBatch;
583+
return this;
584+
}
585+
586+
Builder spanName(String spanName) {
587+
this.spanName = spanName;
588+
return this;
589+
}
590+
591+
Builder oldSpanName(String oldSpanName) {
592+
this.oldSpanName = oldSpanName;
593+
return this;
594+
}
595+
596+
Builder queryText(String queryText) {
597+
this.queryText = queryText;
598+
return this;
599+
}
600+
601+
Builder oldStatement(String oldStatement) {
602+
this.oldStatement = oldStatement;
603+
return this;
604+
}
605+
606+
Builder querySummary(String querySummary) {
607+
this.querySummary = querySummary;
608+
return this;
609+
}
610+
611+
Builder batchSize(long batchSize) {
612+
this.batchSize = batchSize;
613+
return this;
614+
}
615+
616+
Builder operationName(String operationName) {
617+
this.operationName = operationName;
618+
return this;
619+
}
620+
621+
Builder collectionName(String collectionName) {
622+
this.collectionName = collectionName;
623+
return this;
624+
}
625+
626+
BatchScenario build() {
627+
return new BatchScenario(this);
628+
}
629+
}
630+
}
527631
}

0 commit comments

Comments
 (0)