Skip to content
This repository was archived by the owner on Dec 23, 2023. It is now read-only.

Commit 945236d

Browse files
author
Bogdan Drutu
authored
Change hasRemoteParent to be Boolean where null means root Span. (#427)
1 parent 0ff88c0 commit 945236d

File tree

9 files changed

+42
-29
lines changed

9 files changed

+42
-29
lines changed

api/src/main/java/io/opencensus/trace/Sampler.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,20 @@ public abstract class Sampler {
2121
/**
2222
* Called during {@link Span} creation to make a sampling decision.
2323
*
24-
* @param parentContext The parent {@code Span} {@link SpanContext}. May be {@code null} if this
24+
* @param parentContext the parent span's {@link SpanContext}. {@code null} if this is a root
25+
* span.
26+
* @param hasRemoteParent {@code true} if the parent {@code Span} is remote. {@code null} if this
2527
* is a root span.
26-
* @param remoteParent true if the parentContext is remote.
27-
* @param traceId The {@link TraceId} for the new {@code Span}. This will be identical to that in
28+
* @param traceId the {@link TraceId} for the new {@code Span}. This will be identical to that in
2829
* the parentContext, unless this is a root span.
29-
* @param spanId The span ID for the new {@code Span}.
30-
* @param name The name of the new {@code Span}.
31-
* @param parentLinks The parentLinks associated with the new {@code Span}.
30+
* @param spanId the span ID for the new {@code Span}.
31+
* @param name the name of the new {@code Span}.
32+
* @param parentLinks the parentLinks associated with the new {@code Span}.
3233
* @return {@code true} if the {@code Span} is sampled.
3334
*/
3435
public abstract boolean shouldSample(
3536
@Nullable SpanContext parentContext,
36-
boolean remoteParent,
37+
@Nullable Boolean hasRemoteParent,
3738
TraceId traceId,
3839
SpanId spanId,
3940
String name,

api/src/main/java/io/opencensus/trace/export/SpanData.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ public abstract class SpanData {
4444
* @param context the {@code SpanContext} of the {@code Span}.
4545
* @param parentSpanId the parent {@code SpanId} of the {@code Span}. {@code null} if the {@code
4646
* Span} is a root.
47-
* @param hasRemoteParent {@code true} if the parent is on a different process.
47+
* @param hasRemoteParent {@code true} if the parent {@code Span} is remote. {@code null} if this
48+
* is a root span.
4849
* @param name the name of the {@code Span}.
4950
* @param startTimestamp the start {@code Timestamp} of the {@code Span}.
5051
* @param attributes the attributes associated with the {@code Span}.
@@ -61,7 +62,7 @@ public abstract class SpanData {
6162
public static SpanData create(
6263
SpanContext context,
6364
@Nullable SpanId parentSpanId,
64-
boolean hasRemoteParent,
65+
@Nullable Boolean hasRemoteParent,
6566
String name,
6667
Timestamp startTimestamp,
6768
Attributes attributes,
@@ -102,11 +103,14 @@ public static SpanData create(
102103
public abstract SpanId getParentSpanId();
103104

104105
/**
105-
* Returns {@code true} if the parent is on a different process.
106+
* Returns {@code true} if the parent is on a different process. {@code null} if this is a root
107+
* span.
106108
*
107-
* @return {@code true} if the parent is on a different process.
109+
* @return {@code true} if the parent is on a different process. {@code null} if this is a root
110+
* span.
108111
*/
109-
public abstract boolean getHasRemoteParent();
112+
@Nullable
113+
public abstract Boolean getHasRemoteParent();
110114

111115
/**
112116
* Returns the name of this {@code Span}.

api/src/main/java/io/opencensus/trace/samplers/AlwaysSampleSampler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ final class AlwaysSampleSampler extends Sampler {
3232
@Override
3333
public boolean shouldSample(
3434
@Nullable SpanContext parentContext,
35-
boolean remoteParent,
35+
@Nullable Boolean hasRemoteParent,
3636
TraceId traceId,
3737
SpanId spanId,
3838
String name,

api/src/main/java/io/opencensus/trace/samplers/NeverSampleSampler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ final class NeverSampleSampler extends Sampler {
3232
@Override
3333
public boolean shouldSample(
3434
@Nullable SpanContext parentContext,
35-
boolean remoteParent,
35+
@Nullable Boolean hasRemoteParent,
3636
TraceId traceId,
3737
SpanId spanId,
3838
String name,

api/src/main/java/io/opencensus/trace/samplers/ProbabilitySampler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ static ProbabilitySampler create(double probability) {
7171
@Override
7272
public final boolean shouldSample(
7373
@Nullable SpanContext parentContext,
74-
boolean remoteParent,
74+
@Nullable Boolean hasRemoteParent,
7575
TraceId traceId,
7676
SpanId spanId,
7777
String name,

api/src/test/java/io/opencensus/trace/export/SpanDataTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public void spanData_RootActiveSpan() {
126126
SpanData.create(
127127
spanContext,
128128
null,
129-
false,
129+
null,
130130
SPAN_NAME,
131131
startTimestamp,
132132
attributes,
@@ -138,7 +138,7 @@ public void spanData_RootActiveSpan() {
138138
null);
139139
assertThat(spanData.getContext()).isEqualTo(spanContext);
140140
assertThat(spanData.getParentSpanId()).isNull();
141-
assertThat(spanData.getHasRemoteParent()).isFalse();
141+
assertThat(spanData.getHasRemoteParent()).isNull();
142142
assertThat(spanData.getName()).isEqualTo(SPAN_NAME);
143143
assertThat(spanData.getStartTimestamp()).isEqualTo(startTimestamp);
144144
assertThat(spanData.getAttributes()).isEqualTo(attributes);

impl_core/src/main/java/io/opencensus/trace/SpanBuilderImpl.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ final class SpanBuilderImpl extends SpanBuilder {
4040

4141
private SpanImpl startSpanInternal(
4242
@Nullable SpanContext parent,
43-
boolean hasRemoteParent,
43+
@Nullable Boolean hasRemoteParent,
4444
String name,
4545
Sampler sampler,
4646
List<Span> parentLinks,
@@ -57,7 +57,7 @@ private SpanImpl startSpanInternal(
5757
traceId = TraceId.generateRandomId(random);
5858
traceOptionsBuilder = TraceOptions.builder();
5959
// This is a root span so no remote or local parent.
60-
hasRemoteParent = false;
60+
hasRemoteParent = null;
6161
} else {
6262
// New child span.
6363
traceId = parent.getTraceId();
@@ -123,19 +123,22 @@ private SpanBuilderImpl(
123123
@Override
124124
public SpanImpl startSpan() {
125125
SpanContext parentContext = remoteParentSpanContext;
126-
boolean hasRemoteParent = parentContext != null;
126+
Boolean hasRemoteParent = Boolean.TRUE;
127127
TimestampConverter timestampConverter = null;
128-
if (!hasRemoteParent) {
128+
if (remoteParentSpanContext == null) {
129129
// This is not a child of a remote Span. Get the parent SpanContext from the parent Span if
130130
// any.
131131
Span parent = this.parent;
132+
hasRemoteParent = Boolean.FALSE;
132133
if (parent != null) {
133134
parentContext = parent.getContext();
134135
// Pass the timestamp converter from the parent to ensure that the recorded events are in
135136
// the right order. Implementation uses System.nanoTime() which is monotonically increasing.
136137
if (parent instanceof SpanImpl) {
137138
timestampConverter = ((SpanImpl) parent).getTimestampConverter();
138139
}
140+
} else {
141+
hasRemoteParent = null;
139142
}
140143
}
141144
return startSpanInternal(

impl_core/src/main/java/io/opencensus/trace/SpanImpl.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public final class SpanImpl extends Span implements Element<SpanImpl> {
4444
// The parent SpanId of this span. Null if this is a root span.
4545
private final SpanId parentSpanId;
4646
// True if the parent is on a different process.
47-
private final boolean hasRemoteParent;
47+
private final Boolean hasRemoteParent;
4848
// Active trace params when the Span was created.
4949
private final TraceParams traceParams;
5050
// Handler called when the span starts and ends.
@@ -93,7 +93,8 @@ public final class SpanImpl extends Span implements Element<SpanImpl> {
9393
* @param options the options for the new span, importantly Options.RECORD_EVENTS.
9494
* @param name the displayed name for the new span.
9595
* @param parentSpanId the span_id of the parent span, or null if the new span is a root span.
96-
* @param hasRemoteParent true if the parent span is in another process.
96+
* @param hasRemoteParent {@code true} if the parentContext is remote. {@code null} if this is a
97+
* root span.
9798
* @param traceParams trace parameters like sampler and probability.
9899
* @param startEndHandler handler called when the span starts and ends.
99100
* @param timestampConverter null if the span is a root span or the parent is not sampled. If the
@@ -108,7 +109,7 @@ public static SpanImpl startSpan(
108109
@Nullable EnumSet<Options> options,
109110
String name,
110111
@Nullable SpanId parentSpanId,
111-
boolean hasRemoteParent,
112+
@Nullable Boolean hasRemoteParent,
112113
TraceParams traceParams,
113114
StartEndHandler startEndHandler,
114115
@Nullable TimestampConverter timestampConverter,
@@ -480,7 +481,7 @@ private SpanImpl(
480481
@Nullable EnumSet<Options> options,
481482
String name,
482483
@Nullable SpanId parentSpanId,
483-
boolean hasRemoteParent,
484+
@Nullable Boolean hasRemoteParent,
484485
TraceParams traceParams,
485486
StartEndHandler startEndHandler,
486487
@Nullable TimestampConverter timestampConverter,

impl_core/src/test/java/io/opencensus/trace/SpanBuilderImplTest.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public void startSpanNullParent() {
6161
assertThat(span.getContext().getTraceOptions().isSampled()).isTrue();
6262
SpanData spanData = span.toSpanData();
6363
assertThat(spanData.getParentSpanId()).isNull();
64-
assertThat(spanData.getHasRemoteParent()).isFalse();
64+
assertThat(spanData.getHasRemoteParent()).isNull();
6565
assertThat(spanData.getStartTimestamp()).isEqualTo(testClock.now());
6666
assertThat(spanData.getName()).isEqualTo(SPAN_NAME);
6767
}
@@ -78,7 +78,7 @@ public void startSpanNullParentWithRecordEvents() {
7878
assertThat(span.getContext().getTraceOptions().isSampled()).isFalse();
7979
SpanData spanData = span.toSpanData();
8080
assertThat(spanData.getParentSpanId()).isNull();
81-
assertThat(spanData.getHasRemoteParent()).isFalse();
81+
assertThat(spanData.getHasRemoteParent()).isNull();
8282
}
8383

8484
@Test
@@ -99,12 +99,16 @@ public void startChildSpan() {
9999
assertThat(rootSpan.getContext().isValid()).isTrue();
100100
assertThat(rootSpan.getOptions().contains(Options.RECORD_EVENTS)).isTrue();
101101
assertThat(rootSpan.getContext().getTraceOptions().isSampled()).isTrue();
102+
assertThat(((SpanImpl) rootSpan).toSpanData().getHasRemoteParent())
103+
.isNull();
102104
Span childSpan =
103105
SpanBuilderImpl.createWithParent(SPAN_NAME, rootSpan, spanBuilderOptions).startSpan();
104106
assertThat(childSpan.getContext().isValid()).isTrue();
105107
assertThat(childSpan.getContext().getTraceId()).isEqualTo(rootSpan.getContext().getTraceId());
106108
assertThat(((SpanImpl) childSpan).toSpanData().getParentSpanId())
107109
.isEqualTo(rootSpan.getContext().getSpanId());
110+
assertThat(((SpanImpl) childSpan).toSpanData().getHasRemoteParent())
111+
.isFalse();
108112
assertThat(((SpanImpl) childSpan).getTimestampConverter())
109113
.isEqualTo(((SpanImpl) rootSpan).getTimestampConverter());
110114
}
@@ -118,7 +122,7 @@ public void startRemoteSpan_NullParent() {
118122
assertThat(span.getContext().getTraceOptions().isSampled()).isTrue();
119123
SpanData spanData = span.toSpanData();
120124
assertThat(spanData.getParentSpanId()).isNull();
121-
assertThat(spanData.getHasRemoteParent()).isFalse();
125+
assertThat(spanData.getHasRemoteParent()).isNull();
122126
}
123127

124128
@Test
@@ -131,7 +135,7 @@ public void startRemoteSpanInvalidParent() {
131135
assertThat(span.getContext().getTraceOptions().isSampled()).isTrue();
132136
SpanData spanData = span.toSpanData();
133137
assertThat(spanData.getParentSpanId()).isNull();
134-
assertThat(spanData.getHasRemoteParent()).isFalse();
138+
assertThat(spanData.getHasRemoteParent()).isNull();
135139
}
136140

137141
@Test

0 commit comments

Comments
 (0)