Adds getParentSpanContext to ReadableSpan interface (#3454)

* Adds getParentSpanContext to ReadableSpan

* Unit tests
This commit is contained in:
HaloFour 2021-08-07 22:17:40 -04:00 committed by GitHub
parent 8224ee4323
commit b5f56ccac7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 4 deletions

View File

@ -1,4 +1,7 @@
Comparing source compatibility of against
**** MODIFIED INTERFACE: PUBLIC ABSTRACT io.opentelemetry.sdk.trace.ReadableSpan (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++* NEW METHOD: PUBLIC(+) ABSTRACT(+) io.opentelemetry.api.trace.SpanContext getParentSpanContext()
***! MODIFIED INTERFACE: PUBLIC ABSTRACT io.opentelemetry.sdk.trace.samplers.SamplingResult (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++! NEW METHOD: PUBLIC(+) STATIC(+) io.opentelemetry.sdk.trace.samplers.SamplingResult drop()

View File

@ -23,6 +23,14 @@ public interface ReadableSpan {
*/
SpanContext getSpanContext();
/**
* Returns the parent {@link SpanContext} of the {@link Span}, or {@link SpanContext#getInvalid()}
* if this is a root span.
*
* @return the parent {@link SpanContext} of the {@link Span}
*/
SpanContext getParentSpanContext();
/**
* Returns the name of the {@code Span}.
*

View File

@ -202,6 +202,11 @@ final class RecordEventsReadableSpan implements ReadWriteSpan {
return context;
}
@Override
public SpanContext getParentSpanContext() {
return parentSpanContext;
}
/**
* Returns the name of the {@code Span}.
*
@ -451,10 +456,6 @@ final class RecordEventsReadableSpan implements ReadWriteSpan {
}
}
SpanContext getParentSpanContext() {
return parentSpanContext;
}
Resource getResource() {
return resource;
}

View File

@ -214,10 +214,26 @@ class RecordEventsReadableSpanTest {
} finally {
span.end();
}
assertThat(span.getParentSpanContext().isValid()).isFalse();
SpanData spanData = span.toSpanData();
assertThat(SpanId.isValid(spanData.getParentSpanId())).isFalse();
}
@Test
void toSpanData_ChildSpan() {
RecordEventsReadableSpan span = createTestSpan(SpanKind.INTERNAL);
try {
spanDoWork(span, null, null);
} finally {
span.end();
}
assertThat(span.getParentSpanContext().isValid()).isTrue();
assertThat(span.getParentSpanContext().getTraceId()).isEqualTo(traceId);
assertThat(span.getParentSpanContext().getSpanId()).isEqualTo(parentSpanId);
SpanData spanData = span.toSpanData();
assertThat(spanData.getParentSpanId()).isEqualTo(parentSpanId);
}
@Test
void toSpanData_WithInitialAttributes() {
RecordEventsReadableSpan span = createTestSpanWithAttributes(attributes);