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

Commit

Permalink
feat(span): make tracer available on span
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Marton committed Jul 13, 2019
1 parent 5b8165e commit 5bca0ab
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand All @@ -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.
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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] */
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
3 changes: 3 additions & 0 deletions packages/opencensus-core/src/trace/model/root-span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions packages/opencensus-core/src/trace/model/span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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] */
Expand Down Expand Up @@ -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(
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion packages/opencensus-core/src/trace/model/tracer-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion packages/opencensus-core/src/trace/model/tracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
);
Expand Down
5 changes: 5 additions & 0 deletions packages/opencensus-core/src/trace/model/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 3 additions & 1 deletion packages/opencensus-core/test/test-no-record-span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand Down

0 comments on commit 5bca0ab

Please sign in to comment.