-
Notifications
You must be signed in to change notification settings - Fork 133
chore: add is_container_env to telemetry MCP-2 #298
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 14 commits
a480c94
126994c
bf204bb
3d91b40
fe64649
f73c208
671bbeb
c2a1246
678beee
8d53eea
95b0b06
911fdcb
4703628
931816f
0b60fe2
6ba2346
f4bfbf0
3424e0f
83fd1d9
7e3cdb1
c67877e
814ccb9
1dba800
a0c208c
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,55 +7,91 @@ 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"; | ||||||||||
|
||||||||||
async function fileExists(filePath: string): Promise<boolean> { | ||||||||||
try { | ||||||||||
await fs.stat(filePath); | ||||||||||
return true; // File exists | ||||||||||
himanshusinghs marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
} catch (e: unknown) { | ||||||||||
if ( | ||||||||||
e instanceof Error && | ||||||||||
( | ||||||||||
e as Error & { | ||||||||||
code: string; | ||||||||||
} | ||||||||||
).code === "ENOENT" | ||||||||||
) { | ||||||||||
return false; // File does not exist | ||||||||||
} | ||||||||||
throw e; // Re-throw unexpected errors | ||||||||||
gagik marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
type EventResult = { | ||||||||||
success: boolean; | ||||||||||
error?: Error; | ||||||||||
}; | ||||||||||
async function isContainerized(): Promise<boolean> { | ||||||||||
if (process.env.container) { | ||||||||||
return true; | ||||||||||
} | ||||||||||
|
||||||||||
const exists = await Promise.all(["/.dockerenv", "/run/.containerenv", "/var/run/.containerenv"].map(fileExists)); | ||||||||||
|
||||||||||
export const DEVICE_ID_TIMEOUT = 3000; | ||||||||||
return exists.includes(true); | ||||||||||
} | ||||||||||
|
||||||||||
export class Telemetry { | ||||||||||
private isBufferingEvents: boolean = true; | ||||||||||
/** Resolves when the device ID is retrieved or timeout occurs */ | ||||||||||
public deviceIdPromise: Promise<string> | undefined; | ||||||||||
private deviceIdPromise: Promise<string> | undefined; | ||||||||||
private containerEnvPromise: Promise<boolean> | undefined; | ||||||||||
|
||||||||||
private deviceIdAbortController = new AbortController(); | ||||||||||
private eventCache: EventCache; | ||||||||||
private getRawMachineId: () => Promise<string>; | ||||||||||
private getContainerEnv: () => Promise<boolean>; | ||||||||||
|
||||||||||
private constructor( | ||||||||||
private readonly session: Session, | ||||||||||
private readonly userConfig: UserConfig, | ||||||||||
private readonly commonProperties: CommonProperties, | ||||||||||
{ eventCache, getRawMachineId }: { eventCache: EventCache; getRawMachineId: () => Promise<string> } | ||||||||||
{ | ||||||||||
eventCache, | ||||||||||
getRawMachineId, | ||||||||||
getContainerEnv, | ||||||||||
}: { | ||||||||||
eventCache: EventCache; | ||||||||||
getRawMachineId: () => Promise<string>; | ||||||||||
getContainerEnv: () => Promise<boolean>; | ||||||||||
} | ||||||||||
) { | ||||||||||
this.eventCache = eventCache; | ||||||||||
this.getRawMachineId = getRawMachineId; | ||||||||||
this.getContainerEnv = getContainerEnv; | ||||||||||
} | ||||||||||
|
||||||||||
static create( | ||||||||||
session: Session, | ||||||||||
userConfig: UserConfig, | ||||||||||
{ | ||||||||||
commonProperties = { ...MACHINE_METADATA }, | ||||||||||
eventCache = EventCache.getInstance(), | ||||||||||
getRawMachineId = () => nodeMachineId.machineId(true), | ||||||||||
getContainerEnv = isContainerized, | ||||||||||
}: { | ||||||||||
eventCache?: EventCache; | ||||||||||
getRawMachineId?: () => Promise<string>; | ||||||||||
commonProperties?: CommonProperties; | ||||||||||
getContainerEnv?: () => Promise<boolean>; | ||||||||||
} = {} | ||||||||||
): Telemetry { | ||||||||||
const instance = new Telemetry(session, userConfig, commonProperties, { eventCache, getRawMachineId }); | ||||||||||
const instance = new Telemetry(session, userConfig, { | ||||||||||
eventCache, | ||||||||||
getRawMachineId, | ||||||||||
getContainerEnv, | ||||||||||
}); | ||||||||||
|
||||||||||
void instance.start(); | ||||||||||
instance.start(); | ||||||||||
return instance; | ||||||||||
} | ||||||||||
|
||||||||||
private async start(): Promise<void> { | ||||||||||
private start(): void { | ||||||||||
|
||||||||||
if (!this.isTelemetryEnabled()) { | ||||||||||
return; | ||||||||||
} | ||||||||||
|
||||||||||
this.deviceIdPromise = getDeviceId({ | ||||||||||
|
||||||||||
getMachineId: () => this.getRawMachineId(), | ||||||||||
onError: (reason, error) => { | ||||||||||
|
@@ -73,15 +109,11 @@ export class Telemetry { | |||||||||
}, | ||||||||||
abortSignal: this.deviceIdAbortController.signal, | ||||||||||
}); | ||||||||||
|
||||||||||
this.commonProperties.device_id = await this.deviceIdPromise; | ||||||||||
|
||||||||||
this.isBufferingEvents = false; | ||||||||||
this.containerEnvPromise = this.getContainerEnv(); | ||||||||||
|
this.containerEnvPromise = this.getContainerEnv(); | |
const [deviceId, isContainerEnv] = await Promise.all([this.deviceIdPromise, this.getContainerEnvPromise]); | |
this.commonProperties.device_id = deviceId; | |
this.commonProperties.is_container_env = isContainerEnv; |
something like this
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.
with this approach start would take up to 3s which I think it's acceptable, will change
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.
it will have identical behavior to awaiting inside the getCommonProperties, we do not await the start()
function anywhere so it's essentially same as declaring a combined promise, we instantly initialize the telemetry and start buffering event when emit is run.
In both cases we'd instantly be able to emit events, and in both cases if device ID takes 3 seconds, the eventual emission of events will take 3 seconds.
But with start
there's only 1 point where we deal with asynchronous logic, and in the other case we have a bunch of less predictable asyncronous functions listening in into 2 different promises.
Outdated
Copilot
AI
Jun 13, 2025
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 returning a boolean for is_container_env in getAsyncCommonProperties (e.g., true/false) instead of a string to more directly reflect its boolean nature, if this change is compatible with TelemetryBoolSet.
is_container_env: (await this.containerEnvPromise) ? "true" : "false", | |
is_container_env: await this.containerEnvPromise, |
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.
not compatible TelemetryBoolSet expects strings "true" or "false"
Outdated
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.
I don't think we should be awaiting here; an explicit unawaited + buffering with an async start like it was before is easier to reason about, it just needs another to await for container inside start
Otherwise there's potentially hundreds of floating functions awaiting device_id or whatever else. We can assume in worst case scenario this isn't instant (and could never even resolve)
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.
on the device ID there is a timeout, on the container env it is just fs/promises
so we can be sure they will resolve. These promises are private, I think the buferring logic only makes sense if we are tagging on finally or something, to me feels unneeded complication. Our current timeout is 3 seconds, no reason to be extra defensive.
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.
it's fair that we don't need to be extra defensive but either way we're still essentially going to end up buffering these events.
The difference is that with awaiting here the "buffering" would happen through a bunch of async functions in parallel in memory stack waiting on these promises as opposed to more explicit buffering where we store the functions we want to execute ourselves and execute them in order after resolving these promises in one place.
With awaiting in common properties approach we'd, for example, not have any guarantees that these functions end up resolving in the same order. Which we don't need to care about, but it's nice to have better idea of how async execution is going to happen and have 1 point of logic where we resolve all the async issues.
With discussions with @nirinchev, we also want to create a shared telemetry service component across devtools so we would not want to deviate too much unless there are good reasons for it (this setup is modeled after mongosh and Compass telemetry).
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.