-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathintegration.ts
More file actions
56 lines (51 loc) · 1.97 KB
/
integration.ts
File metadata and controls
56 lines (51 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import type { Client, Event, EventHint, IntegrationFn } from '@sentry/core';
import {
_INTERNAL_addFeatureFlagToActiveSpan,
_INTERNAL_copyFlagsFromScopeToEvent,
_INTERNAL_insertFlagToScope,
defineIntegration,
} from '@sentry/core';
import type { LDContext, LDEvaluationDetail, LDInspectionFlagUsedHandler } from './types';
/**
* Sentry integration for capturing feature flag evaluations from LaunchDarkly.
*
* See the [feature flag documentation](https://develop.sentry.dev/sdk/expected-features/#feature-flags) for more information.
*
* @example
* ```
* import * as Sentry from '@sentry/browser';
* import {launchDarklyIntegration, buildLaunchDarklyFlagUsedInspector} from '@sentry/browser';
* import * as LaunchDarkly from 'launchdarkly-js-client-sdk';
*
* Sentry.init(..., integrations: [launchDarklyIntegration()])
* const ldClient = LaunchDarkly.initialize(..., {inspectors: [buildLaunchDarklyFlagUsedHandler()]});
* ```
*/
export const launchDarklyIntegration = defineIntegration(() => {
return {
name: 'LaunchDarkly',
processEvent(event: Event, _hint: EventHint, _client: Client): Event {
return _INTERNAL_copyFlagsFromScopeToEvent(event);
},
};
}) satisfies IntegrationFn;
/**
* LaunchDarkly hook to listen for and buffer flag evaluations. This needs to
* be registered as an 'inspector' in LaunchDarkly initialize() options,
* separately from `launchDarklyIntegration`. Both the hook and the integration
* are needed to capture LaunchDarkly flags.
*/
export function buildLaunchDarklyFlagUsedHandler(): LDInspectionFlagUsedHandler {
return {
name: 'sentry-flag-auditor',
type: 'flag-used',
synchronous: true,
/**
* Handle a flag evaluation by storing its name and value on the current scope.
*/
method: (flagKey: string, flagDetail: LDEvaluationDetail, _context: LDContext) => {
_INTERNAL_insertFlagToScope(flagKey, flagDetail.value);
_INTERNAL_addFeatureFlagToActiveSpan(flagKey, flagDetail.value);
},
};
}