From 03dddeeb19e28c9846ed2222be864f73f034b3ae Mon Sep 17 00:00:00 2001 From: Salvatore Furnari Date: Tue, 23 Jan 2024 12:35:05 +0100 Subject: [PATCH 1/2] Add management of list parameters --- pom.xml | 4 +- .../contrib/neo4j/TracingHelper.java | 11 +++- .../contrib/neo4j/RxTracingTest.java | 58 ++++++++++++++++--- 3 files changed, 61 insertions(+), 12 deletions(-) diff --git a/pom.xml b/pom.xml index 4920318..8a1f866 100644 --- a/pom.xml +++ b/pom.xml @@ -156,8 +156,8 @@ maven-compiler-plugin 3.8.0 - ${java.version} - ${java.version} + 9 + 9 diff --git a/src/main/java/io/opentracing/contrib/neo4j/TracingHelper.java b/src/main/java/io/opentracing/contrib/neo4j/TracingHelper.java index 14ebd3d..8992adc 100644 --- a/src/main/java/io/opentracing/contrib/neo4j/TracingHelper.java +++ b/src/main/java/io/opentracing/contrib/neo4j/TracingHelper.java @@ -19,6 +19,7 @@ import io.opentracing.Tracer.SpanBuilder; import io.opentracing.tag.Tags; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.concurrent.CompletionStage; import java.util.function.Supplier; @@ -66,7 +67,15 @@ static String mapToString(Map map) { } return map.entrySet() .stream() - .map(entry -> entry.getKey() + " -> " + entry.getValue()) + .map(entry -> { + Object value = entry.getValue(); + if (!(value instanceof List)) { + return entry.getKey() + "->" + value; + } + return entry.getKey() + "->" + ((List) value).stream() + .map(Object::toString) + .collect(Collectors.joining(", ", "[", "]")); + }) .collect(Collectors.joining(", ")); } diff --git a/src/test/java/io/opentracing/contrib/neo4j/RxTracingTest.java b/src/test/java/io/opentracing/contrib/neo4j/RxTracingTest.java index 1898572..dc3aeeb 100644 --- a/src/test/java/io/opentracing/contrib/neo4j/RxTracingTest.java +++ b/src/test/java/io/opentracing/contrib/neo4j/RxTracingTest.java @@ -20,11 +20,13 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import static org.neo4j.driver.Values.parameters; import io.opentracing.mock.MockSpan; import io.opentracing.mock.MockTracer; import io.opentracing.tag.Tags; import java.util.List; +import java.util.Objects; import java.util.concurrent.Callable; import java.util.concurrent.TimeUnit; import org.junit.After; @@ -206,16 +208,54 @@ public void testRunInTransactionRxWithFailure() { assertNull(tracer.activeSpan()); } - private void validateSpans(List spans, String... spanNames) { - for (int i = 0; i < spans.size(); i++) { - MockSpan span = spans.get(i); - assertEquals(spanNames[i], span.operationName()); - assertEquals(span.tags().get(Tags.SPAN_KIND.getKey()), Tags.SPAN_KIND_CLIENT); - assertEquals(TracingHelper.COMPONENT_NAME, span.tags().get(Tags.COMPONENT.getKey())); - assertEquals(TracingHelper.DB_TYPE, span.tags().get(Tags.DB_TYPE.getKey())); - assertEquals(0, span.generatedErrors().size()); + @Test + public void testReadTransactionRxUsingListParameter() { + String query = "UNWIND $list AS x RETURN x"; + Flux resultSummaries = Flux.usingWhen(Mono.fromSupplier(driver::rxSession), + session -> session.readTransaction(tx -> { + RxResult result = tx.run(query, parameters("list", List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))); + return Flux.from(result.records()) + .doOnNext(record -> System.out.println(record.get(0).asInt())) + .then(Mono.from(result.consume())); + } + ), RxSession::close); + + resultSummaries.as(StepVerifier::create) + .expectNextCount(1) + .verifyComplete(); + + await().atMost(15, TimeUnit.SECONDS).until(reportedSpansSize(), equalTo(2)); + + List spans = tracer.finishedSpans(); + assertEquals(2, spans.size()); + validateSpansWithParameter(spans, "list->[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]", "runRx", "readTransactionRx"); + + assertNull(tracer.activeSpan()); + } + + private void validateSpansWithParameter(List spans, String parameters, String... spanNames) { + for (int i = 0; i < spans.size(); i++) { + MockSpan span = spans.get(i); + assertEquals(spanNames[i], span.operationName()); + assertEquals(span.tags().get(Tags.SPAN_KIND.getKey()), Tags.SPAN_KIND_CLIENT); + assertEquals(TracingHelper.COMPONENT_NAME, span.tags().get(Tags.COMPONENT.getKey())); + assertEquals(TracingHelper.DB_TYPE, span.tags().get(Tags.DB_TYPE.getKey())); + if (Objects.equals(span.operationName(), "runRx")) + assertEquals(parameters, span.tags().get("parameters")); + assertEquals(0, span.generatedErrors().size()); + } + } + + private void validateSpans(List spans, String... spanNames) { + for (int i = 0; i < spans.size(); i++) { + MockSpan span = spans.get(i); + assertEquals(spanNames[i], span.operationName()); + assertEquals(span.tags().get(Tags.SPAN_KIND.getKey()), Tags.SPAN_KIND_CLIENT); + assertEquals(TracingHelper.COMPONENT_NAME, span.tags().get(Tags.COMPONENT.getKey())); + assertEquals(TracingHelper.DB_TYPE, span.tags().get(Tags.DB_TYPE.getKey())); + assertEquals(0, span.generatedErrors().size()); + } } - } private Callable reportedSpansSize() { return () -> tracer.finishedSpans().size(); From 99ee0e3be8873fb0ddb4d3356ae6e21addbd96ee Mon Sep 17 00:00:00 2001 From: Salvatore Furnari Date: Tue, 23 Jan 2024 12:50:50 +0100 Subject: [PATCH 2/2] Add management of list parameters --- pom.xml | 4 ++-- .../io/opentracing/contrib/neo4j/RxTracingTest.java | 10 ++++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/pom.xml b/pom.xml index 8a1f866..4920318 100644 --- a/pom.xml +++ b/pom.xml @@ -156,8 +156,8 @@ maven-compiler-plugin 3.8.0 - 9 - 9 + ${java.version} + ${java.version} diff --git a/src/test/java/io/opentracing/contrib/neo4j/RxTracingTest.java b/src/test/java/io/opentracing/contrib/neo4j/RxTracingTest.java index dc3aeeb..866ba84 100644 --- a/src/test/java/io/opentracing/contrib/neo4j/RxTracingTest.java +++ b/src/test/java/io/opentracing/contrib/neo4j/RxTracingTest.java @@ -25,6 +25,8 @@ import io.opentracing.mock.MockSpan; import io.opentracing.mock.MockTracer; import io.opentracing.tag.Tags; + +import java.util.LinkedList; import java.util.List; import java.util.Objects; import java.util.concurrent.Callable; @@ -213,7 +215,11 @@ public void testReadTransactionRxUsingListParameter() { String query = "UNWIND $list AS x RETURN x"; Flux resultSummaries = Flux.usingWhen(Mono.fromSupplier(driver::rxSession), session -> session.readTransaction(tx -> { - RxResult result = tx.run(query, parameters("list", List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))); + List list = new LinkedList<>(); + for (int i = 0; i < 10; i++) { + list.add(i); + } + RxResult result = tx.run(query, parameters("list", list)); return Flux.from(result.records()) .doOnNext(record -> System.out.println(record.get(0).asInt())) .then(Mono.from(result.consume())); @@ -228,7 +234,7 @@ public void testReadTransactionRxUsingListParameter() { List spans = tracer.finishedSpans(); assertEquals(2, spans.size()); - validateSpansWithParameter(spans, "list->[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]", "runRx", "readTransactionRx"); + validateSpansWithParameter(spans, "list->[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]", "runRx", "readTransactionRx"); assertNull(tracer.activeSpan()); }