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

Commit abd41db

Browse files
Ken Ashcraftmayurkale22
authored andcommitted
Consolidating Span and RootSpan to allow Spans to recursively have children (#441)
* refactor(tracer): consolidate root span (+10 squashed commits) Squashed commits: [a4f2234] Get the test passing [8276c98] Get allDescendants() working [379b2eb] Big refactor, tests are mostly passing [6d93312] Lerna handles cross-package dependencies [3567e2e] Rough draft of making Span and RootSpan almost identical [eba18f3] Set 'shared' based on the type of span [b56a884] Reformat tests [b1b6dfb] Use parentSpanId if provided [9d4e601] Tests that ensure nested spans are properly translated for Zipkin [585034e] Add tests for nested spans. * Add these files back * Undo spurious package-lock.json changes * test(pluginloader): re-enable tests * Address PR comments * Add tracer tests, clean ups to tracez.page-handler.ts * Allow grandchildren via tracer.startChildSpan()
1 parent fbc618b commit abd41db

34 files changed

Lines changed: 847 additions & 833 deletions

File tree

packages/opencensus-core/src/exporters/console-exporter.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ import {Exporter, ExporterConfig, StatsEventListener} from './types';
2424
/** Do not send span data */
2525
export class NoopExporter implements Exporter {
2626
logger?: loggerTypes.Logger;
27-
onStartSpan(root: modelTypes.RootSpan) {}
28-
onEndSpan(root: modelTypes.RootSpan) {}
29-
publish(rootSpans: modelTypes.RootSpan[]) {
27+
onStartSpan(root: modelTypes.Span) {}
28+
onEndSpan(root: modelTypes.Span) {}
29+
publish(rootSpans: modelTypes.Span[]) {
3030
return Promise.resolve();
3131
}
3232
}
@@ -48,21 +48,21 @@ export class ConsoleExporter implements Exporter {
4848
this.logger = config.logger;
4949
}
5050

51-
onStartSpan(root: modelTypes.RootSpan) {}
51+
onStartSpan(root: modelTypes.Span) {}
5252

5353
/**
5454
* Event called when a span is ended.
5555
* @param root Ended span.
5656
*/
57-
onEndSpan(root: modelTypes.RootSpan) {
57+
onEndSpan(root: modelTypes.Span) {
5858
this.buffer.addToBuffer(root);
5959
}
6060

6161
/**
6262
* Sends the spans information to the console.
6363
* @param rootSpans A list of root spans to publish.
6464
*/
65-
publish(rootSpans: modelTypes.RootSpan[]) {
65+
publish(rootSpans: modelTypes.Span[]) {
6666
rootSpans.map((root) => {
6767
const ROOT_STR = `RootSpan: {traceId: ${root.traceId}, spanId: ${
6868
root.id}, name: ${root.name} }`;

packages/opencensus-core/src/exporters/exporter-buffer.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export class ExporterBuffer {
3838
/** An object to log information to */
3939
private logger: loggerTypes.Logger;
4040
/** Trace queue of a buffer */
41-
private queue: modelTypes.RootSpan[] = [];
41+
private queue: modelTypes.Span[] = [];
4242

4343
/**
4444
* Constructs a new Buffer instance.
@@ -70,15 +70,15 @@ export class ExporterBuffer {
7070
return this.bufferSize;
7171
}
7272

73-
getQueue(): modelTypes.RootSpan[] {
73+
getQueue(): modelTypes.Span[] {
7474
return this.queue;
7575
}
7676

7777
/**
7878
* Add a rootSpan in the buffer.
7979
* @param root RootSpan to be added in the buffer.
8080
*/
81-
addToBuffer(root: modelTypes.RootSpan) {
81+
addToBuffer(root: modelTypes.Span) {
8282
this.queue.push(root);
8383
this.logger.debug('ExporterBuffer: added new rootspan');
8484

packages/opencensus-core/src/exporters/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export interface Exporter extends modelTypes.SpanEventListener {
2525
* Sends a list of root spans to the service.
2626
* @param rootSpans A list of root spans to publish.
2727
*/
28-
publish(rootSpans: modelTypes.RootSpan[]): Promise<number|string|void>;
28+
publish(rootSpans: modelTypes.Span[]): Promise<number|string|void>;
2929
}
3030

3131
/**

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

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,20 @@
1717
import * as logger from '../../../common/console-logger';
1818
import * as types from '../types';
1919
import {NoRecordSpan} from './no-record-span';
20-
import {NoRecordSpanBase} from './no-record-span-base';
2120

22-
/** Implementation for the RootSpan class that does not record trace events. */
23-
export class NoRecordRootSpan extends NoRecordSpanBase implements
24-
types.RootSpan {
21+
/** Implementation for the Span class that does not record trace events. */
22+
export class NoRecordRootSpan extends NoRecordSpan {
2523
/** A tracer object */
2624
private tracer: types.Tracer;
2725
/** Its trace ID. */
2826
private traceIdLocal: string;
2927
/** Its trace state. */
3028
private traceStateLocal?: types.TraceState;
31-
/** set isRootSpan = true */
32-
readonly isRootSpan = true;
29+
/**
30+
* This span's parent Id. This is a string and not a Span because the
31+
* parent was likely started on another machine.
32+
*/
33+
private parentSpanIdLocal: string;
3334

3435
/**
3536
* Constructs a new NoRecordRootSpanImpl instance.
@@ -49,23 +50,23 @@ export class NoRecordRootSpan extends NoRecordSpanBase implements
4950
this.traceIdLocal = traceId;
5051
this.name = name;
5152
this.kind = kind;
52-
this.parentSpanId = parentSpanId;
53+
this.parentSpanIdLocal = parentSpanId;
5354
if (traceState) {
5455
this.traceStateLocal = traceState;
5556
}
5657
this.logger = this.tracer.logger || logger.logger();
5758
}
5859

59-
/** No-op implementation of this method. */
60-
get spans(): types.Span[] {
61-
return [];
62-
}
63-
6460
/** No-op implementation of this method. */
6561
get traceId(): string {
6662
return this.traceIdLocal;
6763
}
6864

65+
/** Gets the ID of the parent span. */
66+
get parentSpanId(): string {
67+
return this.parentSpanIdLocal;
68+
}
69+
6970
/** No-op implementation of this method. */
7071
get traceState(): types.TraceState|undefined {
7172
return this.traceStateLocal;
@@ -85,29 +86,4 @@ export class NoRecordRootSpan extends NoRecordSpanBase implements
8586
end() {
8687
super.end();
8788
}
88-
89-
/**
90-
* Starts a new no record child span in the no record root span.
91-
* @param nameOrOptions Span name string or SpanOptions object.
92-
* @param kind Span kind if not using SpanOptions object.
93-
*/
94-
startChildSpan(
95-
nameOrOptions?: string|types.SpanOptions,
96-
kind?: types.SpanKind): types.Span {
97-
const noRecordChild = new NoRecordSpan();
98-
99-
const spanName =
100-
typeof nameOrOptions === 'object' ? nameOrOptions.name : nameOrOptions;
101-
const spanKind =
102-
typeof nameOrOptions === 'object' ? nameOrOptions.kind : kind;
103-
if (spanName) {
104-
noRecordChild.name = spanName;
105-
}
106-
if (spanKind) {
107-
noRecordChild.kind = spanKind;
108-
}
109-
110-
noRecordChild.start();
111-
return noRecordChild;
112-
}
11389
}

packages/opencensus-core/src/trace/model/no-record/no-record-span-base.ts

Lines changed: 0 additions & 151 deletions
This file was deleted.

0 commit comments

Comments
 (0)