Skip to content

Instantly share code, notes, and snippets.

@devinsba
Created November 8, 2016 21:47
Show Gist options
  • Save devinsba/ca0a8bc0f05746e6e900efa6fbe60b85 to your computer and use it in GitHub Desktop.
Save devinsba/ca0a8bc0f05746e6e900efa6fbe60b85 to your computer and use it in GitHub Desktop.
zipkin/brave aspects
public class RpcNameAspect {
@Pointcut("execution(* com.dealer.metrics.aspects.tracing.context.TracingContext+.execute(..)) && this(tracingContext)")
public void tracingContext(TracingContext tracingContext) {
}
@After("tracingContext(tracingContext)")
public void nameRemoteCall(TracingContext tracingContext) throws Throwable {
if (tracingContext.hasTracingMetadata("service_name")) {
tracingContext.setSpanName(String.format(
"%s.%s",
tracingContext.getTracingMetadata("service_name"),
tracingContext.getTracingMetadata("method_name")
));
}
}
}
public class ServletAspect {
@Autowired(required = true)
protected ServerRequestInterceptor serverRequestInterceptor;
@Autowired(required = true)
protected ServerResponseInterceptor serverResponseInterceptor;
@Autowired
private ServerClientAndLocalSpanState braveSpanState;
@Autowired
private SpanNameProvider spanNameProvider;
@Autowired
private AppIdProvider appIdProvider;
private Endpoint endpoint;
@Pointcut("execution(* javax.servlet.http.HttpServlet.doGet(..))")
public void servletGet() {
}
@Pointcut("execution(* javax.servlet.http.HttpServlet.doPost(..))")
public void servletPost() {
}
@Pointcut("execution(* javax.servlet.http.HttpServlet.service(..))")
public void servletService() {
}
@Pointcut("cflow(execution(* com.dealer.metrics.aspects.tracing.context.TracingContext+.execute()))")
public void insideTracingContext() {
}
@Around("(servletGet() || servletPost() || servletService()) && !cflowbelow(servletService()) && !insideTracingContext()")
public Object doServletRequest(final ProceedingJoinPoint pjp) throws Throwable {
final HttpServletRequest req = (HttpServletRequest) pjp.getArgs()[0];
final HttpServletResponse resp = (HttpServletResponse) pjp.getArgs()[1];
TracingContext tracingContext = new TracingContext();
serverRequestInterceptor.handle(new UrlSanitizingHttpServerRequestAdapter(new ServletHttpServerRequest(req), spanNameProvider));
Object result = tracingContext.execute(new TracingContext.Runner() {
@Override
public Object execute() throws Throwable {
return pjp.proceed();
}
});
if (braveSpanState != null && braveSpanState.getCurrentServerSpan() != null && Boolean.TRUE.equals(braveSpanState.getCurrentServerSpan().getSample())) {
braveSpanState.getCurrentServerSpan().getSpan().setName(tracingContext.getSpanName());
for (KeyValueAnnotation annotation : tracingContext.getAnnotations()) {
if (endpoint == null) {
endpoint = Endpoint.create(
appIdProvider.getApplicationName(),
InetAddressUtils.toInt(InetAddressUtils.getLocalHostLANAddress()),
Integer.parseInt(appIdProvider.getPort())
);
}
braveSpanState.getCurrentServerSpan().getSpan()
.addToBinary_annotations(BinaryAnnotation.create(annotation.getKey(), annotation.getValue(), endpoint));
}
}
serverResponseInterceptor.handle(new HttpServerResponseAdapter(new HttpResponse() {
@Override
public int getHttpStatusCode() {
return resp.getStatus();
}
}));
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment