(client: Client, integration: Integration, integrationIndex: IntegrationIndex)
| 107 | |
| 108 | /** Setup a single integration. */ |
| 109 | export function setupIntegration(client: Client, integration: Integration, integrationIndex: IntegrationIndex): void { |
| 110 | if (integrationIndex[integration.name]) { |
| 111 | DEBUG_BUILD && debug.log(`Integration skipped because it was already installed: ${integration.name}`); |
| 112 | return; |
| 113 | } |
| 114 | integrationIndex[integration.name] = integration; |
| 115 | |
| 116 | // `setupOnce` is only called the first time |
| 117 | if (!installedIntegrations.includes(integration.name) && typeof integration.setupOnce === 'function') { |
| 118 | integration.setupOnce(); |
| 119 | installedIntegrations.push(integration.name); |
| 120 | } |
| 121 | |
| 122 | // `setup` is run for each client |
| 123 | if (integration.setup && typeof integration.setup === 'function') { |
| 124 | integration.setup(client); |
| 125 | } |
| 126 | |
| 127 | if (typeof integration.preprocessEvent === 'function') { |
| 128 | const callback = integration.preprocessEvent.bind(integration) as typeof integration.preprocessEvent; |
| 129 | client.on('preprocessEvent', (event, hint) => callback(event, hint, client)); |
| 130 | } |
| 131 | |
| 132 | if (typeof integration.processEvent === 'function') { |
| 133 | const callback = integration.processEvent.bind(integration) as typeof integration.processEvent; |
| 134 | |
| 135 | const processor = Object.assign((event: Event, hint: EventHint) => callback(event, hint, client), { |
| 136 | id: integration.name, |
| 137 | }); |
| 138 | |
| 139 | client.addEventProcessor(processor); |
| 140 | } |
| 141 | |
| 142 | (['processSpan', 'processSegmentSpan'] as const).forEach(hook => { |
| 143 | const callback = integration[hook]; |
| 144 | if (typeof callback === 'function') { |
| 145 | // The cast is needed because TS can't resolve overloads when the discriminant is a union type. |
| 146 | // Both overloads have the same callback signature so this is safe. |
| 147 | client.on(hook as 'processSpan', (span: StreamedSpanJSON) => callback.call(integration, span, client)); |
| 148 | } |
| 149 | }); |
| 150 | |
| 151 | DEBUG_BUILD && debug.log(`Integration installed: ${integration.name}`); |
| 152 | } |
| 153 | |
| 154 | /** Add an integration to the current scope's client. */ |
| 155 | export function addIntegration(integration: Integration): void { |
no test coverage detected