From 72cad103f1ef856d01afa475e7cfa8a62dedcf40 Mon Sep 17 00:00:00 2001 From: Mayur Kale Date: Thu, 16 May 2019 11:32:09 -0700 Subject: [PATCH 1/4] set CurrentRootSpan correctly --- packages/opencensus-core/src/trace/model/span.ts | 3 +++ .../opencensus-core/src/trace/model/tracer-base.ts | 12 ++++++------ packages/opencensus-core/src/trace/model/tracer.ts | 10 +++++++--- packages/opencensus-core/src/trace/model/types.ts | 3 +++ 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/packages/opencensus-core/src/trace/model/span.ts b/packages/opencensus-core/src/trace/model/span.ts index f06dbe423..7f3227a1c 100644 --- a/packages/opencensus-core/src/trace/model/span.ts +++ b/packages/opencensus-core/src/trace/model/span.ts @@ -305,6 +305,9 @@ export class Span implements types.Span { parentSpanId: this.parentSpanId, traceState: this.traceState }); + if (!this.parentSpanId) { + this.tracer.setCurrentRootSpan(this); + } this.tracer.onStartSpan(this); } diff --git a/packages/opencensus-core/src/trace/model/tracer-base.ts b/packages/opencensus-core/src/trace/model/tracer-base.ts index 98be890b4..baca12e7a 100644 --- a/packages/opencensus-core/src/trace/model/tracer-base.ts +++ b/packages/opencensus-core/src/trace/model/tracer-base.ts @@ -61,6 +61,9 @@ export class CoreTracerBase implements types.TracerBase { return noopPropagation; } + /** Sets the current root span. */ + setCurrentRootSpan(root: types.Span) {} + /** * Starts a tracer. * @param config A tracer configuration object to start a tracer. @@ -134,16 +137,13 @@ export class CoreTracerBase implements types.TracerBase { rootSpan.start(); return fn(rootSpan); } - // Sampling is off this.logger.debug('Sampling is off, starting new no record root span'); - const noRecordRootSpan = new NoRecordRootSpan( - this, name, kind, traceId, parentSpanId, traceState); - return fn(noRecordRootSpan); + } else { + // Tracer is inactive + this.logger.debug('Tracer is inactive, starting new no record root span'); } - // Tracer is inactive - this.logger.debug('Tracer is inactive, starting new no record root span'); const noRecordRootSpan = new NoRecordRootSpan( this, name, kind, traceId, parentSpanId, traceState); return fn(noRecordRootSpan); diff --git a/packages/opencensus-core/src/trace/model/tracer.ts b/packages/opencensus-core/src/trace/model/tracer.ts index cc64a54ae..32f949b3c 100644 --- a/packages/opencensus-core/src/trace/model/tracer.ts +++ b/packages/opencensus-core/src/trace/model/tracer.ts @@ -47,6 +47,11 @@ export class CoreTracer extends CoreTracerBase implements types.Tracer { /** Sets the current root span. */ set currentRootSpan(root: types.Span) { + this.setCurrentRootSpan(root); + } + + /** Sets the current root span. */ + setCurrentRootSpan(root: types.Span) { if (this.contextManager.active) { this.contextManager.set('rootspan', root); } @@ -62,7 +67,6 @@ export class CoreTracer extends CoreTracerBase implements types.Tracer { const self = this; return self.contextManager.runAndReturn(() => { return super.startRootSpan(options, (root) => { - self.currentRootSpan = root; return fn(root); }); }); @@ -74,7 +78,7 @@ export class CoreTracer extends CoreTracerBase implements types.Tracer { if (!root) { return this.logger.debug('cannot start trace - no active trace found'); } - if (this.currentRootSpan !== root) { + if (this.currentRootSpan.traceId !== root.traceId) { this.logger.debug( 'currentRootSpan != root on notifyStart. Need more investigation.'); } @@ -88,7 +92,7 @@ export class CoreTracer extends CoreTracerBase implements types.Tracer { this.logger.debug('cannot end trace - no active trace found'); return; } - if (this.currentRootSpan !== root) { + if (this.currentRootSpan.traceId !== root.traceId) { this.logger.debug( 'currentRootSpan != root on notifyEnd. Need more investigation.'); } diff --git a/packages/opencensus-core/src/trace/model/types.ts b/packages/opencensus-core/src/trace/model/types.ts index 90ee34f8a..1b32afa0c 100644 --- a/packages/opencensus-core/src/trace/model/types.ts +++ b/packages/opencensus-core/src/trace/model/types.ts @@ -517,6 +517,9 @@ export interface TracerBase extends SpanEventListener { * @returns The new Span instance started */ startChildSpan(options?: SpanOptions): Span; + + /** Sets the current root span. */ + setCurrentRootSpan(root: Span): void; } /** Interface for Tracer */ From b7fdcede3567d4904ef2af2db799afaff1e966a9 Mon Sep 17 00:00:00 2001 From: Mayur Kale Date: Thu, 16 May 2019 11:38:14 -0700 Subject: [PATCH 2/4] add comment --- packages/opencensus-core/src/trace/model/tracer-base.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/opencensus-core/src/trace/model/tracer-base.ts b/packages/opencensus-core/src/trace/model/tracer-base.ts index baca12e7a..a110750a8 100644 --- a/packages/opencensus-core/src/trace/model/tracer-base.ts +++ b/packages/opencensus-core/src/trace/model/tracer-base.ts @@ -62,7 +62,9 @@ export class CoreTracerBase implements types.TracerBase { } /** Sets the current root span. */ - setCurrentRootSpan(root: types.Span) {} + setCurrentRootSpan(root: types.Span) { + // no-op, this is only required in case of tracer with cls. + } /** * Starts a tracer. From e51d1b9acbd6f43932082da12020e497abbcdf79 Mon Sep 17 00:00:00 2001 From: Mayur Kale Date: Thu, 16 May 2019 12:14:57 -0700 Subject: [PATCH 3/4] Minor fix --- packages/opencensus-core/src/trace/model/types.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/opencensus-core/src/trace/model/types.ts b/packages/opencensus-core/src/trace/model/types.ts index 1b32afa0c..c613bd99e 100644 --- a/packages/opencensus-core/src/trace/model/types.ts +++ b/packages/opencensus-core/src/trace/model/types.ts @@ -259,8 +259,9 @@ export interface SpanContext { /** Defines an end span event listener */ export interface SpanEventListener { - /** Happens when a span is ended */ + /** Happens when a span is started */ onStartSpan(span: Span): void; + /** Happens when a span is ended */ onEndSpan(span: Span): void; } From 471538c4e5e612a1b3866f7a2d3a10f99437dd44 Mon Sep 17 00:00:00 2001 From: Mayur Kale Date: Thu, 16 May 2019 14:53:11 -0700 Subject: [PATCH 4/4] Fix build --- packages/opencensus-core/src/trace/model/tracer.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/opencensus-core/src/trace/model/tracer.ts b/packages/opencensus-core/src/trace/model/tracer.ts index 32f949b3c..993f6b08f 100644 --- a/packages/opencensus-core/src/trace/model/tracer.ts +++ b/packages/opencensus-core/src/trace/model/tracer.ts @@ -78,7 +78,8 @@ export class CoreTracer extends CoreTracerBase implements types.Tracer { if (!root) { return this.logger.debug('cannot start trace - no active trace found'); } - if (this.currentRootSpan.traceId !== root.traceId) { + if (!this.currentRootSpan || + this.currentRootSpan.traceId !== root.traceId) { this.logger.debug( 'currentRootSpan != root on notifyStart. Need more investigation.'); } @@ -92,7 +93,8 @@ export class CoreTracer extends CoreTracerBase implements types.Tracer { this.logger.debug('cannot end trace - no active trace found'); return; } - if (this.currentRootSpan.traceId !== root.traceId) { + if (!this.currentRootSpan || + this.currentRootSpan.traceId !== root.traceId) { this.logger.debug( 'currentRootSpan != root on notifyEnd. Need more investigation.'); }