Skip to content

Commit 4d4d9e8

Browse files
committed
Init src code
1 parent aa8704a commit 4d4d9e8

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { statsigIntegration } from './integration';
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export type FeatureGate = {
2+
readonly name: string;
3+
readonly value: boolean;
4+
// readonly ruleID: string;
5+
// readonly details: EvaluationDetails;
6+
// readonly __evaluation: GateEvaluation | null;
7+
};
8+
9+
type EventNameToEventDataMap = {
10+
gate_evaluation: { gate: FeatureGate };
11+
};
12+
13+
export interface StatsigClient {
14+
on(
15+
event: keyof EventNameToEventDataMap,
16+
callback: (data: EventNameToEventDataMap[keyof EventNameToEventDataMap]) => void
17+
): void;
18+
}

0 commit comments

Comments
 (0)