Skip to content

Commit

Permalink
Changed accept to reject and added a list of filters
Browse files Browse the repository at this point in the history
  • Loading branch information
marcingrzejszczak committed Jul 25, 2018
1 parent dde4b04 commit ad6b996
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 17 deletions.
7 changes: 5 additions & 2 deletions docs/src/main/asciidoc/spring-cloud-sleuth.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -951,18 +951,21 @@ object, you will have to create a bean of `zipkin2.reporter.Sender` type.

There are cases when you might not want to report a given span e.g. to Zipkin.
In order to filter out spans, you should implement the `SpanFilter` interface
which returns `true` if a span should be reported and `false` otherwise. Example:
which returns `true` if a span should be NOT be reported and `false` otherwise. Example:

[source,java]
----
@Bean SpanFilter mySpanFilter() {
return (span) -> {
// true - if we want span to be reported
// true - if we want span NOT to be reported
// false - otherwise
};
}
----

You can have multiple `SpanFilter`s and the whole list of them will be applied
against the span that is to be reported.

== Zipkin Stream Span Consumer

IMPORTANT: We recommend using Zipkin's native support for message-based span sending.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public interface SpanFilter {

/**
* @param span - span to be reported
* @return - {@code true} if span is to be reported, {@code false} otherwise
* @return - {@code true} if span is to be rejected, {@code false} otherwise
*/
boolean accept(Span span);
boolean reject(Span span);
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public class TraceAutoConfiguration {
public static final String TRACER_BEAN_NAME = "tracer";

@Autowired(required = false) List<SpanAdjuster> spanAdjusters = new ArrayList<>();
@Autowired(required = false) List<SpanFilter> spanFilters = new ArrayList<>();

@Bean
@ConditionalOnMissingBean
Expand Down Expand Up @@ -90,20 +91,26 @@ Tracing tracing(@Value("${spring.zipkin.service.name:${spring.application.name:d

private Reporter<zipkin2.Span> adjustedReporter(Reporter<zipkin2.Span> delegate, SpanFilter spanFilter) {
return span -> {
Span spanToAdjust = span;
for (SpanAdjuster spanAdjuster : this.spanAdjusters) {
spanToAdjust = spanAdjuster.adjust(spanToAdjust);
}
if (spanFilter.accept(spanToAdjust)) {
delegate.report(spanToAdjust);
} else {
Span spanToAdjust = spanToAdjust(span);
if (spanFilters.stream()
.anyMatch(filter -> filter.reject(spanToAdjust))) {
if (log.isDebugEnabled()) {
log.debug("Span [" + spanToAdjust + "] filtered and will not be reported");
}
} else {
delegate.report(spanToAdjust);
}
};
}

private Span spanToAdjust(Span span) {
Span spanToAdjust = span;
for (SpanAdjuster spanAdjuster : this.spanAdjusters) {
spanToAdjust = spanAdjuster.adjust(spanToAdjust);
}
return spanToAdjust;
}

@Bean(name = TRACER_BEAN_NAME)
@ConditionalOnMissingBean
Tracer tracer(Tracing tracing) {
Expand All @@ -123,7 +130,7 @@ Sampler sleuthTraceSampler() {

@Bean
@ConditionalOnMissingBean SpanFilter noOpSpanFilter() {
return (span) -> true;
return (span) -> false;
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public class TraceFilterIntegrationTests extends AbstractMvcIntegrationTest {
@Before
@After
public void clear() {
this.mySpanFilter.enableAll = true;
this.mySpanFilter.disableAll = false;
this.reporter.clear();
}

Expand Down Expand Up @@ -230,7 +230,7 @@ public void should_return_custom_response_headers_when_custom_trace_filter_gets_
@Test
public void should_filter_out_a_span_and_not_report_it() throws Exception {
Long expectedTraceId = new Random().nextLong();
this.mySpanFilter.enableAll = false;
this.mySpanFilter.disableAll = true;
this.mySpanFilter.wasCalled = false;

whenSentPongWithTraceId(expectedTraceId);
Expand Down Expand Up @@ -435,11 +435,11 @@ class MyFilter extends GenericFilterBean {

class MySpanFilter implements SpanFilter {

boolean enableAll = true;
boolean disableAll = false;
boolean wasCalled = false;

@Override public boolean accept(zipkin2.Span span) {
@Override public boolean reject(zipkin2.Span span) {
this.wasCalled = true;
return this.enableAll;
return this.disableAll;
}
}

0 comments on commit ad6b996

Please sign in to comment.