-
-
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.
ci(replay): Collect replay network metrics (#6881)
Adds network info to the collected metrics - it shows the amount of data sent (Net Tx) and received (Net Rx) by the browser. It works by intercepting the network requests with a custom handler that logs the transmission stats. Because Sentry SDK is the only application code sending requests, it equals the amount of data sent & received by the SDK. You can see in the example below that because there are no errors triggered, there's no data sent/received with just the plain Sentry SDK; there's network traffic only when Replay is added.
- Loading branch information
Showing
8 changed files
with
116 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import * as playwright from 'playwright'; | ||
|
||
export class NetworkEvent { | ||
constructor( | ||
public url: string | undefined, | ||
public requestSize: number | undefined, | ||
public responseSize: number | undefined, | ||
public requestTimeNs: bigint | undefined, | ||
public responseTimeNs: bigint | undefined) { } | ||
|
||
public static fromJSON(data: Partial<NetworkEvent>): NetworkEvent { | ||
return new NetworkEvent( | ||
data.url as string, | ||
data.requestSize as number, | ||
data.responseSize as number, | ||
data.requestTimeNs == undefined ? undefined : BigInt(data.requestTimeNs), | ||
data.responseTimeNs == undefined ? undefined : BigInt(data.responseTimeNs), | ||
); | ||
} | ||
} | ||
|
||
export type NetworkUsageSerialized = Partial<{ events: Array<NetworkEvent> }>; | ||
|
||
export class NetworkUsage { | ||
public constructor(public events: Array<NetworkEvent>) { } | ||
|
||
public static fromJSON(data: NetworkUsageSerialized): NetworkUsage { | ||
return new NetworkUsage(data.events?.map(NetworkEvent.fromJSON) || []); | ||
} | ||
} | ||
|
||
export class NetworkUsageCollector { | ||
private _events = new Array<NetworkEvent>(); | ||
|
||
public static async create(page: playwright.Page): Promise<NetworkUsageCollector> { | ||
const self = new NetworkUsageCollector(); | ||
await page.route(_ => true, self._captureRequest.bind(self)); | ||
return self; | ||
} | ||
|
||
public getData(): NetworkUsage { | ||
return new NetworkUsage(this._events); | ||
} | ||
|
||
private async _captureRequest( | ||
route: playwright.Route, request: playwright.Request): Promise<void> { | ||
const url = request.url(); | ||
try { | ||
const event = new NetworkEvent( | ||
url, | ||
request.postDataBuffer()?.length, | ||
undefined, | ||
process.hrtime.bigint(), | ||
undefined | ||
); | ||
this._events.push(event); | ||
// Note: playwright would error out on file:/// requests. They are used to access local test app resources. | ||
if (url.startsWith('file:///')) { | ||
route.continue(); | ||
} else { | ||
const response = await route.fetch(); | ||
const body = await response.body(); | ||
route.fulfill({ response, body }); | ||
event.responseTimeNs = process.hrtime.bigint(); | ||
event.responseSize = body.length; | ||
} | ||
} catch (e) { | ||
console.log(`Error when capturing request: ${request.method()} ${url} - ${e}`) | ||
} | ||
} | ||
} |
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
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