@@ -5,9 +5,8 @@ import logger, { LogId } from "../logger.js";
5
5
import { ApiClient } from "../common/atlas/apiClient.js" ;
6
6
import { MACHINE_METADATA } from "./constants.js" ;
7
7
import { EventCache } from "./eventCache.js" ;
8
- import { createHmac } from "crypto" ;
9
8
import nodeMachineId from "node-machine-id" ;
10
- import { DeferredPromise } from "../helpers/deferred-promise.js " ;
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
+ private deviceIdAbortController = new AbortController ( ) ;
23
23
private eventCache : EventCache ;
24
24
private getRawMachineId : ( ) => Promise < string > ;
25
25
@@ -39,7 +39,6 @@ export class Telemetry {
39
39
{
40
40
commonProperties = { ...MACHINE_METADATA } ,
41
41
eventCache = EventCache . getInstance ( ) ,
42
-
43
42
getRawMachineId = ( ) => nodeMachineId . machineId ( true ) ,
44
43
} : {
45
44
eventCache ?: EventCache ;
@@ -57,50 +56,35 @@ export class Telemetry {
57
56
if ( ! this . isTelemetryEnabled ( ) ) {
58
57
return ;
59
58
}
60
- this . deviceIdPromise = DeferredPromise . fromPromise ( this . getDeviceId ( ) , {
61
- timeout : DEVICE_ID_TIMEOUT ,
62
- onTimeout : ( resolve ) => {
63
- resolve ( "unknown" ) ;
64
- logger . debug ( LogId . telemetryDeviceIdTimeout , "telemetry" , "Device ID retrieval timed out" ) ;
59
+ this . deviceIdPromise = getDeviceId ( {
60
+ getMachineId : ( ) => this . getRawMachineId ( ) ,
61
+ onError : ( reason , error ) => {
62
+ switch ( reason ) {
63
+ case "resolutionError" :
64
+ logger . debug ( LogId . telemetryDeviceIdFailure , "telemetry" , String ( error ) ) ;
65
+ break ;
66
+ case "timeout" :
67
+ logger . debug ( LogId . telemetryDeviceIdTimeout , "telemetry" , "Device ID retrieval timed out" ) ;
68
+ break ;
69
+ case "abort" :
70
+ // No need to log in the case of aborts
71
+ break ;
72
+ }
65
73
} ,
74
+ abortSignal : this . deviceIdAbortController . signal ,
66
75
} ) ;
76
+
67
77
this . commonProperties . device_id = await this . deviceIdPromise ;
68
78
69
79
this . isBufferingEvents = false ;
70
80
}
71
81
72
82
public async close ( ) : Promise < void > {
73
- this . deviceIdPromise ?. resolve ( "unknown" ) ;
83
+ this . deviceIdAbortController . abort ( ) ;
74
84
this . isBufferingEvents = false ;
75
85
await this . emitEvents ( this . eventCache . getEvents ( ) ) ;
76
86
}
77
87
78
- /**
79
- * @returns A hashed, unique identifier for the running device or `"unknown"` if not known.
80
- */
81
- private async getDeviceId ( ) : Promise < string > {
82
- try {
83
- if ( this . commonProperties . device_id ) {
84
- return this . commonProperties . device_id ;
85
- }
86
-
87
- const originalId : string = await this . getRawMachineId ( ) ;
88
-
89
- // Create a hashed format from the all uppercase version of the machine ID
90
- // to match it exactly with the denisbrodbeck/machineid library that Atlas CLI uses.
91
- const hmac = createHmac ( "sha256" , originalId . toUpperCase ( ) ) ;
92
-
93
- /** This matches the message used to create the hashes in Atlas CLI */
94
- const DEVICE_ID_HASH_MESSAGE = "atlascli" ;
95
-
96
- hmac . update ( DEVICE_ID_HASH_MESSAGE ) ;
97
- return hmac . digest ( "hex" ) ;
98
- } catch ( error ) {
99
- logger . debug ( LogId . telemetryDeviceIdFailure , "telemetry" , String ( error ) ) ;
100
- return "unknown" ;
101
- }
102
- }
103
-
104
88
/**
105
89
* Emits events through the telemetry pipeline
106
90
* @param events - The events to emit
0 commit comments