Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/common/telemetry/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,9 @@ export interface IEventNamePropertyMapping {
"breakdownGlobalVirtualEnvs": { "classification": "SystemMetaData", "purpose": "PerformanceAndHealth", "isMeasurement": true, "owner": "eleanorjboyd" },
"breakdownWorkspaces": { "classification": "SystemMetaData", "purpose": "PerformanceAndHealth", "isMeasurement": true, "owner": "eleanorjboyd" },
"locatorsJson": { "classification": "SystemMetaData", "purpose": "PerformanceAndHealth", "owner": "eleanorjboyd" },
"petVersion": { "classification": "SystemMetaData", "purpose": "PerformanceAndHealth", "owner": "eleanorjboyd" },
"petBuildId": { "classification": "SystemMetaData", "purpose": "PerformanceAndHealth", "owner": "eleanorjboyd" },
"petCommitSha": { "classification": "SystemMetaData", "purpose": "PerformanceAndHealth", "owner": "eleanorjboyd" },
"<duration>": { "classification": "SystemMetaData", "purpose": "FeatureInsight", "isMeasurement": true, "owner": "eleanorjboyd" }
}
*/
Expand All @@ -615,6 +618,12 @@ export interface IEventNamePropertyMapping {
breakdownWorkspaces?: number;
/** JSON-serialized Record<locatorName, ms>. Parse with parse_json() in Kusto. */
locatorsJson?: string;
/** PET crate version reported by the `info` RPC. 'unknown' if the call failed or the PET binary doesn't implement it. */
petVersion?: string;
/** PET build identifier (CI build run ID) reported by the `info` RPC. 'unknown' if unavailable. */
petBuildId?: string;
/** PET source git commit SHA reported by the `info` RPC. 'unknown' if unavailable. */
petCommitSha?: string;
};

/* __GDPR__
Expand All @@ -639,6 +648,9 @@ export interface IEventNamePropertyMapping {
"result": { "classification": "SystemMetaData", "purpose": "FeatureInsight", "owner": "eleanorjboyd" },
"errorType": { "classification": "SystemMetaData", "purpose": "FeatureInsight", "owner": "eleanorjboyd" },
"triggerReason": { "classification": "SystemMetaData", "purpose": "FeatureInsight", "owner": "eleanorjboyd" },
"petVersion": { "classification": "SystemMetaData", "purpose": "PerformanceAndHealth", "owner": "eleanorjboyd" },
"petBuildId": { "classification": "SystemMetaData", "purpose": "PerformanceAndHealth", "owner": "eleanorjboyd" },
"petCommitSha": { "classification": "SystemMetaData", "purpose": "PerformanceAndHealth", "owner": "eleanorjboyd" },
"<duration>": { "classification": "SystemMetaData", "purpose": "FeatureInsight", "isMeasurement": true, "owner": "eleanorjboyd" }
}
*/
Expand All @@ -654,18 +666,33 @@ export interface IEventNamePropertyMapping {
* start_failed | unknown.
*/
triggerReason: string;
/** PET crate version reported by the `info` RPC. 'unknown' if the call failed or the PET binary doesn't implement it. */
petVersion?: string;
/** PET build identifier (CI build run ID) reported by the `info` RPC. 'unknown' if unavailable. */
petBuildId?: string;
/** PET source git commit SHA reported by the `info` RPC. 'unknown' if unavailable. */
petCommitSha?: string;
};

/* __GDPR__
"pet.resolve": {
"result": { "classification": "SystemMetaData", "purpose": "FeatureInsight", "owner": "eleanorjboyd" },
"errorType": { "classification": "SystemMetaData", "purpose": "FeatureInsight", "owner": "eleanorjboyd" },
"petVersion": { "classification": "SystemMetaData", "purpose": "PerformanceAndHealth", "owner": "eleanorjboyd" },
"petBuildId": { "classification": "SystemMetaData", "purpose": "PerformanceAndHealth", "owner": "eleanorjboyd" },
"petCommitSha": { "classification": "SystemMetaData", "purpose": "PerformanceAndHealth", "owner": "eleanorjboyd" },
"<duration>": { "classification": "SystemMetaData", "purpose": "FeatureInsight", "isMeasurement": true, "owner": "eleanorjboyd" }
}
*/
[EventNames.PET_RESOLVE]: {
result: 'success' | 'timeout' | 'error';
errorType?: string;
/** PET crate version reported by the `info` RPC. 'unknown' if the call failed or the PET binary doesn't implement it. */
petVersion?: string;
/** PET build identifier (CI build run ID) reported by the `info` RPC. 'unknown' if unavailable. */
petBuildId?: string;
/** PET source git commit SHA reported by the `info` RPC. 'unknown' if unavailable. */
petCommitSha?: string;
};

/* __GDPR__
Expand Down
81 changes: 79 additions & 2 deletions src/managers/common/nativePythonFinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const CONFIGURE_TIMEOUT_MS = 30_000; // 30 seconds for configuration
const MAX_CONFIGURE_TIMEOUT_MS = 60_000; // Max configure timeout after retries (60s)
const REFRESH_TIMEOUT_MS = 30_000; // 30 seconds for full refresh (with 1 retry = 60s max)
const RESOLVE_TIMEOUT_MS = 30_000; // 30 seconds for single resolve
const INFO_TIMEOUT_MS = 2_000; // `info` is a const lookup on PET; 2s is generous

// CLI fallback timeout: generous budget since it's a full process spawn doing a full scan
const CLI_FALLBACK_TIMEOUT_MS = 120_000; // 2 minutes
Expand Down Expand Up @@ -265,6 +266,17 @@ interface PetTelemetryNotification {
};
}

/**
* Response shape of the PET `info` JSON-RPC request.
* `buildId` / `commitSha` are populated only when the PET binary was built by CI
* with the appropriate env vars set; local dev builds omit them.
*/
interface NativePetInfo {
petVersion: string;
buildId?: string;
commitSha?: string;
}

/**
* Error thrown when a JSON-RPC request times out.
*/
Expand Down Expand Up @@ -322,6 +334,13 @@ class NativePythonFinderImpl implements NativePythonFinder {
private isRestarting: boolean = false;
private processExitReason: string | undefined = undefined;
private readonly configureRetry = new ConfigureRetryState();
/**
* Cached PET `info` response for the current connection. Reset to undefined on every
* `start()` and re-populated asynchronously by `kickoffInfoFetch()`. Telemetry callers
* read this via `getPetInfoProperties()`; if the fetch hasn't finished yet (or the PET
* binary is too old to implement `info`), telemetry reports 'unknown'.
*/
private petInfo: NativePetInfo | undefined;

constructor(
private readonly outputChannel: LogOutputChannel,
Expand Down Expand Up @@ -353,7 +372,10 @@ class NativePythonFinderImpl implements NativePythonFinder {
this.outputChannel.info(`Resolved Python Environment ${environment.executable}`);
// Reset restart attempts on successful request
this.restartAttempts = 0;
sendTelemetryEvent(EventNames.PET_RESOLVE, sw.elapsedTime, { result: 'success' });
sendTelemetryEvent(EventNames.PET_RESOLVE, sw.elapsedTime, {
result: 'success',
...this.getPetInfoProperties(),
});
return environment;
} catch (ex) {
// On resolve timeout or connection error (not configure — configure handles its own timeout),
Expand All @@ -376,6 +398,7 @@ class NativePythonFinderImpl implements NativePythonFinder {
{
result: errorType === 'spawn_timeout' ? 'timeout' : 'error',
errorType,
...this.getPetInfoProperties(),
},
ex instanceof Error ? ex : undefined,
);
Expand Down Expand Up @@ -460,6 +483,7 @@ class NativePythonFinderImpl implements NativePythonFinder {
attempt,
result: 'success',
triggerReason,
...this.getPetInfoProperties(),
});

// Reset restart attempts on successful start (process didn't immediately fail)
Expand All @@ -468,7 +492,13 @@ class NativePythonFinderImpl implements NativePythonFinder {
sendTelemetryEvent(
EventNames.PET_PROCESS_RESTART,
sw.elapsedTime,
{ attempt, result: 'error', errorType: classifyError(ex), triggerReason },
{
attempt,
result: 'error',
errorType: classifyError(ex),
triggerReason,
...this.getPetInfoProperties(),
},
ex instanceof Error ? ex : undefined,
);
this.outputChannel.error('[pet] Failed to restart Python Environment Tools:', ex);
Expand Down Expand Up @@ -701,9 +731,54 @@ class NativePythonFinderImpl implements NativePythonFinder {
);

connection.listen();

// Stamp PET telemetry with version/buildId/commitSha. Fire-and-forget — must not block refresh.
this.petInfo = undefined;
this.kickoffInfoFetch(connection);

return connection;
}

/**
* Asks the PET server for its build metadata (version + optional buildId + optional commitSha)
* and caches it in `this.petInfo` for downstream telemetry. Runs once per `start()` call.
*
* Fire-and-forget by design — the response is not awaited so refresh/resolve callers are
* never blocked. The 2 s timeout caps the worst case if PET is misbehaving. If a newer
* connection has replaced `this.connection` by the time the response arrives, the response
* is dropped to avoid clobbering the cache for the newer process.
*/
private kickoffInfoFetch(connection: rpc.MessageConnection): void {
sendRequestWithTimeout<NativePetInfo>(connection, 'info', {}, INFO_TIMEOUT_MS)
.then((result) => {
if (connection !== this.connection) {
return;
}
this.petInfo = result;
this.outputChannel.debug('[pet] info:', result);
})
.catch((ex) => {
if (connection !== this.connection) {
return;
}
// Older PET binaries don't implement `info`; leave petInfo undefined so telemetry reports 'unknown'.
this.outputChannel.debug('[pet] info request failed:', ex);
});
}

/**
* Builds the petVersion/petBuildId/petCommitSha properties for PET telemetry events.
* Always returns concrete strings (defaulting to 'unknown') so Kusto can group by them
* without dealing with nulls.
*/
private getPetInfoProperties(): { petVersion: string; petBuildId: string; petCommitSha: string } {
return {
petVersion: this.petInfo?.petVersion ?? 'unknown',
petBuildId: this.petInfo?.buildId ?? 'unknown',
petCommitSha: this.petInfo?.commitSha ?? 'unknown',
};
}

private async doRefresh(options?: NativePythonEnvironmentKind | Uri[]): Promise<NativeInfo[]> {
let lastError: unknown;

Expand Down Expand Up @@ -840,6 +915,7 @@ class NativePythonFinderImpl implements NativePythonFinder {
searchPathCount,
attempt,
locatorsJson: refreshPerf ? JSON.stringify(refreshPerf.locators) : undefined,
...this.getPetInfoProperties(),
},
);
} catch (ex) {
Expand All @@ -853,6 +929,7 @@ class NativePythonFinderImpl implements NativePythonFinder {
unresolvedCount,
attempt,
errorType,
...this.getPetInfoProperties(),
},
ex instanceof Error ? ex : undefined,
);
Expand Down
Loading