Skip to content

Commit b5f56cc

Browse files
authored
Adds getParentSpanContext to ReadableSpan interface (open-telemetry#3454)
* Adds getParentSpanContext to ReadableSpan * Unit tests
1 parent 8224ee4 commit b5f56cc

4 files changed

Lines changed: 32 additions & 4 deletions

File tree

docs/apidiffs/current_vs_latest/opentelemetry-sdk-trace.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
Comparing source compatibility of against
2+
**** MODIFIED INTERFACE: PUBLIC ABSTRACT io.opentelemetry.sdk.trace.ReadableSpan (not serializable)
3+
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
4+
+++* NEW METHOD: PUBLIC(+) ABSTRACT(+) io.opentelemetry.api.trace.SpanContext getParentSpanContext()
25
***! MODIFIED INTERFACE: PUBLIC ABSTRACT io.opentelemetry.sdk.trace.samplers.SamplingResult (not serializable)
36
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
47
+++! NEW METHOD: PUBLIC(+) STATIC(+) io.opentelemetry.sdk.trace.samplers.SamplingResult drop()

sdk/trace/src/main/java/io/opentelemetry/sdk/trace/ReadableSpan.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ public interface ReadableSpan {
2323
*/
2424
SpanContext getSpanContext();
2525

26+
/**
27+
* Returns the parent {@link SpanContext} of the {@link Span}, or {@link SpanContext#getInvalid()}
28+
* if this is a root span.
29+
*
30+
* @return the parent {@link SpanContext} of the {@link Span}
31+
*/
32+
SpanContext getParentSpanContext();
33+
2634
/**
2735
* Returns the name of the {@code Span}.
2836
*

sdk/trace/src/main/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpan.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,11 @@ public SpanContext getSpanContext() {
202202
return context;
203203
}
204204

205+
@Override
206+
public SpanContext getParentSpanContext() {
207+
return parentSpanContext;
208+
}
209+
205210
/**
206211
* Returns the name of the {@code Span}.
207212
*
@@ -451,10 +456,6 @@ private StatusData getSpanDataStatus() {
451456
}
452457
}
453458

454-
SpanContext getParentSpanContext() {
455-
return parentSpanContext;
456-
}
457-
458459
Resource getResource() {
459460
return resource;
460461
}

sdk/trace/src/test/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpanTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,26 @@ void toSpanData_RootSpan() {
214214
} finally {
215215
span.end();
216216
}
217+
assertThat(span.getParentSpanContext().isValid()).isFalse();
217218
SpanData spanData = span.toSpanData();
218219
assertThat(SpanId.isValid(spanData.getParentSpanId())).isFalse();
219220
}
220221

222+
@Test
223+
void toSpanData_ChildSpan() {
224+
RecordEventsReadableSpan span = createTestSpan(SpanKind.INTERNAL);
225+
try {
226+
spanDoWork(span, null, null);
227+
} finally {
228+
span.end();
229+
}
230+
assertThat(span.getParentSpanContext().isValid()).isTrue();
231+
assertThat(span.getParentSpanContext().getTraceId()).isEqualTo(traceId);
232+
assertThat(span.getParentSpanContext().getSpanId()).isEqualTo(parentSpanId);
233+
SpanData spanData = span.toSpanData();
234+
assertThat(spanData.getParentSpanId()).isEqualTo(parentSpanId);
235+
}
236+
221237
@Test
222238
void toSpanData_WithInitialAttributes() {
223239
RecordEventsReadableSpan span = createTestSpanWithAttributes(attributes);

0 commit comments

Comments
 (0)