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

Commit 608943d

Browse files
authored
Add support for child span count (#332)
* Add support for child span count * remove extra line * remove unwanted check for child count
1 parent 0f76085 commit 608943d

3 files changed

Lines changed: 27 additions & 1 deletion

File tree

packages/opencensus-core/src/trace/model/root-span.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ export class RootSpan extends SpanBase implements types.RootSpan {
3333
private traceStateLocal: types.TraceState;
3434
/** set isRootSpan = true */
3535
readonly isRootSpan = true;
36+
/** A number of children. */
37+
private numberOfChildrenLocal: number;
3638

3739
/**
3840
* Constructs a new RootSpanImpl instance.
@@ -56,6 +58,7 @@ export class RootSpan extends SpanBase implements types.RootSpan {
5658
context && context.kind ? context.kind : types.SpanKind.UNSPECIFIED;
5759
this.logger = tracer.logger || logger.logger();
5860
this.activeTraceParams = tracer.activeTraceParams;
61+
this.numberOfChildrenLocal = 0;
5962
}
6063

6164
/** Gets span list from rootspan instance. */
@@ -73,6 +76,11 @@ export class RootSpan extends SpanBase implements types.RootSpan {
7376
return this.traceStateLocal;
7477
}
7578

79+
/** Gets the number of child span created for this span. */
80+
get numberOfChildren(): number {
81+
return this.numberOfChildrenLocal;
82+
}
83+
7684
/** Starts a rootspan instance. */
7785
start() {
7886
super.start();
@@ -121,6 +129,7 @@ export class RootSpan extends SpanBase implements types.RootSpan {
121129
this.className, {id: this.id, name: this.name, kind: this.kind});
122130
return null;
123131
}
132+
this.numberOfChildrenLocal++;
124133
const newSpan = new Span(this);
125134
let spanName;
126135
let spanKind;

packages/opencensus-core/src/trace/model/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,9 @@ export interface RootSpan extends Span {
458458
/** Get the span list from RootSpan instance */
459459
readonly spans: Span[];
460460

461+
/** Gets the number of child span created for this span. */
462+
readonly numberOfChildren: number;
463+
461464
/** Starts a new Span instance in the RootSpan instance */
462465
startChildSpan(name?: string, kind?: SpanKind, parentSpanId?: string): Span;
463466
startChildSpan(options?: SpanOptions): Span;

packages/opencensus-core/test/test-root-span.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,21 @@ describe('RootSpan', () => {
7070
});
7171
});
7272

73+
describe('get numberOfChildren()', () => {
74+
it('should get numberOfChildren from rootspan instance', () => {
75+
const root = new RootSpan(tracer);
76+
root.start();
77+
assert.equal(root.numberOfChildren, 0);
78+
root.startChildSpan('spanName', types.SpanKind.UNSPECIFIED);
79+
assert.equal(root.numberOfChildren, 1);
80+
81+
for (let i = 0; i < 10; i++) {
82+
root.startChildSpan('spanName' + i, types.SpanKind.UNSPECIFIED);
83+
}
84+
assert.equal(root.numberOfChildren, 11);
85+
});
86+
});
87+
7388
/**
7489
* Should get trace id from rootspan instance
7590
*/
@@ -209,7 +224,6 @@ describe('RootSpan', () => {
209224
rootSpan.addAnnotation('description test', {} as Attributes, Date.now());
210225

211226
assert.ok(rootSpan.annotations.length > 0);
212-
assert.equal(rootSpan.droppedAnnotationsCount, 0);
213227
assert.ok(instanceOfAnnotation(rootSpan.annotations[0]));
214228
});
215229

0 commit comments

Comments
 (0)