Use listeners instead of custom callbacks

This commit is contained in:
Dave Syer
2015-07-28 17:40:27 +01:00
parent cba63d0dec
commit 97ae6151e2
8 changed files with 42 additions and 64 deletions

View File

@@ -1,8 +1,6 @@
package org.springframework.cloud.sleuth;
import java.io.Closeable;
import java.util.ArrayList;
import java.util.List;
import lombok.SneakyThrows;
import lombok.Value;
@@ -33,11 +31,6 @@ public class TraceScope implements Closeable {
*/
private final Span savedSpan;
/**
* List of callbacks to run on close.
*/
private List<Runnable> callbacks = new ArrayList<Runnable>();
@NonFinal
private boolean detached = false;
@@ -73,12 +66,6 @@ public class TraceScope implements Closeable {
return this.span;
}
public void register(Runnable callback) {
if (!this.callbacks .contains(callback)) {
this.callbacks.add(callback);
}
}
@Override
@SneakyThrows
public void close() {
@@ -86,13 +73,6 @@ public class TraceScope implements Closeable {
return;
}
this.detached = true;
for (Runnable callback : this.callbacks) {
try {
callback.run();
} catch (Throwable e) {
log.error("Error with callback on close", e);
}
}
Span cur = TraceContextHolder.getCurrentSpan();
if (cur != this.span) {
ExceptionUtils.error("Tried to close trace span " + this.span + " but " +

View File

@@ -1,6 +1,8 @@
package org.springframework.cloud.sleuth.event;
import lombok.EqualsAndHashCode;
import lombok.Value;
import org.springframework.cloud.sleuth.Span;
import org.springframework.context.ApplicationEvent;
@@ -8,6 +10,8 @@ import org.springframework.context.ApplicationEvent;
* @author Spencer Gibb
*/
@Value
@EqualsAndHashCode(callSuper=false)
@SuppressWarnings("serial")
public class SpanStartedEvent extends ApplicationEvent {
private final Span span;

View File

@@ -1,6 +1,8 @@
package org.springframework.cloud.sleuth.event;
import lombok.EqualsAndHashCode;
import lombok.Value;
import org.springframework.cloud.sleuth.Span;
import org.springframework.context.ApplicationEvent;
@@ -8,6 +10,8 @@ import org.springframework.context.ApplicationEvent;
* @author Spencer Gibb
*/
@Value
@EqualsAndHashCode(callSuper=false)
@SuppressWarnings("serial")
public class SpanStoppedEvent extends ApplicationEvent {
private final Span span;

View File

@@ -1,7 +1,8 @@
package org.springframework.cloud.sleuth.instrument.web.client;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
@@ -9,8 +10,6 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import javax.annotation.PostConstruct;
/**
* @author Spencer Gibb
*/
@@ -42,8 +41,8 @@ public class TraceWebClientAutoConfiguration {
@PostConstruct
public void init() {
if (restTemplate != null) {
restTemplate.getInterceptors().add(traceRestTemplateInterceptor);
if (this.restTemplate != null) {
this.restTemplate.getInterceptors().add(this.traceRestTemplateInterceptor);
}
}
}

View File

@@ -17,9 +17,9 @@ public class Slf4jSpanStartedListener implements ApplicationListener<SpanStarted
@Override
public void onApplicationEvent(SpanStartedEvent event) {
Span span = event.getSpan();
//TODO: what log level?
log.info("Starting span: {}", span);
MDC.put(Trace.SPAN_ID_NAME, span.getSpanId());
MDC.put(Trace.TRACE_ID_NAME, span.getTraceId());
//TODO: what log level?
log.info("Starting span: {}", span);
}
}