
com.undefinedlabs.scope.SpanContext Maven / Gradle / Ivy
package com.undefinedlabs.scope;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class SpanContext implements io.opentracing.SpanContext {
public static final SpanContext EMPTY =
new SpanContext(
TraceId.getInvalid(), TraceId.getInvalid(), null, new HashMap());
private final TraceId traceId;
private final TraceId spanId;
private final TraceId parentSpanId;
private final Map baggage;
private final String traceIdStr;
private final String spanIdStr;
private final String parentSpanIdStr;
private final String spanId64bitStr;
private final String parentSpanId64bitStr;
public SpanContext(
final TraceId traceId,
final TraceId spanId,
final TraceId parentSpanId,
final Map baggage) {
this.traceId = traceId;
this.spanId = spanId;
this.parentSpanId = parentSpanId;
this.baggage = baggage != null ? baggage : new HashMap();
traceIdStr = this.traceId.toLowerCaseBase16();
spanIdStr = this.spanId.toLowerCaseBase16();
parentSpanIdStr =
(this.parentSpanId != null) ? this.parentSpanId.toLowerCaseBase16() : null;
spanId64bitStr = spanIdStr.substring(16);
parentSpanId64bitStr =
parentSpanIdStr != null ? parentSpanIdStr.substring(16) : null;
}
@Override
public String toTraceId() {
return traceIdStr;
}
@Override
public String toSpanId() {
return spanIdStr;
}
public String toParentSpanId() {
return parentSpanIdStr;
}
public String toSpanId64bitStr() {
return spanId64bitStr;
}
public String toParentSpanId64bitStr() {
return parentSpanId64bitStr;
}
@Override
public Iterable> baggageItems() {
return Collections.unmodifiableSet(baggage.entrySet());
}
public TraceId getTraceId() {
return traceId;
}
public TraceId getSpanId() {
return spanId;
}
public TraceId getParentSpanId() {
return parentSpanId;
}
public Map getBaggage() {
return baggage;
}
public boolean hasTrace() {
return (!TraceId.getInvalid().equals(traceId) && !TraceId.getInvalid().equals(spanId));
}
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final SpanContext that = (SpanContext) o;
return new EqualsBuilder()
.append(traceId, that.traceId)
.append(spanId, that.spanId)
.append(parentSpanId, that.parentSpanId)
.append(baggage, that.baggage)
.isEquals();
}
@Override
public int hashCode() {
return new HashCodeBuilder(17, 37)
.append(traceId)
.append(spanId)
.append(parentSpanId)
.append(baggage)
.toHashCode();
}
@Override
public String toString() {
return new StringBuilder()
.append(toTraceId())
.append(":")
.append(toSpanId64bitStr())
.append(":")
.append(getParentSpanId() != null ? toParentSpanId64bitStr() : "null")
.toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy