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

Commit 185230a

Browse files
author
Bogdan Drutu
authored
Add an optional number of child spans that were generated while this span was running. (#426)
1 parent 7b80471 commit 185230a

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public abstract class SpanData {
5151
* @param annotations the annotations associated with the {@code Span}.
5252
* @param networkEvents the network events associated with the {@code Span}.
5353
* @param links the links associated with the {@code Span}.
54+
* @param childSpanCount the number of child spans that were generated while the span was active.
5455
* @param status the {@code Status} of the {@code Span}. {@code null} if the {@code Span} is still
5556
* active.
5657
* @param endTimestamp the end {@code Timestamp} of the {@code Span}. {@code null} if the {@code
@@ -67,6 +68,7 @@ public static SpanData create(
6768
TimedEvents<Annotation> annotations,
6869
TimedEvents<NetworkEvent> networkEvents,
6970
Links links,
71+
@Nullable Integer childSpanCount,
7072
@Nullable Status status,
7173
@Nullable Timestamp endTimestamp) {
7274
return new AutoValue_SpanData(
@@ -79,6 +81,7 @@ public static SpanData create(
7981
annotations,
8082
networkEvents,
8183
links,
84+
childSpanCount,
8285
status,
8386
endTimestamp);
8487
}
@@ -147,6 +150,17 @@ public static SpanData create(
147150
*/
148151
public abstract Links getLinks();
149152

153+
/**
154+
* Returns the number of child spans that were generated while the {@code Span} was running. If
155+
* not {@code null} allows service implementations to detect missing child spans.
156+
*
157+
* <p>This information is not always available.
158+
*
159+
* @return the number of child spans that were generated while the {@code Span} was running.
160+
*/
161+
@Nullable
162+
public abstract Integer getChildSpanCount();
163+
150164
/**
151165
* Returns the {@code Status} or {@code null} if {@code Span} is still active.
152166
*

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public class SpanDataTest {
5858
private static final NetworkEvent sentNetworkEvent =
5959
NetworkEvent.builder(NetworkEvent.Type.SENT, 1).build();
6060
private static final Status status = Status.DEADLINE_EXCEEDED.withDescription("TooSlow");
61+
private static final int CHILD_SPAN_COUNT = 13;
6162
private final Random random = new Random(1234);
6263
private final SpanContext spanContext =
6364
SpanContext.create(
@@ -102,6 +103,7 @@ public void spanData_AllValues() {
102103
annotations,
103104
networkEvents,
104105
links,
106+
CHILD_SPAN_COUNT,
105107
status,
106108
endTimestamp);
107109
assertThat(spanData.getContext()).isEqualTo(spanContext);
@@ -113,6 +115,7 @@ public void spanData_AllValues() {
113115
assertThat(spanData.getAnnotations()).isEqualTo(annotations);
114116
assertThat(spanData.getNetworkEvents()).isEqualTo(networkEvents);
115117
assertThat(spanData.getLinks()).isEqualTo(links);
118+
assertThat(spanData.getChildSpanCount()).isEqualTo(CHILD_SPAN_COUNT);
116119
assertThat(spanData.getStatus()).isEqualTo(status);
117120
assertThat(spanData.getEndTimestamp()).isEqualTo(endTimestamp);
118121
}
@@ -131,6 +134,7 @@ public void spanData_RootActiveSpan() {
131134
networkEvents,
132135
links,
133136
null,
137+
null,
134138
null);
135139
assertThat(spanData.getContext()).isEqualTo(spanContext);
136140
assertThat(spanData.getParentSpanId()).isNull();
@@ -141,6 +145,7 @@ public void spanData_RootActiveSpan() {
141145
assertThat(spanData.getAnnotations()).isEqualTo(annotations);
142146
assertThat(spanData.getNetworkEvents()).isEqualTo(networkEvents);
143147
assertThat(spanData.getLinks()).isEqualTo(links);
148+
assertThat(spanData.getChildSpanCount()).isNull();
144149
assertThat(spanData.getStatus()).isNull();
145150
assertThat(spanData.getEndTimestamp()).isNull();
146151
}
@@ -158,6 +163,7 @@ public void spanData_AllDataEmpty() {
158163
TimedEvents.create(Collections.<SpanData.TimedEvent<Annotation>>emptyList(), 0),
159164
TimedEvents.create(Collections.<SpanData.TimedEvent<NetworkEvent>>emptyList(), 0),
160165
Links.create(Collections.<Link>emptyList(), 0),
166+
0,
161167
status,
162168
endTimestamp);
163169
assertThat(spanData.getContext()).isEqualTo(spanContext);
@@ -169,6 +175,7 @@ public void spanData_AllDataEmpty() {
169175
assertThat(spanData.getAnnotations().getEvents().isEmpty()).isTrue();
170176
assertThat(spanData.getNetworkEvents().getEvents().isEmpty()).isTrue();
171177
assertThat(spanData.getLinks().getLinks().isEmpty()).isTrue();
178+
assertThat(spanData.getChildSpanCount()).isEqualTo(0);
172179
assertThat(spanData.getStatus()).isEqualTo(status);
173180
assertThat(spanData.getEndTimestamp()).isEqualTo(endTimestamp);
174181
}
@@ -186,6 +193,7 @@ public void spanDataEquals() {
186193
annotations,
187194
networkEvents,
188195
links,
196+
CHILD_SPAN_COUNT,
189197
status,
190198
endTimestamp);
191199
SpanData allSpanData2 =
@@ -199,6 +207,7 @@ public void spanDataEquals() {
199207
annotations,
200208
networkEvents,
201209
links,
210+
CHILD_SPAN_COUNT,
202211
status,
203212
endTimestamp);
204213
SpanData emptySpanData =
@@ -212,6 +221,7 @@ public void spanDataEquals() {
212221
TimedEvents.create(Collections.<SpanData.TimedEvent<Annotation>>emptyList(), 0),
213222
TimedEvents.create(Collections.<SpanData.TimedEvent<NetworkEvent>>emptyList(), 0),
214223
Links.create(Collections.<Link>emptyList(), 0),
224+
0,
215225
status,
216226
endTimestamp);
217227
new EqualsTester()
@@ -227,12 +237,13 @@ public void spanData_ToString() {
227237
spanContext,
228238
parentSpanId,
229239
false,
230-
SPAN_NAME,
240+
SPAN_NAME,
231241
startTimestamp,
232242
attributes,
233243
annotations,
234244
networkEvents,
235245
links,
246+
CHILD_SPAN_COUNT,
236247
status,
237248
endTimestamp)
238249
.toString();

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ public SpanData toSpanData() {
220220
annotationsSpanData,
221221
networkEventsSpanData,
222222
linksSpanData,
223+
null, // Not supported yet.
223224
hasBeenEnded ? status : null,
224225
hasBeenEnded ? timestampConverter.convertNanoTime(endNanoTime) : null);
225226
}

0 commit comments

Comments
 (0)