|
| 1 | +import type { Client, Event, EventHint, IntegrationFn } from '@sentry/core'; |
| 2 | + |
| 3 | +import { defineIntegration, fill, logger } from '@sentry/core'; |
| 4 | +import { DEBUG_BUILD } from '../../../debug-build'; |
| 5 | +import { copyFlagsFromScopeToEvent, insertFlagToScope } from '../../../utils/featureFlags'; |
| 6 | +import type { UnleashClient, UnleashClientClass } from './types'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Sentry integration for capturing feature flag evaluations from the Unleash 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 { UnleashClient } from 'unleash-proxy-client'; |
| 16 | + * import * as Sentry from '@sentry/browser'; |
| 17 | + * |
| 18 | + * Sentry.init({ |
| 19 | + * dsn: '___PUBLIC_DSN___', |
| 20 | + * integrations: [Sentry.unleashIntegration({unleashClientClass: UnleashClient})], |
| 21 | + * }); |
| 22 | + * |
| 23 | + * const unleash = new UnleashClient(...); |
| 24 | + * unleash.start(); |
| 25 | + * |
| 26 | + * unleash.isEnabled('my-feature'); |
| 27 | + * unleash.getVariant('other-feature'); |
| 28 | + * Sentry.captureException(new Error('something went wrong')); |
| 29 | + * ``` |
| 30 | + */ |
| 31 | +export const unleashIntegration = defineIntegration( |
| 32 | + ({ unleashClientClass }: { unleashClientClass: UnleashClientClass }) => { |
| 33 | + return { |
| 34 | + name: 'Unleash', |
| 35 | + |
| 36 | + processEvent(event: Event, _hint: EventHint, _client: Client): Event { |
| 37 | + return copyFlagsFromScopeToEvent(event); |
| 38 | + }, |
| 39 | + |
| 40 | + setupOnce() { |
| 41 | + const unleashClientPrototype = unleashClientClass.prototype as UnleashClient; |
| 42 | + fill(unleashClientPrototype, 'isEnabled', _wrappedIsEnabled); |
| 43 | + }, |
| 44 | + }; |
| 45 | + }, |
| 46 | +) satisfies IntegrationFn; |
| 47 | + |
| 48 | +/** |
| 49 | + * Wraps the UnleashClient.isEnabled method to capture feature flag evaluations. Its only side effect is writing to Sentry scope. |
| 50 | + * |
| 51 | + * This wrapper is safe for all isEnabled signatures. If the signature does not match (this: UnleashClient, toggleName: string, ...args: unknown[]) => boolean, |
| 52 | + * we log an error and return the original result. |
| 53 | + * |
| 54 | + * @param original - The original method. |
| 55 | + * @returns Wrapped method. Results should match the original. |
| 56 | + */ |
| 57 | +function _wrappedIsEnabled( |
| 58 | + original: (this: UnleashClient, ...args: unknown[]) => unknown, |
| 59 | +): (this: UnleashClient, ...args: unknown[]) => unknown { |
| 60 | + return function (this: UnleashClient, ...args: unknown[]): unknown { |
| 61 | + const toggleName = args[0]; |
| 62 | + const result = original.apply(this, args); |
| 63 | + |
| 64 | + if (typeof toggleName === 'string' && typeof result === 'boolean') { |
| 65 | + insertFlagToScope(toggleName, result); |
| 66 | + } else if (DEBUG_BUILD) { |
| 67 | + logger.error( |
| 68 | + `[Feature Flags] UnleashClient.isEnabled does not match expected signature. arg0: ${toggleName} (${typeof toggleName}), result: ${result} (${typeof result})`, |
| 69 | + ); |
| 70 | + } |
| 71 | + return result; |
| 72 | + }; |
| 73 | +} |
0 commit comments