Add SpanContinuedEvent
So that it can be used to keep the MDC populated when an async request is continued. Fixes gh-18
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* 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.event;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import org.springframework.cloud.sleuth.Span;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper=false)
|
||||
@SuppressWarnings("serial")
|
||||
public class SpanContinuedEvent extends ApplicationEvent {
|
||||
|
||||
private final Span span;
|
||||
|
||||
public SpanContinuedEvent(Object source, Span span) {
|
||||
super(source);
|
||||
this.span = span;
|
||||
}
|
||||
}
|
||||
@@ -23,6 +23,7 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.slf4j.MDC;
|
||||
import org.springframework.cloud.sleuth.Span;
|
||||
import org.springframework.cloud.sleuth.Trace;
|
||||
import org.springframework.cloud.sleuth.event.SpanContinuedEvent;
|
||||
import org.springframework.cloud.sleuth.event.SpanStartedEvent;
|
||||
import org.springframework.cloud.sleuth.event.SpanStoppedEvent;
|
||||
import org.springframework.context.event.EventListener;
|
||||
@@ -41,23 +42,36 @@ public class Slf4jSpanListener {
|
||||
Span span = event.getSpan();
|
||||
MDC.put(Trace.SPAN_ID_NAME, span.getSpanId());
|
||||
MDC.put(Trace.TRACE_ID_NAME, span.getTraceId());
|
||||
//TODO: what log level?
|
||||
// TODO: what log level?
|
||||
log.info("Starting span: {}", span);
|
||||
if (event.getParent()!=null) {
|
||||
if (event.getParent() != null) {
|
||||
log.info("With parent: {}", event.getParent());
|
||||
}
|
||||
}
|
||||
|
||||
@EventListener(SpanContinuedEvent.class)
|
||||
@Order(Ordered.LOWEST_PRECEDENCE)
|
||||
public void continued(SpanContinuedEvent event) {
|
||||
Span span = event.getSpan();
|
||||
MDC.put(Trace.SPAN_ID_NAME, span.getSpanId());
|
||||
MDC.put(Trace.TRACE_ID_NAME, span.getTraceId());
|
||||
// TODO: what should this log level be?
|
||||
log.info("Continued span: {}", event.getSpan());
|
||||
}
|
||||
|
||||
@EventListener(SpanStoppedEvent.class)
|
||||
@Order(Ordered.LOWEST_PRECEDENCE)
|
||||
public void stop(SpanStoppedEvent event) {
|
||||
//TODO: what should this log level be?
|
||||
// TODO: what should this log level be?
|
||||
log.info("Stopped span: {}", event.getSpan());
|
||||
if (event.getParent()!=null) {
|
||||
if (event.getParent() != null) {
|
||||
log.info("With parent: {}", event.getParent());
|
||||
MDC.put(Trace.SPAN_ID_NAME, event.getParent().getSpanId());
|
||||
}
|
||||
else {
|
||||
MDC.remove(SPAN_ID_NAME);
|
||||
MDC.remove(TRACE_ID_NAME);
|
||||
}
|
||||
MDC.remove(SPAN_ID_NAME);
|
||||
MDC.remove(TRACE_ID_NAME);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ import org.springframework.cloud.sleuth.Span;
|
||||
import org.springframework.cloud.sleuth.Trace;
|
||||
import org.springframework.cloud.sleuth.TraceContextHolder;
|
||||
import org.springframework.cloud.sleuth.TraceScope;
|
||||
import org.springframework.cloud.sleuth.event.SpanContinuedEvent;
|
||||
import org.springframework.cloud.sleuth.event.SpanStartedEvent;
|
||||
import org.springframework.cloud.sleuth.instrument.TraceCallable;
|
||||
import org.springframework.cloud.sleuth.instrument.TraceRunnable;
|
||||
@@ -107,6 +108,7 @@ public class DefaultTrace implements Trace {
|
||||
return NullScope.INSTANCE;
|
||||
Span oldSpan = getCurrentSpan();
|
||||
TraceContextHolder.setCurrentSpan(span);
|
||||
this.publisher.publishEvent(new SpanContinuedEvent(this, span));
|
||||
return new TraceScope(this.publisher, span, oldSpan);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user