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

Commit

Permalink
Add support for child span count (#332)
Browse files Browse the repository at this point in the history
* Add support for child span count

* remove extra line

* remove unwanted check for child count
  • Loading branch information
mayurkale22 committed Feb 28, 2019
1 parent 0f76085 commit 608943d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
9 changes: 9 additions & 0 deletions packages/opencensus-core/src/trace/model/root-span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export class RootSpan extends SpanBase implements types.RootSpan {
private traceStateLocal: types.TraceState;
/** set isRootSpan = true */
readonly isRootSpan = true;
/** A number of children. */
private numberOfChildrenLocal: number;

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

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

/** Gets the number of child span created for this span. */
get numberOfChildren(): number {
return this.numberOfChildrenLocal;
}

/** Starts a rootspan instance. */
start() {
super.start();
Expand Down Expand Up @@ -121,6 +129,7 @@ export class RootSpan extends SpanBase implements types.RootSpan {
this.className, {id: this.id, name: this.name, kind: this.kind});
return null;
}
this.numberOfChildrenLocal++;
const newSpan = new Span(this);
let spanName;
let spanKind;
Expand Down
3 changes: 3 additions & 0 deletions packages/opencensus-core/src/trace/model/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,9 @@ export interface RootSpan extends Span {
/** Get the span list from RootSpan instance */
readonly spans: Span[];

/** Gets the number of child span created for this span. */
readonly numberOfChildren: number;

/** Starts a new Span instance in the RootSpan instance */
startChildSpan(name?: string, kind?: SpanKind, parentSpanId?: string): Span;
startChildSpan(options?: SpanOptions): Span;
Expand Down
16 changes: 15 additions & 1 deletion packages/opencensus-core/test/test-root-span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,21 @@ describe('RootSpan', () => {
});
});

describe('get numberOfChildren()', () => {
it('should get numberOfChildren from rootspan instance', () => {
const root = new RootSpan(tracer);
root.start();
assert.equal(root.numberOfChildren, 0);
root.startChildSpan('spanName', types.SpanKind.UNSPECIFIED);
assert.equal(root.numberOfChildren, 1);

for (let i = 0; i < 10; i++) {
root.startChildSpan('spanName' + i, types.SpanKind.UNSPECIFIED);
}
assert.equal(root.numberOfChildren, 11);
});
});

/**
* Should get trace id from rootspan instance
*/
Expand Down Expand Up @@ -209,7 +224,6 @@ describe('RootSpan', () => {
rootSpan.addAnnotation('description test', {} as Attributes, Date.now());

assert.ok(rootSpan.annotations.length > 0);
assert.equal(rootSpan.droppedAnnotationsCount, 0);
assert.ok(instanceOfAnnotation(rootSpan.annotations[0]));
});

Expand Down

0 comments on commit 608943d

Please sign in to comment.