@@ -7,6 +7,7 @@ import { MACHINE_METADATA } from "./constants.js";
77import { EventCache } from "./eventCache.js" ;
88import nodeMachineId from "node-machine-id" ;
99import { getDeviceId } from "@mongodb-js/device-id" ;
10+ import fs from "fs/promises" ;
1011
1112type EventResult = {
1213 success : boolean ;
@@ -18,7 +19,7 @@ export const DEVICE_ID_TIMEOUT = 3000;
1819export class Telemetry {
1920 private isBufferingEvents : boolean = true ;
2021 /** Resolves when the device ID is retrieved or timeout occurs */
21- public deviceIdPromise : Promise < string > | undefined ;
22+ public dataPromise : Promise < [ string , boolean ] > | undefined ;
2223 private deviceIdAbortController = new AbortController ( ) ;
2324 private eventCache : EventCache ;
2425 private getRawMachineId : ( ) => Promise < string > ;
@@ -52,11 +53,32 @@ export class Telemetry {
5253 return instance ;
5354 }
5455
56+ private async isContainerEnv ( ) : Promise < boolean > {
57+ if ( process . platform !== "linux" ) {
58+ return false ; // we only support linux containers for now
59+ }
60+
61+ if ( process . env . container ) {
62+ return true ;
63+ }
64+
65+ const exists = await Promise . all ( [ "/.dockerenv" , "/run/.containerenv" , "/var/run/.containerenv" ] . map ( async ( file ) => {
66+ try {
67+ await fs . access ( file ) ;
68+ return true ;
69+ } catch {
70+ return false ;
71+ }
72+ } ) ) ;
73+
74+ return exists . includes ( true ) ;
75+ }
76+
5577 private async start ( ) : Promise < void > {
5678 if ( ! this . isTelemetryEnabled ( ) ) {
5779 return ;
5880 }
59- this . deviceIdPromise = getDeviceId ( {
81+ this . dataPromise = Promise . all ( [ getDeviceId ( {
6082 getMachineId : ( ) => this . getRawMachineId ( ) ,
6183 onError : ( reason , error ) => {
6284 switch ( reason ) {
@@ -72,9 +94,12 @@ export class Telemetry {
7294 }
7395 } ,
7496 abortSignal : this . deviceIdAbortController . signal ,
75- } ) ;
97+ } ) , this . isContainerEnv ( ) ] ) ;
98+
99+ const [ deviceId , containerEnv ] = await this . dataPromise ;
76100
77- this . commonProperties . device_id = await this . deviceIdPromise ;
101+ this . commonProperties . device_id = deviceId ;
102+ this . commonProperties . is_container_env = containerEnv ;
78103
79104 this . isBufferingEvents = false ;
80105 }
0 commit comments