Fixed issues with aspects

This commit is contained in:
Marcin Grzejszczak
2016-06-24 02:04:40 +02:00
6 changed files with 83 additions and 22 deletions

View File

@@ -22,6 +22,7 @@ import org.aspectj.lang.annotation.Aspect;
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.cloud.sleuth.TraceKeys;
import org.springframework.cloud.sleuth.util.SpanNameUtil;
/**
* Aspect that creates a new Span for running threads executing methods annotated with
@@ -47,7 +48,8 @@ public class TraceAsyncAspect {
@Around("execution (@org.springframework.scheduling.annotation.Async * *.*(..))")
public Object traceBackgroundThread(final ProceedingJoinPoint pjp) throws Throwable {
Span span = this.tracer.createSpan(pjp.getSignature().getName());
Span span = this.tracer.createSpan(
SpanNameUtil.toLowerHyphen(pjp.getSignature().getName()));
this.tracer.addTag(Span.SPAN_LOCAL_COMPONENT_TAG_NAME, ASYNC_COMPONENT);
this.tracer.addTag(this.traceKeys.getAsync().getPrefix() +
this.traceKeys.getAsync().getClassNameKey(), pjp.getTarget().getClass().getSimpleName());

View File

@@ -24,6 +24,7 @@ import org.aspectj.lang.annotation.Aspect;
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.TraceKeys;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.cloud.sleuth.util.SpanNameUtil;
/**
* Aspect that creates a new Span for running threads executing methods annotated with
@@ -60,7 +61,7 @@ public class TraceSchedulingAspect {
if (this.skipPattern.matcher(pjp.getTarget().getClass().getName()).matches()) {
return pjp.proceed();
}
String spanName = pjp.getSignature().getName();
String spanName = SpanNameUtil.toLowerHyphen(pjp.getSignature().getName());
Span span = this.tracer.createSpan(spanName);
this.tracer.addTag(Span.SPAN_LOCAL_COMPONENT_TAG_NAME, SCHEDULED_COMPONENT);
this.tracer.addTag(this.traceKeys.getAsync().getPrefix() +

View File

@@ -28,6 +28,7 @@ import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.SpanNamer;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.cloud.sleuth.instrument.async.TraceContinuingCallable;
import org.springframework.cloud.sleuth.util.SpanNameUtil;
import org.springframework.web.context.request.async.WebAsyncTask;
/**
@@ -84,8 +85,8 @@ public class TraceWebAspect {
@Pointcut("@within(org.springframework.stereotype.Controller)")
private void anyControllerAnnotated() { } // NOSONAR
@Pointcut("execution(public * *(..))")
private void anyPublicMethod() { } // NOSONAR
@Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
private void anyRequestMappingAnnotatedMethod() { } // NOSONAR
@Pointcut("execution(public java.util.concurrent.Callable *(..))")
private void anyPublicMethodReturningCallable() { } // NOSONAR
@@ -99,33 +100,21 @@ public class TraceWebAspect {
@Pointcut("(anyRestControllerAnnotated() || anyControllerAnnotated()) && anyPublicMethodReturningWebAsyncTask()")
private void anyControllerOrRestControllerWithPublicWebAsyncTaskMethod() { } // NOSONAR
@Around("(anyRestControllerAnnotated() || anyControllerAnnotated()) && anyPublicMethod()")
@Around("(anyRestControllerAnnotated() || anyControllerAnnotated()) && anyRequestMappingAnnotatedMethod()")
@SuppressWarnings("unchecked")
public Object wrapControllerMethodWithCorrelationId(ProceedingJoinPoint pjp) throws Throwable {
String spanName = toLowerHyphen(pjp.getSignature().getName());
String spanName = SpanNameUtil.toLowerHyphen(pjp.getSignature().getName());
Span span = this.tracer.createSpan(spanName);
log.debug("Wrapping controller method [" + spanName + "] in a span " + span);
if (log.isDebugEnabled()) {
log.debug("Wrapping controller method [" + spanName + "] in a span " + span);
}
try {
//Thread.sleep(0, 1);
return pjp.proceed();
} finally {
this.tracer.close(span);
}
}
static String toLowerHyphen(String name) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < name.length(); i++) {
char c = name.charAt(i);
if (c >= 'A' && c <= 'Z') {
result.append('-').append((char) (c + 'a' - 'A'));
} else {
result.append(c);
}
}
return result.toString();
}
@Around("anyControllerOrRestControllerWithPublicAsyncMethod()")
@SuppressWarnings("unchecked")
public Object wrapWithCorrelationId(ProceedingJoinPoint pjp) throws Throwable {

View File

@@ -0,0 +1,40 @@
/*
* Copyright 2013-2016 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.util;
/**
* Utility class that provides the name in hyphen based notation
*
* @author Adrian Cole
*
* @since 1.0.2
*/
public final class SpanNameUtil {
public static String toLowerHyphen(String name) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < name.length(); i++) {
char c = name.charAt(i);
if (c >= 'A' && c <= 'Z') {
result.append('-').append((char) (c + 'a' - 'A'));
} else {
result.append(c);
}
}
return result.toString();
}
}

View File

@@ -54,7 +54,7 @@ public class TraceAsyncIntegrationTests {
public void run() {
then(TraceAsyncIntegrationTests.this.classPerformingAsyncLogic.getSpan())
.hasTraceIdEqualTo(span.getTraceId())
.hasNameEqualTo("invokeAsynchronousLogic")
.hasNameEqualTo("invoke-asynchronous-logic")
.isALocalComponentSpan()
.hasATag("class", "ClassPerformingAsyncLogic")
.hasATag("method", "invokeAsynchronousLogic");

View File

@@ -0,0 +1,29 @@
/*
* Copyright 2013-2016 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.util;
import org.junit.Test;
import org.springframework.cloud.sleuth.assertions.SleuthAssertions;
public class SpanNameUtilTests {
@Test
public void should_convert_a_name_in_hyphen_based_notation() throws Exception {
SleuthAssertions.then(SpanNameUtil.toLowerHyphen("aMethodNameInCamelCaseNotation"))
.isEqualTo("a-method-name-in-camel-case-notation");
}
}