Skip to content

Commit

Permalink
feat(auto-instrumentations-node)!: disable @opentelemetry/instrumenta…
Browse files Browse the repository at this point in the history
…tion-fs by default (#2467)
  • Loading branch information
pichlermarc authored Oct 15, 2024
1 parent 0341e89 commit a558044
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
9 changes: 5 additions & 4 deletions metapackages/auto-instrumentations-node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ For example, to enable only the `env`, `host` detectors:
export OTEL_NODE_RESOURCE_DETECTORS="env,host"
```

By default, all [Supported Instrumentations](#supported-instrumentations) are enabled.
You can use the environment variable `OTEL_NODE_ENABLED_INSTRUMENTATIONS` to enable only certain instrumentations,
By default, all [Supported Instrumentations](#supported-instrumentations) are enabled, unless they are annotated with "default disabled".
You can use the environment variable `OTEL_NODE_ENABLED_INSTRUMENTATIONS` to enable only certain instrumentations, including "default disabled" ones
OR the environment variable `OTEL_NODE_DISABLED_INSTRUMENTATIONS` to disable only certain instrumentations,
by providing a comma-separated list of the instrumentation package names without the `@opentelemetry/instrumentation-` prefix.

Expand All @@ -91,10 +91,10 @@ instrumentations:
export OTEL_NODE_ENABLED_INSTRUMENTATIONS="http,nestjs-core"
```

To disable only [@opentelemetry/instrumentation-fs](https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/instrumentation-fs):
To disable only [@opentelemetry/instrumentation-net](https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/instrumentation-net):

```shell
export OTEL_NODE_DISABLED_INSTRUMENTATIONS="fs"
export OTEL_NODE_DISABLED_INSTRUMENTATIONS="net"
```

If both environment variables are set, `OTEL_NODE_ENABLED_INSTRUMENTATIONS` is applied first, and then `OTEL_NODE_DISABLED_INSTRUMENTATIONS` is applied to that list.
Expand Down Expand Up @@ -170,6 +170,7 @@ registerInstrumentations({
- [@opentelemetry/instrumentation-dns](https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-dns)
- [@opentelemetry/instrumentation-express](https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-express)
- [@opentelemetry/instrumentation-fastify](https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-fastify)
- [@opentelemetry/instrumentation-fs](https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/instrumentation-fs) (default disabled)
- [@opentelemetry/instrumentation-generic-pool](https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-generic-pool)
- [@opentelemetry/instrumentation-graphql](https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-graphql)
- [@opentelemetry/instrumentation-grpc](https://github.com/open-telemetry/opentelemetry-js/tree/main/experimental/packages/opentelemetry-instrumentation-grpc)
Expand Down
8 changes: 7 additions & 1 deletion metapackages/auto-instrumentations-node/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ const InstrumentationMap = {
'@opentelemetry/instrumentation-winston': WinstonInstrumentation,
};

const defaultExcludedInstrumentations = ['@opentelemetry/instrumentation-fs'];

// Config types inferred automatically from the first argument of the constructor
type ConfigArg<T> = T extends new (...args: infer U) => unknown ? U[0] : never;
export type InstrumentationConfigMap = {
Expand Down Expand Up @@ -208,10 +210,14 @@ function getInstrumentationsFromEnv(envVar: string): string[] {

/**
* Returns the list of instrumentations that are enabled based on the environment variable.
* If the environment variable is unset, returns all instrumentation that are enabled by default.
*/
function getEnabledInstrumentationsFromEnv() {
if (!process.env.OTEL_NODE_ENABLED_INSTRUMENTATIONS) {
return Object.keys(InstrumentationMap);
// all keys in the InstrumentationMap except for everything that is not enabled by default.
return Object.keys(InstrumentationMap).filter(
key => !defaultExcludedInstrumentations.includes(key)
);
}

const instrumentationsFromEnv = getInstrumentationsFromEnv(
Expand Down
21 changes: 19 additions & 2 deletions metapackages/auto-instrumentations-node/test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ import { getResourceDetectorsFromEnv } from '../src/utils';

describe('utils', () => {
describe('getNodeAutoInstrumentations', () => {
it('should include all installed instrumentations', () => {
it('should include all default instrumentations', () => {
const instrumentations = getNodeAutoInstrumentations();
const installedInstrumentations = Object.keys(
require('../package.json').dependencies
).filter(depName => {
return depName.startsWith('@opentelemetry/instrumentation-');
return (
depName.startsWith('@opentelemetry/instrumentation-') &&
depName !== '@opentelemetry/instrumentation-fs'
);
});

assert.deepStrictEqual(
Expand Down Expand Up @@ -89,6 +92,20 @@ describe('utils', () => {
}
});

it('should allow enabling non-default instrumentations via OTEL_NODE_ENABLED_INSTRUMENTATIONS environment variable', () => {
process.env.OTEL_NODE_ENABLED_INSTRUMENTATIONS = 'fs'; // separator with and without whitespaces should be allowed
try {
const instrumentations = getNodeAutoInstrumentations();

assert.deepStrictEqual(
new Set(instrumentations.map(i => i.instrumentationName)),
new Set(['@opentelemetry/instrumentation-fs'])
);
} finally {
delete process.env.OTEL_NODE_ENABLED_INSTRUMENTATIONS;
}
});

it('should include all instrumentations except those disabled via OTEL_NODE_DISABLED_INSTRUMENTATIONS environment variable', () => {
process.env.OTEL_NODE_DISABLED_INSTRUMENTATIONS =
'fs,aws-sdk, aws-lambda'; // separator with and without whitespaces should be allowed
Expand Down

0 comments on commit a558044

Please sign in to comment.