From f59737c238760cc878ca9eab118413655767edfe Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Fri, 5 Feb 2016 10:51:23 +0000 Subject: [PATCH] Remove TraceTemplate Users should inject TraceAccessor if they want to interact with the current Span. --- .../cloud/sleuth/template/TraceCallback.java | 23 ------- .../sleuth/template/TraceOperations.java | 27 -------- .../cloud/sleuth/template/TraceTemplate.java | 65 ------------------- .../sleuth/template/TraceTemplateTests.java | 47 -------------- 4 files changed, 162 deletions(-) delete mode 100644 spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/template/TraceCallback.java delete mode 100644 spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/template/TraceOperations.java delete mode 100644 spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/template/TraceTemplate.java delete mode 100644 spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/template/TraceTemplateTests.java diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/template/TraceCallback.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/template/TraceCallback.java deleted file mode 100644 index bbb4c8876..000000000 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/template/TraceCallback.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright 2015 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.template; - -import org.springframework.cloud.sleuth.Span; - -public interface TraceCallback { - T doInTrace(Span span); -} \ No newline at end of file diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/template/TraceOperations.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/template/TraceOperations.java deleted file mode 100644 index d67e2cfac..000000000 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/template/TraceOperations.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright 2015 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.template; - -/** - * @author Dave Syer - * - */ -public interface TraceOperations { - - T trace(TraceCallback task); - -} diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/template/TraceTemplate.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/template/TraceTemplate.java deleted file mode 100644 index aa6f60876..000000000 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/template/TraceTemplate.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright 2013-2015 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.template; - -import org.springframework.cloud.sleuth.Span; -import org.springframework.cloud.sleuth.Tracer; -import org.springframework.cloud.sleuth.instrument.TraceDelegate; - -/** - * @author Spencer Gibb - */ -public class TraceTemplate implements TraceOperations { - - private final Tracer tracer; - - public TraceTemplate(Tracer tracer) { - this.tracer = tracer; - } - - @Override - public T trace(final TraceCallback callback) { - if (this.tracer.isTracing()) { - DelegateCallback delegate = new DelegateCallback<>(this.tracer); - Span span = delegate.startSpan(); - try { - return callback.doInTrace(span); - } finally { - this.tracer.close(span); - } - } else { - return callback.doInTrace(null); - } - } - - class DelegateCallback extends TraceDelegate> { - - public DelegateCallback(Tracer tracer) { - super(tracer, null); - } - - @Override - protected Span startSpan() { - return super.startSpan(); - } - - @Override - public TraceCallback getDelegate() { - throw new UnsupportedOperationException(); - } - } -} diff --git a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/template/TraceTemplateTests.java b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/template/TraceTemplateTests.java deleted file mode 100644 index b7b8cbee1..000000000 --- a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/template/TraceTemplateTests.java +++ /dev/null @@ -1,47 +0,0 @@ -package org.springframework.cloud.sleuth.template; - -import org.junit.After; -import org.junit.Test; -import org.mockito.Mockito; -import org.springframework.cloud.sleuth.Span; -import org.springframework.cloud.sleuth.Tracer; -import org.springframework.cloud.sleuth.sampler.AlwaysSampler; -import org.springframework.cloud.sleuth.trace.DefaultTracer; -import org.springframework.cloud.sleuth.trace.TestSpanContextHolder; -import org.springframework.context.ApplicationEventPublisher; - -import java.util.Random; - -import static org.assertj.core.api.BDDAssertions.then; - -public class TraceTemplateTests { - - Tracer tracer = new DefaultTracer(new AlwaysSampler(), - new Random(), Mockito.mock(ApplicationEventPublisher.class)); - - @After - public void close() { - TestSpanContextHolder.removeCurrentSpan(); - } - - @Test - public void should_pass_trace_to_the_callback_if_tracing_is_active() { - Span initialSpan = this.tracer.startTrace("test"); - TraceTemplate traceTemplate = new TraceTemplate(this.tracer); - - Span spanFromCallback = whenTraceCallbackReturningCurrentTraceIsExecuted(traceTemplate); - - then(spanFromCallback).isNotNull(); - then(spanFromCallback.getTraceId()).isEqualTo(initialSpan.getTraceId()); - } - - private Span whenTraceCallbackReturningCurrentTraceIsExecuted(TraceTemplate traceTemplate) { - return traceTemplate.trace(new TraceCallback() { - @Override - public Span doInTrace(Span span) { - return TestSpanContextHolder.getCurrentSpan(); - } - }); - } - -} \ No newline at end of file