-
Notifications
You must be signed in to change notification settings - Fork 806
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
chore: splitting BasePlugin into browser and node #981
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ef2b6f7
chore: splitting BasePlugin into browser and node to avoid dependency…
obecny 03ccab9
chore: rearrange the params, typo
obecny 291cb34
Merge branch 'master' into base_plugin
obecny 1478311
chore: lint
obecny 6928713
chore: merge branch 'master' into base_plugin
obecny 4697a95
Merge branch 'master' into base_plugin
obecny 0eacf4f
Merge branch 'master' into base_plugin
obecny c102eeb
Merge branch 'master' into base_plugin
mayurkale22 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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; | ||
dyladan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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( | ||
dyladan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 {} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just curious to know, why do we have to do this? Also, do you think we should add this to other common packages like api, tracing etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if there are only dedicated tests for browser then this is the way to avoid testing them in mocha. Some of our packages already have things like this, for example collector has common tests and then separate for node and browser.