Skip to content

Commit

Permalink
feat(scope-manager): Add no-op implementations of ScopeManager
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarchaud committed Jul 12, 2019
1 parent e0d4c61 commit 50d9bab
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/opentelemetry-scope-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
},
"devDependencies": {
"@types/mocha": "^5.2.5",
"@types/node": "^12.6.2",
"c8": "^5.0.1",
"codecov": "^3.1.0",
"gts": "^1.0.0",
Expand Down
42 changes: 42 additions & 0 deletions packages/opentelemetry-scope-base/src/NoopScopeManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Copyright 2019, OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as types from './types';

export class NoopScopeManager implements types.ScopeManager {
active(): unknown {
return null;
}

with<T extends (...args: unknown[]) => ReturnType<T>>(
scope: unknown,
fn: T
): ReturnType<T> {
return fn();
}

bind<T>(target: T, scope?: unknown): T {
return target;
}

enable(): this {
return this;
}

disable(): this {
return this;
}
}
1 change: 1 addition & 0 deletions packages/opentelemetry-scope-base/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
*/

export * from './types';
export * from './NoopScopeManager';
10 changes: 5 additions & 5 deletions packages/opentelemetry-scope-base/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

export interface BaseScopeManager {
export interface ScopeManager {
/**
* Get the current active scope
*/
Expand All @@ -25,17 +25,17 @@ export interface BaseScopeManager {
* @param scope Any object to set as the current active scope
* @param fn A callback to be imediately run within a specific scope
*/
with<T extends (...args: unknown[]) => unknown>(
with<T extends (...args: unknown[]) => ReturnType<T>>(
scope: unknown,
fn: T
): ReturnType<T>;

/**
* Bind an object as the current scope (or a specific one)
* @param object Object to which a scope need to be set
* @param target Any object to which a scope need to be set
* @param [scope] Optionaly specify the scope which you want to assign
*/
bind<T>(object: T, scope?: unknown): T;
bind<T>(target: T, scope?: unknown): T;

/**
* Enable scope management
Expand All @@ -45,5 +45,5 @@ export interface BaseScopeManager {
/**
* Disable scope management
*/
disable(): void;
disable(): this;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* Copyright 2019, OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as assert from 'assert';
import { NoopScopeManager } from '../../src';

describe('NoopScopeManager', () => {
let scopeManager: NoopScopeManager;

describe('.enable()', () => {
it('should works', () => {
assert.doesNotThrow(() => {
scopeManager = new NoopScopeManager();
assert(scopeManager.enable() === scopeManager, 'should return this');
});
});
});

describe('.disable()', () => {
it('should works', () => {
assert.doesNotThrow(() => {
assert(scopeManager.disable() === scopeManager, 'should return this');
});
scopeManager.enable();
});
});

describe('.with()', () => {
it('should run the callback (null as target)', done => {
scopeManager.with(null, done);
});

it('should run the callback (object as target)', done => {
const test = { a: 1 };
scopeManager.with(test, () => {
assert.strictEqual(
scopeManager.active(),
null,
'should not have scope'
);
return done();
});
});

it('should run the callback (when disabled)', done => {
scopeManager.disable();
scopeManager.with(null, () => {
scopeManager.enable();
return done();
});
});
});

describe('.active()', () => {
it('should always return null (when enabled)', () => {
assert.strictEqual(scopeManager.active(), null, 'should not have scope');
});

it('should always return null (when disabled)', () => {
scopeManager.disable();
assert.strictEqual(scopeManager.active(), null, 'should not have scope');
scopeManager.enable();
});
});

describe('.bind()', () => {
it('should return the same target (when enabled)', () => {
const test = { a: 1 };
assert.deepStrictEqual(scopeManager.bind(test), test);
});

it('should return the same target (when disabled)', () => {
scopeManager.disable();
const test = { a: 1 };
assert.deepStrictEqual(scopeManager.bind(test), test);
scopeManager.enable();
});
});
});

0 comments on commit 50d9bab

Please sign in to comment.