-
Notifications
You must be signed in to change notification settings - Fork 72
chore: reinstate telemetry/docker change after revert MCP-49 #339
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ import { MACHINE_METADATA } from "./constants.js"; | |
import { EventCache } from "./eventCache.js"; | ||
import nodeMachineId from "node-machine-id"; | ||
import { getDeviceId } from "@mongodb-js/device-id"; | ||
import fs from "fs/promises"; | ||
|
||
type EventResult = { | ||
success: boolean; | ||
|
@@ -17,8 +18,8 @@ export const DEVICE_ID_TIMEOUT = 3000; | |
|
||
export class Telemetry { | ||
private isBufferingEvents: boolean = true; | ||
/** Resolves when the device ID is retrieved or timeout occurs */ | ||
public deviceIdPromise: Promise<string> | undefined; | ||
/** Resolves when the setup is complete or a timeout occurs */ | ||
public setupPromise: Promise<[string, boolean]> | undefined; | ||
private deviceIdAbortController = new AbortController(); | ||
private eventCache: EventCache; | ||
private getRawMachineId: () => Promise<string>; | ||
|
@@ -48,33 +49,62 @@ export class Telemetry { | |
): Telemetry { | ||
const instance = new Telemetry(session, userConfig, commonProperties, { eventCache, getRawMachineId }); | ||
|
||
void instance.start(); | ||
void instance.setup(); | ||
return instance; | ||
} | ||
|
||
private async start(): Promise<void> { | ||
if (!this.isTelemetryEnabled()) { | ||
return; | ||
private async isContainerEnv(): Promise<boolean> { | ||
if (process.platform !== "linux") { | ||
return false; // we only support linux containers for now | ||
} | ||
|
||
if (process.env.container) { | ||
return true; | ||
} | ||
this.deviceIdPromise = getDeviceId({ | ||
getMachineId: () => this.getRawMachineId(), | ||
onError: (reason, error) => { | ||
switch (reason) { | ||
case "resolutionError": | ||
logger.debug(LogId.telemetryDeviceIdFailure, "telemetry", String(error)); | ||
break; | ||
case "timeout": | ||
logger.debug(LogId.telemetryDeviceIdTimeout, "telemetry", "Device ID retrieval timed out"); | ||
break; | ||
case "abort": | ||
// No need to log in the case of aborts | ||
break; | ||
|
||
const exists = await Promise.all( | ||
gagik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
["/.dockerenv", "/run/.containerenv", "/var/run/.containerenv"].map(async (file) => { | ||
try { | ||
await fs.access(file); | ||
return true; | ||
} catch { | ||
return false; | ||
} | ||
}, | ||
abortSignal: this.deviceIdAbortController.signal, | ||
}); | ||
}) | ||
); | ||
|
||
this.commonProperties.device_id = await this.deviceIdPromise; | ||
return exists.includes(true); | ||
} | ||
|
||
private async setup(): Promise<void> { | ||
if (!this.isTelemetryEnabled()) { | ||
return; | ||
} | ||
this.setupPromise = Promise.all([ | ||
getDeviceId({ | ||
getMachineId: () => this.getRawMachineId(), | ||
onError: (reason, error) => { | ||
switch (reason) { | ||
case "resolutionError": | ||
logger.debug(LogId.telemetryDeviceIdFailure, "telemetry", String(error)); | ||
break; | ||
case "timeout": | ||
logger.debug(LogId.telemetryDeviceIdTimeout, "telemetry", "Device ID retrieval timed out"); | ||
break; | ||
case "abort": | ||
// No need to log in the case of aborts | ||
break; | ||
} | ||
}, | ||
abortSignal: this.deviceIdAbortController.signal, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we turn this abortcontroller into a generalized abort controller and use it with isContainerEnv as well? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sounds like a good improvement for a next PR |
||
}), | ||
this.isContainerEnv(), | ||
]); | ||
|
||
const [deviceId, containerEnv] = await this.setupPromise; | ||
|
||
this.commonProperties.device_id = deviceId; | ||
this.commonProperties.is_container_env = containerEnv; | ||
|
||
this.isBufferingEvents = false; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Consider using
Promise.any
instead ofPromise.all
when checking file existence to short-circuit on the first success and avoid unnecessary filesystem checks.Copilot uses AI. Check for mistakes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wouldn't have thought of this myself but that is a good point 🤖