Skip to content

Commit

Permalink
feat: provide global opt-out switch (DASH0_DISABLE)
Browse files Browse the repository at this point in the history
  • Loading branch information
basti1302 committed Jun 10, 2024
1 parent a544e24 commit 6657064
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ This can be disabled either by setting `OTEL_SERVICE_NAME` or by setting `DASH0_

Additional debug logs can be enabled by setting `DASH0_DEBUG=true`.

### <a id="DASH0_DISABLE">DASH0_DISABLE</a>

Disables the Dash0 Node.js distribution entirely.

### <a id="DASH0_ENABLE_FS_INSTRUMENTATION">DASH0_ENABLE_FS_INSTRUMENTATION</a>

By default, the instrumentation plug-in `@opentelemetry/instrumentation-fs` is disabled. Set `DASH0_ENABLE_FS_INSTRUMENTATION=true` to enable spans for file system access.
Expand Down
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ function init() {
}
}

init();
if (process.env.DASH0_DISABLE == null || process.env.DASH0_DISABLE.toLowerCase() !== 'true') {
init();
} else {
logProhibitiveError(`The distribution has been disabled by setting DASH0_DISABLE=${process.env.DASH0_DISABLE}.`);
}

function logProhibitiveError(message: string) {
console.error(`[${prefix}] ${message} OpenTelemetry data will not be sent to Dash0.`);
Expand Down
4 changes: 4 additions & 0 deletions test/collector/CollectorChildProcessWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ export default class CollectorChildProcessWrapper extends ChildProcessWrapper {
async fetchTelemetry() {
return <OpenTelemetryData>await collector().sendRequest({ command: 'telemetry' });
}

async clear() {
await collector().sendRequest({ command: 'clear' });
}
}
8 changes: 8 additions & 0 deletions test/collector/Sink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ export default class Sink {
return this.telemetry;
}

clear() {
this.telemetry = {
traces: [],
metrics: [],
logs: [],
};
}

printStats() {
console.log(JSON.stringify(this.stats(), null, 2));
}
Expand Down
3 changes: 3 additions & 0 deletions test/collector/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ function registerIpcMessageListener() {
case 'telemetry':
sendToParentProcess({ id, ...sink.getTelemetry() });
break;
case 'clear':
sink.clear();
break;
default:
console.error(`Unknown message: ${inspect(message)}`);
}
Expand Down
4 changes: 4 additions & 0 deletions test/integration/rootHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export const mochaHooks = {
console.debug('[rootHooks] global mock collector started');
},

async beforeEach() {
collectorInstance.clear();
},

async afterAll() {
console.debug('[rootHooks] stopping global mock collector');
await collectorInstance.stop();
Expand Down
36 changes: 35 additions & 1 deletion test/integration/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ import runCommand from '../util/runCommand';
import waitUntil from '../util/waitUntil';
import ChildProcessWrapper, { defaultAppConfiguration } from './ChildProcessWrapper';
import { collector } from './rootHooks';
import delay from '../util/delay';

const skipWhenNodeJsVersionIsSmallerThan = '18.0.0';

const { fail } = expect;

const appPort = 1302;
let expectedDistroVersion: number;

Expand Down Expand Up @@ -104,7 +107,7 @@ describe('attach', () => {
await appUnderTest.stop();
});

it('should attach via --require and derive a service name from the package.json file ', async () => {
it('should attach via --require and derive a service name from the package.json file', async () => {
await waitUntil(async () => {
const telemetry = await waitForTelemetry();
expectMatchingSpan(
Expand All @@ -122,6 +125,37 @@ describe('attach', () => {
});
});

describe('disable via DASH0_DISABLE', () => {
let appUnderTest: ChildProcessWrapper;

before(async () => {
const appConfiguration = defaultAppConfiguration(appPort);
if (!appConfiguration.env) {
appConfiguration.env = {};
}
appConfiguration.env.DASH0_DISABLE = 'true';
appUnderTest = new ChildProcessWrapper(appConfiguration);
await appUnderTest.start();
});

after(async () => {
await appUnderTest.stop();
});

it('should do nothing if disabled', async () => {
await delay(1000);
const response = await fetch(`http://localhost:${appPort}/ohai`);
await delay(2000);
expect(response.status).to.equal(200);
const responsePayload = await response.json();
expect(responsePayload).to.deep.equal({ message: 'We make Observability easy for every developer.' });

if (await collector().hasTelemetry()) {
fail('The collector received telemetry data although it should not have received anything.');
}
});
});

async function waitForTelemetry() {
const response = await fetch(`http://localhost:${appPort}/ohai`);
expect(response.status).to.equal(200);
Expand Down

0 comments on commit 6657064

Please sign in to comment.