import type { MongoLogEntry } from 'mongodb-log-writer'; import { MongoLogWriter, mongoLogId } from 'mongodb-log-writer'; import isElectronRenderer from 'is-electron-renderer'; import createDebug from 'debug'; import type { Writable } from 'stream'; import type { HadronIpcRenderer } from 'hadron-ipc'; let preferences: { getPreferences(): { trackUsageStatistics: boolean }; }; type TrackProps = Record | (() => Record); type TrackFunction = (event: string, properties?: TrackProps) => void; function emit( ipc: HadronIpcRenderer | null, event: string, data: Record ): void { // We use ipc.callQuiet instead of ipc.call because we already // print debugging messages below void ipc?.callQuiet?.(event, data); if (typeof process !== 'undefined' && typeof process.emit === 'function') { (process as any).emit(event, data); } } export function createLoggerAndTelemetry(component: string): { log: ReturnType; mongoLogId: typeof mongoLogId; debug: ReturnType; track: TrackFunction; } { // This application may not be running in an Node.js/Electron context. const ipc: HadronIpcRenderer | null = isElectronRenderer ? require('hadron-ipc') : null; // Do not create an actual Writable stream here, since the callback // logic in Node.js streams would mean that two writes from the // same event loop tick would not be written synchronously, // allowing another logger's write to be written out-of-order. const target = { write(line: string, callback: () => void) { emit(ipc, 'compass:log', { line }); callback(); }, end(callback: () => void) { callback(); }, } as Writable; const writer = new MongoLogWriter('', null, target); const track = (...args: [string, TrackProps?]) => { void Promise.resolve() .then(() => trackAsync(...args)) .catch((error) => debug('track failed', error)); }; const trackAsync = async ( event: string, properties: TrackProps = {} ): Promise => { // Avoid circular dependency between compass-logging and compass-preferences-model // Note that this is mainly a performance optimization, since the main process // telemetry code also checks this preference value, so it is safe to fall back to 'true'. try { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore-error Types from the dependency may not be available in early bootstrap. preferences ??= (await import('compass-preferences-model')) .preferencesAccess; } catch { preferences ??= { getPreferences() { return { trackUsageStatistics: true }; }, }; } const { trackUsageStatistics = true } = preferences?.getPreferences(); if (!trackUsageStatistics) { return; } const data = { event, properties, }; if (typeof properties === 'function') { try { data.properties = await properties(); } catch (error) { // When an error arises during the fetching of properties, // for instance if we can't fetch host information, // we track a new error indicating the failure. // This is so that we are aware of which events might be misreported. emit(ipc, 'compass:track', { event: 'Error Fetching Attributes', properties: { event_name: event, }, }); return; } } emit(ipc, 'compass:track', data); }; const debug = createDebug(`mongodb-compass:${component.toLowerCase()}`); writer.on('log', ({ s, ctx, msg, attr }: MongoLogEntry) => { if (attr) { debug(msg, { s, ctx, ...attr }); } else { debug(msg, { s, ctx }); } }); return { log: writer.bindComponent(component), mongoLogId, debug, track, }; } export default createLoggerAndTelemetry;