Skip to content

Commit c53609b

Browse files
mydeaschew2381
authored andcommitted
fix(loader): Ensure we add integrations (#47082)
I noticed one gap in our new loader implementation, namely that if you configure any integrations yourself, the defaults will not be added anymore. This PR fixes this by ensuring we add them only if they haven't been added by the user.
1 parent c570ef5 commit c53609b

File tree

3 files changed

+52
-30
lines changed

3 files changed

+52
-30
lines changed

src/sentry/templates/sentry/js-sdk-loader.js.tmpl

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,37 +60,45 @@
6060
}
6161
// Add loader as SDK source
6262
_window.SENTRY_SDK_SOURCE = 'loader';
63-
var SDK = _window[_namespace];
64-
var oldInit_1 = SDK.init;
65-
// Add necessary integrations based on config
66-
var integrations = [];
67-
if (_config.tracesSampleRate) {
68-
integrations.push(new SDK.BrowserTracing());
69-
}
70-
if (_config.replaysSessionSampleRate || _config.replaysOnErrorSampleRate) {
71-
integrations.push(new SDK.Replay());
72-
}
73-
if (integrations.length) {
74-
_config.integrations = integrations;
75-
}
63+
var SDK_1 = _window[_namespace];
64+
var oldInit_1 = SDK_1.init;
7665
// Configure it using provided DSN and config object
77-
SDK.init = function (options) {
66+
SDK_1.init = function (options) {
7867
var target = _config;
7968
for (var key in options) {
8069
if (Object.prototype.hasOwnProperty.call(options, key)) {
8170
target[key] = options[key];
8271
}
8372
}
73+
setupDefaultIntegrations(target, SDK_1);
8474
oldInit_1(target);
8575
};
86-
sdkLoaded(callbacks, SDK);
76+
sdkLoaded(callbacks, SDK_1);
8777
}
8878
catch (o_O) {
8979
console.error(o_O);
9080
}
9181
});
9282
_currentScriptTag.parentNode.insertBefore(_newScriptTag, _currentScriptTag);
9383
}
84+
// We want to ensure to only add default integrations if they haven't been added by the user.
85+
function setupDefaultIntegrations(config, SDK) {
86+
var integrations = config.integrations || [];
87+
// integrations can be a function, in which case we will not add any defaults
88+
if (!Array.isArray(integrations)) {
89+
return;
90+
}
91+
var integrationNames = integrations.map(function (integration) { return integration.name; });
92+
// Add necessary integrations based on config
93+
if (config.tracesSampleRate && integrationNames.indexOf('BrowserTracing') === -1) {
94+
integrations.push(new SDK.BrowserTracing());
95+
}
96+
if ((config.replaysSessionSampleRate || config.replaysOnErrorSampleRate) &&
97+
integrationNames.indexOf('Replay') === -1) {
98+
integrations.push(new SDK.Replay());
99+
}
100+
config.integrations = integrations;
101+
}
94102
function sdkIsLoaded() {
95103
var __sentry = _window.__SENTRY__;
96104
// If there is a global __SENTRY__ that means that in any of the callbacks init() was already invoked
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{% load sentry_helpers %}!function(n,e,t,r,o,a,i,c,_,p){for(var s=p,forceLoad=!1,f=0;f<document.scripts.length;f++)if(document.scripts[f].src.indexOf(i)>-1){s&&"no"===document.scripts[f].getAttribute("data-lazy")&&(s=!1);break}var u=!1,l=[],d=function(n){("e"in n||"p"in n||n.f&&n.f.indexOf("capture")>-1||n.f&&n.f.indexOf("showReportDialog")>-1)&&s&&E(l),d.data.push(n)};function E(i){if(!u){u=!0;var p=e.scripts[0],s=e.createElement(t);s.src=c,s.crossOrigin="anonymous",s.addEventListener("load",(function(){try{n[r]&&n[r].__SENTRY_LOADER__&&(n[r]=R),n[o]&&n[o].__SENTRY_LOADER__&&(n[o]=h),n.SENTRY_SDK_SOURCE="loader";var e=n[a],t=e.init,c=[];_.tracesSampleRate&&c.push(new e.BrowserTracing),(_.replaysSessionSampleRate||_.replaysOnErrorSampleRate)&&c.push(new e.Replay),c.length&&(_.integrations=c),e.init=function(n){var e=_;for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r]);t(e)},function(e,t){try{for(var a=0;a<e.length;a++)"function"==typeof e[a]&&e[a]();var i=d.data,c=!(void 0===(u=n.__SENTRY__)||!u.hub||!u.hub.getClient());i.sort((function(n){return"init"===n.f?-1:0}));var _=!1;for(a=0;a<i.length;a++)if(i[a].f){_=!0;var p=i[a];!1===c&&"init"!==p.f&&t.init(),c=!0,t[p.f].apply(t,p.a)}!1===c&&!1===_&&t.init();var s=n[r],f=n[o];for(a=0;a<i.length;a++)"e"in i[a]&&s?s.apply(n,i[a].e):"p"in i[a]&&f&&f.apply(n,[i[a].p])}catch(n){console.error(n)}var u}(i,e)}catch(n){console.error(n)}})),p.parentNode.insertBefore(s,p)}}d.data=[],n[a]=n[a]||{},n[a].onLoad=function(n){l.push(n),s&&!forceLoad||E(l)},n[a].forceLoad=function(){forceLoad=!0,s&&setTimeout((function(){E(l)}))},["init","addBreadcrumb","captureMessage","captureException","captureEvent","configureScope","withScope","showReportDialog"].forEach((function(e){n[a][e]=function(){d({f:e,a:arguments})}}));var R=n[r];n[r]=function(){d({e:[].slice.call(arguments)}),R&&R.apply(n,arguments)},n[r].__SENTRY_LOADER__=!0;var h=n[o];n[o]=function(e){d({p:"reason"in e?e.reason:"detail"in e&&"reason"in e.detail?e.detail.reason:e}),h&&h.apply(n,arguments)},n[o].__SENTRY_LOADER__=!0,s||setTimeout((function(){E(l)}))}(window,document,"script","onerror","onunhandledrejection","Sentry",'{{ publicKey|safe }}','{{ jsSdkUrl|safe }}',{{ config|to_json|safe }},{{ isLazy|safe|lower }});
1+
{% load sentry_helpers %}!function(n,e,r,t,a,i,o,c,_,f){for(var p=f,forceLoad=!1,s=0;s<document.scripts.length;s++)if(document.scripts[s].src.indexOf(o)>-1){p&&"no"===document.scripts[s].getAttribute("data-lazy")&&(p=!1);break}var u=!1,l=[],d=function(n){("e"in n||"p"in n||n.f&&n.f.indexOf("capture")>-1||n.f&&n.f.indexOf("showReportDialog")>-1)&&p&&R(l),d.data.push(n)};function R(o){if(!u){u=!0;var f=e.scripts[0],p=e.createElement(r);p.src=c,p.crossOrigin="anonymous",p.addEventListener("load",(function(){try{n[t]&&n[t].__SENTRY_LOADER__&&(n[t]=E),n[a]&&n[a].__SENTRY_LOADER__&&(n[a]=v),n.SENTRY_SDK_SOURCE="loader";var e=n[i],r=e.init;e.init=function(n){var t=_;for(var a in n)Object.prototype.hasOwnProperty.call(n,a)&&(t[a]=n[a]);!function(n,e){var r=n.integrations||[];if(!Array.isArray(r))return;var t=r.map((function(n){return n.name}));n.tracesSampleRate&&-1===t.indexOf("BrowserTracing")&&r.push(new e.BrowserTracing);(n.replaysSessionSampleRate||n.replaysOnErrorSampleRate)&&-1===t.indexOf("Replay")&&r.push(new e.Replay);n.integrations=r}(t,e),r(t)},function(e,r){try{for(var i=0;i<e.length;i++)"function"==typeof e[i]&&e[i]();var o=d.data,c=!(void 0===(u=n.__SENTRY__)||!u.hub||!u.hub.getClient());o.sort((function(n){return"init"===n.f?-1:0}));var _=!1;for(i=0;i<o.length;i++)if(o[i].f){_=!0;var f=o[i];!1===c&&"init"!==f.f&&r.init(),c=!0,r[f.f].apply(r,f.a)}!1===c&&!1===_&&r.init();var p=n[t],s=n[a];for(i=0;i<o.length;i++)"e"in o[i]&&p?p.apply(n,o[i].e):"p"in o[i]&&s&&s.apply(n,[o[i].p])}catch(n){console.error(n)}var u}(o,e)}catch(n){console.error(n)}})),f.parentNode.insertBefore(p,f)}}d.data=[],n[i]=n[i]||{},n[i].onLoad=function(n){l.push(n),p&&!forceLoad||R(l)},n[i].forceLoad=function(){forceLoad=!0,p&&setTimeout((function(){R(l)}))},["init","addBreadcrumb","captureMessage","captureException","captureEvent","configureScope","withScope","showReportDialog"].forEach((function(e){n[i][e]=function(){d({f:e,a:arguments})}}));var E=n[t];n[t]=function(){d({e:[].slice.call(arguments)}),E&&E.apply(n,arguments)},n[t].__SENTRY_LOADER__=!0;var v=n[a];n[a]=function(e){d({p:"reason"in e?e.reason:"detail"in e&&"reason"in e.detail?e.detail.reason:e}),v&&v.apply(n,arguments)},n[a].__SENTRY_LOADER__=!0,p||setTimeout((function(){R(l)}))}(window,document,"script","onerror","onunhandledrejection","Sentry",'{{ publicKey|safe }}','{{ jsSdkUrl|safe }}',{{ config|to_json|safe }},{{ isLazy|safe|lower }});

src/sentry/templates/sentry/js-sdk-loader.ts

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -92,20 +92,6 @@ declare const __LOADER__IS_LAZY__: any;
9292

9393
const oldInit = SDK.init;
9494

95-
// Add necessary integrations based on config
96-
const integrations: unknown[] = [];
97-
if (_config.tracesSampleRate) {
98-
integrations.push(new SDK.BrowserTracing());
99-
}
100-
101-
if (_config.replaysSessionSampleRate || _config.replaysOnErrorSampleRate) {
102-
integrations.push(new SDK.Replay());
103-
}
104-
105-
if (integrations.length) {
106-
_config.integrations = integrations;
107-
}
108-
10995
// Configure it using provided DSN and config object
11096
SDK.init = function (options) {
11197
const target = _config;
@@ -114,6 +100,8 @@ declare const __LOADER__IS_LAZY__: any;
114100
target[key] = options[key];
115101
}
116102
}
103+
104+
setupDefaultIntegrations(target, SDK);
117105
oldInit(target);
118106
};
119107

@@ -126,6 +114,32 @@ declare const __LOADER__IS_LAZY__: any;
126114
_currentScriptTag.parentNode!.insertBefore(_newScriptTag, _currentScriptTag);
127115
}
128116

117+
// We want to ensure to only add default integrations if they haven't been added by the user.
118+
function setupDefaultIntegrations(config: any, SDK: any) {
119+
const integrations: {name: string}[] = config.integrations || [];
120+
121+
// integrations can be a function, in which case we will not add any defaults
122+
if (!Array.isArray(integrations)) {
123+
return;
124+
}
125+
126+
const integrationNames = integrations.map(integration => integration.name);
127+
128+
// Add necessary integrations based on config
129+
if (config.tracesSampleRate && integrationNames.indexOf('BrowserTracing') === -1) {
130+
integrations.push(new SDK.BrowserTracing());
131+
}
132+
133+
if (
134+
(config.replaysSessionSampleRate || config.replaysOnErrorSampleRate) &&
135+
integrationNames.indexOf('Replay') === -1
136+
) {
137+
integrations.push(new SDK.Replay());
138+
}
139+
140+
config.integrations = integrations;
141+
}
142+
129143
function sdkIsLoaded() {
130144
const __sentry = _window.__SENTRY__;
131145
// If there is a global __SENTRY__ that means that in any of the callbacks init() was already invoked

0 commit comments

Comments
 (0)