diff --git a/pom.xml b/pom.xml index 58da79494..96e8782b4 100644 --- a/pom.xml +++ b/pom.xml @@ -148,7 +148,7 @@ org.projectlombok lombok - 1.12.6 + 1.16.4 provided diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/trace/MilliSpan.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/trace/MilliSpan.java new file mode 100644 index 000000000..da2a320a4 --- /dev/null +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/trace/MilliSpan.java @@ -0,0 +1,68 @@ +package org.springframework.cloud.sleuth.trace; + +import lombok.Builder; +import lombok.Data; +import lombok.Singular; + +import java.util.List; +import java.util.Map; + +/** + * @author Spencer Gibb + */ +@Data +@Builder +public class MilliSpan implements Span { + private final long begin; + private long end; + private final String description; + private final String traceId; + @Singular + private final List parents; + private final String spanId; + private final Map kVAnnotations; + private final String processId; + @Singular + private final List timelineAnnotations; + + @Override + public synchronized void stop() { + if (end == 0) { + if (begin == 0) + throw new IllegalStateException("Span for " + description + + " has not been started"); + end = System.currentTimeMillis(); + //TODO figure out how to Trace.deliver(this) + } + } + + @Override + public synchronized long getAccumulatedMillis() { + if (begin == 0) + return 0; + if (end > 0) + return end - begin; + return System.currentTimeMillis() - begin; + } + + @Override + public synchronized boolean isRunning() { + return begin != 0 && end == 0; + } + + @Override + public Span child(String description) { + return null; + } + + @Override + public void addKVAnnotation(String key, String value) { + kVAnnotations.put(key, value); + } + + @Override + public void addTimelineAnnotation(String msg) { + timelineAnnotations.add(new TimelineAnnotation(System.currentTimeMillis(), msg)); + } + +} diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/trace/Span.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/trace/Span.java index 48c8f656d..673b0d6be 100644 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/trace/Span.java +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/trace/Span.java @@ -1,5 +1,6 @@ package org.springframework.cloud.sleuth.trace; +import java.util.Collection; import java.util.List; import java.util.Map; @@ -20,12 +21,12 @@ public interface Span { /** * Get the start time, in milliseconds */ - long getStartTimeMillis(); + long getBegin(); /** * Get the stop time, in milliseconds */ - long getStopTimeMillis(); + long getEnd(); /** * Return the total amount of time elapsed since start was called, if running, @@ -51,13 +52,13 @@ public interface Span { * The spanId is immutable and cannot be changed. It is safe to access this * from multiple threads. */ - long getSpanId(); + String getSpanId(); /** * A pseudo-unique (random) number assigned to the trace associated with this * span */ - long getTraceId(); + String getTraceId(); /** * Create a child span of this span with the given description @@ -67,16 +68,9 @@ public interface Span { /** * Returns the parent IDs of the span.

*

- * The array will be empty if there are no parents. + * The collection will be empty if there are no parents. */ - long[] getParents(); - - /** - * Set the parents of this span.

- *

- * Any existing parents will be cleared by this call. - */ - void setParents(long[] parents); + List getParents(); /** * Add a data annotation associated with this span @@ -108,9 +102,4 @@ public interface Span { * Will never be null. */ String getProcessId(); - - /** - * Set the process id of a span. - */ - void setProcessId(String s); } diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/trace/SpanHolder.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/trace/SpanHolder.java new file mode 100644 index 000000000..38a054b16 --- /dev/null +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/trace/SpanHolder.java @@ -0,0 +1,16 @@ +package org.springframework.cloud.sleuth.trace; + +/** + * @author Spencer Gibb + */ +public class SpanHolder { + private static final ThreadLocal currentSpan = new ThreadLocal<>(); + + public Span getCurrentSpan() { + return currentSpan.get(); + } + + public void setCurrentSpan(Span span) { + currentSpan.set(span); + } +} diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/trace/Trace.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/trace/Trace.java index 0728c44ab..6c1f57ea4 100644 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/trace/Trace.java +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/trace/Trace.java @@ -1,5 +1,7 @@ package org.springframework.cloud.sleuth.trace; +import java.util.Collection; + /** * The Trace class is the primary way to interact with the library. It provides * methods to create and manipulate spans. @@ -85,4 +87,6 @@ public interface Trace { * @return Span representing the current trace, or null if not tracing. */ public Span currentSpan(); + + public void setSpanReceivers(Collection spanReceivers); } diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/trace/TraceScope.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/trace/TraceScope.java index f138a5afa..803f84f4f 100644 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/trace/TraceScope.java +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/trace/TraceScope.java @@ -1,7 +1,42 @@ package org.springframework.cloud.sleuth.trace; +import lombok.Data; + +import java.io.Closeable; +import java.io.IOException; + /** * @author Spencer Gibb */ -public class TraceScope { +@Data +public class TraceScope implements Closeable { + /** + * the span for this scope + */ + private final Span span; + + /** + * the span that was "current" before this scope was entered + */ + private final Span savedSpan; + + private boolean detached = false; + + /** + * Remove this span as the current thread, but don't stop it yet or + * send it for collection. This is useful if the span object is then + * passed to another thread for use with Trace.continueTrace(). + * + * @return the same Span object + */ + public Span detach() { + //TODO: implement detach + return span; + } + + @Override + public void close() throws IOException { + span.stop(); + //TODO: set savedSpan to currentSpan in SpanHolder + } }