Skip to content

Commit

Permalink
Added SpanFilter
Browse files Browse the repository at this point in the history
without this change we cannot filter out spans to be sent to Zipkin
with this change we give users a hook to achieve that

fixes gh-1033
  • Loading branch information
marcingrzejszczak committed Jul 25, 2018
1 parent 32cacb6 commit dde4b04
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 6 deletions.
16 changes: 16 additions & 0 deletions docs/src/main/asciidoc/spring-cloud-sleuth.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,22 @@ object, you will have to create a bean of `zipkin2.reporter.Sender` type.
}
----

== Filtering out spans to report

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:

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

== 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
@@ -0,0 +1,37 @@
/*
* Copyright 2013-2018 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
*
* http://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 org.springframework.cloud.sleuth;

import zipkin2.Span;

/**
* Adds ability to filter out spans that shouldn't be reported
*
* <b>IMPORTANT</b> - DO NOT apply filtering to child spans of an RPC
* call.
*
* @author Marcin Grzejszczak
* @since 2.1.0
*/
public interface SpanFilter {

/**
* @param span - span to be reported
* @return - {@code true} if span is to be reported, {@code false} otherwise
*/
boolean accept(Span span);
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@
import brave.propagation.ExtraFieldPropagation;
import brave.propagation.Propagation;
import brave.sampler.Sampler;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.sleuth.DefaultSpanNamer;
import org.springframework.cloud.sleuth.SpanAdjuster;
import org.springframework.cloud.sleuth.SpanFilter;
import org.springframework.cloud.sleuth.SpanNamer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -55,6 +58,8 @@
@EnableConfigurationProperties(SleuthProperties.class)
public class TraceAutoConfiguration {

private static final Log log = LogFactory.getLog(TraceAutoConfiguration.class);

public static final String TRACER_BEAN_NAME = "tracer";

@Autowired(required = false) List<SpanAdjuster> spanAdjusters = new ArrayList<>();
Expand All @@ -68,27 +73,34 @@ Tracing tracing(@Value("${spring.zipkin.service.name:${spring.application.name:d
Reporter<zipkin2.Span> reporter,
Sampler sampler,
ErrorParser errorParser,
SleuthProperties sleuthProperties
SleuthProperties sleuthProperties,
SpanFilter spanFilter
) {
return Tracing.newBuilder()
.sampler(sampler)
.errorParser(errorParser)
.localServiceName(serviceName)
.propagationFactory(factory)
.currentTraceContext(currentTraceContext)
.spanReporter(adjustedReporter(reporter))
.spanReporter(adjustedReporter(reporter, spanFilter))
.traceId128Bit(sleuthProperties.isTraceId128())
.supportsJoin(sleuthProperties.isSupportsJoin())
.build();
}

private Reporter<zipkin2.Span> adjustedReporter(Reporter<zipkin2.Span> delegate) {
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);
}
delegate.report(spanToAdjust);
if (spanFilter.accept(spanToAdjust)) {
delegate.report(spanToAdjust);
} else {
if (log.isDebugEnabled()) {
log.debug("Span [" + spanToAdjust + "] filtered and will not be reported");
}
}
};
}

Expand All @@ -109,6 +121,11 @@ Sampler sleuthTraceSampler() {
return new DefaultSpanNamer();
}

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

@Bean
@ConditionalOnMissingBean
Propagation.Factory sleuthPropagation(SleuthProperties sleuthProperties) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementServerProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.sleuth.SpanFilter;
import org.springframework.cloud.sleuth.instrument.DefaultTestAutoConfiguration;
import org.springframework.cloud.sleuth.util.ArrayListSpanReporter;
import org.springframework.cloud.sleuth.util.SpanUtil;
Expand Down Expand Up @@ -81,12 +82,14 @@ public class TraceFilterIntegrationTests extends AbstractMvcIntegrationTest {
@Autowired MyFilter myFilter;
@Autowired ArrayListSpanReporter reporter;
@Autowired Tracer tracer;
@Autowired MySpanFilter mySpanFilter;

private static Span span;

@Before
@After
public void clearSpans() {
public void clear() {
this.mySpanFilter.enableAll = true;
this.reporter.clear();
}

Expand Down Expand Up @@ -224,6 +227,18 @@ public void should_return_custom_response_headers_when_custom_trace_filter_gets_
then(this.reporter.getSpans().get(0).tags()).containsEntry("custom", "tag");
}

@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.wasCalled = false;

whenSentPongWithTraceId(expectedTraceId);

then(this.mySpanFilter.wasCalled).isTrue();
then(this.reporter.getSpans()).isEmpty();
}

@Override
protected void configureMockMvcBuilder(DefaultMockMvcBuilder mockMvcBuilder) {
mockMvcBuilder.addFilters(this.traceFilter, this.myFilter);
Expand All @@ -239,6 +254,10 @@ private MvcResult whenSentPingWithTraceId(Long passedTraceId) throws Exception {
return sendPingWithTraceId(TRACE_ID_NAME, passedTraceId);
}

private MvcResult whenSentPongWithTraceId(Long passedTraceId) throws Exception {
return sendPingWithTraceId(TRACE_ID_NAME, passedTraceId);
}

private MvcResult whenSentInfoWithTraceId(Long passedTraceId) throws Exception {
return sendRequestWithTraceId("/additionalContextPath/info", TRACE_ID_NAME,
passedTraceId);
Expand All @@ -265,6 +284,11 @@ private MvcResult sendPingWithTraceId(String headerName, Long traceId)
return sendRequestWithTraceId("/ping", headerName, traceId);
}

private MvcResult sendPongWithTraceId(String headerName, Long traceId)
throws Exception {
return sendRequestWithTraceId("/pong", headerName, traceId);
}

private MvcResult sendDeferredWithTraceId(String headerName, Long traceId)
throws Exception {
return sendRequestWithTraceId("/deferred", headerName, traceId);
Expand Down Expand Up @@ -319,6 +343,13 @@ public String ping() {
return "ping";
}

@RequestMapping("/pong")
public String pong() {
logger.info("ping");
span = this.tracer.currentSpan();
return "pong";
}

@RequestMapping("/throwsException")
public void throwsException() {
throw new RuntimeException();
Expand Down Expand Up @@ -352,6 +383,10 @@ ManagementServerProperties managementServerProperties() {
}
}

@Bean SpanFilter spanFilter() {
return new MySpanFilter();
}

@Bean
public ArrayListSpanReporter testSpanReporter() {
return new ArrayListSpanReporter();
Expand Down Expand Up @@ -396,4 +431,15 @@ class MyFilter extends GenericFilterBean {
chain.doFilter(request, response);
}
}
//end::response_headers[]
//end::response_headers[]

class MySpanFilter implements SpanFilter {

boolean enableAll = true;
boolean wasCalled = false;

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

0 comments on commit dde4b04

Please sign in to comment.