@@ -6,8 +6,7 @@ import { ApiClient } from "../common/atlas/apiClient.js";
6
6
import { MACHINE_METADATA } from "./constants.js" ;
7
7
import { EventCache } from "./eventCache.js" ;
8
8
import 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" ;
11
10
12
11
type EventResult = {
13
12
success : boolean ;
@@ -19,7 +18,8 @@ export const DEVICE_ID_TIMEOUT = 3000;
19
18
export class Telemetry {
20
19
private isBufferingEvents : boolean = true ;
21
20
/** 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 ;
23
23
private eventCache : EventCache ;
24
24
private getRawMachineId : ( ) => Promise < string > ;
25
25
@@ -58,37 +58,24 @@ export class Telemetry {
58
58
if ( ! this . isTelemetryEnabled ( ) ) {
59
59
return ;
60
60
}
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 ) ) ,
67
65
} ) ;
68
- this . commonProperties . device_id = await this . deviceIdPromise ;
66
+
67
+ this . resolveDeviceId = resolveDeviceId ;
68
+ this . commonProperties . device_id = await deviceId ;
69
69
70
70
this . isBufferingEvents = false ;
71
71
}
72
72
73
73
public async close ( ) : Promise < void > {
74
- this . deviceIdPromise ?. resolve ( "unknown" ) ;
74
+ this . resolveDeviceId ?. ( "unknown" ) ;
75
75
this . isBufferingEvents = false ;
76
76
await this . emitEvents ( this . eventCache . getEvents ( ) ) ;
77
77
}
78
78
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
-
92
79
/**
93
80
* Emits events through the telemetry pipeline
94
81
* @param events - The events to emit
0 commit comments