Skip to content

Commit 8c99771

Browse files
authored
fix(ember): Fix runtime config options not being merged (#3791)
Runtime options can be ignored in production mode because of @embroider macros differs between dev and production. Instead of relying on the config object reference being provided, we'll use a global instead to hold on to the merged config between the init call and the performance initializer code.
1 parent 4425e49 commit 8c99771

File tree

3 files changed

+32
-13
lines changed

3 files changed

+32
-13
lines changed

packages/ember/addon/index.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,33 @@ import { next } from '@ember/runloop';
55
import { assert, warn } from '@ember/debug';
66
import Ember from 'ember';
77
import { timestampWithMs } from '@sentry/utils';
8-
import { OwnConfig } from './types';
8+
import { GlobalConfig, OwnConfig } from './types';
9+
import { getGlobalObject } from '@sentry/utils';
910

1011
declare module '@ember/debug' {
1112
export function assert(desc: string, test: unknown): void;
1213
}
1314

15+
function _getSentryInitConfig() {
16+
const _global = getGlobalObject<GlobalConfig>();
17+
_global.__sentryEmberConfig = _global.__sentryEmberConfig ?? {};
18+
return _global.__sentryEmberConfig;
19+
}
20+
1421
export function InitSentryForEmber(_runtimeConfig: BrowserOptions | undefined) {
15-
const config = getOwnConfig<OwnConfig>().sentryConfig;
22+
const environmentConfig = getOwnConfig<OwnConfig>().sentryConfig;
1623

17-
assert('Missing configuration.', config);
18-
assert('Missing configuration for Sentry.', config.sentry || _runtimeConfig);
24+
assert('Missing configuration.', environmentConfig);
25+
assert('Missing configuration for Sentry.', environmentConfig.sentry || _runtimeConfig);
1926

20-
if (!config.sentry) {
27+
if (!environmentConfig.sentry) {
2128
// If environment config is not specified but the above assertion passes, use runtime config.
22-
config.sentry = { ..._runtimeConfig } as any;
29+
environmentConfig.sentry = { ..._runtimeConfig } as any;
2330
}
2431

25-
// Permanently merge options into config, preferring runtime config
26-
Object.assign(config.sentry, _runtimeConfig || {});
27-
const initConfig = Object.assign({}, config.sentry);
32+
// Merge runtime config into environment config, preferring runtime.
33+
Object.assign(environmentConfig.sentry, _runtimeConfig || {});
34+
const initConfig = Object.assign({}, environmentConfig.sentry);
2835

2936
initConfig._metadata = initConfig._metadata || {};
3037
initConfig._metadata.sdk = {
@@ -38,10 +45,14 @@ export function InitSentryForEmber(_runtimeConfig: BrowserOptions | undefined) {
3845
version: SDK_VERSION,
3946
};
4047

48+
// Persist Sentry init options so they are identical when performance initializers call init again.
49+
const sentryInitConfig = _getSentryInitConfig();
50+
Object.assign(sentryInitConfig, initConfig);
51+
4152
Sentry.init(initConfig);
4253

4354
if (macroCondition(isDevelopingApp())) {
44-
if (config.ignoreEmberOnErrorWarning) {
55+
if (environmentConfig.ignoreEmberOnErrorWarning) {
4556
return;
4657
}
4758
next(null, function() {

packages/ember/addon/instance-initializers/sentry-performance.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@ import * as Sentry from '@sentry/browser';
55
import { Span, Transaction, Integration } from '@sentry/types';
66
import { EmberRunQueues } from '@ember/runloop/-private/types';
77
import { getActiveTransaction } from '..';
8-
import { browserPerformanceTimeOrigin, timestampWithMs } from '@sentry/utils';
8+
import { browserPerformanceTimeOrigin, getGlobalObject, timestampWithMs } from '@sentry/utils';
99
import { macroCondition, isTesting, getOwnConfig } from '@embroider/macros';
10-
import { EmberSentryConfig, OwnConfig } from '../types';
10+
import { EmberSentryConfig, GlobalConfig, OwnConfig } from '../types';
1111

1212
function getSentryConfig() {
13-
return getOwnConfig<OwnConfig>().sentryConfig;
13+
const _global = getGlobalObject<GlobalConfig>();
14+
_global.__sentryEmberConfig = _global.__sentryEmberConfig ?? {};
15+
const environmentConfig = getOwnConfig<OwnConfig>().sentryConfig;
16+
Object.assign(environmentConfig.sentry, _global.__sentryEmberConfig);
17+
return environmentConfig;
1418
}
1519

1620
export function initialize(appInstance: ApplicationInstance): void {

packages/ember/addon/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@ export type EmberSentryConfig = {
1818
export type OwnConfig = {
1919
sentryConfig: EmberSentryConfig;
2020
};
21+
22+
export type GlobalConfig = {
23+
__sentryEmberConfig: EmberSentryConfig['sentry'];
24+
};

0 commit comments

Comments
 (0)