Skip to content

Commit e6da588

Browse files
committed
Add ld test
1 parent 7c2c161 commit e6da588

File tree

6 files changed

+139
-14
lines changed

6 files changed

+139
-14
lines changed

dev-packages/browser-integration-tests/suites/integrations/featureFlags/featureFlags/onSpan/subject.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,3 @@ window.withNestedSpans = callback => {
1414
});
1515
});
1616
};
17-
18-
// btnStartNestedSpan.addEventListener('click', () => {
19-
// Sentry.startSpanManual(
20-
// { name: 'test-nested-span', attributes: { [Sentry.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url' } },
21-
// async span => {
22-
// await new Promise(resolve => {
23-
// btnEndNestedSpan.addEventListener('click', resolve);
24-
// });
25-
// span.end();
26-
// },
27-
// );
28-
// });

dev-packages/browser-integration-tests/suites/integrations/featureFlags/featureFlags/onSpan/test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ sentryTest("Feature flags are added to active span's attributes on span end.", a
2828
const envelopeRequestPromise = getMultipleSentryEnvelopeRequests<EventAndTraceHeader>(
2929
page,
3030
1,
31-
{}, // envelopeType: 'transaction' },
32-
eventAndTraceHeaderRequestParser, // properFullEnvelopeRequestParser
31+
{},
32+
eventAndTraceHeaderRequestParser,
3333
);
3434

3535
// withNestedSpans is a util used to start 3 nested spans: root-span (not recorded in transaction_event.spans), span, and nested-span.
@@ -44,6 +44,7 @@ sentryTest("Feature flags are added to active span's attributes on span end.", a
4444
});
4545
return true;
4646
}, MAX_FLAGS_PER_SPAN);
47+
4748
const event = (await envelopeRequestPromise)[0][0];
4849
const innerSpan = event.spans?.[0];
4950
const outerSpan = event.spans?.[1];
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import * as Sentry from '@sentry/browser';
2+
3+
window.Sentry = Sentry;
4+
window.sentryLDIntegration = Sentry.launchDarklyIntegration();
5+
6+
Sentry.init({
7+
dsn: 'https://public@dsn.ingest.sentry.io/1337',
8+
sampleRate: 1.0,
9+
tracesSampleRate: 1.0,
10+
integrations: [
11+
Sentry.browserTracingIntegration({ instrumentNavigation: false, instrumentPageLoad: false }),
12+
window.sentryLDIntegration,
13+
],
14+
});
15+
16+
// Manually mocking this because LD only has mock test utils for the React SDK.
17+
// Also, no SDK has mock utils for FlagUsedHandler's.
18+
const MockLaunchDarkly = {
19+
initialize(_clientId, context, options) {
20+
const flagUsedHandler = options.inspectors ? options.inspectors[0].method : undefined;
21+
22+
return {
23+
variation(key, defaultValue) {
24+
if (flagUsedHandler) {
25+
flagUsedHandler(key, { value: defaultValue }, context);
26+
}
27+
return defaultValue;
28+
},
29+
};
30+
},
31+
};
32+
33+
window.initializeLD = () => {
34+
return MockLaunchDarkly.initialize(
35+
'example-client-id',
36+
{ kind: 'user', key: 'example-context-key' },
37+
{ inspectors: [Sentry.buildLaunchDarklyFlagUsedHandler()] },
38+
);
39+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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({ name: 'test-root-span' }, rootSpan => {
8+
window.traceId = rootSpan.spanContext().traceId;
9+
10+
window.Sentry.startSpan({ name: 'test-span' }, _span => {
11+
window.Sentry.startSpan({ name: 'test-nested-span' }, _nestedSpan => {
12+
callback();
13+
});
14+
});
15+
});
16+
};
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: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { expect } from '@playwright/test';
2+
import { sentryTest } from '../../../../../utils/fixtures';
3+
import {
4+
type EventAndTraceHeader,
5+
eventAndTraceHeaderRequestParser,
6+
getMultipleSentryEnvelopeRequests,
7+
shouldSkipFeatureFlagsTest,
8+
shouldSkipTracingTest,
9+
} from '../../../../../utils/helpers';
10+
import { MAX_FLAGS_PER_SPAN } from '../../constants';
11+
12+
sentryTest("Feature flags are added to active span's attributes on span end.", async ({ getLocalTestUrl, page }) => {
13+
if (shouldSkipFeatureFlagsTest() || shouldSkipTracingTest()) {
14+
sentryTest.skip();
15+
}
16+
17+
await page.route('https://dsn.ingest.sentry.io/**/*', route => {
18+
return route.fulfill({
19+
status: 200,
20+
contentType: 'application/json',
21+
body: JSON.stringify({}),
22+
});
23+
});
24+
25+
const url = await getLocalTestUrl({ testDir: __dirname, skipDsnRouteHandler: true });
26+
await page.goto(url);
27+
28+
const envelopeRequestPromise = getMultipleSentryEnvelopeRequests<EventAndTraceHeader>(
29+
page,
30+
1,
31+
{},
32+
eventAndTraceHeaderRequestParser,
33+
);
34+
35+
// withNestedSpans is a util used to start 3 nested spans: root-span (not recorded in transaction_event.spans), span, and nested-span.
36+
await page.evaluate(maxFlags => {
37+
(window as any).withNestedSpans(() => {
38+
const ldClient = (window as any).initializeLD();
39+
for (let i = 1; i <= maxFlags; i++) {
40+
ldClient.variation(`feat${i}`, false);
41+
}
42+
ldClient.variation(`feat${maxFlags + 1}`, true); // dropped
43+
ldClient.variation('feat3', true); // update
44+
});
45+
return true;
46+
}, MAX_FLAGS_PER_SPAN);
47+
48+
const event = (await envelopeRequestPromise)[0][0];
49+
const innerSpan = event.spans?.[0];
50+
const outerSpan = event.spans?.[1];
51+
const outerSpanFlags = Object.entries(outerSpan?.data ?? {}).filter(([key, _val]) =>
52+
key.startsWith('flag.evaluation'),
53+
);
54+
const innerSpanFlags = Object.entries(innerSpan?.data ?? {}).filter(([key, _val]) =>
55+
key.startsWith('flag.evaluation'),
56+
);
57+
58+
expect(innerSpanFlags).toEqual([]);
59+
60+
const expectedOuterSpanFlags = [];
61+
for (let i = 1; i <= 2; i++) {
62+
expectedOuterSpanFlags.push([`flag.evaluation.feat${i}`, false]);
63+
}
64+
for (let i = 4; i <= MAX_FLAGS_PER_SPAN; i++) {
65+
expectedOuterSpanFlags.push([`flag.evaluation.feat${i}`, false]);
66+
}
67+
expectedOuterSpanFlags.push(['flag.evaluation.feat3', true]);
68+
expect(outerSpanFlags).toEqual(expectedOuterSpanFlags);
69+
});

0 commit comments

Comments
 (0)