Skip to content

Added a TestSpanReporter #574

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

Merged
merged 1 commit into from
Feb 8, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@
*/
package io.micrometer.tracing.brave.bridge;

import java.util.Collections;
import java.util.List;

import brave.handler.MutableSpan;
import brave.handler.SpanHandler;
import brave.propagation.TraceContext;
import io.micrometer.common.lang.Nullable;
import io.micrometer.tracing.exporter.FinishedSpan;
import io.micrometer.tracing.exporter.SpanExportingPredicate;
import io.micrometer.tracing.exporter.SpanFilter;
import io.micrometer.tracing.exporter.SpanReporter;

import java.util.Collections;
import java.util.List;

/**
* Wraps the {@link SpanHandler} with additional predicate, reporting and filtering logic.
*
Expand All @@ -46,8 +47,8 @@ public class CompositeSpanHandler extends SpanHandler {
* @param reporters reporters that export spans
* @param spanFilters filters that mutate spans before reporting them
*/
public CompositeSpanHandler(List<SpanExportingPredicate> predicates, List<SpanReporter> reporters,
List<SpanFilter> spanFilters) {
public CompositeSpanHandler(@Nullable List<SpanExportingPredicate> predicates,
@Nullable List<SpanReporter> reporters, @Nullable List<SpanFilter> spanFilters) {
this.filters = predicates == null ? Collections.emptyList() : predicates;
this.reporters = reporters == null ? Collections.emptyList() : reporters;
this.spanFilters = spanFilters == null ? Collections.emptyList() : spanFilters;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* Copyright 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micrometer.tracing.brave.bridge;

import brave.handler.MutableSpan;
import brave.handler.SpanHandler.Cause;
import brave.internal.codec.HexCodec;
import brave.propagation.TraceContext;
import io.micrometer.tracing.exporter.TestSpanReporter;
import org.assertj.core.api.BDDAssertions;
import org.junit.jupiter.api.Test;

import java.util.Collections;

class CompositeSpanHandlerTests {

@Test
void should_store_spans_through_test_span_reporter() {
TestSpanReporter testSpanReporter = new TestSpanReporter();
TraceContext traceContext = TraceContext.newBuilder().traceId(id()).spanId(id()).build();
MutableSpan mutableSpan = new MutableSpan(traceContext, null);
mutableSpan.name("bar");

boolean success = new CompositeSpanHandler(null, Collections.singletonList(testSpanReporter), null)
.end(traceContext, mutableSpan, Cause.FINISHED);

BDDAssertions.then(success).isTrue();
BDDAssertions.then(testSpanReporter.spans()).hasSize(1);
BDDAssertions.then(testSpanReporter.poll().getName()).isEqualTo("bar");
}

private static long id() {
return HexCodec.lowerHexToUnsignedLong("ff000000000000000000000000000041");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.micrometer.tracing.otel.bridge;

import io.micrometer.common.lang.Nullable;
import io.micrometer.tracing.exporter.FinishedSpan;
import io.micrometer.tracing.exporter.SpanExportingPredicate;
import io.micrometer.tracing.exporter.SpanFilter;
Expand Down Expand Up @@ -53,9 +54,10 @@ public class CompositeSpanExporter implements io.opentelemetry.sdk.trace.export.
* @param reporters reporters that export spans
* @param spanFilters filters that mutate spans before reporting them
*/
public CompositeSpanExporter(Collection<io.opentelemetry.sdk.trace.export.SpanExporter> exporters,
List<SpanExportingPredicate> predicates, List<SpanReporter> reporters, List<SpanFilter> spanFilters) {
this.exporters = exporters;
public CompositeSpanExporter(@Nullable Collection<io.opentelemetry.sdk.trace.export.SpanExporter> exporters,
@Nullable List<SpanExportingPredicate> predicates, @Nullable List<SpanReporter> reporters,
@Nullable List<SpanFilter> spanFilters) {
this.exporters = exporters == null ? Collections.emptyList() : exporters;
this.predicates = predicates == null ? Collections.emptyList() : predicates;
this.reporters = reporters == null ? Collections.emptyList() : reporters;
this.spanFilters = spanFilters == null ? Collections.emptyList() : spanFilters;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.micrometer.tracing.exporter.SpanExportingPredicate;
import io.micrometer.tracing.exporter.SpanFilter;
import io.micrometer.tracing.exporter.SpanReporter;
import io.micrometer.tracing.exporter.TestSpanReporter;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.api.trace.SpanKind;
Expand Down Expand Up @@ -112,6 +113,20 @@ void should_not_call_exporter_if_no_spans() {
BDDAssertions.then(resultCode.isSuccess()).isTrue();
}

@Test
void should_store_spans_through_test_span_reporter() {
TestSpanReporter testSpanReporter = new TestSpanReporter();
SpanData barSpan = new CustomSpanData("bar");

CompletableResultCode resultCode = new CompositeSpanExporter(null, null,
Collections.singletonList(testSpanReporter), null)
.export(Collections.singletonList(barSpan));

BDDAssertions.then(resultCode.isSuccess()).isTrue();
BDDAssertions.then(testSpanReporter.spans()).hasSize(1);
BDDAssertions.then(testSpanReporter.poll().getName()).isEqualTo("bar");
}

static class CustomSpanData implements SpanData {

private String name;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* Copyright 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micrometer.tracing.exporter;

import io.micrometer.common.lang.Nullable;

import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
* {@link SpanReporter} with access to stored {@link FinishedSpan}.
*
* @author Marcin Grzejszczak
* @since 1.3.0
*/
public class TestSpanReporter implements SpanReporter {

private final Queue<FinishedSpan> spans = new ConcurrentLinkedQueue<>();

/**
* Polls stored spans for the latest entry.
* @return latest stored span
*/
@Nullable
public FinishedSpan poll() {
return this.spans.poll();
}

/**
* Returns collected spans.
* @return collected spans
*/
public Queue<FinishedSpan> spans() {
return new ConcurrentLinkedQueue<>(this.spans);
}

@Override
public void report(FinishedSpan span) {
this.spans.add(span);
}

@Override
public void close() throws Exception {
this.spans.clear();
}

}