Add zuul instrumentation
This commit is contained in:
6
pom.xml
6
pom.xml
@@ -27,7 +27,6 @@
|
||||
|
||||
<modules>
|
||||
<module>spring-cloud-sleuth-core</module>
|
||||
<module>spring-cloud-sleuth-zipkin</module>
|
||||
<module>spring-cloud-sleuth-sample</module>
|
||||
<module>docs</module>
|
||||
</modules>
|
||||
@@ -155,6 +154,11 @@
|
||||
<artifactId>hystrix-core</artifactId>
|
||||
<version>1.4.5</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.netflix.zuul</groupId>
|
||||
<artifactId>zuul-core</artifactId>
|
||||
<version>1.0.28</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
<artifactId>aspectjrt</artifactId>
|
||||
|
||||
@@ -47,6 +47,11 @@
|
||||
<artifactId>hystrix-core</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.netflix.zuul</groupId>
|
||||
<artifactId>zuul-core</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.reactivex</groupId>
|
||||
<artifactId>rxjava</artifactId>
|
||||
|
||||
@@ -86,6 +86,7 @@ public class TraceFilter extends OncePerRequestFilter {
|
||||
// Send new span id back
|
||||
addToResponseIfNotPresent(response, SPAN_ID_NAME, span.getSpanId());
|
||||
|
||||
//TODO: trace description?
|
||||
traceScope = trace.startSpan("traceFilter", span);
|
||||
}
|
||||
else {
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package org.springframework.cloud.sleuth.instrument.zuul;
|
||||
|
||||
import org.springframework.cloud.sleuth.Trace;
|
||||
import org.springframework.cloud.sleuth.TraceScope;
|
||||
|
||||
import com.netflix.zuul.ZuulFilter;
|
||||
import com.netflix.zuul.context.RequestContext;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
public class TracePostFilter extends ZuulFilter {
|
||||
|
||||
private Trace trace;
|
||||
|
||||
public TracePostFilter(Trace trace) {
|
||||
this.trace = trace;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String filterType() {
|
||||
return "post";
|
||||
}
|
||||
|
||||
@Override
|
||||
public int filterOrder() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldFilter() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object run() {
|
||||
TraceScope traceScope = (TraceScope) RequestContext.getCurrentContext().get(
|
||||
"traceScope");
|
||||
|
||||
if (traceScope != null) {
|
||||
traceScope.close();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package org.springframework.cloud.sleuth.instrument.zuul;
|
||||
|
||||
import static org.springframework.cloud.sleuth.Trace.SPAN_ID_NAME;
|
||||
import static org.springframework.cloud.sleuth.Trace.TRACE_ID_NAME;
|
||||
import static org.springframework.util.StringUtils.hasText;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.springframework.cloud.sleuth.MilliSpan;
|
||||
import org.springframework.cloud.sleuth.Span;
|
||||
import org.springframework.cloud.sleuth.Trace;
|
||||
import org.springframework.cloud.sleuth.TraceScope;
|
||||
|
||||
import com.netflix.zuul.ZuulFilter;
|
||||
import com.netflix.zuul.context.RequestContext;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
public class TracePreFilter extends ZuulFilter {
|
||||
|
||||
private Trace trace;
|
||||
|
||||
public TracePreFilter(Trace trace) {
|
||||
this.trace = trace;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String filterType() {
|
||||
return "pre";
|
||||
}
|
||||
|
||||
@Override
|
||||
public int filterOrder() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldFilter() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object run() {
|
||||
RequestContext context = RequestContext.getCurrentContext();
|
||||
HttpServletRequest request = context.getRequest();
|
||||
|
||||
String spanId = request.getHeader(SPAN_ID_NAME);
|
||||
String traceId = request.getHeader(TRACE_ID_NAME);
|
||||
TraceScope traceScope = null;
|
||||
if (hasText(spanId) && hasText(traceId)) {
|
||||
|
||||
Span span = MilliSpan.builder().traceId(traceId)
|
||||
.parents(Collections.singletonList(spanId))
|
||||
// TODO: use parent() when lombok plugin supports it
|
||||
.build();
|
||||
|
||||
// TODO: trace description?
|
||||
traceScope = trace.startSpan("traceZuulFilter", span);
|
||||
}
|
||||
else {
|
||||
traceScope = trace.startSpan("traceZuulFilter");
|
||||
|
||||
}
|
||||
|
||||
context.set("traceScope", traceScope);
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package org.springframework.cloud.sleuth.instrument.zuul;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.cloud.sleuth.Trace;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import com.netflix.zuul.ZuulFilter;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass(ZuulFilter.class)
|
||||
@ConditionalOnBean(Trace.class)
|
||||
public class TraceZuulAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
public TracePreFilter tracePreFilter(Trace trace) {
|
||||
return new TracePreFilter(trace);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public TracePostFilter tracePostFilter(Trace trace) {
|
||||
return new TracePostFilter(trace);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user