Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add start method on tracer and initialize PluginLoader #168

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -20,33 +20,51 @@ import {
BinaryTraceContext,
HttpTraceContext,
NOOP_SPAN,
NoopLogger,
} from '@opentelemetry/core';
import { BinaryFormat, HttpTextFormat } from '@opentelemetry/types';
import {
BinaryFormat,
HttpTextFormat,
Tracer,
Logger,
} from '@opentelemetry/types';
import { BasicTracerConfig } from '../src/types';
import { ScopeManager } from '@opentelemetry/scope-base';
import { ScopeManager, NoopScopeManager } from '@opentelemetry/scope-base';
import { Span } from './Span';

/**
* This class represents a basic tracer.
*/
export class BasicTracer implements types.Tracer {
private readonly _defaultAttributes: types.Attributes;
private readonly _binaryFormat: types.BinaryFormat;
private readonly _httpTextFormat: types.HttpTextFormat;
private readonly _sampler: types.Sampler;
private readonly _scopeManager: ScopeManager;
private _defaultAttributes: types.Attributes = {};
private _binaryFormat!: types.BinaryFormat;
private _httpTextFormat!: types.HttpTextFormat;
private _logger!: Logger;
private _sampler: types.Sampler = ALWAYS_SAMPLER;
private _scopeManager: ScopeManager = new NoopScopeManager();

/**
* Constructs a new Tracer instance.
*/
constructor(config: BasicTracerConfig) {
constructor() {}

/**
* Starts tracing.
* @param config A configuration object to start tracer.
* @returns The started tracer instance.
*/
start(config: BasicTracerConfig): Tracer {
this._binaryFormat = config.binaryFormat || new BinaryTraceContext();
this._defaultAttributes = config.defaultAttributes || {};
this._httpTextFormat = config.httpTextFormat || new HttpTraceContext();
this._logger = config.logger || new NoopLogger();
this._sampler = config.sampler || ALWAYS_SAMPLER;
this._scopeManager = config.scopeManager;
return this;
}

stop() {}

/**
* Starts a new Span or returns the default NoopSpan based on the sampling
* decision.
@@ -57,6 +75,7 @@ export class BasicTracer implements types.Tracer {
if (!this._sampler.shouldSample(parentSpanContext)) {
// TODO: propagate SpanContext, for more information see
// https://github.com/open-telemetry/opentelemetry-js/pull/99#issuecomment-513325536
this._logger.debug('Sampling is off, starting no recording span');
return NOOP_SPAN;
}

@@ -28,48 +28,53 @@ import { NoopScopeManager } from '@opentelemetry/scope-base';
import { Span } from '../src/Span';

describe('BasicTracer', () => {
describe('constructor', () => {
it('should construct an instance with required only options', () => {
const tracer = new BasicTracer({
let tracer: BasicTracer;
beforeEach(() => {
tracer = new BasicTracer();
});

describe('start', () => {
it('should start an instance with required only options', () => {
tracer.start({
scopeManager: new NoopScopeManager(),
});
assert.ok(tracer instanceof BasicTracer);
});

it('should construct an instance with binary format', () => {
const tracer = new BasicTracer({
it('should start an instance with binary format', () => {
tracer.start({
binaryFormat: new BinaryTraceContext(),
scopeManager: new NoopScopeManager(),
});
assert.ok(tracer instanceof BasicTracer);
});

it('should construct an instance with http text format', () => {
const tracer = new BasicTracer({
it('should start an instance with http text format', () => {
tracer.start({
httpTextFormat: new HttpTraceContext(),
scopeManager: new NoopScopeManager(),
});
assert.ok(tracer instanceof BasicTracer);
});

it('should construct an instance with logger', () => {
const tracer = new BasicTracer({
it('should start an instance with logger', () => {
tracer.start({
logger: new NoopLogger(),
scopeManager: new NoopScopeManager(),
});
assert.ok(tracer instanceof BasicTracer);
});

it('should construct an instance with sampler', () => {
const tracer = new BasicTracer({
it('should start an instance with sampler', () => {
tracer.start({
scopeManager: new NoopScopeManager(),
sampler: ALWAYS_SAMPLER,
});
assert.ok(tracer instanceof BasicTracer);
});

it('should construct an instance with default attributes', () => {
const tracer = new BasicTracer({
it('should start an instance with default attributes', () => {
tracer.start({
defaultAttributes: {
region: 'eu-west',
asg: 'my-asg',
@@ -82,7 +87,7 @@ describe('BasicTracer', () => {

describe('.startSpan()', () => {
it('should start a span with name only', () => {
const tracer = new BasicTracer({
tracer.start({
scopeManager: new NoopScopeManager(),
});
const span = tracer.startSpan('my-span');
@@ -91,7 +96,7 @@ describe('BasicTracer', () => {
});

it('should start a span with name and options', () => {
const tracer = new BasicTracer({
tracer.start({
scopeManager: new NoopScopeManager(),
});
const span = tracer.startSpan('my-span', {});
@@ -100,7 +105,7 @@ describe('BasicTracer', () => {
});

it('should return a default span with no sampling', () => {
const tracer = new BasicTracer({
tracer.start({
sampler: NEVER_SAMPLER,
scopeManager: new NoopScopeManager(),
});
@@ -117,7 +122,7 @@ describe('BasicTracer', () => {

describe('.getCurrentSpan()', () => {
it('should return null with NoopScopeManager', () => {
const tracer = new BasicTracer({
tracer.start({
scopeManager: new NoopScopeManager(),
});
const currentSpan = tracer.getCurrentSpan();
@@ -127,7 +132,7 @@ describe('BasicTracer', () => {

describe('.withSpan()', () => {
it('should run scope with NoopScopeManager scope manager', done => {
const tracer = new BasicTracer({
tracer.start({
scopeManager: new NoopScopeManager(),
});
const span = tracer.startSpan('my-span');
@@ -144,7 +149,7 @@ describe('BasicTracer', () => {

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

describe('.getHttpTextFormat()', () => {
it('should get default HTTP text formatter', () => {
const tracer = new BasicTracer({
tracer.start({
scopeManager: new NoopScopeManager(),
});
assert.ok(tracer.getHttpTextFormat() instanceof HttpTraceContext);
@@ -16,19 +16,48 @@

import { BasicTracer, BasicTracerConfig } from '@opentelemetry/basic-tracer';
import { AsyncHooksScopeManager } from '@opentelemetry/scope-async-hooks';
import { PluginLoader } from './instrumentation/PluginLoader';
import { Tracer } from '@opentelemetry/types';
import { NoopLogger } from '@opentelemetry/core';

const DEFAULT_INSTRUMENTATION_MODULES = {
http: false, // TODO: change to true after pull/161

This comment has been minimized.

Copy link
@OlivierAlbertini

OlivierAlbertini Sep 10, 2019

Member

It's merged. It can be set to true

grpc: false,
};

/**
* This class represents a node tracer with `async_hooks` module.
*/
export class NodeTracer extends BasicTracer {
private _pluginLoader!: PluginLoader;

/**
* Constructs a new Tracer instance.
*/
constructor(config: BasicTracerConfig) {
super(
constructor() {

This comment has been minimized.

Copy link
@rochdev

rochdev Aug 7, 2019

Member

Removing the constructor will have the same effect.

super();
}

/**
* Starts tracing.
* @param config A configuration object to start tracer.
* @returns The started tracer instance.
*/
start(config: BasicTracerConfig): Tracer {
super.start(
Object.assign({}, { scopeManager: new AsyncHooksScopeManager() }, config)
);
this._pluginLoader = new PluginLoader(
this,
config.logger || new NoopLogger()
);
// TODO: allow overriding default instrumented modules
this._pluginLoader.load(DEFAULT_INSTRUMENTATION_MODULES);
return this;
}

// @todo: Integrate Plugin Loader (pull/126).
stop() {
super.stop();
this._pluginLoader.unload();
}
}
@@ -27,48 +27,53 @@ import { AsyncHooksScopeManager } from '@opentelemetry/scope-async-hooks';
import { NodeTracer } from '../src/NodeTracer';

describe('NodeTracer', () => {
describe('constructor', () => {
it('should construct an instance with required only options', () => {
const tracer = new NodeTracer({
let tracer: NodeTracer;
beforeEach(() => {
tracer = new NodeTracer();
});

describe('start', () => {
it('should start an instance with required only options', () => {
tracer.start({
scopeManager: new AsyncHooksScopeManager(),
});
assert.ok(tracer instanceof NodeTracer);
});

it('should construct an instance with binary format', () => {
const tracer = new NodeTracer({
it('should start an instance with binary format', () => {
tracer.start({
binaryFormat: new BinaryTraceContext(),
scopeManager: new AsyncHooksScopeManager(),
});
assert.ok(tracer instanceof NodeTracer);
});

it('should construct an instance with http text format', () => {
const tracer = new NodeTracer({
it('should start an instance with http text format', () => {
tracer.start({
httpTextFormat: new HttpTraceContext(),
scopeManager: new AsyncHooksScopeManager(),
});
assert.ok(tracer instanceof NodeTracer);
});

it('should construct an instance with logger', () => {
const tracer = new NodeTracer({
it('should start an instance with logger', () => {
tracer.start({
logger: new NoopLogger(),
scopeManager: new AsyncHooksScopeManager(),
});
assert.ok(tracer instanceof NodeTracer);
});

it('should construct an instance with sampler', () => {
const tracer = new NodeTracer({
it('should start an instance with sampler', () => {
tracer.start({
scopeManager: new AsyncHooksScopeManager(),
sampler: ALWAYS_SAMPLER,
});
assert.ok(tracer instanceof NodeTracer);
});

it('should construct an instance with default attributes', () => {
const tracer = new NodeTracer({
it('should start an instance with default attributes', () => {
tracer.start({
defaultAttributes: {
region: 'eu-west',
asg: 'my-asg',
@@ -81,23 +86,23 @@ describe('NodeTracer', () => {

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

it('should start a span with name and options', () => {
const tracer = new NodeTracer({
tracer.start({
scopeManager: new AsyncHooksScopeManager(),
});
const span = tracer.startSpan('my-span', {});
assert.ok(span);
});

it('should return a default span with no sampling', () => {
const tracer = new NodeTracer({
tracer.start({
sampler: NEVER_SAMPLER,
scopeManager: new AsyncHooksScopeManager(),
});
@@ -114,7 +119,7 @@ describe('NodeTracer', () => {

describe('.getCurrentSpan()', () => {
it('should return null with AsyncHooksScopeManager when no span started', () => {
const tracer = new NodeTracer({
tracer.start({
scopeManager: new AsyncHooksScopeManager(),
});
assert.deepStrictEqual(tracer.getCurrentSpan(), null);
@@ -123,7 +128,7 @@ describe('NodeTracer', () => {

describe('.withSpan()', () => {
it('should run scope with AsyncHooksScopeManager scope manager', done => {
const tracer = new NodeTracer({
tracer.start({
scopeManager: new AsyncHooksScopeManager(),
});
const span = tracer.startSpan('my-span');
@@ -135,7 +140,7 @@ describe('NodeTracer', () => {
});

it('should run scope with AsyncHooksScopeManager scope manager with multiple spans', done => {
const tracer = new NodeTracer({
tracer.start({
scopeManager: new AsyncHooksScopeManager(),
});
const span = tracer.startSpan('my-span');
@@ -165,7 +170,7 @@ describe('NodeTracer', () => {

describe('getBinaryFormat', () => {
it('should get default binary formatter', () => {
const tracer = new NodeTracer({
tracer.start({
scopeManager: new AsyncHooksScopeManager(),
});
assert.ok(tracer.getBinaryFormat() instanceof BinaryTraceContext);
@@ -174,7 +179,7 @@ describe('NodeTracer', () => {

describe('.getHttpTextFormat()', () => {
it('should get default HTTP text formatter', () => {
const tracer = new NodeTracer({
tracer.start({
scopeManager: new AsyncHooksScopeManager(),
});
assert.ok(tracer.getHttpTextFormat() instanceof HttpTraceContext);
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.