Skip to content

Commit

Permalink
feat: make scopemanager optional param (#257)
Browse files Browse the repository at this point in the history
* feat: make scopemanager optional param

* fix: make constant all caps
  • Loading branch information
mayurkale22 authored Sep 16, 2019
1 parent 84567f5 commit 6fca212
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 99 deletions.
5 changes: 1 addition & 4 deletions packages/opentelemetry-basic-tracer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,10 @@ npm install --save @opentelemetry/basic-tracer
```
const opentelemetry = require('@opentelemetry/core');
const { BasicTracer } = require('@opentelemetry/basic-tracer');
const { NoopScopeManager } = require('@opentelemetry/scope-base');
// To start a trace, you first need to initialize the Tracer.
// NOTE: the default OpenTelemetry tracer does not record any tracing information.
const tracer = new BasicTracer({
scopeManager: new NoopScopeManager()
});
const tracer = new BasicTracer();
// Initialize the OpenTelemetry APIs to use the BasicTracer bindings
opentelemetry.initGlobalTracer(tracer);
Expand Down
3 changes: 2 additions & 1 deletion packages/opentelemetry-basic-tracer/src/BasicTracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { mergeConfig } from './utility';
import { SpanProcessor } from './SpanProcessor';
import { NoopSpanProcessor } from './NoopSpanProcessor';
import { MultiSpanProcessor } from './MultiSpanProcessor';
import { DEFAULT_CONFIG } from './config';

/**
* This class represents a basic tracer.
Expand All @@ -53,7 +54,7 @@ export class BasicTracer implements types.Tracer {
/**
* Constructs a new Tracer instance.
*/
constructor(config: BasicTracerConfig) {
constructor(config: BasicTracerConfig = DEFAULT_CONFIG) {
const localConfig = mergeConfig(config);
this._binaryFormat = localConfig.binaryFormat;
this._defaultAttributes = localConfig.defaultAttributes;
Expand Down
2 changes: 1 addition & 1 deletion packages/opentelemetry-basic-tracer/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const DEFAULT_MAX_LINKS_PER_SPAN = 32;
* non-primitive values (like `traceParams`), the user-provided value will be
* used to extend the default value.
*/
export const defaultConfig = {
export const DEFAULT_CONFIG = {
defaultAttributes: {},
binaryFormat: new BinaryTraceContext(),
httpTextFormat: new HttpTraceContext(),
Expand Down
2 changes: 1 addition & 1 deletion packages/opentelemetry-basic-tracer/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export interface BasicTracerConfig {
/**
* Scope manager keeps context across in-process operations.
*/
scopeManager: ScopeManager;
scopeManager?: ScopeManager;

/** Trace Parameters */
traceParams?: TraceParams;
Expand Down
4 changes: 2 additions & 2 deletions packages/opentelemetry-basic-tracer/src/utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ import {
DEFAULT_MAX_EVENTS_PER_SPAN,
DEFAULT_MAX_LINKS_PER_SPAN,
} from './config';
import { defaultConfig } from './config';
import { DEFAULT_CONFIG } from './config';

/**
* Function to merge Default configuration (as specified in './config') with
* user provided configurations.
*/
export function mergeConfig(userConfig: BasicTracerConfig) {
const traceParams = userConfig.traceParams;
const target = Object.assign({}, defaultConfig, userConfig);
const target = Object.assign({}, DEFAULT_CONFIG, userConfig);

// the user-provided value will be used to extend the default value.
if (traceParams) {
Expand Down
55 changes: 12 additions & 43 deletions packages/opentelemetry-basic-tracer/test/BasicTracer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,26 +129,21 @@ describe('BasicTracer', () => {
region: 'eu-west',
asg: 'my-asg',
},
scopeManager: new NoopScopeManager(),
});
assert.ok(tracer instanceof BasicTracer);
});
});

describe('.startSpan()', () => {
it('should start a span with name only', () => {
const tracer = new BasicTracer({
scopeManager: new NoopScopeManager(),
});
const tracer = new BasicTracer();
const span = tracer.startSpan('my-span');
assert.ok(span);
assert.ok(span instanceof Span);
});

it('should start a span with name and options', () => {
const tracer = new BasicTracer({
scopeManager: new NoopScopeManager(),
});
const tracer = new BasicTracer();
const span = tracer.startSpan('my-span', {});
assert.ok(span);
assert.ok(span instanceof Span);
Expand All @@ -162,7 +157,6 @@ describe('BasicTracer', () => {

it('should start a span with defaultAttributes and spanoptions->attributes', () => {
const tracer = new BasicTracer({
scopeManager: new NoopScopeManager(),
defaultAttributes: { foo: 'bar' },
});
const span = tracer.startSpan('my-span', {
Expand All @@ -174,7 +168,6 @@ describe('BasicTracer', () => {

it('should start a span with defaultAttributes and undefined spanoptions->attributes', () => {
const tracer = new BasicTracer({
scopeManager: new NoopScopeManager(),
defaultAttributes: { foo: 'bar' },
});
const span = tracer.startSpan('my-span', {}) as Span;
Expand All @@ -183,9 +176,7 @@ describe('BasicTracer', () => {
});

it('should start a span with spanoptions->attributes', () => {
const tracer = new BasicTracer({
scopeManager: new NoopScopeManager(),
});
const tracer = new BasicTracer();
const span = tracer.startSpan('my-span', {
attributes: { foo: 'foo', bar: 'bar' },
}) as Span;
Expand All @@ -194,9 +185,7 @@ describe('BasicTracer', () => {
});

it('should start a span with name and parent spancontext', () => {
const tracer = new BasicTracer({
scopeManager: new NoopScopeManager(),
});
const tracer = new BasicTracer();
const state = new TraceState('a=1,b=2');
const span = tracer.startSpan('my-span', {
parent: {
Expand All @@ -214,9 +203,7 @@ describe('BasicTracer', () => {
});

it('should start a span with name and parent span', () => {
const tracer = new BasicTracer({
scopeManager: new NoopScopeManager(),
});
const tracer = new BasicTracer();
const span = tracer.startSpan('my-span');
const childSpan = tracer.startSpan('child-span', {
parent: span,
Expand All @@ -229,19 +216,15 @@ describe('BasicTracer', () => {
});

it('should start a span with name and with invalid parent span', () => {
const tracer = new BasicTracer({
scopeManager: new NoopScopeManager(),
});
const tracer = new BasicTracer();
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(),
});
const tracer = new BasicTracer();
const span = tracer.startSpan('my-span', {
parent: { traceId: '0', spanId: '0' },
});
Expand All @@ -256,7 +239,6 @@ describe('BasicTracer', () => {
it('should return a no recording span when never sampling', () => {
const tracer = new BasicTracer({
sampler: NEVER_SAMPLER,
scopeManager: new NoopScopeManager(),
logger: new NoopLogger(),
});
const span = tracer.startSpan('my-span');
Expand All @@ -272,7 +254,6 @@ describe('BasicTracer', () => {
it('should create real span when not sampled but recording events true', () => {
const tracer = new BasicTracer({
sampler: NEVER_SAMPLER,
scopeManager: new NoopScopeManager(),
});
const span = tracer.startSpan('my-span', { isRecordingEvents: true });
assert.ok(span instanceof Span);
Expand All @@ -283,7 +264,6 @@ describe('BasicTracer', () => {
it('should not create real span when not sampled and recording events false', () => {
const tracer = new BasicTracer({
sampler: NEVER_SAMPLER,
scopeManager: new NoopScopeManager(),
logger: new NoopLogger(),
});
const span = tracer.startSpan('my-span', { isRecordingEvents: false });
Expand All @@ -295,7 +275,6 @@ describe('BasicTracer', () => {
it('should not create real span when not sampled and no recording events configured', () => {
const tracer = new BasicTracer({
sampler: NEVER_SAMPLER,
scopeManager: new NoopScopeManager(),
logger: new NoopLogger(),
});
const span = tracer.startSpan('my-span');
Expand Down Expand Up @@ -332,9 +311,7 @@ describe('BasicTracer', () => {

describe('.getCurrentSpan()', () => {
it('should return null with NoopScopeManager', () => {
const tracer = new BasicTracer({
scopeManager: new NoopScopeManager(),
});
const tracer = new BasicTracer();
const currentSpan = tracer.getCurrentSpan();
assert.deepStrictEqual(currentSpan, null);
});
Expand All @@ -351,9 +328,7 @@ describe('BasicTracer', () => {

describe('.withSpan()', () => {
it('should run scope with NoopScopeManager scope manager', done => {
const tracer = new BasicTracer({
scopeManager: new NoopScopeManager(),
});
const tracer = new BasicTracer();
const span = tracer.startSpan('my-span');
tracer.withSpan(span, () => {
assert.deepStrictEqual(tracer.getCurrentSpan(), null);
Expand All @@ -364,9 +339,7 @@ describe('BasicTracer', () => {

describe('.bind()', () => {
it('should bind scope with NoopScopeManager scope manager', done => {
const tracer = new BasicTracer({
scopeManager: new NoopScopeManager(),
});
const tracer = new BasicTracer();
const span = tracer.startSpan('my-span');
const fn = () => {
assert.deepStrictEqual(tracer.getCurrentSpan(), null);
Expand All @@ -384,18 +357,14 @@ describe('BasicTracer', () => {

describe('.getBinaryFormat()', () => {
it('should get default binary formatter', () => {
const tracer = new BasicTracer({
scopeManager: new NoopScopeManager(),
});
const tracer = new BasicTracer();
assert.ok(tracer.getBinaryFormat() instanceof BinaryTraceContext);
});
});

describe('.getHttpTextFormat()', () => {
it('should get default HTTP text formatter', () => {
const tracer = new BasicTracer({
scopeManager: new NoopScopeManager(),
});
const tracer = new BasicTracer();
assert.ok(tracer.getHttpTextFormat() instanceof HttpTraceContext);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import * as assert from 'assert';
import { MultiSpanProcessor } from '../src/MultiSpanProcessor';
import { SpanProcessor, Span, BasicTracer } from '../src';
import { NoopScopeManager } from '@opentelemetry/scope-base';

class TestProcessor implements SpanProcessor {
spans: Span[] = [];
Expand All @@ -31,9 +30,7 @@ class TestProcessor implements SpanProcessor {
}

describe('MultiSpanProcessor', () => {
const tracer = new BasicTracer({
scopeManager: new NoopScopeManager(),
});
const tracer = new BasicTracer();
const span = tracer.startSpan('one');

it('should handle empty span processor', () => {
Expand Down
2 changes: 0 additions & 2 deletions packages/opentelemetry-basic-tracer/test/Span.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@ import {
SpanContext,
} from '@opentelemetry/types';
import { BasicTracer } from '../src';
import { NoopScopeManager } from '@opentelemetry/scope-base';
import { NoopLogger } from '@opentelemetry/core';

describe('Span', () => {
const tracer = new BasicTracer({
scopeManager: new NoopScopeManager(),
logger: new NoopLogger(),
});
const name = 'span1';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,10 @@ import {
BasicTracer,
ExportResult,
} from '../../src';
import { NoopScopeManager } from '@opentelemetry/scope-base';

describe('InMemorySpanExporter', () => {
const memoryExporter = new InMemorySpanExporter();
const tracer = new BasicTracer({
scopeManager: new NoopScopeManager(),
});
const tracer = new BasicTracer();
tracer.addSpanProcessor(new SimpleSpanProcessor(memoryExporter));

afterEach(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { Span, BasicTracer } from '../../src';
import { SpanExporter } from '../../src/export/SpanExporter';
import { ReadableSpan } from '../../src/export/ReadableSpan';
import { SpanContext, SpanKind, TraceOptions } from '@opentelemetry/types';
import { NoopScopeManager } from '@opentelemetry/scope-base';

class TestExporter implements SpanExporter {
spansDataList: ReadableSpan[] = [];
Expand All @@ -34,9 +33,7 @@ class TestExporter implements SpanExporter {
}

describe('SimpleSpanProcessor', () => {
const tracer = new BasicTracer({
scopeManager: new NoopScopeManager(),
});
const tracer = new BasicTracer();
const exporter = new TestExporter();

describe('constructor', () => {
Expand Down
38 changes: 5 additions & 33 deletions packages/opentelemetry-node-tracer/src/NodeTracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,46 +14,18 @@
* limitations under the License.
*/

import { BasicTracer } from '@opentelemetry/basic-tracer';
import { BasicTracer, BasicTracerConfig } from '@opentelemetry/basic-tracer';
import { AsyncHooksScopeManager } from '@opentelemetry/scope-async-hooks';
import { ScopeManager } from '@opentelemetry/scope-base';
import {
Attributes,
BinaryFormat,
HttpTextFormat,
Logger,
Sampler,
} from '@opentelemetry/types';
import { Plugins } from './instrumentation/PluginLoader';

/**
* NodeTracerConfig provides an interface for configuring a Node Tracer.
*/
export interface NodeTracerConfig {
export interface NodeTracerConfig extends BasicTracerConfig {
/**
* Binary formatter which can serialize/deserialize Spans.
* Plugins options.
*/
binaryFormat?: BinaryFormat;
/**
* Attributed that will be applied on every span created by Tracer.
* Useful to add infrastructure and environment information to your spans.
*/
defaultAttributes?: Attributes;
/**
* HTTP text formatter which can inject/extract Spans.
*/
httpTextFormat?: HttpTextFormat;
/**
* User provided logger.
*/
logger?: Logger;
/**
* Sampler determines if a span should be recorded or should be a NoopSpan.
*/
sampler?: Sampler;
/**
* Scope manager keeps context across in-process operations.
*/
scopeManager?: ScopeManager;
plugins?: Plugins;
}

/**
Expand Down

0 comments on commit 6fca212

Please sign in to comment.