-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
getInstrumentations
method (#82)
This adds an export (with an API very similar to the getInstrumentations from `@opentelemetry/auto-instrumentations-node`) that users of this distro can use to cleanly customize the default set of provided instrumentations.
- Loading branch information
1 parent
5f3b9b8
commit 9002961
Showing
8 changed files
with
114 additions
and
22 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
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 |
---|---|---|
@@ -1,7 +1,9 @@ | ||
const {ElasticNodeSDK} = require('./sdk'); | ||
const {getInstrumentations} = require('./instrumentations'); | ||
|
||
// TODO: this should reexport things from @otel/sdk-node (like 'api', 'core', etc.) | ||
|
||
module.exports = { | ||
ElasticNodeSDK, | ||
getInstrumentations, | ||
}; |
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,66 @@ | ||
/** | ||
* @typedef {import('@opentelemetry/instrumentation').Instrumentation} Instrumentation | ||
* | ||
* @callback InstrumentationFactory | ||
* @returns {Instrumentation} | ||
* | ||
* @typedef {{ | ||
* "@opentelemetry/instrumentation-http": import('@opentelemetry/instrumentation-http').HttpInstrumentationConfig | InstrumentationFactory, | ||
* "@opentelemetry/instrumentation-express": import('@opentelemetry/instrumentation-express').ExpressInstrumentationConfig | InstrumentationFactory | ||
* }} InstrumentaionsMap | ||
*/ | ||
|
||
const {HttpInstrumentation} = require('@opentelemetry/instrumentation-http'); | ||
const { | ||
ExpressInstrumentation, | ||
} = require('@opentelemetry/instrumentation-express'); | ||
|
||
// Instrumentations attach their Hook (for require-in-the-middle or import-in-the-middle) | ||
// when the `enable` method is called and this happens inside their constructor | ||
// https://github.com/open-telemetry/opentelemetry-js/blob/1b4999f386e0240b7f65350e8360ccc2930b0fe6/experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts#L71 | ||
// | ||
// The SDK cannot construct any instrumentation until it has resolved the config. So to | ||
// do a lazy creation of instrumentations we have factory functions that can receive | ||
// the user's config and can default to something else if needed. | ||
/** @type {Record<keyof InstrumentaionsMap, (cfg: any) => Instrumentation>} */ | ||
const INSTRUMENTATIONS = { | ||
'@opentelemetry/instrumentation-http': (cfg) => | ||
new HttpInstrumentation(cfg), | ||
'@opentelemetry/instrumentation-express': (cfg) => | ||
new ExpressInstrumentation(cfg), | ||
}; | ||
|
||
/** | ||
* Get the list of instrumentations baed on options | ||
* @param {Partial<InstrumentaionsMap>} [opts={}] | ||
* @returns {Array<Instrumentation>} | ||
*/ | ||
function getInstrumentations(opts = {}) { | ||
/** @type {Array<Instrumentation>} */ | ||
const instrumentations = []; | ||
|
||
Object.keys(INSTRUMENTATIONS).forEach((name) => { | ||
const isFactory = typeof opts[name] === 'function'; | ||
const isObject = typeof opts[name] === 'object'; | ||
const instrFactory = isFactory ? opts[name] : INSTRUMENTATIONS[name]; | ||
const instrConfig = isObject ? opts[name] : undefined; | ||
|
||
// We should instantiate a instrumentation if: | ||
// - there is no config passed (elastic SDK will use its defaults) | ||
// - the configuration passed is not disabling it | ||
let instr; | ||
if (!instrConfig || instrConfig.enabled !== false) { | ||
instr = instrFactory(instrConfig); | ||
} | ||
|
||
if (instr) { | ||
instrumentations.push(instr); | ||
} | ||
}); | ||
|
||
return instrumentations; | ||
} | ||
|
||
module.exports = { | ||
getInstrumentations, | ||
}; |
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,3 @@ | ||
// TODO: check if we need to reexport types from instrumentations | ||
export {getInstrumentations} from './instrumentations'; | ||
export {ElasticNodeSDK} from './sdk'; |
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