Skip to content

Commit bf204bb

Browse files
committed
fix: tests
1 parent 126994c commit bf204bb

File tree

6 files changed

+76
-50
lines changed

6 files changed

+76
-50
lines changed

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ try {
2020
version: packageInfo.version,
2121
});
2222

23-
const telemetry = Telemetry.create({ session, userConfig: config });
23+
const telemetry = Telemetry.create(session, config);
2424

2525
const server = new Server({
2626
mcpServer,

src/logger.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export const LogId = {
2525
telemetryMetadataError: mongoLogId(1_002_005),
2626
telemetryDeviceIdFailure: mongoLogId(1_002_006),
2727
telemetryDeviceIdTimeout: mongoLogId(1_002_007),
28+
telemetryContainerEnvFailure: mongoLogId(1_002_008),
2829

2930
toolExecute: mongoLogId(1_003_001),
3031
toolExecuteFailure: mongoLogId(1_003_002),

src/telemetry/telemetry.ts

Lines changed: 67 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { EventCache } from "./eventCache.js";
88
import nodeMachineId from "node-machine-id";
99
import { getDeviceId } from "@mongodb-js/device-id";
1010
import fs from "fs/promises";
11+
import { get } from "http";
1112

1213
type EventResult = {
1314
success: boolean;
@@ -22,30 +23,74 @@ export type TelemetryOptions = {
2223
commonProperties?: CommonProperties;
2324
eventCache?: EventCache;
2425
getRawMachineId?: () => Promise<string>;
26+
getContainerEnv?: () => Promise<boolean>;
2527
};
2628

29+
async function fileExists(filePath: string): Promise<boolean> {
30+
try {
31+
await fs.stat(filePath);
32+
return true; // File exists
33+
} catch (e: unknown) {
34+
if (
35+
e instanceof Error &&
36+
(
37+
e as Error & {
38+
code: string;
39+
}
40+
).code === "ENOENT"
41+
) {
42+
return false; // File does not exist
43+
}
44+
throw e; // Re-throw unexpected errors
45+
}
46+
}
47+
48+
async function isContainerized(): Promise<boolean> {
49+
for (const file of ["/.dockerenv", "/run/.containerenv", "/var/run/.containerenv"]) {
50+
const exists = await fileExists(file);
51+
if (exists) {
52+
return true;
53+
}
54+
}
55+
return !!process.env.container;
56+
}
57+
2758
export class Telemetry {
2859
private isBufferingEvents: boolean = true;
2960
/** Resolves when the device ID is retrieved or timeout occurs */
3061
public deviceIdPromise: Promise<string> | undefined;
3162
private deviceIdAbortController = new AbortController();
63+
private eventCache: EventCache;
64+
private getRawMachineId: () => Promise<string>;
65+
private getContainerEnv: () => Promise<boolean>;
3266

3367
private constructor(
3468
private readonly session: Session,
3569
private readonly userConfig: UserConfig,
3670
private readonly commonProperties: CommonProperties,
37-
private readonly eventCache: EventCache,
38-
private readonly getRawMachineId: () => Promise<string>
39-
) {}
40-
41-
static create({ session, userConfig, commonProperties, eventCache, getRawMachineId }: TelemetryOptions): Telemetry {
42-
const instance = new Telemetry(
43-
session,
44-
userConfig,
45-
commonProperties || { ...MACHINE_METADATA },
46-
eventCache || EventCache.getInstance(),
47-
getRawMachineId || (() => nodeMachineId.machineId(true))
48-
);
71+
{ eventCache, getRawMachineId, getContainerEnv }: { eventCache: EventCache; getRawMachineId: () => Promise<string>; getContainerEnv: () => Promise<boolean> }
72+
) {
73+
this.eventCache = eventCache;
74+
this.getRawMachineId = getRawMachineId;
75+
this.getContainerEnv = getContainerEnv;
76+
}
77+
78+
static create(
79+
session: Session,
80+
userConfig: UserConfig,
81+
{
82+
commonProperties = { ...MACHINE_METADATA },
83+
eventCache = EventCache.getInstance(),
84+
getRawMachineId = () => nodeMachineId.machineId(true),
85+
getContainerEnv = isContainerized,
86+
}: {
87+
commonProperties?: CommonProperties;
88+
eventCache?: EventCache;
89+
getRawMachineId?: () => Promise<string>;
90+
getContainerEnv?: () => Promise<boolean>;
91+
} = {}
92+
): Telemetry {
93+
const instance = new Telemetry(session, userConfig, commonProperties, { eventCache, getRawMachineId, getContainerEnv });
4994

5095
void instance.start();
5196
return instance;
@@ -73,9 +118,17 @@ export class Telemetry {
73118
abortSignal: this.deviceIdAbortController.signal,
74119
});
75120

121+
// try {
122+
// this.commonProperties.is_container_env = (await this.getContainerEnv()) ? "true" : "false";
123+
// } catch (error: unknown) {
124+
// logger.info(
125+
// LogId.telemetryContainerEnvFailure,
126+
// "telemetry",
127+
// `Failed trying to check if is in container environment ${error as string}`
128+
// );
129+
// }
76130
this.commonProperties.device_id = await this.deviceIdPromise;
77-
this.commonProperties.is_container_env = (await this.isContainerized()) ? "true" : "false";
78-
131+
79132
this.isBufferingEvents = false;
80133
}
81134

@@ -117,35 +170,6 @@ export class Telemetry {
117170
};
118171
}
119172

120-
private async fileExists(filePath: string): Promise<boolean> {
121-
try {
122-
await fs.stat(filePath);
123-
return true; // File exists
124-
} catch (e: unknown) {
125-
if (
126-
e instanceof Error &&
127-
(
128-
e as Error & {
129-
code: string;
130-
}
131-
).code === "ENOENT"
132-
) {
133-
return false; // File does not exist
134-
}
135-
throw e; // Re-throw unexpected errors
136-
}
137-
}
138-
139-
private async isContainerized(): Promise<boolean> {
140-
for (const file of ["/.dockerenv", "/run/.containerenv", "/var/run/.containerenv"]) {
141-
const fileExists = await this.fileExists(file);
142-
if (fileExists) {
143-
return true;
144-
}
145-
}
146-
return !!process.env.container;
147-
}
148-
149173
/**
150174
* Checks if telemetry is currently enabled
151175
* This is a method rather than a constant to capture runtime config changes

tests/integration/helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export function setupIntegrationTest(getUserConfig: () => UserConfig): Integrati
6767

6868
userConfig.telemetry = "disabled";
6969

70-
const telemetry = Telemetry.create({ session, userConfig });
70+
const telemetry = Telemetry.create(session, userConfig);
7171

7272
mcpServer = new Server({
7373
session,

tests/integration/telemetry.test.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@ describe("Telemetry", () => {
1010

1111
const actualHashedId = createHmac("sha256", actualId.toUpperCase()).update("atlascli").digest("hex");
1212

13-
const telemetry = Telemetry.create({
14-
session: new Session({
15-
apiBaseUrl: "",
16-
}),
17-
userConfig: config,
13+
const telemetry = Telemetry.create(new Session({ apiBaseUrl: "" }), config, {
14+
getContainerEnv: () => Promise.resolve(false),
1815
});
1916

2017
expect(telemetry.getCommonProperties().device_id).toBe(undefined);

tests/unit/telemetry.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ describe("Telemetry", () => {
130130
userConfig: config,
131131
eventCache: mockEventCache,
132132
getRawMachineId: () => Promise.resolve(machineId),
133+
getContainerEnv: () => Promise.resolve(false),
133134
});
134135

135136
config.telemetry = "enabled";
@@ -216,6 +217,7 @@ describe("Telemetry", () => {
216217
session,
217218
userConfig: config,
218219
getRawMachineId: () => Promise.resolve(machineId),
220+
getContainerEnv: () => Promise.resolve(false),
219221
});
220222

221223
expect(telemetry["isBufferingEvents"]).toBe(true);
@@ -234,6 +236,7 @@ describe("Telemetry", () => {
234236
session,
235237
userConfig: config,
236238
getRawMachineId: () => Promise.reject(new Error("Failed to get device ID")),
239+
getContainerEnv: () => Promise.resolve(false),
237240
});
238241

239242
expect(telemetry["isBufferingEvents"]).toBe(true);
@@ -258,6 +261,7 @@ describe("Telemetry", () => {
258261
session,
259262
userConfig: config,
260263
getRawMachineId: () => new Promise(() => {}),
264+
getContainerEnv: () => Promise.resolve(false),
261265
});
262266

263267
expect(telemetry["isBufferingEvents"]).toBe(true);

0 commit comments

Comments
 (0)