Skip to content

Commit

Permalink
Add latencyNanos to SpanData.
Browse files Browse the repository at this point in the history
  • Loading branch information
Oberon00 committed Dec 16, 2019
1 parent 07e7a3b commit 9875df5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ public SpanData toSpanData() {
.setName(getName())
.setAttributes(hasBeenEnded ? attributes : new HashMap<>(attributes))
.setEndEpochNanos(getEndEpochNanos())
.setLatencyNanos(getLatencyNanos())
.setStatus(getStatus())
.setTimedEvents(adaptTimedEvents());
}
Expand Down
33 changes: 33 additions & 0 deletions sdk/src/main/java/io/opentelemetry/sdk/trace/SpanData.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.opentelemetry.sdk.trace;

import com.google.auto.value.AutoValue;
import com.google.common.base.Optional;
import io.opentelemetry.sdk.common.InstrumentationLibraryInfo;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.trace.AttributeValue;
Expand Down Expand Up @@ -180,6 +181,15 @@ public abstract class SpanData {
*/
public abstract boolean getHasBeenEnded();

/**
* Returns the latency of the {@code Span} in nanos. If still active then returns now() - start
* time.
*
* @return the latency of the {@code Span} in nanos.
* @since 0.4.0
*/
public abstract long getLatencyNanos();

/**
* An immutable implementation of {@link Link}.
*
Expand Down Expand Up @@ -288,6 +298,12 @@ public abstract static class Builder {

abstract List<io.opentelemetry.trace.Link> getLinks();

abstract Optional<Long> getStartEpochNanos();

abstract Optional<Long> getEndEpochNanos();

abstract Optional<Long> getLatencyNanos();

/**
* Create a new SpanData instance from the data in this.
*
Expand All @@ -299,6 +315,14 @@ public SpanData build() {
setAttributes(Collections.unmodifiableMap(new HashMap<>(getAttributes())));
setTimedEvents(Collections.unmodifiableList(new ArrayList<>(getTimedEvents())));
setLinks(Collections.unmodifiableList(new ArrayList<>(getLinks())));

// Calculate latency from start & end if possible.
if (!getLatencyNanos().isPresent()
&& getStartEpochNanos().isPresent()
&& getEndEpochNanos().isPresent()
&& getEndEpochNanos().get() >= getStartEpochNanos().get()) {
setLatencyNanos(getEndEpochNanos().get() - getStartEpochNanos().get());
}
return autoBuild();
}

Expand Down Expand Up @@ -459,5 +483,14 @@ public abstract Builder setInstrumentationLibraryInfo(
* @since 0.4.0
*/
public abstract Builder setHasBeenEnded(boolean hasBeenEnded);

/**
* Sets to true if the span has been ended.
*
* @param latencyNanos A boolean indicating if the span has been ended.
* @return this
* @since 0.4.0
*/
public abstract Builder setLatencyNanos(long latencyNanos);
}
}

0 comments on commit 9875df5

Please sign in to comment.