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

Commit c30010d

Browse files
authored
set CurrentRootSpan correctly (#532)
* set CurrentRootSpan correctly * add comment * Minor fix * Fix build
1 parent a7af26c commit c30010d

File tree

4 files changed

+25
-10
lines changed

4 files changed

+25
-10
lines changed

packages/opencensus-core/src/trace/model/span.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,9 @@ export class Span implements types.Span {
305305
parentSpanId: this.parentSpanId,
306306
traceState: this.traceState
307307
});
308+
if (!this.parentSpanId) {
309+
this.tracer.setCurrentRootSpan(this);
310+
}
308311

309312
this.tracer.onStartSpan(this);
310313
}

packages/opencensus-core/src/trace/model/tracer-base.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ export class CoreTracerBase implements types.TracerBase {
6161
return noopPropagation;
6262
}
6363

64+
/** Sets the current root span. */
65+
setCurrentRootSpan(root: types.Span) {
66+
// no-op, this is only required in case of tracer with cls.
67+
}
68+
6469
/**
6570
* Starts a tracer.
6671
* @param config A tracer configuration object to start a tracer.
@@ -134,16 +139,13 @@ export class CoreTracerBase implements types.TracerBase {
134139
rootSpan.start();
135140
return fn(rootSpan);
136141
}
137-
138142
// Sampling is off
139143
this.logger.debug('Sampling is off, starting new no record root span');
140-
const noRecordRootSpan = new NoRecordRootSpan(
141-
this, name, kind, traceId, parentSpanId, traceState);
142-
return fn(noRecordRootSpan);
144+
} else {
145+
// Tracer is inactive
146+
this.logger.debug('Tracer is inactive, starting new no record root span');
143147
}
144148

145-
// Tracer is inactive
146-
this.logger.debug('Tracer is inactive, starting new no record root span');
147149
const noRecordRootSpan = new NoRecordRootSpan(
148150
this, name, kind, traceId, parentSpanId, traceState);
149151
return fn(noRecordRootSpan);

packages/opencensus-core/src/trace/model/tracer.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ export class CoreTracer extends CoreTracerBase implements types.Tracer {
4747

4848
/** Sets the current root span. */
4949
set currentRootSpan(root: types.Span) {
50+
this.setCurrentRootSpan(root);
51+
}
52+
53+
/** Sets the current root span. */
54+
setCurrentRootSpan(root: types.Span) {
5055
if (this.contextManager.active) {
5156
this.contextManager.set('rootspan', root);
5257
}
@@ -62,7 +67,6 @@ export class CoreTracer extends CoreTracerBase implements types.Tracer {
6267
const self = this;
6368
return self.contextManager.runAndReturn(() => {
6469
return super.startRootSpan(options, (root) => {
65-
self.currentRootSpan = root;
6670
return fn(root);
6771
});
6872
});
@@ -74,7 +78,8 @@ export class CoreTracer extends CoreTracerBase implements types.Tracer {
7478
if (!root) {
7579
return this.logger.debug('cannot start trace - no active trace found');
7680
}
77-
if (this.currentRootSpan !== root) {
81+
if (!this.currentRootSpan ||
82+
this.currentRootSpan.traceId !== root.traceId) {
7883
this.logger.debug(
7984
'currentRootSpan != root on notifyStart. Need more investigation.');
8085
}
@@ -88,7 +93,8 @@ export class CoreTracer extends CoreTracerBase implements types.Tracer {
8893
this.logger.debug('cannot end trace - no active trace found');
8994
return;
9095
}
91-
if (this.currentRootSpan !== root) {
96+
if (!this.currentRootSpan ||
97+
this.currentRootSpan.traceId !== root.traceId) {
9298
this.logger.debug(
9399
'currentRootSpan != root on notifyEnd. Need more investigation.');
94100
}

packages/opencensus-core/src/trace/model/types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,9 @@ export interface SpanContext {
259259

260260
/** Defines an end span event listener */
261261
export interface SpanEventListener {
262-
/** Happens when a span is ended */
262+
/** Happens when a span is started */
263263
onStartSpan(span: Span): void;
264+
/** Happens when a span is ended */
264265
onEndSpan(span: Span): void;
265266
}
266267

@@ -517,6 +518,9 @@ export interface TracerBase extends SpanEventListener {
517518
* @returns The new Span instance started
518519
*/
519520
startChildSpan(options?: SpanOptions): Span;
521+
522+
/** Sets the current root span. */
523+
setCurrentRootSpan(root: Span): void;
520524
}
521525

522526
/** Interface for Tracer */

0 commit comments

Comments
 (0)