Don't trace /hystrix.stream

This commit is contained in:
Spencer Gibb
2015-10-19 15:48:14 -06:00
parent 2259fc6ca9
commit d58a3241f2
3 changed files with 86 additions and 1 deletions

View File

@@ -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> {
T doInTrace(TraceScope traceScope);
}
private final Trace trace;
public TraceTemplate(Trace trace) {
this.trace = trace;
}
public <T> T trace(final TraceCallback<T> callback) {
DelegateCallback<T> 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<T> extends TraceDelegate<TraceCallback<T>> {
public DelegateCallback(Trace trace) {
super(trace, null);
}
@Override
protected TraceScope startSpan() {
return super.startSpan();
}
@Override
public TraceCallback<T> getDelegate() {
throw new UnsupportedOperationException();
}
}
}

View File

@@ -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;

View File

@@ -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);