Skip to content

Commit 4da8068

Browse files
committed
Fix global type, add unit tests, add generic ffs test
1 parent ffe9bcd commit 4da8068

File tree

9 files changed

+149
-2
lines changed

9 files changed

+149
-2
lines changed
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
export const FLAG_BUFFER_SIZE = 100; // Corresponds to constant in featureFlags.ts, in browser utils.
1+
// Corresponds to constants in featureFlags.ts, in browser utils.
2+
export const FLAG_BUFFER_SIZE = 100;
3+
export const MAX_FLAGS_PER_SPAN = 10;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import * as Sentry from '@sentry/browser';
2+
3+
window.Sentry = Sentry;
4+
5+
Sentry.init({
6+
dsn: 'https://public@dsn.ingest.sentry.io/1337',
7+
sampleRate: 1.0,
8+
tracesSampleRate: 1.0,
9+
integrations: [
10+
Sentry.browserTracingIntegration({ instrumentNavigation: false, instrumentPageLoad: false }),
11+
Sentry.featureFlagsIntegration(),
12+
],
13+
});

dev-packages/browser-integration-tests/suites/integrations/featureFlags/featureFlags/onSpan/sample.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const btnStartSpan = document.getElementById('btnStartSpan');
2+
const btnEndSpan = document.getElementById('btnEndSpan');
3+
const btnStartNestedSpan = document.getElementById('btnStartNestedSpan');
4+
const btnEndNestedSpan = document.getElementById('btnEndNestedSpan');
5+
6+
window.withNestedSpans = (callback) => {
7+
window.Sentry.startSpan(
8+
{ name: 'test-root-span' },
9+
rootSpan => {
10+
window.traceId = rootSpan.spanContext().traceId;
11+
12+
window.Sentry.startSpan(
13+
{ name: 'test-span' },
14+
_span => {
15+
16+
window.Sentry.startSpan(
17+
{ name: 'test-nested-span' },
18+
_nestedSpan => {
19+
callback();
20+
}
21+
);
22+
},
23+
);
24+
},
25+
);
26+
};
27+
28+
// btnStartNestedSpan.addEventListener('click', () => {
29+
// Sentry.startSpanManual(
30+
// { name: 'test-nested-span', attributes: { [Sentry.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url' } },
31+
// async span => {
32+
// await new Promise(resolve => {
33+
// btnEndNestedSpan.addEventListener('click', resolve);
34+
// });
35+
// span.end();
36+
// },
37+
// );
38+
// });
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
</head>
6+
<body>
7+
<button id="btnStartSpan">Start Span</button>
8+
<button id="btnEndSpan">End Span</button>
9+
<button id="btnStartNestedSpan">Start Nested Span</button>
10+
<button id="btnEndNestedSpan">End Nested Span</button>
11+
</body>
12+
</html>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { expect } from '@playwright/test';
2+
import { sentryTest } from '../../../../../utils/fixtures';
3+
import { shouldSkipFeatureFlagsTest, shouldSkipTracingTest, getMultipleSentryEnvelopeRequests, eventAndTraceHeaderRequestParser, type EventAndTraceHeader } from '../../../../../utils/helpers';
4+
import { MAX_FLAGS_PER_SPAN } from '../../constants';
5+
6+
sentryTest("Feature flags are added to active span's attributes on span end.", async ({ getLocalTestUrl, page }) => {
7+
if (shouldSkipFeatureFlagsTest() || shouldSkipTracingTest()) {
8+
sentryTest.skip();
9+
}
10+
11+
await page.route('https://dsn.ingest.sentry.io/**/*', route => {
12+
return route.fulfill({
13+
status: 200,
14+
contentType: 'application/json',
15+
body: JSON.stringify({}),
16+
});
17+
});
18+
19+
const url = await getLocalTestUrl({ testDir: __dirname, skipDsnRouteHandler: true });
20+
await page.goto(url);
21+
22+
const envelopeRequestPromise = getMultipleSentryEnvelopeRequests<EventAndTraceHeader>(
23+
page,
24+
1,
25+
{}, // envelopeType: 'transaction' },
26+
eventAndTraceHeaderRequestParser, // properFullEnvelopeRequestParser
27+
);
28+
29+
// withNestedSpans is a util used to start 3 nested spans: root-span (not recorded in transaction_event.spans), span, and nested-span.
30+
await page.evaluate(maxFlags => {
31+
(window as any).withNestedSpans(() => {
32+
const flagsIntegration = (window as any).Sentry.getClient().getIntegrationByName('FeatureFlags');
33+
for (let i = 1; i <= maxFlags; i++) {
34+
flagsIntegration.addFeatureFlag(`feat${i}`, false);
35+
}
36+
flagsIntegration.addFeatureFlag(`feat${maxFlags + 1}`, true); // dropped flag
37+
flagsIntegration.addFeatureFlag('feat3', true); // update
38+
});
39+
return true;
40+
}, MAX_FLAGS_PER_SPAN);
41+
const event = (await envelopeRequestPromise)[0][0];
42+
const innerSpan = event.spans?.[0];
43+
const outerSpan = event.spans?.[1];
44+
const outerSpanFlags = Object.entries(outerSpan?.data ?? {}).filter(([key, _val]) => key.startsWith('flag.evaluation'));
45+
const innerSpanFlags = Object.entries(innerSpan?.data ?? {}).filter(([key, _val]) => key.startsWith('flag.evaluation'));
46+
47+
expect(innerSpanFlags).toEqual([]);
48+
49+
const expectedOuterSpanFlags = [];
50+
for (let i = 1; i <= 2; i++) {
51+
expectedOuterSpanFlags.push([`flag.evaluation.feat${i}`, false]);
52+
}
53+
for (let i = 4; i <= MAX_FLAGS_PER_SPAN; i++) {
54+
expectedOuterSpanFlags.push([`flag.evaluation.feat${i}`, false]);
55+
}
56+
expectedOuterSpanFlags.push(['flag.evaluation.feat3', true]);
57+
expect(outerSpanFlags).toEqual(expectedOuterSpanFlags);
58+
});

packages/browser/src/utils/featureFlags.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,15 @@ export function insertToFlagBuffer(flags: FeatureFlag[], name: string, value: un
9191
if (index !== -1) {
9292
// The flag was found, remove it from its current position - O(n)
9393
flags.splice(index, 1);
94+
console.log('dup flag', name);
9495
}
9596

9697
if (flags.length === maxSize) {
9798
if (allowEviction) {
9899
// If at capacity, pop the earliest flag - O(n)
99100
flags.shift();
100101
} else {
102+
console.log('dropping flag', name);
101103
return;
102104
}
103105
}

packages/browser/test/utils/featureFlags.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,27 @@ describe('flags', () => {
5959
]);
6060
});
6161

62+
it('drops new entries when allowEviction is false and buffer is full', () => {
63+
const buffer: FeatureFlag[] = [];
64+
const maxSize = 0;
65+
insertToFlagBuffer(buffer, 'feat1', true, maxSize, false);
66+
insertToFlagBuffer(buffer, 'feat2', true, maxSize, false);
67+
insertToFlagBuffer(buffer, 'feat3', true, maxSize, false);
68+
69+
expect(buffer).toEqual([]);
70+
});
71+
72+
it('still updates order and values when allowEviction is false and buffer is full', () => {
73+
const buffer: FeatureFlag[] = [];
74+
const maxSize = 1;
75+
insertToFlagBuffer(buffer, 'feat1', false, maxSize, false);
76+
insertToFlagBuffer(buffer, 'feat1', true, maxSize, false);
77+
78+
expect(buffer).toEqual([
79+
{ flag: 'feat1', result: true },
80+
]);
81+
});
82+
6283
it('does not allocate unnecessary space', () => {
6384
const buffer: FeatureFlag[] = [];
6485
const maxSize = 1000;

packages/core/src/utils-hoist/worldwide.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export type InternalGlobal = {
6161
/**
6262
* A map of spans to feature flag buffers. Populated by feature flag integrations.
6363
*/
64-
_spanToFlagBufferMap?: WeakMap<Span, Set<FeatureFlag>>;
64+
_spanToFlagBufferMap?: WeakMap<Span, FeatureFlag[]>;
6565
} & Carrier;
6666

6767
/** Get's the global object for the current JavaScript runtime */

0 commit comments

Comments
 (0)