Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added SpanProcessor OnEnding callback #6367

Merged
merged 18 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
spotless and documentation clarifications
  • Loading branch information
JonasKunz committed Aug 7, 2024
commit f3fd785ad04520e2b2e87dad153be5924d78460d
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ public InstrumentationScopeInfo getInstrumentationScopeInfo() {
@Override
public long getLatencyNanos() {
synchronized (lock) {
return (hasEnded == EndState.NOT_ENDED ? clock.now() : endEpochNanos) - startEpochNanos;
return (hasEnded == EndState.NOT_ENDED ? clock.now() : endEpochNanos) - startEpochNanos;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,27 @@ static SpanProcessor composite(Iterable<SpanProcessor> processors) {
*/
boolean isEndRequired();


/**
* Called just before a {@link io.opentelemetry.api.trace.Span} is ended, if the {@link
* Span#isRecording()} returns true. This means that the span will still be mutable
* Span#isRecording()} returns true. This means that the span will still be mutable. Note that the
* span will only be modifiable synchronously from this callback, concurrent modifications from
* other threads will be prevented.
*
* <p>This method is called synchronously on the execution thread, should not throw or block the
* execution thread.
*
* <p>Note: This method is experimental and might be subject to future changes.
*
* @param span the {@code Span} that is just about to be ended.
*/
default void onEnding(ReadWriteSpan span) {
}
default void onEnding(ReadWriteSpan span) {}
JonasKunz marked this conversation as resolved.
Show resolved Hide resolved

/**
* Returns {@code true} if this {@link SpanProcessor} requires before-end events.
* Returns {@code true} if this {@link SpanProcessor} requires onEnding events.
*
* <p>Note: This method is experimental and might be subject to future changes.
*
* @return {@code true} if this {@link SpanProcessor} requires onEnding events.
*/
default boolean isOnEndingRequired() {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,17 @@ void beforeEnd_spanStillMutable() {
AttributeKey<String> dummyAttrib = AttributeKey.stringKey("processor_foo");

AtomicBoolean endedStateInProcessor = new AtomicBoolean();
doAnswer(invocation -> {
ReadWriteSpan sp = invocation.getArgument(0, ReadWriteSpan.class);
assertThat(sp.hasEnded()).isFalse();
sp.end(); //should have no effect, nested end should be detected
endedStateInProcessor.set(sp.hasEnded());
sp.setAttribute(dummyAttrib, "bar");
return null;
}).when(spanProcessor).onEnding(any());
doAnswer(
invocation -> {
ReadWriteSpan sp = invocation.getArgument(0, ReadWriteSpan.class);
assertThat(sp.hasEnded()).isFalse();
sp.end(); // should have no effect, nested end should be detected
endedStateInProcessor.set(sp.hasEnded());
sp.setAttribute(dummyAttrib, "bar");
return null;
})
.when(spanProcessor)
.onEnding(any());

span.end();
verify(spanProcessor).onEnding(same(span));
Expand All @@ -173,13 +176,16 @@ void beforeEnd_latencyPinned() {
SdkSpan span = createTestSpan(SpanKind.INTERNAL);

AtomicLong spanLatencyInProcessor = new AtomicLong();
doAnswer(invocation -> {
ReadWriteSpan sp = invocation.getArgument(0, ReadWriteSpan.class);

testClock.advance(Duration.ofSeconds(100));
spanLatencyInProcessor.set(sp.getLatencyNanos());
return null;
}).when(spanProcessor).onEnding(any());
doAnswer(
invocation -> {
ReadWriteSpan sp = invocation.getArgument(0, ReadWriteSpan.class);

testClock.advance(Duration.ofSeconds(100));
spanLatencyInProcessor.set(sp.getLatencyNanos());
return null;
})
.when(spanProcessor)
.onEnding(any());

testClock.advance(Duration.ofSeconds(1));
long expectedDuration = testClock.now() - START_EPOCH_NANOS;
Expand Down
Loading