Skip to content

Commit 3c686c5

Browse files
committed
Copy flags when attaching to event
1 parent dc7e74a commit 3c686c5

File tree

3 files changed

+52
-2
lines changed
  • dev-packages/browser-integration-tests/suites/integrations/featureFlags/launchdarkly
  • packages/browser/src/integrations/featureFlags/launchdarkly

3 files changed

+52
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { expect } from '@playwright/test';
2+
3+
import { sentryTest } from '../../../../../utils/fixtures';
4+
5+
import { envelopeRequestParser, waitForErrorRequest } from '../../../../../utils/helpers';
6+
7+
sentryTest('Flags on error event are not affected by later evaluations', async ({ getLocalTestPath, page }) => {
8+
await page.route('https://dsn.ingest.sentry.io/**/*', route => {
9+
return route.fulfill({
10+
status: 200,
11+
contentType: 'application/json',
12+
body: JSON.stringify({ id: 'test-id' }),
13+
});
14+
});
15+
16+
const url = await getLocalTestPath({ testDir: __dirname, skipDsnRouteHandler: true });
17+
await page.goto(url);
18+
19+
// initial evals
20+
await page.waitForFunction(() => {
21+
const ldClient = (window as any).initializeLD();
22+
ldClient.variation('hello', true);
23+
ldClient.variation('world', false);
24+
return true;
25+
});
26+
27+
// trigger error
28+
const reqPromise = waitForErrorRequest(page);
29+
await page.locator('#error').click();
30+
const req = await reqPromise;
31+
const event = envelopeRequestParser(req);
32+
33+
// more evals
34+
await page.waitForFunction(() => {
35+
const ldClient = (window as any).ldClient;
36+
ldClient.variation('goodbye', false);
37+
ldClient.variation('hello', false);
38+
return true;
39+
});
40+
41+
const expectedFlags = [
42+
{ flag: 'hello', result: true },
43+
{ flag: 'world', result: false },
44+
];
45+
expect(event.contexts?.flags?.values).toEqual(expectedFlags);
46+
});

dev-packages/browser-integration-tests/suites/integrations/featureFlags/launchdarkly/init.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,13 @@ const MockLaunchDarkly = {
2727
},
2828
};
2929

30+
window.ldClient = undefined;
31+
3032
window.initializeLD = () => {
31-
return MockLaunchDarkly.initialize(
33+
window.ldClient = MockLaunchDarkly.initialize(
3234
'example-client-id',
3335
{ kind: 'user', key: 'example-context-key' },
3436
{ inspectors: [buildLaunchDarklyFlagUsedHandler()] },
3537
);
38+
return window.ldClient;
3639
};

packages/browser/src/integrations/featureFlags/launchdarkly/integration.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ export const launchDarklyIntegration = defineIntegration(() => {
2626
processEvent(event: Event, _hint: EventHint, _client: Client): Event {
2727
const scope = getCurrentScope();
2828
const flagContext = scope.getScopeData().contexts.flags;
29+
const flagBuffer = flagContext ? flagContext.values : [];
2930

3031
if (event.contexts === undefined) {
3132
event.contexts = {};
3233
}
33-
event.contexts.flags = flagContext;
34+
event.contexts.flags = { values: [...flagBuffer] };
3435
return event;
3536
},
3637
};

0 commit comments

Comments
 (0)