Skip to content

Commit 35e78c6

Browse files
authored
feat(node): Add support for SENTRY_DEBUG env variable (#15972)
- set the `debug` option by reading `options.debug ?? process.env.SENTRY_DEBUG` - add unit test for a bunch of supported env variables
1 parent bbe0589 commit 35e78c6

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

packages/node/src/sdk/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ function getClientOptions(
214214
release,
215215
tracesSampleRate,
216216
spotlight,
217+
debug: envToBool(options.debug ?? process.env.SENTRY_DEBUG),
217218
};
218219

219220
const integrations = options.integrations;

packages/node/test/sdk/init.test.ts

+107
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,113 @@ describe('init()', () => {
149149

150150
expect(client).toBeInstanceOf(NodeClient);
151151
});
152+
153+
describe('environment variable options', () => {
154+
const originalProcessEnv = { ...process.env };
155+
156+
afterEach(() => {
157+
process.env = originalProcessEnv;
158+
global.__SENTRY__ = {};
159+
cleanupOtel();
160+
vi.clearAllMocks();
161+
});
162+
163+
it('sets debug from `SENTRY_DEBUG` env variable', () => {
164+
process.env.SENTRY_DEBUG = '1';
165+
166+
const client = init({ dsn: PUBLIC_DSN });
167+
168+
expect(client?.getOptions()).toEqual(
169+
expect.objectContaining({
170+
debug: true,
171+
}),
172+
);
173+
});
174+
175+
it('prefers `debug` option over `SENTRY_DEBUG` env variable', () => {
176+
process.env.SENTRY_DEBUG = '1';
177+
178+
const client = init({ dsn: PUBLIC_DSN, debug: false });
179+
180+
expect(client?.getOptions()).toEqual(
181+
expect.objectContaining({
182+
debug: false,
183+
}),
184+
);
185+
});
186+
187+
it('sets tracesSampleRate from `SENTRY_TRACES_SAMPLE_RATE` env variable', () => {
188+
process.env.SENTRY_TRACES_SAMPLE_RATE = '0.5';
189+
190+
const client = init({ dsn: PUBLIC_DSN });
191+
192+
expect(client?.getOptions()).toEqual(
193+
expect.objectContaining({
194+
tracesSampleRate: 0.5,
195+
}),
196+
);
197+
});
198+
199+
it('prefers `tracesSampleRate` option over `SENTRY_TRACES_SAMPLE_RATE` env variable', () => {
200+
process.env.SENTRY_TRACES_SAMPLE_RATE = '0.5';
201+
202+
const client = init({ dsn: PUBLIC_DSN, tracesSampleRate: 0.1 });
203+
204+
expect(client?.getOptions()).toEqual(
205+
expect.objectContaining({
206+
tracesSampleRate: 0.1,
207+
}),
208+
);
209+
});
210+
211+
it('sets release from `SENTRY_RELEASE` env variable', () => {
212+
process.env.SENTRY_RELEASE = '1.0.0';
213+
214+
const client = init({ dsn: PUBLIC_DSN });
215+
216+
expect(client?.getOptions()).toEqual(
217+
expect.objectContaining({
218+
release: '1.0.0',
219+
}),
220+
);
221+
});
222+
223+
it('prefers `release` option over `SENTRY_RELEASE` env variable', () => {
224+
process.env.SENTRY_RELEASE = '1.0.0';
225+
226+
const client = init({ dsn: PUBLIC_DSN, release: '2.0.0' });
227+
228+
expect(client?.getOptions()).toEqual(
229+
expect.objectContaining({
230+
release: '2.0.0',
231+
}),
232+
);
233+
});
234+
235+
it('sets environment from `SENTRY_ENVIRONMENT` env variable', () => {
236+
process.env.SENTRY_ENVIRONMENT = 'production';
237+
238+
const client = init({ dsn: PUBLIC_DSN });
239+
240+
expect(client?.getOptions()).toEqual(
241+
expect.objectContaining({
242+
environment: 'production',
243+
}),
244+
);
245+
});
246+
247+
it('prefers `environment` option over `SENTRY_ENVIRONMENT` env variable', () => {
248+
process.env.SENTRY_ENVIRONMENT = 'production';
249+
250+
const client = init({ dsn: PUBLIC_DSN, environment: 'staging' });
251+
252+
expect(client?.getOptions()).toEqual(
253+
expect.objectContaining({
254+
environment: 'staging',
255+
}),
256+
);
257+
});
258+
});
152259
});
153260

154261
describe('validateOpenTelemetrySetup', () => {

0 commit comments

Comments
 (0)