-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(metrics): Add timings
method to metrics
#12226
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
Merged
Changes from all commits
Commits
Show all changes
6 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
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
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
39 changes: 39 additions & 0 deletions
39
dev-packages/browser-integration-tests/suites/metrics/timing/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,39 @@ | ||
import * as Sentry from '@sentry/browser'; | ||
|
||
window.Sentry = Sentry; | ||
|
||
Sentry.init({ | ||
dsn: 'https://[email protected]/1337', | ||
tracesSampleRate: 1.0, | ||
release: '1.0.0', | ||
autoSessionTracking: false, | ||
}); | ||
|
||
window.timingSync = () => { | ||
// Ensure we always have a wrapping span | ||
return Sentry.startSpan({ name: 'manual span' }, () => { | ||
return Sentry.metrics.timing('timingSync', () => { | ||
sleepSync(200); | ||
return 'sync done'; | ||
}); | ||
}); | ||
}; | ||
|
||
window.timingAsync = () => { | ||
// Ensure we always have a wrapping span | ||
return Sentry.startSpan({ name: 'manual span' }, () => { | ||
return Sentry.metrics.timing('timingAsync', async () => { | ||
await new Promise(resolve => setTimeout(resolve, 200)); | ||
return 'async done'; | ||
}); | ||
}); | ||
}; | ||
|
||
function sleepSync(milliseconds) { | ||
var start = new Date().getTime(); | ||
for (var i = 0; i < 1e7; i++) { | ||
if (new Date().getTime() - start > milliseconds) { | ||
break; | ||
} | ||
} | ||
} |
175 changes: 175 additions & 0 deletions
175
dev-packages/browser-integration-tests/suites/metrics/timing/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,175 @@ | ||
import { expect } from '@playwright/test'; | ||
|
||
import { sentryTest } from '../../../utils/fixtures'; | ||
import { | ||
envelopeRequestParser, | ||
properEnvelopeRequestParser, | ||
shouldSkipTracingTest, | ||
waitForTransactionRequest, | ||
} from '../../../utils/helpers'; | ||
|
||
sentryTest('allows to wrap sync methods with a timing metric', async ({ getLocalTestUrl, page }) => { | ||
if (shouldSkipTracingTest()) { | ||
sentryTest.skip(); | ||
} | ||
|
||
await page.route('https://dsn.ingest.sentry.io/**/*', route => { | ||
return route.fulfill({ | ||
status: 200, | ||
contentType: 'application/json', | ||
body: JSON.stringify({ id: 'test-id' }), | ||
}); | ||
}); | ||
|
||
const url = await getLocalTestUrl({ testDir: __dirname }); | ||
|
||
const beforeTime = Math.floor(Date.now() / 1000); | ||
|
||
const metricsPromiseReq = page.waitForRequest(req => { | ||
const postData = req.postData(); | ||
if (!postData) { | ||
return false; | ||
} | ||
|
||
try { | ||
// this implies this is a metrics envelope | ||
return typeof envelopeRequestParser(req) === 'string'; | ||
} catch { | ||
return false; | ||
} | ||
}); | ||
|
||
const transactionPromise = waitForTransactionRequest(page); | ||
|
||
await page.goto(url); | ||
await page.waitForFunction('typeof window.timingSync === "function"'); | ||
const response = await page.evaluate('window.timingSync()'); | ||
|
||
expect(response).toBe('sync done'); | ||
|
||
const statsdString = envelopeRequestParser<string>(await metricsPromiseReq); | ||
const transactionEvent = properEnvelopeRequestParser(await transactionPromise); | ||
|
||
expect(typeof statsdString).toEqual('string'); | ||
|
||
const parsedStatsd = /timingSync@second:(0\.\d+)\|d\|#(.+)\|T(\d+)/.exec(statsdString); | ||
|
||
expect(parsedStatsd).toBeTruthy(); | ||
|
||
const duration = parseFloat(parsedStatsd![1]); | ||
const tags = parsedStatsd![2]; | ||
const timestamp = parseInt(parsedStatsd![3], 10); | ||
|
||
expect(timestamp).toBeGreaterThanOrEqual(beforeTime); | ||
expect(tags).toEqual('release:1.0.0,transaction:manual span'); | ||
expect(duration).toBeGreaterThan(0.2); | ||
expect(duration).toBeLessThan(1); | ||
|
||
expect(transactionEvent).toBeDefined(); | ||
expect(transactionEvent.transaction).toEqual('manual span'); | ||
|
||
const spans = transactionEvent.spans || []; | ||
|
||
expect(spans.length).toBe(1); | ||
const span = spans[0]; | ||
expect(span.op).toEqual('metrics.timing'); | ||
expect(span.description).toEqual('timingSync'); | ||
expect(span.timestamp! - span.start_timestamp).toEqual(duration); | ||
expect(span._metrics_summary).toEqual({ | ||
'd:timingSync@second': [ | ||
{ | ||
count: 1, | ||
max: duration, | ||
min: duration, | ||
sum: duration, | ||
tags: { | ||
release: '1.0.0', | ||
transaction: 'manual span', | ||
}, | ||
}, | ||
], | ||
}); | ||
}); | ||
|
||
sentryTest('allows to wrap async methods with a timing metric', async ({ getLocalTestUrl, page }) => { | ||
if (shouldSkipTracingTest()) { | ||
sentryTest.skip(); | ||
} | ||
|
||
await page.route('https://dsn.ingest.sentry.io/**/*', route => { | ||
return route.fulfill({ | ||
status: 200, | ||
contentType: 'application/json', | ||
body: JSON.stringify({ id: 'test-id' }), | ||
}); | ||
}); | ||
|
||
const url = await getLocalTestUrl({ testDir: __dirname }); | ||
|
||
const beforeTime = Math.floor(Date.now() / 1000); | ||
|
||
const metricsPromiseReq = page.waitForRequest(req => { | ||
const postData = req.postData(); | ||
if (!postData) { | ||
return false; | ||
} | ||
|
||
try { | ||
// this implies this is a metrics envelope | ||
return typeof envelopeRequestParser(req) === 'string'; | ||
} catch { | ||
return false; | ||
} | ||
}); | ||
|
||
const transactionPromise = waitForTransactionRequest(page); | ||
|
||
await page.goto(url); | ||
await page.waitForFunction('typeof window.timingAsync === "function"'); | ||
const response = await page.evaluate('window.timingAsync()'); | ||
|
||
expect(response).toBe('async done'); | ||
|
||
const statsdString = envelopeRequestParser<string>(await metricsPromiseReq); | ||
const transactionEvent = properEnvelopeRequestParser(await transactionPromise); | ||
|
||
expect(typeof statsdString).toEqual('string'); | ||
|
||
const parsedStatsd = /timingAsync@second:(0\.\d+)\|d\|#(.+)\|T(\d+)/.exec(statsdString); | ||
|
||
expect(parsedStatsd).toBeTruthy(); | ||
|
||
const duration = parseFloat(parsedStatsd![1]); | ||
const tags = parsedStatsd![2]; | ||
const timestamp = parseInt(parsedStatsd![3], 10); | ||
|
||
expect(timestamp).toBeGreaterThanOrEqual(beforeTime); | ||
expect(tags).toEqual('release:1.0.0,transaction:manual span'); | ||
expect(duration).toBeGreaterThan(0.2); | ||
expect(duration).toBeLessThan(1); | ||
|
||
expect(transactionEvent).toBeDefined(); | ||
expect(transactionEvent.transaction).toEqual('manual span'); | ||
|
||
const spans = transactionEvent.spans || []; | ||
|
||
expect(spans.length).toBe(1); | ||
const span = spans[0]; | ||
expect(span.op).toEqual('metrics.timing'); | ||
expect(span.description).toEqual('timingAsync'); | ||
expect(span.timestamp! - span.start_timestamp).toEqual(duration); | ||
expect(span._metrics_summary).toEqual({ | ||
'd:timingAsync@second': [ | ||
{ | ||
count: 1, | ||
max: duration, | ||
min: duration, | ||
sum: duration, | ||
tags: { | ||
release: '1.0.0', | ||
transaction: 'manual span', | ||
}, | ||
}, | ||
], | ||
}); | ||
}); |
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
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ooops turns out metrics summaries never worked for minified CDN bundles 😬 we had no tests covering this, now we have!