diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/TraceTemplate.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/TraceTemplate.java new file mode 100644 index 000000000..e3d30579b --- /dev/null +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/TraceTemplate.java @@ -0,0 +1,67 @@ +/* + * Copyright 2013-2015 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 org.springframework.cloud.sleuth.instrument.TraceDelegate; + +/** + * @author Spencer Gibb + */ +public class TraceTemplate { + + public interface TraceCallback { + T doInTrace(TraceScope traceScope); + } + + private final Trace trace; + + public TraceTemplate(Trace trace) { + this.trace = trace; + } + + public T trace(final TraceCallback callback) { + DelegateCallback delegate = new DelegateCallback<>(this.trace); + + if (delegate.getParent() != null) { + TraceScope traceScope = delegate.startSpan(); + try { + return callback.doInTrace(traceScope); + } finally { + traceScope.close(); + } + } else { + return callback.doInTrace(null); + } + } + + class DelegateCallback extends TraceDelegate> { + + public DelegateCallback(Trace trace) { + super(trace, null); + } + + @Override + protected TraceScope startSpan() { + return super.startSpan(); + } + + @Override + public TraceCallback getDelegate() { + throw new UnsupportedOperationException(); + } + } +} diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/TraceFilter.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/TraceFilter.java index 1fcab34fb..691ecc68d 100644 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/TraceFilter.java +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/TraceFilter.java @@ -68,7 +68,7 @@ public class TraceFilter extends OncePerRequestFilter implements ApplicationEven + ".TRACE"; public static final Pattern DEFAULT_SKIP_PATTERN = Pattern - .compile("/api-docs.*|/autoconfig|/configprops|/dump|/info|/metrics.*|/mappings|/trace|/swagger.*|.*\\.png|.*\\.css|.*\\.js|.*\\.html|/favicon.ico"); + .compile("/api-docs.*|/autoconfig|/configprops|/dump|/info|/metrics.*|/mappings|/trace|/swagger.*|.*\\.png|.*\\.css|.*\\.js|.*\\.html|/favicon.ico|/hystrix.stream"); private final Trace trace; private final Pattern skipPattern; diff --git a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/web/TraceFilterTests.java b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/web/TraceFilterTests.java index 399b64f5d..85baa7a0a 100644 --- a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/web/TraceFilterTests.java +++ b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/web/TraceFilterTests.java @@ -18,6 +18,7 @@ package org.springframework.cloud.sleuth.instrument.web; import static org.mockito.Matchers.anyObject; import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static org.mockito.MockitoAnnotations.initMocks; @@ -76,6 +77,23 @@ public class TraceFilterTests { .header("User-Agent", "MockMvc"); } + @Test + public void notTraced() throws Exception { + TraceFilter filter = new TraceFilter(trace); + + when(this.trace.startSpan(anyString())).thenReturn(traceScope); + + request = get("/favicon.ico") + .accept(MediaType.ALL) + .buildRequest(new MockServletContext()); + + filter.doFilter(request, response, filterChain); + + verify(this.trace, never()).startSpan(anyString()); + + verify(this.traceScope, never()).close(); + } + @Test public void startsNewTrace() throws Exception { TraceFilter filter = new TraceFilter(trace);