-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
test(browser:) Add test about re-sampling new traces #16730
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+130
−0
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
...ges/browser-integration-tests/suites/tracing/trace-lifetime/startNewTraceSampling/init.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import * as Sentry from '@sentry/browser'; | ||
|
||
window.Sentry = Sentry; | ||
|
||
// Force this so that the initial sampleRand is consistent | ||
Math.random = () => 0.45; | ||
|
||
Sentry.init({ | ||
dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
integrations: [Sentry.browserTracingIntegration()], | ||
tracePropagationTargets: ['http://sentry-test-site.example'], | ||
tracesSampler: ({ name }) => { | ||
if (name === 'new-trace') { | ||
return 0.9; | ||
} | ||
|
||
return 0.5; | ||
}, | ||
}); |
13 changes: 13 additions & 0 deletions
13
.../browser-integration-tests/suites/tracing/trace-lifetime/startNewTraceSampling/subject.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const newTraceBtn = document.getElementById('newTrace'); | ||
newTraceBtn.addEventListener('click', async () => { | ||
Sentry.startNewTrace(() => { | ||
// We want top ensure the new trace is sampled, so we force the sample_rand to a value above 0.9 | ||
Sentry.getCurrentScope().setPropagationContext({ | ||
...Sentry.getCurrentScope().getPropagationContext(), | ||
sampleRand: 0.85, | ||
}); | ||
Sentry.startSpan({ op: 'ui.interaction.click', name: 'new-trace' }, async () => { | ||
await fetch('http://sentry-test-site.example'); | ||
}); | ||
}); | ||
}); |
9 changes: 9 additions & 0 deletions
9
...owser-integration-tests/suites/tracing/trace-lifetime/startNewTraceSampling/template.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8" /> | ||
</head> | ||
<body> | ||
<button id="newTrace">new Trace</button> | ||
</body> | ||
</html> |
89 changes: 89 additions & 0 deletions
89
...ges/browser-integration-tests/suites/tracing/trace-lifetime/startNewTraceSampling/test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { expect } from '@playwright/test'; | ||
import { sentryTest } from '../../../../utils/fixtures'; | ||
import type { EventAndTraceHeader } from '../../../../utils/helpers'; | ||
import { | ||
eventAndTraceHeaderRequestParser, | ||
getFirstSentryEnvelopeRequest, | ||
shouldSkipTracingTest, | ||
waitForTransactionRequest, | ||
} from '../../../../utils/helpers'; | ||
|
||
sentryTest( | ||
'new trace started with `startNewTrace` is sampled according to the `tracesSampler`', | ||
async ({ getLocalTestUrl, page }) => { | ||
if (shouldSkipTracingTest()) { | ||
sentryTest.skip(); | ||
} | ||
|
||
const url = await getLocalTestUrl({ testDir: __dirname }); | ||
|
||
await page.route('http://sentry-test-site.example/**', route => { | ||
return route.fulfill({ | ||
status: 200, | ||
contentType: 'application/json', | ||
body: JSON.stringify({}), | ||
}); | ||
}); | ||
|
||
const [pageloadEvent, pageloadTraceHeaders] = await getFirstSentryEnvelopeRequest<EventAndTraceHeader>( | ||
page, | ||
url, | ||
eventAndTraceHeaderRequestParser, | ||
); | ||
|
||
const pageloadTraceContext = pageloadEvent.contexts?.trace; | ||
|
||
expect(pageloadEvent.type).toEqual('transaction'); | ||
|
||
expect(pageloadTraceContext).toMatchObject({ | ||
op: 'pageload', | ||
trace_id: expect.stringMatching(/^[0-9a-f]{32}$/), | ||
span_id: expect.stringMatching(/^[0-9a-f]{16}$/), | ||
data: { | ||
'sentry.sample_rate': 0.5, | ||
}, | ||
}); | ||
expect(pageloadTraceContext).not.toHaveProperty('parent_span_id'); | ||
|
||
expect(pageloadTraceHeaders).toEqual({ | ||
environment: 'production', | ||
public_key: 'public', | ||
sample_rate: '0.5', | ||
sampled: 'true', | ||
trace_id: pageloadTraceContext?.trace_id, | ||
sample_rand: '0.45', | ||
}); | ||
|
||
const transactionPromise = waitForTransactionRequest(page, event => { | ||
return event.transaction === 'new-trace'; | ||
}); | ||
|
||
await page.locator('#newTrace').click(); | ||
|
||
const [newTraceTransactionEvent, newTraceTransactionTraceHeaders] = eventAndTraceHeaderRequestParser( | ||
await transactionPromise, | ||
); | ||
|
||
const newTraceTransactionTraceContext = newTraceTransactionEvent.contexts?.trace; | ||
expect(newTraceTransactionTraceContext).toMatchObject({ | ||
op: 'ui.interaction.click', | ||
trace_id: expect.stringMatching(/^[0-9a-f]{32}$/), | ||
span_id: expect.stringMatching(/^[0-9a-f]{16}$/), | ||
data: { | ||
'sentry.sample_rate': 0.9, | ||
}, | ||
}); | ||
|
||
expect(newTraceTransactionTraceHeaders).toEqual({ | ||
environment: 'production', | ||
public_key: 'public', | ||
sample_rate: '0.9', | ||
sampled: 'true', | ||
trace_id: newTraceTransactionTraceContext?.trace_id, | ||
transaction: 'new-trace', | ||
sample_rand: '0.85', | ||
}); | ||
|
||
expect(newTraceTransactionTraceContext?.trace_id).not.toEqual(pageloadTraceContext?.trace_id); | ||
}, | ||
); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.