Skip to content

Commit

Permalink
chore(basic-tracer): test coverage (open-telemetry#207)
Browse files Browse the repository at this point in the history
* chore(basic-tracer): increase feature coverage for BasicTracer

* chore(basic-tracer): increase feature coverage for Span

* fix(basic-tracer): pass compilation & prettify
  • Loading branch information
bkspace authored and mayurkale22 committed Aug 20, 2019
1 parent 0941b7f commit 7f5d71b
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 10 deletions.
47 changes: 37 additions & 10 deletions packages/opentelemetry-basic-tracer/test/BasicTracer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ import {
TraceState,
NoRecordingSpan,
} from '@opentelemetry/core';
import { TraceOptions } from '@opentelemetry/types';
import { BasicTracer } from '../src/BasicTracer';
import { NoopScopeManager } from '@opentelemetry/scope-base';
import { NoopScopeManager, ScopeManager } from '@opentelemetry/scope-base';
import { Span } from '../src/Span';
import { TraceOptions } from '@opentelemetry/types';

describe('BasicTracer', () => {
describe('constructor', () => {
Expand Down Expand Up @@ -142,6 +142,16 @@ describe('BasicTracer', () => {
childSpan.end();
});

it('should start a span with name and with invalid parent span', () => {
const tracer = new BasicTracer({
scopeManager: new NoopScopeManager(),
});
const span = tracer.startSpan('my-span', {
parent: ('invalid-parent' as unknown) as undefined,
}) as Span;
assert.deepStrictEqual(span.parentSpanId, undefined);
});

it('should start a span with name and with invalid spancontext', () => {
const tracer = new BasicTracer({
scopeManager: new NoopScopeManager(),
Expand Down Expand Up @@ -172,7 +182,7 @@ describe('BasicTracer', () => {
span.end();
});

it('Should create real span when not sampled but recording events true', () => {
it('should create real span when not sampled but recording events true', () => {
const tracer = new BasicTracer({
sampler: NEVER_SAMPLER,
scopeManager: new NoopScopeManager(),
Expand All @@ -183,7 +193,7 @@ describe('BasicTracer', () => {
assert.strictEqual(span.isRecordingEvents(), true);
});

it('Should not create real span when not sampled and recording events false', () => {
it('should not create real span when not sampled and recording events false', () => {
const tracer = new BasicTracer({
sampler: NEVER_SAMPLER,
scopeManager: new NoopScopeManager(),
Expand All @@ -194,7 +204,7 @@ describe('BasicTracer', () => {
assert.strictEqual(span.isRecordingEvents(), false);
});

it('Should not create real span when not sampled and no recording events configured', () => {
it('should not create real span when not sampled and no recording events configured', () => {
const tracer = new BasicTracer({
sampler: NEVER_SAMPLER,
scopeManager: new NoopScopeManager(),
Expand All @@ -205,7 +215,7 @@ describe('BasicTracer', () => {
assert.strictEqual(span.isRecordingEvents(), false);
});

it('Should create real span when sampled and recording events true', () => {
it('should create real span when sampled and recording events true', () => {
const tracer = new BasicTracer({
sampler: ALWAYS_SAMPLER,
scopeManager: new NoopScopeManager(),
Expand All @@ -216,11 +226,19 @@ describe('BasicTracer', () => {
assert.strictEqual(span.isRecordingEvents(), true);
});

// @todo: implement
it('should start a Span with always sampling');
it('should set default attributes on span', () => {
const defaultAttributes = {
foo: 'bar',
};
const tracer = new BasicTracer({
scopeManager: new NoopScopeManager(),
defaultAttributes,
});

// @todo: implement
it('should set default attributes on span');
const span = tracer.startSpan('my-span') as Span;
assert.ok(span instanceof Span);
assert.deepStrictEqual(span.attributes, defaultAttributes);
});
});

describe('.getCurrentSpan()', () => {
Expand All @@ -231,6 +249,15 @@ describe('BasicTracer', () => {
const currentSpan = tracer.getCurrentSpan();
assert.deepStrictEqual(currentSpan, null);
});

it('should return current span when it exists', () => {
const tracer = new BasicTracer({
scopeManager: {
active: () => 'foo',
} as ScopeManager,
});
assert.deepStrictEqual(tracer.getCurrentSpan(), 'foo');
});
});

describe('.withSpan()', () => {
Expand Down
24 changes: 24 additions & 0 deletions packages/opentelemetry-basic-tracer/test/Span.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,5 +244,29 @@ describe('Span', () => {
);
assert.strictEqual(readableSpan.status.message, 'This is an error');
span.end();

// shouldn't update status
span.setStatus({
code: CanonicalCode.OK,
message: 'OK',
});
assert.strictEqual(span.status.code, CanonicalCode.PERMISSION_DENIED);
});

it('should only end a span once', () => {
const span = new Span(tracer, logger, name, spanContext, SpanKind.SERVER);
span.end(1234);
span.end(4567);
assert.strictEqual(span.endTime, 1234);
});

it('should update name', () => {
const span = new Span(tracer, logger, name, spanContext, SpanKind.SERVER);
span.updateName('foo-span');
span.end();

// shouldn't update name
span.updateName('bar-span');
assert.strictEqual(span.name, 'foo-span');
});
});

0 comments on commit 7f5d71b

Please sign in to comment.