diff --git a/packages/opencensus-core/src/trace/model/no-record/no-record-root-span.ts b/packages/opencensus-core/src/trace/model/no-record/no-record-root-span.ts index 0e821ad1a..1ca54c4e1 100644 --- a/packages/opencensus-core/src/trace/model/no-record/no-record-root-span.ts +++ b/packages/opencensus-core/src/trace/model/no-record/no-record-root-span.ts @@ -20,8 +20,6 @@ import { NoRecordSpan } from './no-record-span'; /** Implementation for the Span class that does not record trace events. */ export class NoRecordRootSpan extends NoRecordSpan { - /** A tracer object */ - private tracer: types.TracerBase; /** Its trace ID. */ private traceIdLocal: string; /** Its trace state. */ @@ -31,6 +29,8 @@ export class NoRecordRootSpan extends NoRecordSpan { * parent was likely started on another machine. */ private parentSpanIdLocal: string; + /** A tracer object */ + tracer: types.TracerBase; /** * Constructs a new NoRecordRootSpanImpl instance. @@ -50,7 +50,7 @@ export class NoRecordRootSpan extends NoRecordSpan { parentSpanId: string, traceState?: types.TraceState ) { - super(); + super(tracer); this.tracer = tracer; this.traceIdLocal = traceId; this.name = name; diff --git a/packages/opencensus-core/src/trace/model/no-record/no-record-span.ts b/packages/opencensus-core/src/trace/model/no-record/no-record-span.ts index 270ee94d3..a4a9cddad 100644 --- a/packages/opencensus-core/src/trace/model/no-record/no-record-span.ts +++ b/packages/opencensus-core/src/trace/model/no-record/no-record-span.ts @@ -31,6 +31,8 @@ export class NoRecordSpan implements types.Span { private endedLocal = false; /** The Span ID of this span */ readonly id: string; + /** A tracer object */ + tracer: types.TracerBase; /** An object to log information to */ logger: Logger = noopLogger; /** A set of attributes, each in the format [KEY]:[VALUE] */ @@ -66,7 +68,8 @@ export class NoRecordSpan implements types.Span { droppedMessageEventsCount = 0; /** Constructs a new SpanBaseModel instance. */ - constructor(parent?: NoRecordSpan) { + constructor(tracer: types.TracerBase, parent?: NoRecordSpan) { + this.tracer = tracer; this.id = randomSpanId(); if (parent) { this.root = parent.root; @@ -199,7 +202,7 @@ export class NoRecordSpan implements types.Span { * @param [options] A SpanOptions object to start a child span. */ startChildSpan(options?: types.SpanOptions): types.Span { - const noRecordChild = new NoRecordSpan(this); + const noRecordChild = new NoRecordSpan(this.tracer, this); if (options && options.name) noRecordChild.name = options.name; if (options && options.kind) noRecordChild.kind = options.kind; noRecordChild.start(); diff --git a/packages/opencensus-core/src/trace/model/root-span.ts b/packages/opencensus-core/src/trace/model/root-span.ts index 784951eb3..c3ef2f6ea 100644 --- a/packages/opencensus-core/src/trace/model/root-span.ts +++ b/packages/opencensus-core/src/trace/model/root-span.ts @@ -29,6 +29,8 @@ export class RootSpan extends Span { * parent was likely started on another machine. */ private parentSpanIdLocal: string; + /** A tracer object */ + tracer: types.TracerBase; /** * Constructs a new RootSpanImpl instance. @@ -49,6 +51,7 @@ export class RootSpan extends Span { traceState?: types.TraceState ) { super(tracer); + this.tracer = tracer; this.traceIdLocal = traceId; this.name = name; this.kind = kind; diff --git a/packages/opencensus-core/src/trace/model/span.ts b/packages/opencensus-core/src/trace/model/span.ts index a285b761a..5bf24b0e7 100644 --- a/packages/opencensus-core/src/trace/model/span.ts +++ b/packages/opencensus-core/src/trace/model/span.ts @@ -29,8 +29,6 @@ const STATUS_OK = { /** Defines a base model for spans. */ export class Span implements types.Span { protected className: string; - /** A tracer object */ - private tracer: types.TracerBase; /** The clock used to mesure the beginning and ending of a span */ private clock!: Clock; /** Indicates if this span was started */ @@ -41,6 +39,8 @@ export class Span implements types.Span { private spansLocal: types.Span[]; /** The Span ID of this span */ readonly id: string; + /** A tracer object */ + tracer: types.TracerBase; /** An object to log information to */ logger: Logger = noopLogger; /** A set of attributes, each in the format [KEY]:[VALUE] */ @@ -386,7 +386,7 @@ export class Span implements types.Span { this.className, { id: this.id, name: this.name, kind: this.kind } ); - return new NoRecordSpan(); + return new NoRecordSpan(this.tracer); } if (!this.started) { this.logger.debug( @@ -395,7 +395,7 @@ export class Span implements types.Span { this.className, { id: this.id, name: this.name, kind: this.kind } ); - return new NoRecordSpan(); + return new NoRecordSpan(this.tracer); } const child = new Span(this.tracer, this); diff --git a/packages/opencensus-core/src/trace/model/tracer-base.ts b/packages/opencensus-core/src/trace/model/tracer-base.ts index 8a80a0621..826498587 100644 --- a/packages/opencensus-core/src/trace/model/tracer-base.ts +++ b/packages/opencensus-core/src/trace/model/tracer-base.ts @@ -231,7 +231,7 @@ export class CoreTracerBase implements types.TracerBase { this.logger.debug( 'no current trace found - must start a new root span first' ); - return new NoRecordSpan(); + return new NoRecordSpan(this); } const span = options.childOf.startChildSpan(options); diff --git a/packages/opencensus-core/src/trace/model/tracer.ts b/packages/opencensus-core/src/trace/model/tracer.ts index 90425c6e9..bf1f0657b 100644 --- a/packages/opencensus-core/src/trace/model/tracer.ts +++ b/packages/opencensus-core/src/trace/model/tracer.ts @@ -121,7 +121,7 @@ export class CoreTracer extends CoreTracerBase implements types.Tracer { return super.startChildSpan( Object.assign( - { childOf: this.currentRootSpan || new NoRecordSpan() }, + { childOf: this.currentRootSpan || new NoRecordSpan(this) }, options ) ); diff --git a/packages/opencensus-core/src/trace/model/types.ts b/packages/opencensus-core/src/trace/model/types.ts index 68bf5451a..35d7b9d1f 100644 --- a/packages/opencensus-core/src/trace/model/types.ts +++ b/packages/opencensus-core/src/trace/model/types.ts @@ -315,6 +315,11 @@ export interface Span { /** The Span ID of this span */ readonly id: string; + /** A tracer object, exposong the tracer makes it possible to create child + * spans from the span instance like. span.tracer.startChildSpan() + */ + tracer: TracerBase; + /** If the parent span is in another process. */ remoteParent: boolean; diff --git a/packages/opencensus-core/test/test-no-record-span.ts b/packages/opencensus-core/test/test-no-record-span.ts index 0e63aa480..6aff94e09 100644 --- a/packages/opencensus-core/test/test-no-record-span.ts +++ b/packages/opencensus-core/test/test-no-record-span.ts @@ -15,12 +15,14 @@ */ import * as assert from 'assert'; +import { CoreTracerBase } from '../src/trace/model/tracer-base'; import { CanonicalCode, LinkType, MessageEventType } from '../src'; import { NoRecordSpan } from '../src/trace/model/no-record/no-record-span'; describe('NoRecordSpan()', () => { it('do not crash', () => { - const noRecordSpan = new NoRecordSpan(); + const tracer = new CoreTracerBase(); + const noRecordSpan = new NoRecordSpan(tracer); noRecordSpan.addAnnotation('MyAnnotation'); noRecordSpan.addAnnotation('MyAnnotation', { myString: 'bar' }); noRecordSpan.addAnnotation('MyAnnotation', {