Skip to content

Commit e24017b

Browse files
authored
Avoid calling exporter if no traces to send (#224)
`CompositeSpanExporter` is calling the exporter even if there are no traces to send. Avoid calling the exporter when the traces list is empty to be more efficient and avoid issues in exporter implementations that do not expect an empty list.
1 parent 4775b24 commit e24017b

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

micrometer-tracing-bridges/micrometer-tracing-bridge-otel/src/main/java/io/micrometer/tracing/otel/bridge/CompositeSpanExporter.java

+3
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ public CompletableResultCode export(Collection<SpanData> spans) {
7070
}
7171
return OtelFinishedSpan.toOtel(finishedSpan);
7272
}).collect(Collectors.toList());
73+
if (changedSpanData.isEmpty()) {
74+
return CompletableResultCode.ofSuccess();
75+
}
7376
List<CompletableResultCode> results = new ArrayList<>();
7477
changedSpanData.forEach(spanData -> {
7578
this.reporters.forEach(reporter -> {

micrometer-tracing-bridges/micrometer-tracing-bridge-otel/src/test/java/io/micrometer/tracing/otel/bridge/CompositeSpanExporterTests.java

+21
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,27 @@ void should_shutdown_all_exporters() {
9191
BDDAssertions.then(resultCode.isSuccess()).isTrue();
9292
}
9393

94+
@Test
95+
void should_not_call_exporter_if_no_spans() {
96+
SpanExporter exporter = mock(SpanExporter.class);
97+
given(exporter.export(BDDMockito.any())).willReturn(CompletableResultCode.ofSuccess());
98+
SpanExportingPredicate predicate = span -> span.getName().equals("foo");
99+
SpanFilter filter = span -> span.setName("baz");
100+
SpanReporter reporter = mock(SpanReporter.class);
101+
102+
SpanData barSpan = new CustomSpanData("bar");
103+
104+
CompletableResultCode resultCode = new CompositeSpanExporter(Collections.singleton(exporter),
105+
Collections.singletonList(predicate), Collections.singletonList(reporter),
106+
Collections.singletonList(filter))
107+
.export(Collections.singletonList(barSpan));
108+
109+
then(reporter).shouldHaveNoInteractions();
110+
111+
then(exporter).shouldHaveNoInteractions();
112+
BDDAssertions.then(resultCode.isSuccess()).isTrue();
113+
}
114+
94115
static class CustomSpanData implements SpanData {
95116

96117
private String name;

0 commit comments

Comments
 (0)