@@ -6,8 +6,7 @@ import { ApiClient } from "../common/atlas/apiClient.js";
66import { MACHINE_METADATA } from "./constants.js" ;
77import { EventCache } from "./eventCache.js" ;
88import nodeMachineId from "node-machine-id" ;
9- import { DeferredPromise } from "../deferred-promise.js" ;
10- import { getDeviceId as extractDeviceId } from "@mongodb-js/device-id" ;
9+ import { getDeviceId } from "@mongodb-js/device-id" ;
1110
1211type EventResult = {
1312 success : boolean ;
@@ -19,7 +18,8 @@ export const DEVICE_ID_TIMEOUT = 3000;
1918export class Telemetry {
2019 private isBufferingEvents : boolean = true ;
2120 /** Resolves when the device ID is retrieved or timeout occurs */
22- public deviceIdPromise : DeferredPromise < string > | undefined ;
21+ public deviceIdPromise : Promise < string > | undefined ;
22+ public resolveDeviceId : ( ( value : string ) => void ) | undefined ;
2323 private eventCache : EventCache ;
2424 private getRawMachineId : ( ) => Promise < string > ;
2525
@@ -58,37 +58,24 @@ export class Telemetry {
5858 if ( ! this . isTelemetryEnabled ( ) ) {
5959 return ;
6060 }
61- this . deviceIdPromise = DeferredPromise . fromPromise ( this . getDeviceId ( ) , {
62- timeout : DEVICE_ID_TIMEOUT ,
63- onTimeout : ( resolve ) => {
64- resolve ( "unknown" ) ;
65- logger . debug ( LogId . telemetryDeviceIdTimeout , "telemetry" , "Device ID retrieval timed out" ) ;
66- } ,
61+ const { value : deviceId , resolve : resolveDeviceId } = getDeviceId ( {
62+ getMachineId : ( ) => this . getRawMachineId ( ) ,
63+ isNodeMachineId : true ,
64+ onError : ( error ) => logger . debug ( LogId . telemetryDeviceIdFailure , "telemetry" , String ( error ) ) ,
6765 } ) ;
68- this . commonProperties . device_id = await this . deviceIdPromise ;
66+
67+ this . resolveDeviceId = resolveDeviceId ;
68+ this . commonProperties . device_id = await deviceId ;
6969
7070 this . isBufferingEvents = false ;
7171 }
7272
7373 public async close ( ) : Promise < void > {
74- this . deviceIdPromise ?. resolve ( "unknown" ) ;
74+ this . resolveDeviceId ?. ( "unknown" ) ;
7575 this . isBufferingEvents = false ;
7676 await this . emitEvents ( this . eventCache . getEvents ( ) ) ;
7777 }
7878
79- /**
80- * @returns A hashed, unique identifier for the running device or `"unknown"` if not known.
81- */
82- private async getDeviceId ( ) : Promise < string > {
83- if ( this . commonProperties . device_id ) {
84- return this . commonProperties . device_id ;
85- }
86- return extractDeviceId ( {
87- getMachineId : ( ) => this . getRawMachineId ( ) ,
88- isNodeMachineId : true ,
89- } ) . value ;
90- }
91-
9279 /**
9380 * Emits events through the telemetry pipeline
9481 * @param events - The events to emit
0 commit comments