|
| 1 | +import type { Client, Event, EventHint, IntegrationFn } from '@sentry/core'; |
| 2 | + |
| 3 | +import { defineIntegration, logger } from '@sentry/core'; |
| 4 | +import { DEBUG_BUILD } from '../../../debug-build'; |
| 5 | +import { copyFlagsFromScopeToEvent, insertFlagToScope } from '../../../utils/featureFlags'; |
| 6 | +import type { StatsigClient, FeatureGate } from './types'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Sentry integration for capturing feature flag evaluations from the Statsig js-client SDK. |
| 10 | + * |
| 11 | + * See the [feature flag documentation](https://develop.sentry.dev/sdk/expected-features/#feature-flags) for more information. |
| 12 | + * |
| 13 | + * @example |
| 14 | + * ``` |
| 15 | + * import { StatsigClient } from ''; |
| 16 | + * import * as Sentry from '@sentry/browser'; |
| 17 | + * |
| 18 | + * const statsigClient = new StatsigClient(); |
| 19 | + * |
| 20 | + * Sentry.init({ |
| 21 | + * dsn: '___PUBLIC_DSN___', |
| 22 | + * integrations: [Sentry.statsigIntegration({statsigClient})], |
| 23 | + * }); |
| 24 | + * |
| 25 | + * await statsigClient.initializeAsync(); // or statsigClient.initializeSync(); |
| 26 | + * |
| 27 | + * const result = statsigClient.checkGate('my-feature-gate'); |
| 28 | + * Sentry.captureException(new Error('something went wrong')); |
| 29 | + * ``` |
| 30 | + */ |
| 31 | +export const statsigIntegration = defineIntegration( |
| 32 | + ({ statsigClient }: { statsigClient: StatsigClient }) => { |
| 33 | + return { |
| 34 | + name: 'Statsig', |
| 35 | + |
| 36 | + processEvent(event: Event, _hint: EventHint, _client: Client): Event { |
| 37 | + return copyFlagsFromScopeToEvent(event); |
| 38 | + }, |
| 39 | + |
| 40 | + setupOnce() { |
| 41 | + statsigClient.on('gate_evaluation', (event: { gate: FeatureGate }) => { |
| 42 | + try { |
| 43 | + insertFlagToScope(event.gate.name, event.gate.value); |
| 44 | + } catch (error) { |
| 45 | + if (!(error instanceof TypeError)) { |
| 46 | + throw error; |
| 47 | + } |
| 48 | + |
| 49 | + if (DEBUG_BUILD) { |
| 50 | + logger.error(`[Feature Flags] Error reading Statsig gate evaluation: ${error.message}`); |
| 51 | + } |
| 52 | + } |
| 53 | + }); |
| 54 | + }, |
| 55 | + }; |
| 56 | + }, |
| 57 | +) satisfies IntegrationFn; |
0 commit comments