From 7a4dcf6f055686645eccc132b400cc3e2005e6a1 Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Fri, 24 Jun 2016 01:50:37 +0200 Subject: [PATCH] Fixed the aspect for controllers --- .../sleuth/instrument/web/TraceWebAspect.java | 27 ++++--------- .../cloud/sleuth/util/SpanNameUtil.java | 40 +++++++++++++++++++ .../cloud/sleuth/util/SpanNameUtilTests.java | 29 ++++++++++++++ 3 files changed, 77 insertions(+), 19 deletions(-) create mode 100644 spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/util/SpanNameUtil.java create mode 100644 spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/util/SpanNameUtilTests.java diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/TraceWebAspect.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/TraceWebAspect.java index 9e7569c6e..4a33f04a0 100644 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/TraceWebAspect.java +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/TraceWebAspect.java @@ -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 { diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/util/SpanNameUtil.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/util/SpanNameUtil.java new file mode 100644 index 000000000..cc563298b --- /dev/null +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/util/SpanNameUtil.java @@ -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(); + } +} diff --git a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/util/SpanNameUtilTests.java b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/util/SpanNameUtilTests.java new file mode 100644 index 000000000..35ba6c3f7 --- /dev/null +++ b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/util/SpanNameUtilTests.java @@ -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"); + } +} \ No newline at end of file