Remove TraceTemplate
Users should inject TraceAccessor if they want to interact with the current Span.
This commit is contained in:
@@ -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> {
|
||||
T doInTrace(Span span);
|
||||
}
|
||||
@@ -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> T trace(TraceCallback<T> task);
|
||||
|
||||
}
|
||||
@@ -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> T trace(final TraceCallback<T> callback) {
|
||||
if (this.tracer.isTracing()) {
|
||||
DelegateCallback<T> 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<T> extends TraceDelegate<TraceCallback<T>> {
|
||||
|
||||
public DelegateCallback(Tracer tracer) {
|
||||
super(tracer, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Span startSpan() {
|
||||
return super.startSpan();
|
||||
}
|
||||
|
||||
@Override
|
||||
public TraceCallback<T> getDelegate() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<Span>() {
|
||||
@Override
|
||||
public Span doInTrace(Span span) {
|
||||
return TestSpanContextHolder.getCurrentSpan();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user