-
Notifications
You must be signed in to change notification settings - Fork 804
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: creating one auto loader for instrumentation and old plugins (#…
…1731) * feat: creating one auto loader for instrumentation and old plugins * feat: adding temporary tests for autoloader * chore: adding auto loader for instrumentation and plugins * chore: removing temporary test * chore: reverting changes done temporary * chore: linting * chore: updating submodule for opentelemetry-proto * chore: updating submodule for exporter-collector-grpc
- Loading branch information
Showing
44 changed files
with
1,940 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Dependency directories | ||
!test/node/node_modules |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright The 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 * as api from '@opentelemetry/api'; | ||
import { | ||
disableInstrumentations, | ||
enableInstrumentations, | ||
parseInstrumentationOptions, | ||
} from './autoLoaderUtils'; | ||
import { loadOldPlugins } from './platform'; | ||
import { AutoLoaderOptions } from './types_internal'; | ||
|
||
/** | ||
* It will register instrumentations and plugins | ||
* @param options | ||
* @return returns function to unload instrumentation and plugins that were | ||
* registered | ||
*/ | ||
export function registerInstrumentations( | ||
options: AutoLoaderOptions | ||
): () => void { | ||
const { | ||
instrumentations, | ||
pluginsNode, | ||
pluginsWeb, | ||
} = parseInstrumentationOptions(options.instrumentations); | ||
const tracerWithLogger = (options.tracerProvider as unknown) as { | ||
logger: api.Logger; | ||
}; | ||
const tracerProvider = | ||
options.tracerProvider || api.trace.getTracerProvider(); | ||
const meterProvider = options.meterProvider || api.metrics.getMeterProvider(); | ||
const logger = | ||
options.logger || tracerWithLogger?.logger || new api.NoopLogger(); | ||
|
||
enableInstrumentations( | ||
instrumentations, | ||
logger, | ||
tracerProvider, | ||
meterProvider | ||
); | ||
|
||
const unload = loadOldPlugins( | ||
pluginsNode, | ||
pluginsWeb, | ||
logger, | ||
tracerProvider | ||
); | ||
|
||
return () => { | ||
unload(); | ||
disableInstrumentations(instrumentations); | ||
}; | ||
} |
94 changes: 94 additions & 0 deletions
94
packages/opentelemetry-instrumentation/src/autoLoaderUtils.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,94 @@ | ||
/* | ||
* Copyright The 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, MeterProvider, TracerProvider } from '@opentelemetry/api'; | ||
import { Instrumentation } from './types'; | ||
import { AutoLoaderResult, InstrumentationOption } from './types_internal'; | ||
|
||
import { | ||
NodePlugins, | ||
NodePluginsTracerConfiguration, | ||
OldClassPlugin, | ||
} from './types_plugin_only'; | ||
|
||
/** | ||
* Parses the options and returns instrumentations, node plugins and | ||
* web plugins | ||
* @param options | ||
*/ | ||
export function parseInstrumentationOptions( | ||
options: InstrumentationOption[] = [] | ||
): AutoLoaderResult { | ||
let instrumentations: Instrumentation[] = []; | ||
let pluginsNode: NodePlugins = {}; | ||
let pluginsWeb: OldClassPlugin[] = []; | ||
for (let i = 0, j = options.length; i < j; i++) { | ||
const option = options[i] as any; | ||
if (Array.isArray(option)) { | ||
const results = parseInstrumentationOptions(option); | ||
instrumentations = instrumentations.concat(results.instrumentations); | ||
pluginsWeb = pluginsWeb.concat(results.pluginsWeb); | ||
pluginsNode = Object.assign({}, pluginsNode, results.pluginsNode); | ||
} else if ((option as NodePluginsTracerConfiguration).plugins) { | ||
pluginsNode = Object.assign( | ||
{}, | ||
pluginsNode, | ||
(option as NodePluginsTracerConfiguration).plugins | ||
); | ||
} else if (typeof option === 'function') { | ||
instrumentations.push(new option()); | ||
} else if ((option as Instrumentation).instrumentationName) { | ||
instrumentations.push(option); | ||
} else if ((option as OldClassPlugin).moduleName) { | ||
pluginsWeb.push(option as OldClassPlugin); | ||
} | ||
} | ||
|
||
return { instrumentations, pluginsNode, pluginsWeb }; | ||
} | ||
|
||
/** | ||
* Enable instrumentations | ||
* @param instrumentations | ||
* @param logger | ||
* @param tracerProvider | ||
* @param meterProvider | ||
*/ | ||
export function enableInstrumentations( | ||
instrumentations: Instrumentation[], | ||
logger: Logger, | ||
tracerProvider?: TracerProvider, | ||
meterProvider?: MeterProvider | ||
) { | ||
for (let i = 0, j = instrumentations.length; i < j; i++) { | ||
const instrumentation = instrumentations[i]; | ||
if (tracerProvider) { | ||
instrumentation.setTracerProvider(tracerProvider); | ||
} | ||
if (meterProvider) { | ||
instrumentation.setMeterProvider(meterProvider); | ||
} | ||
instrumentation.enable(); | ||
} | ||
} | ||
|
||
/** | ||
* Disable instrumentations | ||
* @param instrumentations | ||
*/ | ||
export function disableInstrumentations(instrumentations: Instrumentation[]) { | ||
instrumentations.forEach(instrumentation => instrumentation.disable()); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,4 @@ | |
*/ | ||
|
||
export * from './instrumentation'; | ||
export * from './old/autoLoader'; |
44 changes: 44 additions & 0 deletions
44
packages/opentelemetry-instrumentation/src/platform/browser/old/autoLoader.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,44 @@ | ||
/* | ||
* Copyright The 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. | ||
*/ | ||
|
||
// This should be removed after plugins are gone | ||
|
||
import * as api from '@opentelemetry/api'; | ||
import { NodePlugins, OldClassPlugin } from '../../../types_plugin_only'; | ||
|
||
/** | ||
* Loads provided web plugins | ||
* @param pluginsNode | ||
* @param pluginsWeb | ||
* @param logger | ||
* @param tracerProvider | ||
* @return returns function to disable all plugins | ||
*/ | ||
export function loadOldPlugins( | ||
pluginsNode: NodePlugins, | ||
pluginsWeb: OldClassPlugin[], | ||
logger: api.Logger, | ||
tracerProvider: api.TracerProvider | ||
): () => void { | ||
pluginsWeb.forEach(plugin => { | ||
plugin.enable([], tracerProvider, logger); | ||
}); | ||
return () => { | ||
pluginsWeb.forEach(plugin => { | ||
plugin.disable(); | ||
}); | ||
}; | ||
} |
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
Oops, something went wrong.