@sentry/core
Version:
Base implementation for all Sentry JavaScript SDKs
141 lines (140 loc) • 5.64 kB
JavaScript
import { getClient } from "./currentScopes.js";
import { DEBUG_BUILD } from "./debug-build.js";
import { debug } from "./utils/debug-logger.js";
// src/integration.ts
const installedIntegrations = [];
/**
* Remove duplicates from the given array, preferring the last instance of any duplicate. Not guaranteed to
* preserve the order of integrations in the array.
*
* @private
*/
function filterDuplicates(integrations) {
const integrationsByName = {};
integrations.forEach((currentInstance) => {
const { name } = currentInstance;
const existingInstance = integrationsByName[name];
if (existingInstance && !existingInstance.isDefaultInstance && currentInstance.isDefaultInstance) return;
integrationsByName[name] = currentInstance;
});
return Object.values(integrationsByName);
}
/** Gets integrations to install */
function getIntegrationsToSetup(options) {
const defaultIntegrations = options.defaultIntegrations || [];
const userIntegrations = options.integrations;
defaultIntegrations.forEach((integration) => {
integration.isDefaultInstance = true;
});
let integrations;
if (Array.isArray(userIntegrations)) integrations = [...defaultIntegrations, ...userIntegrations];
else if (typeof userIntegrations === "function") {
const resolvedUserIntegrations = userIntegrations(defaultIntegrations);
integrations = Array.isArray(resolvedUserIntegrations) ? resolvedUserIntegrations : [resolvedUserIntegrations];
} else integrations = defaultIntegrations;
return filterDuplicates(integrations);
}
/**
* Given a list of integration instances this installs them all. When `withDefaults` is set to `true` then all default
* integrations are added unless they were already provided before.
* @param integrations array of integration instances
* @param withDefault should enable default integrations
*/
function setupIntegrations(client, integrations) {
const integrationIndex = {};
integrations.forEach((integration) => {
if (integration?.beforeSetup) integration.beforeSetup(client);
});
integrations.forEach((integration) => {
if (integration) setupIntegration(client, integration, integrationIndex);
});
return integrationIndex;
}
/**
* Execute the `afterAllSetup` hooks of the given integrations.
*/
function afterSetupIntegrations(client, integrations) {
for (const integration of integrations) if (integration?.afterAllSetup) integration.afterAllSetup(client);
}
/** Setup a single integration. */
function setupIntegration(client, integration, integrationIndex) {
if (integrationIndex[integration.name]) {
DEBUG_BUILD && debug.log(`Integration skipped because it was already installed: ${integration.name}`);
return;
}
integrationIndex[integration.name] = integration;
if (!installedIntegrations.includes(integration.name) && typeof integration.setupOnce === "function") {
integration.setupOnce();
installedIntegrations.push(integration.name);
}
if (integration.setup && typeof integration.setup === "function") integration.setup(client);
if (typeof integration.preprocessEvent === "function") {
const callback = integration.preprocessEvent.bind(integration);
client.on("preprocessEvent", (event, hint) => callback(event, hint, client));
}
if (typeof integration.processEvent === "function") {
const callback = integration.processEvent.bind(integration);
const processor = Object.assign((event, hint) => callback(event, hint, client), { id: integration.name });
client.addEventProcessor(processor);
}
["processSpan", "processSegmentSpan"].forEach((hook) => {
const callback = integration[hook];
if (typeof callback === "function") client.on(hook, (span) => callback.call(integration, span, client));
});
DEBUG_BUILD && debug.log(`Integration installed: ${integration.name}`);
}
/** Add an integration to the current scope's client. */
function addIntegration(integration) {
const client = getClient();
if (!client) {
DEBUG_BUILD && debug.warn(`Cannot add integration "${integration.name}" because no SDK Client is available.`);
return;
}
client.addIntegration(integration);
}
/**
* Define an integration function that can be used to create an integration instance.
* Note that this by design hides the implementation details of the integration, as they are considered internal.
*/
function defineIntegration(fn) {
return fn;
}
/**
* Wrap a parent integration with an extended integration.
* Any passed integration function will call the parent integration function first, if it exists.
*
* Example usage:
*
* @example
* ```typescript
* const parentIntegration = defineIntegration(() => ({
* name: 'ParentIntegration',
* setupOnce: () => {
* console.log('ParentIntegration setupOnce');
* },
* }));
*
* const extendedIntegration = extendIntegration(parentIntegration, {
* setupOnce: () => {
* console.log('ExtendedIntegration setupOnce');
* },
* });
* ```
*/
function extendIntegration(integration, extendedIntegration) {
const wrappedIntegration = {
...integration,
...extendedIntegration
};
for (const key in extendedIntegration) {
const baseValue = integration[key];
const extendedValue = extendedIntegration[key];
if (typeof baseValue === "function" && typeof extendedValue === "function") wrappedIntegration[key] = new Proxy(baseValue, { apply: (target, thisArg, args) => {
Reflect.apply(target, thisArg, args);
return Reflect.apply(extendedValue, thisArg, args);
} });
}
return wrappedIntegration;
}
export { addIntegration, afterSetupIntegrations, defineIntegration, extendIntegration, getIntegrationsToSetup, installedIntegrations, setupIntegration, setupIntegrations };
//# sourceMappingURL=integration.js.map