@@ -2,22 +2,28 @@ import type { Client, Event, EventHint, IntegrationFn } from '@sentry/core';
22
33import { defineIntegration } from '@sentry/core' ;
44import { copyFlagsFromScopeToEvent , insertFlagToScope } from '../../../utils/featureFlags' ;
5- import type { UnleashClient , UnleashClientClass } from './types' ;
5+ import type { IVariant , UnleashClient , UnleashClientClass } from './types' ;
66
77/**
8- * Sentry integration for capturing feature flags from the Unleash SDK.
8+ * Sentry integration for capturing feature flag evaluations from the Unleash SDK.
99 *
1010 * See the [feature flag documentation](https://develop.sentry.dev/sdk/expected-features/#feature-flags) for more information.
1111 *
1212 * @example
1313 * ```
14+ * import { UnleashClient } from 'unleash-proxy-client';
1415 * import * as Sentry from '@sentry/browser';
15- * TODO:
16+ *
17+ * const unleashIntegration = Sentry.unleashIntegration(UnleashClient);
1618 *
1719 * Sentry.init({
1820 * dsn: '___PUBLIC_DSN___',
19- * integrations: [TODO:]
21+ * integrations: [unleashIntegration],
2022 * });
23+ *
24+ * const unleashClient = new UnleashClient(...);
25+ * unleashClient.isEnabled('my-feature');
26+ * Sentry.captureException(new Error('something went wrong'));
2127 * ```
2228 */
2329export const unleashIntegration = defineIntegration ( ( unleashClientClass : UnleashClientClass ) => {
@@ -29,6 +35,8 @@ export const unleashIntegration = defineIntegration((unleashClientClass: Unleash
2935 } ,
3036
3137 setupOnce ( ) {
38+ const unleashClientPrototype = unleashClientClass . prototype as UnleashClient ;
39+
3240 const sentryIsEnabled = {
3341 apply : (
3442 target : ( this : UnleashClient , toggleName : string ) => boolean ,
@@ -40,9 +48,23 @@ export const unleashIntegration = defineIntegration((unleashClientClass: Unleash
4048 return result ;
4149 } ,
4250 } ;
43- const unleashClientPrototype = unleashClientClass . prototype as UnleashClient ;
4451 const originalIsEnabled = unleashClientPrototype . isEnabled . bind ( unleashClientPrototype ) ;
4552 unleashClientPrototype . isEnabled = new Proxy ( originalIsEnabled , sentryIsEnabled ) ;
53+
54+ const sentryGetVariant = {
55+ apply : (
56+ target : ( this : UnleashClient , toggleName : string ) => IVariant ,
57+ thisArg : UnleashClient ,
58+ args : [ toggleName : string ] ,
59+ ) => {
60+ const variant = Reflect . apply ( target , thisArg , args ) ;
61+ const result = variant . enabled ;
62+ insertFlagToScope ( args [ 0 ] , result ) ;
63+ return variant ;
64+ } ,
65+ } ;
66+ const originalGetVariant = unleashClientPrototype . getVariant . bind ( unleashClientPrototype ) ;
67+ unleashClientPrototype . getVariant = new Proxy ( originalGetVariant , sentryGetVariant ) ;
4668 } ,
4769 } ;
4870} ) satisfies IntegrationFn ;
0 commit comments