-
Notifications
You must be signed in to change notification settings - Fork 3.3k
internal: (studio) add telemetry around time it takes to download the bundle and initialize studio fully #31834
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
ryanthemanuel
merged 2 commits into
develop
from
ryanm/internal/add-telemetry-for-studio-download
Jun 6, 2025
Merged
Changes from all commits
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
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
111 changes: 111 additions & 0 deletions
111
packages/server/lib/cloud/studio/telemetry/TelemetryManager.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,111 @@ | ||
import { performance } from 'perf_hooks' | ||
import { BUNDLE_LIFECYCLE_MARK_NAMES, BUNDLE_LIFECYCLE_MEASURE_NAMES, BUNDLE_LIFECYCLE_MEASURES, BUNDLE_LIFECYCLE_TELEMETRY_GROUP_NAMES, BUNDLE_LIFECYCLE_TELEMETRY_GROUPS } from './constants/bundle-lifecycle' | ||
import { INITIALIZATION_MARK_NAMES, INITIALIZATION_MEASURE_NAMES, INITIALIZATION_MEASURES, INITIALIZATION_TELEMETRY_GROUP_NAMES, INITIALIZATION_TELEMETRY_GROUPS } from './constants/initialization' | ||
|
||
export const MARK_NAMES = Object.freeze({ | ||
...BUNDLE_LIFECYCLE_MARK_NAMES, | ||
...INITIALIZATION_MARK_NAMES, | ||
} as const) | ||
|
||
type MarkName = (typeof MARK_NAMES)[keyof typeof MARK_NAMES] | ||
|
||
export const MEASURE_NAMES = Object.freeze({ | ||
...BUNDLE_LIFECYCLE_MEASURE_NAMES, | ||
...INITIALIZATION_MEASURE_NAMES, | ||
} as const) | ||
|
||
type MeasureName = (typeof MEASURE_NAMES)[keyof typeof MEASURE_NAMES] | ||
|
||
const MEASURES: Record<MeasureName, [MarkName, MarkName]> = Object.freeze({ | ||
...BUNDLE_LIFECYCLE_MEASURES, | ||
...INITIALIZATION_MEASURES, | ||
} as const) | ||
|
||
export const TELEMETRY_GROUP_NAMES = Object.freeze({ | ||
...BUNDLE_LIFECYCLE_TELEMETRY_GROUP_NAMES, | ||
...INITIALIZATION_TELEMETRY_GROUP_NAMES, | ||
} as const) | ||
|
||
export const TELEMETRY_GROUPS = Object.freeze({ | ||
...BUNDLE_LIFECYCLE_TELEMETRY_GROUPS, | ||
...INITIALIZATION_TELEMETRY_GROUPS, | ||
} as const) | ||
|
||
export type TelemetryGroupName = keyof typeof TELEMETRY_GROUPS | ||
|
||
class TelemetryManager { | ||
private static instance: TelemetryManager | ||
private groupMetadata: Partial<Record<TelemetryGroupName, Record<string, unknown>>> = {} | ||
|
||
private constructor () {} | ||
|
||
public static getInstance (): TelemetryManager { | ||
if (!TelemetryManager.instance) { | ||
TelemetryManager.instance = new TelemetryManager() | ||
} | ||
|
||
return TelemetryManager.instance | ||
} | ||
|
||
public mark (name: MarkName) { | ||
performance.mark(name) | ||
} | ||
|
||
public getMeasure (measureName: MeasureName): number { | ||
const [startMark, endMark] = MEASURES[measureName] | ||
|
||
try { | ||
const measure = performance.measure(measureName, startMark, endMark) | ||
|
||
return measure?.duration ?? -1 | ||
} catch (error) { | ||
return -1 | ||
} | ||
} | ||
|
||
public getMeasures ( | ||
names: MeasureName[], | ||
clear: boolean = false, | ||
): Partial<Record<MeasureName, number>> { | ||
const result: Partial<Record<MeasureName, number>> = {} | ||
|
||
for (const name of names) { | ||
result[name] = this.getMeasure(name) | ||
} | ||
if (clear) { | ||
this.clearMeasures(names) | ||
} | ||
|
||
return result | ||
} | ||
|
||
public clearMeasureGroup (groupName: TelemetryGroupName) { | ||
const measures = TELEMETRY_GROUPS[groupName] | ||
|
||
this.clearMeasures(measures) | ||
} | ||
|
||
public clearMeasures (names: MeasureName[]) { | ||
for (const name of names) { | ||
performance.clearMeasures(name) | ||
performance.clearMarks(MEASURES[name][0]) | ||
performance.clearMarks(MEASURES[name][1]) | ||
} | ||
} | ||
|
||
public addGroupMetadata (groupName: TelemetryGroupName, metadata: Record<string, unknown>) { | ||
this.groupMetadata[groupName] = this.groupMetadata[groupName] || {} | ||
this.groupMetadata[groupName] = { | ||
...this.groupMetadata[groupName], | ||
...metadata, | ||
} | ||
} | ||
|
||
public reset () { | ||
performance.clearMarks() | ||
performance.clearMeasures() | ||
this.groupMetadata = {} | ||
} | ||
} | ||
|
||
export const telemetryManager = TelemetryManager.getInstance() |
113 changes: 113 additions & 0 deletions
113
packages/server/lib/cloud/studio/telemetry/TelemetryReporter.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,113 @@ | ||
import Debug from 'debug' | ||
import { | ||
TELEMETRY_GROUPS, | ||
TelemetryGroupName, | ||
telemetryManager, | ||
} from './TelemetryManager' | ||
import type { CloudDataSource } from '@packages/data-context/src/sources/CloudDataSource' | ||
import { CloudRequest } from '../../api/cloud_request' | ||
import { getCloudMetadata } from '../../get_cloud_metadata' | ||
|
||
const debug = Debug('cypress:server:cloud:studio:telemetry:reporter') | ||
|
||
interface TelemetryReporterOptions { | ||
projectSlug?: string | ||
cloudDataSource: CloudDataSource | ||
} | ||
|
||
export class TelemetryReporter { | ||
private static instance: TelemetryReporter | ||
private projectSlug?: string | ||
private cloudDataSource: CloudDataSource | ||
|
||
private constructor ({ | ||
projectSlug, | ||
cloudDataSource, | ||
}: TelemetryReporterOptions) { | ||
this.projectSlug = projectSlug | ||
this.cloudDataSource = cloudDataSource | ||
} | ||
|
||
public static initialize (options: TelemetryReporterOptions): void { | ||
if (TelemetryReporter.instance) { | ||
// initialize gets called multiple times (e.g. if you switch between projects) | ||
// we need to reset the telemetry manager to avoid accumulating measures | ||
telemetryManager.reset() | ||
} | ||
|
||
TelemetryReporter.instance = new TelemetryReporter(options) | ||
} | ||
|
||
public static getInstance (): TelemetryReporter { | ||
if (!TelemetryReporter.instance) { | ||
throw new Error('TelemetryReporter not initialized') | ||
} | ||
|
||
return TelemetryReporter.instance | ||
} | ||
|
||
public reportTelemetry ( | ||
telemetryGroupName: TelemetryGroupName, | ||
metadata?: Record<string, unknown>, | ||
): void { | ||
this._reportTelemetry(telemetryGroupName, metadata).catch((e: unknown) => { | ||
debug( | ||
'Error reporting telemetry to cloud: %o, original telemetry: %s', | ||
e, | ||
telemetryGroupName, | ||
) | ||
}) | ||
} | ||
|
||
private async _reportTelemetry ( | ||
telemetryGroupName: TelemetryGroupName, | ||
metadata?: Record<string, unknown>, | ||
): Promise<void> { | ||
debug('Reporting telemetry for group: %s', telemetryGroupName) | ||
|
||
try { | ||
const groupMeasures = [...TELEMETRY_GROUPS[telemetryGroupName]] | ||
const measures = telemetryManager.getMeasures(groupMeasures) | ||
|
||
const payload = { | ||
projectSlug: this.projectSlug, | ||
telemetryGroupName, | ||
measures, | ||
metadata, | ||
} | ||
|
||
const { cloudUrl, cloudHeaders } = await getCloudMetadata(this.cloudDataSource) | ||
|
||
await CloudRequest.post( | ||
`${cloudUrl}/studio/telemetry`, | ||
payload, | ||
{ | ||
headers: { | ||
'Content-Type': 'application/json', | ||
...cloudHeaders, | ||
}, | ||
}, | ||
) | ||
} catch (e: unknown) { | ||
debug( | ||
'Error reporting telemetry to cloud: %o, original telemetry: %s', | ||
e, | ||
telemetryGroupName, | ||
) | ||
} | ||
} | ||
} | ||
|
||
export const initializeTelemetryReporter = ( | ||
options: TelemetryReporterOptions, | ||
) => { | ||
TelemetryReporter.initialize(options) | ||
} | ||
|
||
export const reportTelemetry = ( | ||
telemetryGroupName: TelemetryGroupName, | ||
metadata?: Record<string, unknown>, | ||
) => { | ||
TelemetryReporter.getInstance().reportTelemetry(telemetryGroupName, metadata) | ||
telemetryManager.clearMeasureGroup(telemetryGroupName) | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.