Skip to content

Commit e344829

Browse files
committed
set default environment variable earlier so it can be used in tracestate
1 parent 50544ce commit e344829

File tree

5 files changed

+37
-31
lines changed

5 files changed

+37
-31
lines changed

packages/core/src/baseclient.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,8 +399,8 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
399399
const options = this.getOptions();
400400
const { environment, release, dist, maxValueLength = 250 } = options;
401401

402-
if (!('environment' in event)) {
403-
event.environment = 'environment' in options ? environment : 'production';
402+
if (event.environment === undefined && environment !== undefined) {
403+
event.environment = environment;
404404
}
405405

406406
if (event.release === undefined && release !== undefined) {

packages/core/src/sdk.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export function initAndBind<F extends Client, O extends Options>(clientClass: Cl
1616
if (options.debug === true) {
1717
logger.enable();
1818
}
19+
options.environment = options.environment || 'production';
1920
const hub = getCurrentHub();
2021
const client = new clientClass(options);
2122
hub.bindClient(client);

packages/core/test/lib/base.test.ts

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,6 @@ describe('BaseClient', () => {
176176
const client = new TestClient({ dsn: PUBLIC_DSN });
177177
client.captureException(new Error('test exception'));
178178
expect(TestBackend.instance!.event).toEqual({
179-
environment: 'production',
180179
event_id: '42',
181180
exception: {
182181
values: [
@@ -245,7 +244,6 @@ describe('BaseClient', () => {
245244
const client = new TestClient({ dsn: PUBLIC_DSN });
246245
client.captureMessage('test message');
247246
expect(TestBackend.instance!.event).toEqual({
248-
environment: 'production',
249247
event_id: '42',
250248
level: 'info',
251249
message: 'test message',
@@ -321,7 +319,6 @@ describe('BaseClient', () => {
321319
client.captureEvent({ message: 'message' }, undefined, scope);
322320
expect(TestBackend.instance!.event!.message).toBe('message');
323321
expect(TestBackend.instance!.event).toEqual({
324-
environment: 'production',
325322
event_id: '42',
326323
message: 'message',
327324
timestamp: 2020,
@@ -335,7 +332,6 @@ describe('BaseClient', () => {
335332
client.captureEvent({ message: 'message', timestamp: 1234 }, undefined, scope);
336333
expect(TestBackend.instance!.event!.message).toBe('message');
337334
expect(TestBackend.instance!.event).toEqual({
338-
environment: 'production',
339335
event_id: '42',
340336
message: 'message',
341337
timestamp: 1234,
@@ -348,28 +344,12 @@ describe('BaseClient', () => {
348344
const scope = new Scope();
349345
client.captureEvent({ message: 'message' }, { event_id: 'wat' }, scope);
350346
expect(TestBackend.instance!.event!).toEqual({
351-
environment: 'production',
352347
event_id: 'wat',
353348
message: 'message',
354349
timestamp: 2020,
355350
});
356351
});
357352

358-
test('sets default environment to `production` it none provided', () => {
359-
expect.assertions(1);
360-
const client = new TestClient({
361-
dsn: PUBLIC_DSN,
362-
});
363-
const scope = new Scope();
364-
client.captureEvent({ message: 'message' }, undefined, scope);
365-
expect(TestBackend.instance!.event!).toEqual({
366-
environment: 'production',
367-
event_id: '42',
368-
message: 'message',
369-
timestamp: 2020,
370-
});
371-
});
372-
373353
test('adds the configured environment', () => {
374354
expect.assertions(1);
375355
const client = new TestClient({
@@ -411,7 +391,6 @@ describe('BaseClient', () => {
411391
const scope = new Scope();
412392
client.captureEvent({ message: 'message' }, undefined, scope);
413393
expect(TestBackend.instance!.event!).toEqual({
414-
environment: 'production',
415394
event_id: '42',
416395
message: 'message',
417396
release: 'v1.0.0',
@@ -452,7 +431,6 @@ describe('BaseClient', () => {
452431
scope.setUser({ id: 'user' });
453432
client.captureEvent({ message: 'message' }, undefined, scope);
454433
expect(TestBackend.instance!.event!).toEqual({
455-
environment: 'production',
456434
event_id: '42',
457435
extra: { b: 'b' },
458436
message: 'message',
@@ -469,7 +447,6 @@ describe('BaseClient', () => {
469447
scope.setFingerprint(['abcd']);
470448
client.captureEvent({ message: 'message' }, undefined, scope);
471449
expect(TestBackend.instance!.event!).toEqual({
472-
environment: 'production',
473450
event_id: '42',
474451
fingerprint: ['abcd'],
475452
message: 'message',
@@ -515,7 +492,6 @@ describe('BaseClient', () => {
515492
expect(TestBackend.instance!.event!).toEqual({
516493
breadcrumbs: [normalizedBreadcrumb, normalizedBreadcrumb, normalizedBreadcrumb],
517494
contexts: normalizedObject,
518-
environment: 'production',
519495
event_id: '42',
520496
extra: normalizedObject,
521497
timestamp: 2020,
@@ -561,7 +537,6 @@ describe('BaseClient', () => {
561537
expect(TestBackend.instance!.event!).toEqual({
562538
breadcrumbs: [normalizedBreadcrumb, normalizedBreadcrumb, normalizedBreadcrumb],
563539
contexts: normalizedObject,
564-
environment: 'production',
565540
event_id: '42',
566541
extra: normalizedObject,
567542
timestamp: 2020,
@@ -612,7 +587,6 @@ describe('BaseClient', () => {
612587
expect(TestBackend.instance!.event!).toEqual({
613588
breadcrumbs: [normalizedBreadcrumb, normalizedBreadcrumb, normalizedBreadcrumb],
614589
contexts: normalizedObject,
615-
environment: 'production',
616590
event_id: '42',
617591
extra: normalizedObject,
618592
timestamp: 2020,

packages/core/test/lib/sdk.test.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,19 @@ jest.mock('@sentry/hub', () => ({
1414
bindClient(client: Client): boolean;
1515
getClient(): boolean;
1616
} {
17-
return {
17+
const mockHub = {
18+
_stack: [],
1819
getClient(): boolean {
1920
return false;
2021
},
2122
bindClient(client: Client): boolean {
23+
(this._stack as any[]).push({ client });
2224
client.setupIntegrations();
2325
return true;
2426
},
2527
};
28+
global.__SENTRY__.hub = mockHub;
29+
return mockHub;
2630
},
2731
}));
2832

@@ -41,6 +45,15 @@ describe('SDK', () => {
4145
});
4246

4347
describe('initAndBind', () => {
48+
test("sets environment to 'production' if none is provided", () => {
49+
initAndBind(TestClient, { dsn: PUBLIC_DSN });
50+
expect(global.__SENTRY__.hub._stack[0].client.getOptions().environment).toEqual('production');
51+
});
52+
test("doesn't overwrite given environment", () => {
53+
initAndBind(TestClient, { dsn: PUBLIC_DSN, environment: 'dogpark' });
54+
expect(global.__SENTRY__.hub._stack[0].client.getOptions().environment).toEqual('dogpark');
55+
});
56+
4457
test('installs default integrations', () => {
4558
const DEFAULT_INTEGRATIONS: Integration[] = [
4659
new MockIntegration('MockIntegration 1'),

packages/tracing/test/hub.test.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable @typescript-eslint/unbound-method */
2-
import { BrowserClient } from '@sentry/browser';
3-
import { getMainCarrier, Hub } from '@sentry/hub';
2+
import { BrowserClient, init as initSDK } from '@sentry/browser';
3+
import { getCurrentHub, getMainCarrier, Hub } from '@sentry/hub';
44
import * as hubModule from '@sentry/hub';
55
import * as utilsModule from '@sentry/utils'; // for mocking
66
import { computeTracestate, getGlobalObject, isNodeEnv, logger } from '@sentry/utils';
@@ -66,6 +66,24 @@ describe('Hub', () => {
6666

6767
expect(transaction.tracestate).toEqual(b64Value);
6868
});
69+
70+
it('uses default environment if none given', () => {
71+
const release = 'off.leash.park';
72+
initSDK({
73+
dsn: 'https://[email protected]/12312012',
74+
release,
75+
});
76+
const transaction = getCurrentHub().startTransaction({ name: 'FETCH /ball' });
77+
78+
const b64Value = computeTracestate({
79+
trace_id: transaction.traceId,
80+
environment: 'production',
81+
release,
82+
public_key: 'dogsarebadatkeepingsecrets',
83+
});
84+
85+
expect(transaction.tracestate).toEqual(b64Value);
86+
});
6987
});
7088

7189
describe('getTransaction()', () => {

0 commit comments

Comments
 (0)