com.yammer.telemetry.tracing.Trace Maven / Gradle / Ivy
The newest version!
package com.yammer.telemetry.tracing;
import com.google.common.base.Optional;
import java.math.BigInteger;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.logging.Logger;
public class Trace {
private static final Logger LOG = Logger.getLogger(SpanHelper.class.getName());
private final BigInteger traceId;
private final ConcurrentMap> childSpans;
private final ConcurrentMap> annotations;
private SpanData root = null;
public Trace(BigInteger traceId) {
this.traceId = traceId;
this.childSpans = new ConcurrentHashMap<>();
this.annotations = new ConcurrentHashMap<>();
}
public BigInteger getTraceId() {
return traceId;
}
public SpanData getRoot() {
return root;
}
public List getChildren(BigInteger spanId) {
if (spanId == null) {
return Collections.emptyList();
}
return Optional.fromNullable(childSpans.get(spanId)).or(Collections.emptyList());
}
public List getAnnotations(BigInteger spanId) {
if (spanId == null) {
return Collections.emptyList();
}
return Optional.fromNullable(annotations.get(spanId)).or(Collections.emptyList());
}
public void addSpan(SpanData spanData) {
final Optional parentSpanId = spanData.getParentSpanId();
if (!parentSpanId.isPresent()) {
this.root = spanData;
} else {
final List newSiblings = new LinkedList<>();
newSiblings.add(spanData);
final List siblings = childSpans.putIfAbsent(parentSpanId.get(), newSiblings);
if (siblings != null) {
siblings.add(spanData);
}
}
for (AnnotationData annotation : spanData.getAnnotations()) {
addAnnotation(spanData.getSpanId(), annotation);
}
}
public void addAnnotation(BigInteger spanId, AnnotationData data) {
final LinkedList currentAnnotation = new LinkedList<>();
currentAnnotation.add(data);
List previousAnnotations = annotations.putIfAbsent(spanId, currentAnnotation);
if (previousAnnotations != null) {
previousAnnotations.add(data);
}
}
}