forked from open-telemetry/opentelemetry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: splitting BasePlugin into browser and node (open-telemetry#981)
- Loading branch information
Showing
11 changed files
with
170 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
packages/opentelemetry-core/src/platform/BaseAbstractPlugin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/*! | ||
* Copyright 2020, 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 | ||
* | ||
* https://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 { | ||
Tracer, | ||
Plugin, | ||
Logger, | ||
PluginConfig, | ||
TracerProvider, | ||
PluginInternalFiles, | ||
} from '@opentelemetry/api'; | ||
|
||
/** This class represent the base to patch plugin. */ | ||
export abstract class BaseAbstractPlugin<T> implements Plugin<T> { | ||
abstract readonly moduleName: string; // required for internalFilesExports | ||
supportedVersions?: string[]; | ||
readonly version?: string; // required for internalFilesExports | ||
|
||
protected readonly _basedir?: string; // required for internalFilesExports | ||
protected _config!: PluginConfig; | ||
protected _internalFilesExports!: { [module: string]: unknown }; // output for internalFilesExports | ||
protected readonly _internalFilesList?: PluginInternalFiles; // required for internalFilesExports | ||
protected _logger!: Logger; | ||
protected _moduleExports!: T; | ||
protected _tracer!: Tracer; | ||
|
||
constructor( | ||
protected readonly _tracerName: string, | ||
protected readonly _tracerVersion?: string | ||
) {} | ||
|
||
disable(): void { | ||
this.unpatch(); | ||
} | ||
|
||
abstract enable( | ||
moduleExports: T, | ||
tracerProvider: TracerProvider, | ||
logger: Logger, | ||
config?: PluginConfig | ||
): T; | ||
|
||
protected abstract patch(): T; | ||
protected abstract unpatch(): void; | ||
} |
43 changes: 43 additions & 0 deletions
43
packages/opentelemetry-core/src/platform/browser/BasePlugin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/*! | ||
* Copyright 2020, 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 | ||
* | ||
* https://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 { | ||
Logger, | ||
Plugin, | ||
PluginConfig, | ||
TracerProvider, | ||
} from '@opentelemetry/api'; | ||
import { BaseAbstractPlugin } from '../BaseAbstractPlugin'; | ||
|
||
/** This class represent the base to patch plugin. */ | ||
export abstract class BasePlugin<T> extends BaseAbstractPlugin<T> | ||
implements Plugin<T> { | ||
enable( | ||
moduleExports: T, | ||
tracerProvider: TracerProvider, | ||
logger: Logger, | ||
config?: PluginConfig | ||
): T { | ||
this._moduleExports = moduleExports; | ||
this._tracer = tracerProvider.getTracer( | ||
this._tracerName, | ||
this._tracerVersion | ||
); | ||
this._logger = logger; | ||
if (config) this._config = config; | ||
return this.patch(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
packages/opentelemetry-core/test/platform/browser/BasePlugin.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* Copyright 2020, 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 | ||
* | ||
* https://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 { NOOP_TRACER, NoopTracerProvider } from '@opentelemetry/api'; | ||
import * as assert from 'assert'; | ||
import { BasePlugin, NoopLogger } from '../../../src'; | ||
|
||
const provider = new NoopTracerProvider(); | ||
const logger = new NoopLogger(); | ||
describe('BasePlugin', () => { | ||
describe('enable', () => { | ||
it('should enable plugin', () => { | ||
const moduleExports = { foo: function() {} }; | ||
const plugin = new TestPlugin('foo', '1'); | ||
const patch = plugin.enable(moduleExports, provider, logger); | ||
|
||
assert.strictEqual(plugin['_tracer'], NOOP_TRACER); | ||
assert.strictEqual(plugin['_tracerName'], 'foo'); | ||
assert.strictEqual(plugin['_tracerVersion'], '1'); | ||
assert.strictEqual(plugin['_logger'], logger); | ||
assert.strictEqual(patch, moduleExports); | ||
}); | ||
}); | ||
}); | ||
|
||
class TestPlugin extends BasePlugin<{ [key: string]: Function }> { | ||
readonly moduleName = 'test-package'; | ||
readonly version = '0.1.0'; | ||
|
||
patch(): { [key: string]: Function } { | ||
return this._moduleExports; | ||
} | ||
|
||
protected unpatch(): void {} | ||
} |