-
Notifications
You must be signed in to change notification settings - Fork 42
feat(telemetry): instrument connection lifecycle #942
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
Open
EhabY
wants to merge
7
commits into
main
Choose a base branch
from
feat/issue-905-connection-telemetry
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
01fc8cf
feat(telemetry): instrument connection lifecycle
EhabY d5c7263
refactor(telemetry): simplify connection instrumentation
EhabY cbffa11
add changelog entry
EhabY edd48db
test(telemetry): tighten connection telemetry tests
EhabY 5ed31ab
refactor(telemetry): address review feedback for connection lifecycle
EhabY b2d1c2a
fix(telemetry): track concurrent delays and tighten ssh tests
EhabY a8a1853
refactor(telemetry): clarify reconnect cycle event name
EhabY 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| import type { NetworkInfo } from "../remote/sshProcess"; | ||
| import type { TelemetryReporter } from "../telemetry/reporter"; | ||
|
|
||
| const NETWORK_SAMPLE_INTERVAL_MS = 60_000; | ||
| const NETWORK_LATENCY_CHANGE_RATIO = 0.1; | ||
|
|
||
| export type ProcessLossCause = "stale_network_info" | "missing_network_info"; | ||
|
|
||
| interface NetworkSample { | ||
| readonly emittedAtMs: number; | ||
| readonly p2p: boolean; | ||
| readonly preferredDerp: string; | ||
| readonly latencyMs: number; | ||
| } | ||
|
|
||
| interface ProcessDiscoveryResult { | ||
| readonly pid: number | undefined; | ||
| readonly attempts: number; | ||
| } | ||
|
|
||
| export class SshTelemetry { | ||
| readonly #telemetry: TelemetryReporter; | ||
| #processStartedAtMs: number | undefined; | ||
| #processLostAtMs: number | undefined; | ||
| #lastNetworkSample: NetworkSample | undefined; | ||
|
|
||
| public constructor(telemetry: TelemetryReporter) { | ||
| this.#telemetry = telemetry; | ||
| } | ||
|
|
||
| public traceProcessDiscovery( | ||
| fn: () => Promise<ProcessDiscoveryResult>, | ||
| ): Promise<number | undefined> { | ||
| return this.#telemetry.trace("ssh.process.discovered", async (span) => { | ||
| const { pid, attempts } = await fn(); | ||
| span.setProperty("found", String(pid !== undefined)); | ||
| span.setMeasurement("attempts", attempts); | ||
| return pid; | ||
| }); | ||
| } | ||
|
|
||
| public processStarted(): void { | ||
| this.#processStartedAtMs = performance.now(); | ||
| this.#processLostAtMs = undefined; | ||
| } | ||
|
|
||
| public processLost(cause: ProcessLossCause): void { | ||
| if ( | ||
| this.#processStartedAtMs === undefined || | ||
| this.#processLostAtMs !== undefined | ||
| ) { | ||
| return; | ||
| } | ||
| const now = performance.now(); | ||
| this.#processLostAtMs = now; | ||
| this.#telemetry.log( | ||
| "ssh.process.lost", | ||
| { cause }, | ||
| { uptimeMs: now - this.#processStartedAtMs }, | ||
| ); | ||
| } | ||
|
|
||
| public processRecovered(): void { | ||
| if (this.#processLostAtMs === undefined) { | ||
| return; | ||
| } | ||
| this.#telemetry.log( | ||
| "ssh.process.recovered", | ||
| {}, | ||
| { recoveryDurationMs: performance.now() - this.#processLostAtMs }, | ||
| ); | ||
| this.#processLostAtMs = undefined; | ||
| } | ||
|
|
||
| /** Handover to a different SSH process. Always emits `ssh.process.replaced`, | ||
| * even when the prior process was already lost (replacement is operationally | ||
| * distinct from recovery). */ | ||
| public processReplaced(): void { | ||
| const now = performance.now(); | ||
| if (this.#processStartedAtMs !== undefined) { | ||
| const wasLost = this.#processLostAtMs !== undefined; | ||
| const measurements: Record<string, number> = { | ||
| previousUptimeMs: now - this.#processStartedAtMs, | ||
| }; | ||
| if (this.#processLostAtMs !== undefined) { | ||
| measurements.lostDurationMs = now - this.#processLostAtMs; | ||
| } | ||
| this.#telemetry.log( | ||
| "ssh.process.replaced", | ||
| { wasLost: String(wasLost) }, | ||
| measurements, | ||
| ); | ||
| } | ||
| this.#processStartedAtMs = now; | ||
| this.#processLostAtMs = undefined; | ||
| this.#lastNetworkSample = undefined; | ||
| } | ||
|
|
||
| /** Terminal teardown signal. Emits regardless of prior lost state so | ||
| * consumers always see a session-ending event. */ | ||
| public disposed(): void { | ||
| if (this.#processStartedAtMs === undefined) { | ||
| return; | ||
| } | ||
| const now = performance.now(); | ||
| const wasLost = this.#processLostAtMs !== undefined; | ||
| this.#telemetry.log( | ||
| "ssh.process.disposed", | ||
| { wasLost: String(wasLost) }, | ||
| { uptimeMs: now - this.#processStartedAtMs }, | ||
| ); | ||
| this.#processStartedAtMs = undefined; | ||
| this.#processLostAtMs = undefined; | ||
| this.#lastNetworkSample = undefined; | ||
| } | ||
|
|
||
| public networkSampled(network: NetworkInfo): void { | ||
| const now = performance.now(); | ||
| const previous = this.#lastNetworkSample; | ||
| if (previous && !shouldEmitSample(previous, network, now)) { | ||
| return; | ||
| } | ||
|
|
||
| this.#lastNetworkSample = { | ||
| emittedAtMs: now, | ||
| p2p: network.p2p, | ||
| preferredDerp: network.preferred_derp, | ||
| latencyMs: network.latency, | ||
| }; | ||
| this.#telemetry.log( | ||
| "ssh.network.sampled", | ||
| { | ||
| p2p: String(network.p2p), | ||
| preferredDerp: network.preferred_derp, | ||
| }, | ||
| { | ||
| latencyMs: network.latency, | ||
| downloadMbits: bytesPerSecondToMbits(network.download_bytes_sec), | ||
| uploadMbits: bytesPerSecondToMbits(network.upload_bytes_sec), | ||
| }, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /** Emit on p2p flip, DERP change, large latency swing, or heartbeat interval. */ | ||
| function shouldEmitSample( | ||
| previous: NetworkSample, | ||
| current: NetworkInfo, | ||
| now: number, | ||
| ): boolean { | ||
| if (now - previous.emittedAtMs >= NETWORK_SAMPLE_INTERVAL_MS) { | ||
| return true; | ||
| } | ||
| if (current.p2p !== previous.p2p) { | ||
| return true; | ||
| } | ||
| if (current.preferred_derp !== previous.preferredDerp) { | ||
| return true; | ||
| } | ||
| return hasMeaningfulLatencyChange(current.latency, previous.latencyMs); | ||
| } | ||
|
|
||
| function hasMeaningfulLatencyChange( | ||
| current: number, | ||
| previous: number, | ||
| ): boolean { | ||
| if (previous === 0) { | ||
| return current !== 0; | ||
| } | ||
| return Math.abs(current - previous) / previous > NETWORK_LATENCY_CHANGE_RATIO; | ||
| } | ||
|
|
||
| function bytesPerSecondToMbits(bytesPerSecond: number): number { | ||
| return (bytesPerSecond * 8) / 1_000_000; | ||
| } | ||
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.