Skip to content

Commit

Permalink
Adding start time into Span (#489)
Browse files Browse the repository at this point in the history
With start time in the span, rendering collected telemetry becomes easier.
  • Loading branch information
spuliaiev-sfdc committed Mar 20, 2024
1 parent a481956 commit 2e31563
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
Expand Up @@ -34,6 +34,7 @@ public final class Trace {
public static final class Span {

private final String name;
private final long startTimeEpocMilli;
private final StopWatch stopWatch;
private final List<Span> children = new ArrayList<>();
private boolean done = false;
Expand All @@ -43,7 +44,7 @@ public static final class Span {
if (name.isBlank()) {
throw new IllegalArgumentException("A blank name is not allowed!");
}

startTimeEpocMilli = System.currentTimeMillis();
stopWatch = new StopWatch();
stopWatch.start();
}
Expand All @@ -68,6 +69,10 @@ public long getDuration(TimeUnit timeUnit) {
return stopWatch.getDuration(timeUnit);
}

public long getStartTimeEpocMilli() {
return startTimeEpocMilli;
}

public String getName() {
return name;
}
Expand Down
Expand Up @@ -53,11 +53,13 @@ interface TraceTreeVisitor {

public static record SpanNode(
String name,
long startTimeEpocMilli,
long durationNanos,
double percentageOfRoot,
java.util.List<SpanNode> children) {
public SpanNode(String name, long durationNanos, double percentageOfRoot, java.util.List<SpanNode> children) {
public SpanNode(String name, long startTimeEpocMilli, long durationNanos, double percentageOfRoot, java.util.List<SpanNode> children) {
this.name = requireNonNull(name);
this.startTimeEpocMilli = startTimeEpocMilli;
this.durationNanos = durationNanos;
this.percentageOfRoot = percentageOfRoot;
this.children = requireNonNull(children);
Expand All @@ -73,6 +75,7 @@ void visit(TraceTreeVisitor visitor) {
public JsonObject toJson() {
var node = new JsonObject();
node.addProperty("name", name());
node.addProperty("startTimeEpocMilli", startTimeEpocMilli());
node.addProperty("durationNanos", durationNanos());
node.addProperty("persentageOfRoot", percentageOfRoot());
if (!children().isEmpty()) {
Expand Down Expand Up @@ -102,7 +105,8 @@ private static SpanNode createNode(Span span, long rootDurationNanos) {
}

var durationNanos = span.getDuration(TimeUnit.NANOSECONDS);
return new SpanNode(span.getName(), durationNanos, percentage(durationNanos, rootDurationNanos), children);
var startTimeEpocMilli = span.getStartTimeEpocMilli();
return new SpanNode(span.getName(), startTimeEpocMilli, durationNanos, percentage(durationNanos, rootDurationNanos), children);
}

private static double percentage(long duration, long rootDuration) {
Expand Down
Expand Up @@ -34,11 +34,12 @@ void ensure_output_produced_for_sample_trace() throws Exception {
private TraceTree simpleSampleTree() {
var rootNode = new TraceTree.SpanNode(
"Root",
1999999L,
100000L,
80.0F,
List.of(
new TraceTree.SpanNode("Child 1", 50000L, 3.0F, Collections.emptyList()),
new TraceTree.SpanNode("Child 2", 30000L, 5.0F, Collections.emptyList())));
new TraceTree.SpanNode("Child 1", 1999999L, 50000L, 3.0F, Collections.emptyList()),
new TraceTree.SpanNode("Child 2", 1999999L, 30000L, 5.0F, Collections.emptyList())));

return new TraceTree(rootNode);
}
Expand Down

0 comments on commit 2e31563

Please sign in to comment.