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

Commit

Permalink
set CurrentRootSpan correctly (#532)
Browse files Browse the repository at this point in the history
* set CurrentRootSpan correctly

* add comment

* Minor fix

* Fix build
  • Loading branch information
mayurkale22 committed May 17, 2019
1 parent a7af26c commit c30010d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
3 changes: 3 additions & 0 deletions packages/opencensus-core/src/trace/model/span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
14 changes: 8 additions & 6 deletions packages/opencensus-core/src/trace/model/tracer-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ export class CoreTracerBase implements types.TracerBase {
return noopPropagation;
}

/** Sets the current root span. */
setCurrentRootSpan(root: types.Span) {
// no-op, this is only required in case of tracer with cls.
}

/**
* Starts a tracer.
* @param config A tracer configuration object to start a tracer.
Expand Down Expand Up @@ -134,16 +139,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);
Expand Down
12 changes: 9 additions & 3 deletions packages/opencensus-core/src/trace/model/tracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
});
});
Expand All @@ -74,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 !== root) {
if (!this.currentRootSpan ||
this.currentRootSpan.traceId !== root.traceId) {
this.logger.debug(
'currentRootSpan != root on notifyStart. Need more investigation.');
}
Expand All @@ -88,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 !== root) {
if (!this.currentRootSpan ||
this.currentRootSpan.traceId !== root.traceId) {
this.logger.debug(
'currentRootSpan != root on notifyEnd. Need more investigation.');
}
Expand Down
6 changes: 5 additions & 1 deletion packages/opencensus-core/src/trace/model/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -517,6 +518,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 */
Expand Down

0 comments on commit c30010d

Please sign in to comment.