From bd6c156a3cb57d61cd21f6e8ddf19226fcb7c72e Mon Sep 17 00:00:00 2001 From: vmarchaud Date: Fri, 12 Jul 2019 16:51:59 +0200 Subject: [PATCH] feat(scope-manager): Add no-op implementations of ScopeManager --- .../opentelemetry-scope-base/package.json | 1 + .../src/NoopScopeManager.ts | 42 +++++++++ .../opentelemetry-scope-base/src/index.ts | 1 + .../opentelemetry-scope-base/src/types.ts | 10 +- .../test/noop/NoopScopeManager.test.ts | 92 +++++++++++++++++++ 5 files changed, 141 insertions(+), 5 deletions(-) create mode 100644 packages/opentelemetry-scope-base/src/NoopScopeManager.ts create mode 100644 packages/opentelemetry-scope-base/test/noop/NoopScopeManager.test.ts diff --git a/packages/opentelemetry-scope-base/package.json b/packages/opentelemetry-scope-base/package.json index eec3ec49af8..6faf15c5852 100644 --- a/packages/opentelemetry-scope-base/package.json +++ b/packages/opentelemetry-scope-base/package.json @@ -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", diff --git a/packages/opentelemetry-scope-base/src/NoopScopeManager.ts b/packages/opentelemetry-scope-base/src/NoopScopeManager.ts new file mode 100644 index 00000000000..ed2d039c6fd --- /dev/null +++ b/packages/opentelemetry-scope-base/src/NoopScopeManager.ts @@ -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 ReturnType>( + scope: unknown, + fn: T + ): ReturnType { + return fn(); + } + + bind(target: T, scope?: unknown): T { + return target; + } + + enable(): this { + return this; + } + + disable(): this { + return this; + } +} diff --git a/packages/opentelemetry-scope-base/src/index.ts b/packages/opentelemetry-scope-base/src/index.ts index f21192eb003..4ff0407a9cf 100644 --- a/packages/opentelemetry-scope-base/src/index.ts +++ b/packages/opentelemetry-scope-base/src/index.ts @@ -15,3 +15,4 @@ */ export * from './types'; +export * from './NoopScopeManager'; diff --git a/packages/opentelemetry-scope-base/src/types.ts b/packages/opentelemetry-scope-base/src/types.ts index 0cc18e8912e..9e3f1bb5533 100644 --- a/packages/opentelemetry-scope-base/src/types.ts +++ b/packages/opentelemetry-scope-base/src/types.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -export interface BaseScopeManager { +export interface ScopeManager { /** * Get the current active scope */ @@ -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 unknown>( + with ReturnType>( scope: unknown, fn: T ): ReturnType; /** * 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(object: T, scope?: unknown): T; + bind(target: T, scope?: unknown): T; /** * Enable scope management @@ -45,5 +45,5 @@ export interface BaseScopeManager { /** * Disable scope management */ - disable(): void; + disable(): this; } diff --git a/packages/opentelemetry-scope-base/test/noop/NoopScopeManager.test.ts b/packages/opentelemetry-scope-base/test/noop/NoopScopeManager.test.ts new file mode 100644 index 00000000000..b6ec6791a21 --- /dev/null +++ b/packages/opentelemetry-scope-base/test/noop/NoopScopeManager.test.ts @@ -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 work', () => { + assert.doesNotThrow(() => { + scopeManager = new NoopScopeManager(); + assert(scopeManager.enable() === scopeManager, 'should return this'); + }); + }); + }); + + describe('.disable()', () => { + it('should work', () => { + 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(); + }); + }); +});