-
Notifications
You must be signed in to change notification settings - Fork 93
chore: defer machine ID resolution #161
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 17 commits
80e71af
9022a61
2dfb0a1
ab2909e
79c8d16
cbb905c
9be3e12
5348a3a
c869d63
e0339a4
80bc86e
189e97d
8575d9a
9d49948
5d981ee
89c3568
bc57a97
8816bef
c611836
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
type DeferredPromiseOptions<T> = { | ||
timeout?: number; | ||
onTimeout?: (resolve: (value: T) => void, reject: (reason: Error) => void) => void; | ||
}; | ||
|
||
/** Creates a promise and exposes its resolve and reject methods, with an optional timeout. */ | ||
export class DeferredPromise<T> extends Promise<T> { | ||
resolve!: (value: T) => void; | ||
reject!: (reason: unknown) => void; | ||
private timeoutId?: NodeJS.Timeout; | ||
|
||
constructor( | ||
executor: (resolve: (value: T) => void, reject: (reason: Error) => void) => void, | ||
{ timeout, onTimeout }: DeferredPromiseOptions<T> = {} | ||
) { | ||
let resolveFn: (value: T) => void; | ||
let rejectFn: (reason?: unknown) => void; | ||
|
||
super((resolve, reject) => { | ||
resolveFn = resolve; | ||
rejectFn = reject; | ||
}); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
this.resolve = resolveFn!; | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
this.reject = rejectFn!; | ||
|
||
if (timeout !== undefined && onTimeout) { | ||
this.timeoutId = setTimeout(() => { | ||
onTimeout(this.resolve, this.reject); | ||
}, timeout); | ||
} | ||
|
||
executor( | ||
(value: T) => { | ||
if (this.timeoutId) clearTimeout(this.timeoutId); | ||
this.resolve(value); | ||
}, | ||
(reason: Error) => { | ||
if (this.timeoutId) clearTimeout(this.timeoutId); | ||
this.reject(reason); | ||
} | ||
); | ||
} | ||
|
||
static fromPromise<T>(promise: Promise<T>, options: DeferredPromiseOptions<T> = {}): DeferredPromise<T> { | ||
return new DeferredPromise<T>((resolve, reject) => { | ||
promise | ||
.then((value) => { | ||
resolve(value); | ||
}) | ||
.catch((reason) => { | ||
reject(reason as Error); | ||
}); | ||
}, options); | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,23 +5,101 @@ import logger, { LogId } from "../logger.js"; | |
import { ApiClient } from "../common/atlas/apiClient.js"; | ||
import { MACHINE_METADATA } from "./constants.js"; | ||
import { EventCache } from "./eventCache.js"; | ||
import { createHmac } from "crypto"; | ||
import nodeMachineId from "node-machine-id"; | ||
import { DeferredPromise } from "../deferred-promise.js"; | ||
|
||
type EventResult = { | ||
success: boolean; | ||
error?: Error; | ||
}; | ||
|
||
export const DEVICE_ID_TIMEOUT = 3000; | ||
|
||
export class Telemetry { | ||
private readonly commonProperties: CommonProperties; | ||
private isBufferingEvents: boolean = true; | ||
/** Resolves when the device ID is retrieved or timeout occurs */ | ||
public deviceIdPromise: DeferredPromise<string> | undefined; | ||
private eventCache: EventCache; | ||
private getRawMachineId: () => Promise<string>; | ||
|
||
constructor( | ||
private constructor( | ||
private readonly session: Session, | ||
private readonly userConfig: UserConfig, | ||
private readonly eventCache: EventCache = EventCache.getInstance() | ||
private readonly commonProperties: CommonProperties, | ||
{ eventCache, getRawMachineId }: { eventCache: EventCache; getRawMachineId: () => Promise<string> } | ||
) { | ||
this.commonProperties = { | ||
...MACHINE_METADATA, | ||
}; | ||
this.eventCache = eventCache; | ||
this.getRawMachineId = getRawMachineId; | ||
} | ||
|
||
static create( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. while this is not asyncronous. I'm using |
||
session: Session, | ||
userConfig: UserConfig, | ||
{ | ||
commonProperties = { ...MACHINE_METADATA }, | ||
eventCache = EventCache.getInstance(), | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access | ||
getRawMachineId = () => nodeMachineId.machineId(true), | ||
}: { | ||
eventCache?: EventCache; | ||
getRawMachineId?: () => Promise<string>; | ||
commonProperties?: CommonProperties; | ||
} = {} | ||
): Telemetry { | ||
const instance = new Telemetry(session, userConfig, commonProperties, { eventCache, getRawMachineId }); | ||
|
||
void instance.start(); | ||
return instance; | ||
} | ||
|
||
private async start(): Promise<void> { | ||
if (!this.isTelemetryEnabled()) { | ||
return; | ||
} | ||
this.deviceIdPromise = DeferredPromise.fromPromise(this.getDeviceId(), { | ||
timeout: DEVICE_ID_TIMEOUT, | ||
onTimeout: (resolve) => { | ||
resolve("unknown"); | ||
logger.debug(LogId.telemetryDeviceIdTimeout, "telemetry", "Device ID retrieval timed out"); | ||
}, | ||
}); | ||
this.commonProperties.device_id = await this.deviceIdPromise; | ||
|
||
this.isBufferingEvents = false; | ||
} | ||
|
||
public async close(): Promise<void> { | ||
this.deviceIdPromise?.resolve("unknown"); | ||
this.isBufferingEvents = false; | ||
await this.emitEvents(this.eventCache.getEvents()); | ||
} | ||
|
||
/** | ||
* @returns A hashed, unique identifier for the running device or `"unknown"` if not known. | ||
*/ | ||
private async getDeviceId(): Promise<string> { | ||
try { | ||
if (this.commonProperties.device_id) { | ||
return this.commonProperties.device_id; | ||
} | ||
|
||
const originalId: string = await this.getRawMachineId(); | ||
|
||
// Create a hashed format from the all uppercase version of the machine ID | ||
// to match it exactly with the denisbrodbeck/machineid library that Atlas CLI uses. | ||
const hmac = createHmac("sha256", originalId.toUpperCase()); | ||
|
||
/** This matches the message used to create the hashes in Atlas CLI */ | ||
const DEVICE_ID_HASH_MESSAGE = "atlascli"; | ||
|
||
hmac.update(DEVICE_ID_HASH_MESSAGE); | ||
return hmac.digest("hex"); | ||
} catch (error) { | ||
logger.debug(LogId.telemetryDeviceIdFailure, "telemetry", String(error)); | ||
return "unknown"; | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -78,6 +156,11 @@ export class Telemetry { | |
* Falls back to caching if both attempts fail | ||
*/ | ||
private async emit(events: BaseEvent[]): Promise<void> { | ||
if (this.isBufferingEvents) { | ||
this.eventCache.appendEvents(events); | ||
return; | ||
} | ||
|
||
const cachedEvents = this.eventCache.getEvents(); | ||
const allEvents = [...cachedEvents, ...events]; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { createHmac } from "crypto"; | ||
import { Telemetry } from "../../src/telemetry/telemetry.js"; | ||
import { Session } from "../../src/session.js"; | ||
import { config } from "../../src/config.js"; | ||
import nodeMachineId from "node-machine-id"; | ||
|
||
describe("Telemetry", () => { | ||
it("should resolve the actual machine ID", async () => { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access | ||
const actualId: string = await nodeMachineId.machineId(true); | ||
|
||
const actualHashedId = createHmac("sha256", actualId.toUpperCase()).update("atlascli").digest("hex"); | ||
|
||
const telemetry = Telemetry.create( | ||
new Session({ | ||
apiBaseUrl: "", | ||
}), | ||
config | ||
); | ||
|
||
expect(telemetry.getCommonProperties().device_id).toBe(undefined); | ||
expect(telemetry["isBufferingEvents"]).toBe(true); | ||
|
||
await telemetry.deviceIdPromise; | ||
|
||
expect(telemetry.getCommonProperties().device_id).toBe(actualHashedId); | ||
expect(telemetry["isBufferingEvents"]).toBe(false); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,8 +76,6 @@ export function setupMongoDBIntegrationTest(): MongoDBIntegrationTest { | |
let dbsDir = path.join(tmpDir, "mongodb-runner", "dbs"); | ||
for (let i = 0; i < 10; i++) { | ||
try { | ||
// TODO: Fix this type once mongodb-runner is updated. | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. drive-by |
||
mongoCluster = await MongoCluster.start({ | ||
tmpDir: dbsDir, | ||
logDir: path.join(tmpDir, "mongodb-runner", "logs"), | ||
|
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.
Do we need the assertions here?
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.
yeah, this is more of a late intialization assertion. typescript isn't smart enough to understand they're being set in the constructor
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 sure why though - when testing locally, I don't see it complaining when I remove the assertions - in what situations are you seeing errors?
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.
okay I'm realizing now the assertions were leftover from an older implementation that had a different structure; removed them now. as we assert them later on instead.