1- import * as Sentry from '@sentry/browser' ;
2- import type { Client as SentryClient , Event , EventHint , IntegrationFn } from '@sentry/types' ;
1+ import type { Client , Event , EventHint , IntegrationFn } from '@sentry/types' ;
32import type { LDContext , LDEvaluationDetail , LDInspectionFlagUsedHandler } from 'launchdarkly-js-client-sdk' ;
4- import type { LaunchDarklyOptions } from '../types' ;
3+
54import { insertToFlagBuffer } from '@sentry/utils' ;
5+ import { defineIntegration , getCurrentScope } from '@sentry/core' ;
66
77/**
88 * Sentry integration for capturing feature flags from LaunchDarkly.
@@ -11,19 +11,20 @@ import { insertToFlagBuffer } from '@sentry/utils';
1111 *
1212 * @example
1313 * ```
14- * import {buildSentryFlagUsedInspector, buildLaunchDarklyIntegration} from '@sentry/launchdarkly';
15- * import {LDClient} from 'launchdarkly-js-client-sdk';
14+ * import * as Sentry from '@sentry/browser';
15+ * import {launchDarklyIntegration, buildLaunchDarklyFlagUsedInspector} from '@sentry/browser';
16+ * import * as LaunchDarkly from 'launchdarkly-js-client-sdk';
1617 *
17- * Sentry.init(..., integrations: [buildLaunchDarklyIntegration ()])
18- * const ldClient = LDClient .initialize(..., {inspectors: [buildSentryFlagUsedInspector ()]});
18+ * Sentry.init(..., integrations: [launchDarklyIntegration ()])
19+ * const ldClient = LaunchDarkly .initialize(..., {inspectors: [buildLaunchDarklyFlagUsedInspector ()]});
1920 * ```
2021 */
21- export const buildLaunchDarklyIntegration = ( ( _options ?: LaunchDarklyOptions ) => {
22+ export const launchDarklyIntegration = defineIntegration ( ( ) => {
2223 return {
23- name : 'launchdarkly ' ,
24+ name : 'LaunchDarkly ' ,
2425
25- processEvent ( event : Event , _hint : EventHint , _client : SentryClient ) : Event {
26- const scope = Sentry . getCurrentScope ( ) ;
26+ processEvent ( event : Event , _hint : EventHint , _client : Client ) : Event {
27+ const scope = getCurrentScope ( ) ;
2728 const flagContext = scope . getScopeData ( ) . contexts . flags ;
2829
2930 if ( event . contexts === undefined ) {
@@ -36,13 +37,13 @@ export const buildLaunchDarklyIntegration = ((_options?: LaunchDarklyOptions) =>
3637} ) satisfies IntegrationFn ;
3738
3839/**
39- * LaunchDarkly hook that listens for flag evaluations and updates the
40- * flagBuffer in our current scope.
40+ * Constructs a LaunchDarkly hook that listens for flag evaluations and updates
41+ * the flagBuffer in our current scope.
4142 *
42- * This needs to be registered separately in the LDClient, after initializing
43- * Sentry.
43+ * This needs to be registered separately in the LD SDK initialize() options,
44+ * after initializing Sentry.
4445 */
45- export function buildSentryFlagUsedInspector ( ) : LDInspectionFlagUsedHandler {
46+ export function buildLaunchDarklyFlagUsedInspector ( ) : LDInspectionFlagUsedHandler {
4647 return {
4748 name : 'sentry-flag-auditor' ,
4849 type : 'flag-used' ,
@@ -55,14 +56,15 @@ export function buildSentryFlagUsedInspector(): LDInspectionFlagUsedHandler {
5556 */
5657 method : ( flagKey : string , flagDetail : LDEvaluationDetail , _context : LDContext ) => {
5758 if ( typeof flagDetail . value === 'boolean' ) {
58- const scopeContexts = Sentry . getCurrentScope ( ) . getScopeData ( ) . contexts ;
59+ const scopeContexts = getCurrentScope ( ) . getScopeData ( ) . contexts ;
5960 if ( ! scopeContexts . flags ) {
60- scopeContexts . flags = { values : [ ] }
61+ scopeContexts . flags = { values : [ ] } ;
6162 }
6263 const flagBuffer = scopeContexts . flags . values ;
6364 insertToFlagBuffer ( flagBuffer , flagKey , flagDetail . value ) ;
65+ console . log ( 'inserted' )
6466 }
6567 return ;
66- }
67- }
68+ } ,
69+ } ;
6870}
0 commit comments