Skip to content
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

Add support of tracing list variable as parameter #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/main/java/io/opentracing/contrib/neo4j/TracingHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -66,7 +67,15 @@ static String mapToString(Map<String, Object> 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(", "));
}

Expand Down
64 changes: 55 additions & 9 deletions src/test/java/io/opentracing/contrib/neo4j/RxTracingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@
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.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import org.junit.After;
Expand Down Expand Up @@ -206,16 +210,58 @@ public void testRunInTransactionRxWithFailure() {
assertNull(tracer.activeSpan());
}

private void validateSpans(List<MockSpan> 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<ResultSummary> resultSummaries = Flux.usingWhen(Mono.fromSupplier(driver::rxSession),
session -> session.readTransaction(tx -> {
List<Integer> 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()));
}
), RxSession::close);

resultSummaries.as(StepVerifier::create)
.expectNextCount(1)
.verifyComplete();

await().atMost(15, TimeUnit.SECONDS).until(reportedSpansSize(), equalTo(2));

List<MockSpan> spans = tracer.finishedSpans();
assertEquals(2, spans.size());
validateSpansWithParameter(spans, "list->[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]", "runRx", "readTransactionRx");

assertNull(tracer.activeSpan());
}

private void validateSpansWithParameter(List<MockSpan> 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<MockSpan> 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<Integer> reportedSpansSize() {
return () -> tracer.finishedSpans().size();
Expand Down