-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ref(browser): Introduce client reports envelope helper (#4588)
Leverage the new envelope utility functions to construct client report envelopes sent in the browser transport. This also opens us up to more easily add client reports to node or other environments.
- Loading branch information
1 parent
041186e
commit 2e5b0b8
Showing
5 changed files
with
92 additions
and
20 deletions.
There are no files selected for viewing
This file contains 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 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 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,24 @@ | ||
import { ClientReport, ClientReportEnvelope, ClientReportItem } from '@sentry/types'; | ||
|
||
import { createEnvelope } from './envelope'; | ||
import { dateTimestampInSeconds } from './time'; | ||
|
||
/** | ||
* Creates client report envelope | ||
* @param discarded_events An array of discard events | ||
* @param dsn A DSN that can be set on the header. Optional. | ||
*/ | ||
export function createClientReportEnvelope( | ||
discarded_events: ClientReport['discarded_events'], | ||
dsn?: string, | ||
timestamp?: number, | ||
): ClientReportEnvelope { | ||
const clientReportItem: ClientReportItem = [ | ||
{ type: 'client_report' }, | ||
{ | ||
timestamp: timestamp || dateTimestampInSeconds(), | ||
discarded_events, | ||
}, | ||
]; | ||
return createEnvelope<ClientReportEnvelope>(dsn ? { dsn } : {}, [clientReportItem]); | ||
} |
This file contains 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 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,51 @@ | ||
import { ClientReport } from '@sentry/types'; | ||
|
||
import { createClientReportEnvelope } from '../src/clientreport'; | ||
import { serializeEnvelope } from '../src/envelope'; | ||
|
||
const DEFAULT_DISCARDED_EVENTS: Array<ClientReport['discarded_events']> = [ | ||
{ | ||
reason: 'before_send', | ||
category: 'event', | ||
quantity: 30, | ||
}, | ||
{ | ||
reason: 'network_error', | ||
category: 'transaction', | ||
quantity: 23, | ||
}, | ||
]; | ||
|
||
const MOCK_DSN = 'https://[email protected]/1'; | ||
|
||
describe('createClientReportEnvelope', () => { | ||
const testTable: Array< | ||
[string, Parameters<typeof createClientReportEnvelope>[0], Parameters<typeof createClientReportEnvelope>[1]] | ||
> = [ | ||
['with no discard reasons', [], undefined], | ||
['with a dsn', [], MOCK_DSN], | ||
['with discard reasons', DEFAULT_DISCARDED_EVENTS, MOCK_DSN], | ||
]; | ||
it.each(testTable)('%s', (_: string, discardedEvents, dsn) => { | ||
const env = createClientReportEnvelope(discardedEvents, dsn); | ||
|
||
expect(env[0]).toEqual(dsn ? { dsn } : {}); | ||
|
||
const items = env[1]; | ||
expect(items).toHaveLength(1); | ||
const clientReportItem = items[0]; | ||
|
||
expect(clientReportItem[0]).toEqual({ type: 'client_report' }); | ||
expect(clientReportItem[1]).toEqual({ timestamp: expect.any(Number), discarded_events: discardedEvents }); | ||
}); | ||
|
||
it('serializes an envelope', () => { | ||
const env = createClientReportEnvelope(DEFAULT_DISCARDED_EVENTS, MOCK_DSN, 123456); | ||
const serializedEnv = serializeEnvelope(env); | ||
expect(serializedEnv).toMatchInlineSnapshot(` | ||
"{\\"dsn\\":\\"https://[email protected]/1\\"} | ||
{\\"type\\":\\"client_report\\"} | ||
{\\"timestamp\\":123456,\\"discarded_events\\":[{\\"reason\\":\\"before_send\\",\\"category\\":\\"event\\",\\"quantity\\":30},{\\"reason\\":\\"network_error\\",\\"category\\":\\"transaction\\",\\"quantity\\":23}]}" | ||
`); | ||
}); | ||
}); |