diff options
author | savaki <matt.ho@gmail.com> | 2018-07-13 23:32:27 -0700 |
---|---|---|
committer | savaki <matt.ho@gmail.com> | 2018-07-21 07:44:40 -0700 |
commit | a65edc8093b7b31fb03e26836b8c70f87e988732 (patch) | |
tree | 7822ccf4657042e90a3f787e688f1a4a4d3f9353 | |
parent | 3fc8941cf3748cfad98bcb5264ca2a2547a7892c (diff) | |
download | opencensus-java-a65edc8093b7b31fb03e26836b8c70f87e988732.tar.gz |
fixed checkstyle problems
-rw-r--r-- | buildscripts/import-control.xml | 7 | ||||
-rw-r--r-- | contrib/spring/src/main/java/io/opencensus/contrib/spring/aop/CensusSpringAspect.java | 31 | ||||
-rw-r--r-- | contrib/spring/src/main/java/io/opencensus/contrib/spring/aop/CensusSpringSqlAspect.java (renamed from contrib/spring/src/main/java/io/opencensus/contrib/spring/aop/CensusSpringSQLAspect.java) | 36 | ||||
-rw-r--r-- | contrib/spring/src/main/java/io/opencensus/contrib/spring/aop/Handler.java | 26 | ||||
-rw-r--r-- | contrib/spring/src/main/java/io/opencensus/contrib/spring/aop/Trace.java | 26 | ||||
-rw-r--r-- | contrib/spring/src/test/java/io/opencensus/contrib/spring/aop/CensusSpringInterceptorTest.java | 52 | ||||
-rw-r--r-- | contrib/spring/src/test/java/io/opencensus/contrib/spring/aop/Sample.java | 30 | ||||
-rw-r--r-- | contrib/spring/src/test/resources/spring.xml | 2 |
8 files changed, 155 insertions, 55 deletions
diff --git a/buildscripts/import-control.xml b/buildscripts/import-control.xml index 607f626c..031bc52b 100644 --- a/buildscripts/import-control.xml +++ b/buildscripts/import-control.xml @@ -103,6 +103,13 @@ General guidelines on imports: <allow pkg="com.google.cloud"/> <allow pkg="io.opencensus.trace"/> </subpackage> + <subpackage name="spring"> + <allow pkg="io.opencensus.trace"/> + <allow pkg="org.aspectj.lang"/> + <allow pkg="org.aspectj.lang.annotation"/> + <allow pkg="org.aspectj.lang.reflect"/> + <allow pkg="org.springframework.beans.factory.annotation"/> + </subpackage> <subpackage name="zpages"> <allow pkg="com.sun.net.httpserver"/> <allow pkg="io.opencensus.contrib.grpc.metrics"/> diff --git a/contrib/spring/src/main/java/io/opencensus/contrib/spring/aop/CensusSpringAspect.java b/contrib/spring/src/main/java/io/opencensus/contrib/spring/aop/CensusSpringAspect.java index 7baaa590..238e306a 100644 --- a/contrib/spring/src/main/java/io/opencensus/contrib/spring/aop/CensusSpringAspect.java +++ b/contrib/spring/src/main/java/io/opencensus/contrib/spring/aop/CensusSpringAspect.java @@ -1,21 +1,42 @@ +/* + * Copyright 2018, OpenCensus 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 io.opencensus.contrib.spring.aop; import io.opencensus.trace.SpanBuilder; import io.opencensus.trace.Tracing; +import java.lang.reflect.Method; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.beans.factory.annotation.Configurable; -import java.lang.reflect.Method; - -/** - * CensusSpringAspect handles logic for the @Trace annotation - */ +/** CensusSpringAspect handles logic for the @Trace annotation. */ @Aspect @Configurable public class CensusSpringAspect { + /** + * trace handles methods executed with the @Trace annotation. A new span will be created with an + * optionally customizable span name. + * + * @param call the join point to execute + * @return the result of the invocation + * @throws Throwable if the underlying target throws an exception + */ @Around("@annotation(Trace)") public Object trace(ProceedingJoinPoint call) throws Throwable { MethodSignature signature = (MethodSignature) call.getSignature(); diff --git a/contrib/spring/src/main/java/io/opencensus/contrib/spring/aop/CensusSpringSQLAspect.java b/contrib/spring/src/main/java/io/opencensus/contrib/spring/aop/CensusSpringSqlAspect.java index d13172d7..c59ae8f9 100644 --- a/contrib/spring/src/main/java/io/opencensus/contrib/spring/aop/CensusSpringSQLAspect.java +++ b/contrib/spring/src/main/java/io/opencensus/contrib/spring/aop/CensusSpringSqlAspect.java @@ -1,3 +1,19 @@ +/* + * Copyright 2018, OpenCensus 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 io.opencensus.contrib.spring.aop; import io.opencensus.trace.SpanBuilder; @@ -10,12 +26,18 @@ import org.aspectj.lang.annotation.Pointcut; import org.springframework.beans.factory.annotation.Configurable; /** + * CensusSpringSqlAspect captures span from all SQL invocations that utilize + * java.sql.Statement.execute* */ @Aspect @Configurable -public class CensusSpringSQLAspect { +public class CensusSpringSqlAspect { private static final Tracer tracer = Tracing.getTracer(); + /** + * trace handles invocations of java.sql.Statement.execute*. A new span will be created whose name + * is (execute|executeQuery|executeQuery)-(hash of sql). + */ @Around("execute() || testing()") public Object trace(ProceedingJoinPoint call) throws Throwable { if (call.getArgs().length == 0 || call.getArgs()[0] == null) { @@ -30,16 +52,14 @@ public class CensusSpringSQLAspect { } /** - * execute creates spans around all invocations of Statement.execute*. The raw SQL - * will be stored in an annotation associated with the Span + * execute creates spans around all invocations of Statement.execute*. The raw SQL will be stored + * in an annotation associated with the Span */ @Pointcut("execution(public !void java.sql.Statement.execute*(java.lang.String))") - protected void execute() { - } + protected void execute() {} - @Pointcut("execution(public void io.opencensus.contrib.spring.aop.Sample.execute*(java.lang.String))") - protected void testing() { - } + @Pointcut("execution(public void Sample.execute*(java.lang.String))") + protected void testing() {} private static String makeSpanName(ProceedingJoinPoint call, String sql) { String hash = Integer.toHexString(hashCode(sql.toCharArray())); diff --git a/contrib/spring/src/main/java/io/opencensus/contrib/spring/aop/Handler.java b/contrib/spring/src/main/java/io/opencensus/contrib/spring/aop/Handler.java index 2bb622f3..dabd47aa 100644 --- a/contrib/spring/src/main/java/io/opencensus/contrib/spring/aop/Handler.java +++ b/contrib/spring/src/main/java/io/opencensus/contrib/spring/aop/Handler.java @@ -1,3 +1,19 @@ +/* + * Copyright 2018, OpenCensus 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 io.opencensus.contrib.spring.aop; import io.opencensus.common.Scope; @@ -7,16 +23,14 @@ import io.opencensus.trace.Tracer; import io.opencensus.trace.Tracing; import org.aspectj.lang.ProceedingJoinPoint; -/** - * Handler defines common logic for wrapping a span around the specified JoinPoint. - */ +/** Handler defines common logic for wrapping a span around the specified JoinPoint. */ final class Handler { private static final Tracer tracer = Tracing.getTracer(); - private Handler() { - } + private Handler() {} - static Object proceed(ProceedingJoinPoint call, SpanBuilder builder, String... annotations) throws Throwable { + static Object proceed(ProceedingJoinPoint call, SpanBuilder builder, String... annotations) + throws Throwable { try (Scope scope = builder.startScopedSpan()) { for (String annotation : annotations) { diff --git a/contrib/spring/src/main/java/io/opencensus/contrib/spring/aop/Trace.java b/contrib/spring/src/main/java/io/opencensus/contrib/spring/aop/Trace.java index e022c576..60f7c16c 100644 --- a/contrib/spring/src/main/java/io/opencensus/contrib/spring/aop/Trace.java +++ b/contrib/spring/src/main/java/io/opencensus/contrib/spring/aop/Trace.java @@ -1,3 +1,19 @@ +/* + * Copyright 2018, OpenCensus 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 io.opencensus.contrib.spring.aop; import java.lang.annotation.ElementType; @@ -8,17 +24,15 @@ import java.lang.annotation.Target; /** * Trace specifies the annotated method should be included in the Trace. * - * <p> - * By default, the name of the method will be used for the span name. However, the - * span name can be explicitly set via the name interface. - * </p> + * <p>By default, the name of the method will be used for the span name. However, the span name can + * be explicitly set via the name interface. */ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface Trace { /** - * @return the optional custom span name; if not specified the method name will be - * used as the span name + * @return the optional custom span name; if not specified the method name will be used as the + * span name */ String name() default ""; } diff --git a/contrib/spring/src/test/java/io/opencensus/contrib/spring/aop/CensusSpringInterceptorTest.java b/contrib/spring/src/test/java/io/opencensus/contrib/spring/aop/CensusSpringInterceptorTest.java index 0513c942..9f248c87 100644 --- a/contrib/spring/src/test/java/io/opencensus/contrib/spring/aop/CensusSpringInterceptorTest.java +++ b/contrib/spring/src/test/java/io/opencensus/contrib/spring/aop/CensusSpringInterceptorTest.java @@ -1,5 +1,23 @@ +/* + * Copyright 2018, OpenCensus 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 io.opencensus.contrib.spring.aop; +import static com.google.common.truth.Truth.assertThat; + import io.opencensus.testing.export.TestHandler; import io.opencensus.trace.Annotation; import io.opencensus.trace.Tracing; @@ -7,6 +25,7 @@ import io.opencensus.trace.config.TraceParams; import io.opencensus.trace.export.SpanData; import io.opencensus.trace.export.SpanExporter; import io.opencensus.trace.samplers.Samplers; +import java.util.List; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -15,12 +34,6 @@ import org.junit.runners.JUnit4; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; -import java.util.List; - -import static com.google.common.truth.Truth.assertThat; - -/** - */ @RunWith(JUnit4.class) public class CensusSpringInterceptorTest { ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); @@ -34,12 +47,12 @@ public class CensusSpringInterceptorTest { SpanExporter exporter = Tracing.getExportComponent().getSpanExporter(); exporter.registerHandler("testing", handler); - TraceParams params = Tracing - .getTraceConfig() - .getActiveTraceParams() - .toBuilder() - .setSampler(Samplers.alwaysSample()) - .build(); + TraceParams params = + Tracing.getTraceConfig() + .getActiveTraceParams() + .toBuilder() + .setSampler(Samplers.alwaysSample()) + .build(); Tracing.getTraceConfig().updateActiveTraceParams(params); } @@ -76,7 +89,7 @@ public class CensusSpringInterceptorTest { } @Test - public void testSQLExecute() throws Exception { + public void testSqlExecute() throws Exception { // When String sql = "select 1"; Sample sample = (Sample) context.getBean("sample"); @@ -94,7 +107,7 @@ public class CensusSpringInterceptorTest { } @Test - public void testSQLQuery() throws Exception { + public void testSqlQuery() throws Exception { // When String sql = "select 2"; Sample sample = (Sample) context.getBean("sample"); @@ -104,15 +117,16 @@ public class CensusSpringInterceptorTest { List<SpanData> data = handler.waitForExport(1); assertThat(data).isNotNull(); assertThat(data.size()).isEqualTo(1); - assertThat(data.get(0).getName()).isEqualTo("executeQuery-4705ea0e"); // sql-{hash of sql statement} + assertThat(data.get(0).getName()).isEqualTo("executeQuery-4705ea0e"); - List<SpanData.TimedEvent<Annotation>> events = data.get(0).getAnnotations().getEvents(); + SpanData.TimedEvents<Annotation> annotations = data.get(0).getAnnotations(); + List<SpanData.TimedEvent<Annotation>> events = annotations.getEvents(); assertThat(events.size()).isEqualTo(1); assertThat(events.get(0).getEvent().getDescription()).isEqualTo(sql); } @Test - public void testSQLUpdate() throws Exception { + public void testSqlUpdate() throws Exception { // When String sql = "update content set value = 1"; Sample sample = (Sample) context.getBean("sample"); @@ -122,10 +136,10 @@ public class CensusSpringInterceptorTest { List<SpanData> data = handler.waitForExport(1); assertThat(data).isNotNull(); assertThat(data.size()).isEqualTo(1); - assertThat(data.get(0).getName()).isEqualTo("executeUpdate-acaeb423"); // sql-{hash of sql statement} + assertThat(data.get(0).getName()).isEqualTo("executeUpdate-acaeb423"); List<SpanData.TimedEvent<Annotation>> events = data.get(0).getAnnotations().getEvents(); assertThat(events.size()).isEqualTo(1); assertThat(events.get(0).getEvent().getDescription()).isEqualTo(sql); } -}
\ No newline at end of file +} diff --git a/contrib/spring/src/test/java/io/opencensus/contrib/spring/aop/Sample.java b/contrib/spring/src/test/java/io/opencensus/contrib/spring/aop/Sample.java index 7c1e1594..073911d5 100644 --- a/contrib/spring/src/test/java/io/opencensus/contrib/spring/aop/Sample.java +++ b/contrib/spring/src/test/java/io/opencensus/contrib/spring/aop/Sample.java @@ -1,9 +1,23 @@ +/* + * Copyright 2018, OpenCensus 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 io.opencensus.contrib.spring.aop; import java.sql.SQLException; -/** - */ public class Sample { @Trace() void example1() { @@ -25,15 +39,11 @@ public class Sample { Thread.sleep(delay); } - public void execute(String sql) throws SQLException { - } + public void execute(String sql) throws SQLException {} - public void executeQuery(String sql) throws SQLException { - } + public void executeQuery(String sql) throws SQLException {} - public void executeUpdate(String sql) throws SQLException { - } + public void executeUpdate(String sql) throws SQLException {} - public void executeLargeUpdate(String sql) throws SQLException { - } + public void executeLargeUpdate(String sql) throws SQLException {} } diff --git a/contrib/spring/src/test/resources/spring.xml b/contrib/spring/src/test/resources/spring.xml index 0405202a..930dade4 100644 --- a/contrib/spring/src/test/resources/spring.xml +++ b/contrib/spring/src/test/resources/spring.xml @@ -12,5 +12,5 @@ <bean id="censusAspect" class="io.opencensus.contrib.spring.aop.CensusSpringAspect"/> <!-- traces all SQL calls --> - <bean id="censusSQLAspect" class="io.opencensus.contrib.spring.aop.CensusSpringSQLAspect"/> + <bean id="censusSQLAspect" class="io.opencensus.contrib.spring.aop.CensusSpringSqlAspect"/> </beans>
\ No newline at end of file |