updates to interfaces and supporting classes

This commit is contained in:
Spencer Gibb
2015-06-22 13:26:15 -06:00
parent 35bda8cc5b
commit 4d6f0cfcfd
6 changed files with 132 additions and 20 deletions

View File

@@ -148,7 +148,7 @@
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.12.6</version>
<version>1.16.4</version>
<scope>provided</scope>
</dependency>
<dependency>

View File

@@ -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<String> parents;
private final String spanId;
private final Map<String, String> kVAnnotations;
private final String processId;
@Singular
private final List<TimelineAnnotation> 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));
}
}

View File

@@ -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.<p/>
* <p/>
* 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.<p/>
* <p/>
* Any existing parents will be cleared by this call.
*/
void setParents(long[] parents);
List<String> 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);
}

View File

@@ -0,0 +1,16 @@
package org.springframework.cloud.sleuth.trace;
/**
* @author Spencer Gibb
*/
public class SpanHolder {
private static final ThreadLocal<Span> currentSpan = new ThreadLocal<>();
public Span getCurrentSpan() {
return currentSpan.get();
}
public void setCurrentSpan(Span span) {
currentSpan.set(span);
}
}

View File

@@ -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<SpanReceiver> spanReceivers);
}

View File

@@ -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
}
}