Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement child span count #674

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/opentelemetry-exporter-collector/src/transform.ts
Expand Up @@ -197,7 +197,7 @@ export function toCollectorSpan(span: ReadableSpan): collectorTypes.Span {
status: span.status,
sameProcessAsParentSpan: !!span.parentSpanId,
links: toCollectorLinks(span),
// childSpanCount: // not implemented
childSpanCount: span.numberOfChildren,
};
}

Expand Down
12 changes: 6 additions & 6 deletions packages/opentelemetry-exporter-collector/src/types.ts
Expand Up @@ -371,12 +371,12 @@ export interface Span {
*/
sameProcessAsParentSpan?: boolean;

//@TODO - do we use it in opentelemetry or it is not needed?
// /**
// * An optional number of child spans that were generated while this span was
// * active. If set, allows an implementation to detect missing child spans.
// */
// childSpanCount?: number;
/**
* An optional number of child spans that were generated while this span was
* active. If set, allows an implementation to detect missing child spans.
*/
childSpanCount?: number;

/**
* The included links.
*/
Expand Down
2 changes: 2 additions & 0 deletions packages/opentelemetry-exporter-collector/test/helper.ts
Expand Up @@ -67,6 +67,7 @@ export const mockedReadableSpan: ReadableSpan = {
},
],
duration: [0, 8885000],
numberOfChildren: 1,
};

export function ensureSpanIsCorrect(span: collectorTypes.Span) {
Expand All @@ -79,6 +80,7 @@ export function ensureSpanIsCorrect(span: collectorTypes.Span) {
kind: 0,
startTime: '2019-11-18T23:36:05.429803070Z',
endTime: '2019-11-18T23:36:05.438688070Z',
childSpanCount: 1,
attributes: {
droppedAttributesCount: 0,
attributeMap: {
Expand Down
1 change: 1 addition & 0 deletions packages/opentelemetry-exporter-jaeger/test/jaeger.test.ts
Expand Up @@ -124,6 +124,7 @@ describe('JaegerExporter', () => {
links: [],
events: [],
duration: [32, 800000000],
numberOfChildren: 0,
};

exporter.export([readableSpan], (result: ExportResult) => {
Expand Down
3 changes: 3 additions & 0 deletions packages/opentelemetry-exporter-jaeger/test/transform.test.ts
Expand Up @@ -66,6 +66,7 @@ describe('transform', () => {
},
],
duration: [32, 800000000],
numberOfChildren: 0,
};

const thriftSpan = spanToThrift(readableSpan);
Expand Down Expand Up @@ -139,6 +140,7 @@ describe('transform', () => {
links: [],
events: [],
duration: [32, 800000000],
numberOfChildren: 0,
};

const thriftSpan = spanToThrift(readableSpan);
Expand Down Expand Up @@ -202,6 +204,7 @@ describe('transform', () => {
],
events: [],
duration: [32, 800000000],
numberOfChildren: 0,
};

const thriftSpan = spanToThrift(readableSpan);
Expand Down
3 changes: 3 additions & 0 deletions packages/opentelemetry-exporter-zipkin/test/zipkin.test.ts
Expand Up @@ -45,6 +45,7 @@ function getReadableSpan() {
attributes: {},
links: [],
events: [],
numberOfChildren: 0,
};
return readableSpan;
}
Expand Down Expand Up @@ -157,6 +158,7 @@ describe('ZipkinExporter', () => {
attributes: { key3: 'value3' },
},
],
numberOfChildren: 0,
};
const span2: ReadableSpan = {
name: 'my-span',
Expand All @@ -174,6 +176,7 @@ describe('ZipkinExporter', () => {
attributes: {},
links: [],
events: [],
numberOfChildren: 0,
};

const exporter = new ZipkinExporter({
Expand Down
1 change: 1 addition & 0 deletions packages/opentelemetry-tracing/src/BasicTracer.ts
Expand Up @@ -184,6 +184,7 @@ export class BasicTracer implements types.Tracer {
}

if (typeof (parent as types.Span).context === 'function') {
(parent as Span).addChild();
return (parent as Span).context();
}
return undefined;
Expand Down
6 changes: 6 additions & 0 deletions packages/opentelemetry-tracing/src/Span.ts
Expand Up @@ -44,6 +44,7 @@ export class Span implements types.Span, ReadableSpan {
code: types.CanonicalCode.OK,
};
endTime: types.HrTime = [0, 0];
numberOfChildren: number = 0;
private _ended = false;
private _duration: types.HrTime = [-1, -1];
private readonly _logger: types.Logger;
Expand Down Expand Up @@ -180,6 +181,11 @@ export class Span implements types.Span, ReadableSpan {
return this._duration;
}

addChild(): void {
if (this._ended) return;
this.numberOfChildren++;
}

private _isSpanEnded(): boolean {
if (this._ended) {
this._logger.warn(
Expand Down
1 change: 1 addition & 0 deletions packages/opentelemetry-tracing/src/export/ReadableSpan.ts
Expand Up @@ -36,4 +36,5 @@ export interface ReadableSpan {
readonly links: Link[];
readonly events: TimedEvent[];
readonly duration: HrTime;
readonly numberOfChildren: number;
}
27 changes: 27 additions & 0 deletions packages/opentelemetry-tracing/test/BasicTracer.test.ts
Expand Up @@ -211,6 +211,33 @@ describe('BasicTracer', () => {
childSpan.end();
});

it('should record the child count spans', () => {
const tracer = new BasicTracer();
const span = tracer.startSpan('my-span');
const childSpan = tracer.startSpan('child-span', {
parent: span,
});
assert.strictEqual((span as Span).numberOfChildren, 1);
const grandChildSpan = tracer.startSpan('grand-child-span', {
parent: span,
});
assert.strictEqual((span as Span).numberOfChildren, 2);
grandChildSpan.end();
span.end();
childSpan.end();
});

it('should not record the child span count when span is ended', () => {
const tracer = new BasicTracer();
const span = tracer.startSpan('my-span');
span.end();
const childSpan = tracer.startSpan('child-span', {
parent: span,
});
childSpan.end();
assert.strictEqual((span as Span).numberOfChildren, 0);
});

it('should start a span with name and with invalid parent span', () => {
const tracer = new BasicTracer();
const span = tracer.startSpan('my-span', {
Expand Down