Skip to content

Commit

Permalink
fix: pass SpanContext to span instead of destructuring
Browse files Browse the repository at this point in the history
  • Loading branch information
mayurkale22 committed Aug 2, 2019
1 parent 4f362fd commit 36fe796
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 37 deletions.
16 changes: 3 additions & 13 deletions packages/opentelemetry-basic-tracer/src/BasicTracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,34 +54,24 @@ export class BasicTracer implements types.Tracer {
* decision.
*/
startSpan(name: string, options: types.SpanOptions = {}): types.Span {
const parentSpanContext = this._getParentSpanContext(options.parent);
let parentSpanContext = this._getParentSpanContext(options.parent);
// make sampling decision
if (!this._sampler.shouldSample(parentSpanContext)) {
// TODO: propagate SpanContext, for more information see
// https://github.com/open-telemetry/opentelemetry-js/pull/99#issuecomment-513325536
return NOOP_SPAN;
}

let traceId;
let parentSpanId;
let traceState;
if (!parentSpanContext || !isValid(parentSpanContext)) {
// New root span.
traceId = randomTraceId();
} else {
// New child span.
traceId = parentSpanContext.traceId;
parentSpanId = parentSpanContext.spanId;
traceState = parentSpanContext.traceState;
parentSpanContext = { traceId: randomTraceId(), spanId: '' };
}

const span = new Span(
this,
name,
traceId,
parentSpanContext,
options.kind || types.SpanKind.INTERNAL,
parentSpanId,
traceState,
options.startTime
);
// Set default attributes
Expand Down
12 changes: 5 additions & 7 deletions packages/opentelemetry-basic-tracer/src/Span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import * as types from '@opentelemetry/types';
import { randomSpanId } from '@opentelemetry/core';
import { performance } from 'perf_hooks';
import { TraceOptions, SpanKind, TraceState } from '@opentelemetry/types';
import { TraceOptions, SpanKind, SpanContext } from '@opentelemetry/types';

/**
* This class represents a span.
Expand All @@ -42,21 +42,19 @@ export class Span implements types.Span {
constructor(
parentTracer: types.Tracer,
spanName: string,
traceId: string,
parentSpanContext: SpanContext,
kind: SpanKind,
parentSpanId?: string,
traceState?: TraceState,
startTime?: number
) {
this._tracer = parentTracer;
this._name = spanName;
this._spanContext = {
traceId,
traceId: parentSpanContext.traceId,
spanId: randomSpanId(),
traceOptions: TraceOptions.SAMPLED,
traceState,
traceState: parentSpanContext.traceState,
};
this._parentId = parentSpanId;
this._parentId = parentSpanContext.spanId;
this._kind = kind;
this._startTime = startTime || performance.now();
}
Expand Down
34 changes: 17 additions & 17 deletions packages/opentelemetry-basic-tracer/test/Span.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,35 @@ import { NoopTracer } from '@opentelemetry/core';
describe('Span', () => {
const tracer = new NoopTracer();
const name = 'span1';
const traceId = 'd4cda95b652f4a1592b449d5929fda1b';
const parentSpanId = '5d0c63257de34c92';
const parentSpanContext: SpanContext = {
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
spanId: '6e0c63257de34c92',
traceOptions: TraceOptions.SAMPLED,
};

it('should create a Span instance', () => {
const span = new Span(tracer, name, traceId, SpanKind.SERVER);
const span = new Span(tracer, name, parentSpanContext, SpanKind.SERVER);
assert.ok(span instanceof Span);
assert.strictEqual(span.tracer(), tracer);
});

it('should get the span context of span', () => {
const span = new Span(tracer, name, traceId, SpanKind.CLIENT, parentSpanId);
const span = new Span(tracer, name, parentSpanContext, SpanKind.CLIENT);
const context = span.context();
assert.strictEqual(context.traceId, traceId);
assert.strictEqual(context.traceId, parentSpanContext.traceId);
assert.strictEqual(context.traceOptions, TraceOptions.SAMPLED);
assert.strictEqual(context.traceState, undefined);
assert.ok(context.spanId.match(/[a-f0-9]{16}/));
assert.ok(span.isRecordingEvents());
});

it('should return true when isRecordingEvents:true', () => {
const span = new Span(tracer, name, traceId, SpanKind.CLIENT);
const span = new Span(tracer, name, parentSpanContext, SpanKind.CLIENT);
assert.ok(span.isRecordingEvents());
});

it('should set an attribute', () => {
const span = new Span(tracer, name, traceId, SpanKind.CLIENT);
const span = new Span(tracer, name, parentSpanContext, SpanKind.CLIENT);

['String', 'Number', 'Boolean'].map(attType => {
span.setAttribute('testKey' + attType, 'testValue' + attType);
Expand All @@ -61,24 +64,24 @@ describe('Span', () => {
});

it('should set an event', () => {
const span = new Span(tracer, name, traceId, SpanKind.CLIENT);
const span = new Span(tracer, name, parentSpanContext, SpanKind.CLIENT);
span.addEvent('sent');
span.addEvent('rev', { attr1: 'value', attr2: 123, attr3: true });
});

it('should set a link', () => {
const spanContext: SpanContext = {
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
spanId: '6e0c63257de34c92',
traceId: 'a3cda95b652f4a1592b449d5929fda1b',
spanId: '5e0c63257de34c92',
traceOptions: TraceOptions.SAMPLED,
};
const span = new Span(tracer, name, traceId, SpanKind.CLIENT);
const span = new Span(tracer, name, parentSpanContext, SpanKind.CLIENT);
span.addLink(spanContext);
span.addLink(spanContext, { attr1: 'value', attr2: 123, attr3: true });
});

it('should set an error status', () => {
const span = new Span(tracer, name, traceId, SpanKind.CLIENT);
const span = new Span(tracer, name, parentSpanContext, SpanKind.CLIENT);
span.setStatus({
code: CanonicalCode.PERMISSION_DENIED,
message: 'This is an error',
Expand All @@ -89,17 +92,14 @@ describe('Span', () => {
const span = new Span(
tracer,
name,
traceId,
parentSpanContext,
SpanKind.SERVER,
undefined,
undefined,
100
);
const context = span.context();

assert.strictEqual(
span.toString(),
`Span{"traceId":"${context.traceId}","spanId":"${context.spanId}","name":"${name}","kind":1,"status":{"code":0},"startTime":100,"endTime":0}`
`Span{"traceId":"${context.traceId}","spanId":"${context.spanId}","parentId":"${parentSpanContext.spanId}","name":"${name}","kind":1,"status":{"code":0},"startTime":100,"endTime":0}`
);
});
});

0 comments on commit 36fe796

Please sign in to comment.