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

Commit ad6c0d4

Browse files
Peter Martonmayurkale22
authored andcommitted
feat(span): make tracer available on span (#629)
* feat(span): make tracer available on span * refactor(span): readonly tracer property * docs(changelog): document span tracer property
1 parent 5b8165e commit ad6c0d4

File tree

9 files changed

+26
-12
lines changed

9 files changed

+26
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
All notable changes to this project will be documented in this file.
44

55
## Unreleased
6+
- Feat, make `tracer` property available on spans.
67

78
## 0.0.15 - 2019-07-09
89
- Update dependency codecov to 3.5 (#612)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ import { NoRecordSpan } from './no-record-span';
2020

2121
/** Implementation for the Span class that does not record trace events. */
2222
export class NoRecordRootSpan extends NoRecordSpan {
23-
/** A tracer object */
24-
private tracer: types.TracerBase;
2523
/** Its trace ID. */
2624
private traceIdLocal: string;
2725
/** Its trace state. */
@@ -31,6 +29,8 @@ export class NoRecordRootSpan extends NoRecordSpan {
3129
* parent was likely started on another machine.
3230
*/
3331
private parentSpanIdLocal: string;
32+
/** A tracer object */
33+
readonly tracer: types.TracerBase;
3434

3535
/**
3636
* Constructs a new NoRecordRootSpanImpl instance.
@@ -50,7 +50,7 @@ export class NoRecordRootSpan extends NoRecordSpan {
5050
parentSpanId: string,
5151
traceState?: types.TraceState
5252
) {
53-
super();
53+
super(tracer);
5454
this.tracer = tracer;
5555
this.traceIdLocal = traceId;
5656
this.name = name;

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ export class NoRecordSpan implements types.Span {
3131
private endedLocal = false;
3232
/** The Span ID of this span */
3333
readonly id: string;
34+
/** A tracer object */
35+
readonly tracer: types.TracerBase;
3436
/** An object to log information to */
3537
logger: Logger = noopLogger;
3638
/** A set of attributes, each in the format [KEY]:[VALUE] */
@@ -66,7 +68,8 @@ export class NoRecordSpan implements types.Span {
6668
droppedMessageEventsCount = 0;
6769

6870
/** Constructs a new SpanBaseModel instance. */
69-
constructor(parent?: NoRecordSpan) {
71+
constructor(tracer: types.TracerBase, parent?: NoRecordSpan) {
72+
this.tracer = tracer;
7073
this.id = randomSpanId();
7174
if (parent) {
7275
this.root = parent.root;
@@ -199,7 +202,7 @@ export class NoRecordSpan implements types.Span {
199202
* @param [options] A SpanOptions object to start a child span.
200203
*/
201204
startChildSpan(options?: types.SpanOptions): types.Span {
202-
const noRecordChild = new NoRecordSpan(this);
205+
const noRecordChild = new NoRecordSpan(this.tracer, this);
203206
if (options && options.name) noRecordChild.name = options.name;
204207
if (options && options.kind) noRecordChild.kind = options.kind;
205208
noRecordChild.start();

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ export class RootSpan extends Span {
2929
* parent was likely started on another machine.
3030
*/
3131
private parentSpanIdLocal: string;
32+
/** A tracer object */
33+
readonly tracer: types.TracerBase;
3234

3335
/**
3436
* Constructs a new RootSpanImpl instance.
@@ -49,6 +51,7 @@ export class RootSpan extends Span {
4951
traceState?: types.TraceState
5052
) {
5153
super(tracer);
54+
this.tracer = tracer;
5255
this.traceIdLocal = traceId;
5356
this.name = name;
5457
this.kind = kind;

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ const STATUS_OK = {
2929
/** Defines a base model for spans. */
3030
export class Span implements types.Span {
3131
protected className: string;
32-
/** A tracer object */
33-
private tracer: types.TracerBase;
3432
/** The clock used to mesure the beginning and ending of a span */
3533
private clock!: Clock;
3634
/** Indicates if this span was started */
@@ -41,6 +39,8 @@ export class Span implements types.Span {
4139
private spansLocal: types.Span[];
4240
/** The Span ID of this span */
4341
readonly id: string;
42+
/** A tracer object */
43+
readonly tracer: types.TracerBase;
4444
/** An object to log information to */
4545
logger: Logger = noopLogger;
4646
/** A set of attributes, each in the format [KEY]:[VALUE] */
@@ -386,7 +386,7 @@ export class Span implements types.Span {
386386
this.className,
387387
{ id: this.id, name: this.name, kind: this.kind }
388388
);
389-
return new NoRecordSpan();
389+
return new NoRecordSpan(this.tracer);
390390
}
391391
if (!this.started) {
392392
this.logger.debug(
@@ -395,7 +395,7 @@ export class Span implements types.Span {
395395
this.className,
396396
{ id: this.id, name: this.name, kind: this.kind }
397397
);
398-
return new NoRecordSpan();
398+
return new NoRecordSpan(this.tracer);
399399
}
400400

401401
const child = new Span(this.tracer, this);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ export class CoreTracerBase implements types.TracerBase {
231231
this.logger.debug(
232232
'no current trace found - must start a new root span first'
233233
);
234-
return new NoRecordSpan();
234+
return new NoRecordSpan(this);
235235
}
236236
const span = options.childOf.startChildSpan(options);
237237

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ export class CoreTracer extends CoreTracerBase implements types.Tracer {
121121

122122
return super.startChildSpan(
123123
Object.assign(
124-
{ childOf: this.currentRootSpan || new NoRecordSpan() },
124+
{ childOf: this.currentRootSpan || new NoRecordSpan(this) },
125125
options
126126
)
127127
);

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,11 @@ export interface Span {
315315
/** The Span ID of this span */
316316
readonly id: string;
317317

318+
/** A tracer object, exposong the tracer makes it possible to create child
319+
* spans from the span instance like. span.tracer.startChildSpan()
320+
*/
321+
tracer: TracerBase;
322+
318323
/** If the parent span is in another process. */
319324
remoteParent: boolean;
320325

packages/opencensus-core/test/test-no-record-span.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515
*/
1616

1717
import * as assert from 'assert';
18+
import { CoreTracerBase } from '../src/trace/model/tracer-base';
1819
import { CanonicalCode, LinkType, MessageEventType } from '../src';
1920
import { NoRecordSpan } from '../src/trace/model/no-record/no-record-span';
2021

2122
describe('NoRecordSpan()', () => {
2223
it('do not crash', () => {
23-
const noRecordSpan = new NoRecordSpan();
24+
const tracer = new CoreTracerBase();
25+
const noRecordSpan = new NoRecordSpan(tracer);
2426
noRecordSpan.addAnnotation('MyAnnotation');
2527
noRecordSpan.addAnnotation('MyAnnotation', { myString: 'bar' });
2628
noRecordSpan.addAnnotation('MyAnnotation', {

0 commit comments

Comments
 (0)