Skip to content

Commit 0b60fe2

Browse files
committed
fix: address comments
1 parent 931816f commit 0b60fe2

File tree

4 files changed

+20
-19
lines changed

4 files changed

+20
-19
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, config);
23+
const telemetry = await Telemetry.create(session, config);
2424

2525
const server = new Server({
2626
mcpServer,

src/telemetry/telemetry.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ async function isContainerized(): Promise<boolean> {
3939
}
4040

4141
export class Telemetry {
42-
private deviceIdPromise: Promise<string> | undefined;
43-
private containerEnvPromise: Promise<boolean> | undefined;
42+
private deviceId: string | undefined;
43+
private containerEnv: boolean | undefined;
4444
private deviceIdAbortController = new AbortController();
4545
private eventCache: EventCache;
4646
private getRawMachineId: () => Promise<string>;
@@ -64,7 +64,7 @@ export class Telemetry {
6464
this.getContainerEnv = getContainerEnv;
6565
}
6666

67-
static create(
67+
static async create(
6868
session: Session,
6969
userConfig: UserConfig,
7070
{
@@ -76,23 +76,23 @@ export class Telemetry {
7676
getRawMachineId?: () => Promise<string>;
7777
getContainerEnv?: () => Promise<boolean>;
7878
} = {}
79-
): Telemetry {
79+
): Promise<Telemetry> {
8080
const instance = new Telemetry(session, userConfig, {
8181
eventCache,
8282
getRawMachineId,
8383
getContainerEnv,
8484
});
8585

86-
instance.start();
86+
await instance.start();
8787
return instance;
8888
}
8989

90-
private start(): void {
90+
private async start(): Promise<void> {
9191
if (!this.isTelemetryEnabled()) {
9292
return;
9393
}
9494

95-
this.deviceIdPromise = getDeviceId({
95+
const deviceIdPromise = getDeviceId({
9696
getMachineId: () => this.getRawMachineId(),
9797
onError: (reason, error) => {
9898
switch (reason) {
@@ -109,7 +109,9 @@ export class Telemetry {
109109
},
110110
abortSignal: this.deviceIdAbortController.signal,
111111
});
112-
this.containerEnvPromise = this.getContainerEnv();
112+
const containerEnvPromise = this.getContainerEnv();
113+
114+
[this.deviceId, this.containerEnv] = await Promise.all([deviceIdPromise, containerEnvPromise]);
113115
}
114116

115117
public async close(): Promise<void> {
@@ -138,16 +140,16 @@ export class Telemetry {
138140
* Gets the common properties for events
139141
* @returns Object containing common properties for all events
140142
*/
141-
private async getCommonProperties(): Promise<CommonProperties> {
143+
private getCommonProperties(): CommonProperties {
142144
return {
143145
...MACHINE_METADATA,
144146
mcp_client_version: this.session.agentRunner?.version,
145147
mcp_client_name: this.session.agentRunner?.name,
146148
session_id: this.session.sessionId,
147149
config_atlas_auth: this.session.apiClient.hasCredentials() ? "true" : "false",
148150
config_connection_string: this.userConfig.connectionString ? "true" : "false",
149-
is_container_env: (await this.containerEnvPromise) ? "true" : "false",
150-
device_id: await this.deviceIdPromise,
151+
is_container_env: this.containerEnv ? "true" : "false",
152+
device_id: this.deviceId,
151153
};
152154
}
153155

@@ -204,11 +206,10 @@ export class Telemetry {
204206
* Attempts to send events through the provided API client
205207
*/
206208
private async sendEvents(client: ApiClient, events: BaseEvent[]): Promise<void> {
207-
const commonProperties = await this.getCommonProperties();
208209
await client.sendEvents(
209210
events.map((event) => ({
210211
...event,
211-
properties: { ...commonProperties, ...event.properties },
212+
properties: { ...this.getCommonProperties(), ...event.properties },
212213
}))
213214
);
214215
}

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 = await Telemetry.create(session, userConfig);
7171

7272
mcpServer = new Server({
7373
session,

tests/unit/telemetry.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ describe("Telemetry", () => {
9999
}
100100
}
101101

102-
beforeEach(() => {
102+
beforeEach(async () => {
103103
// Reset mocks before each test
104104
jest.clearAllMocks();
105105

@@ -137,7 +137,7 @@ describe("Telemetry", () => {
137137
getContainerEnv: () => Promise.resolve(false),
138138
};
139139

140-
telemetry = Telemetry.create(session, config, telemetryConfig);
140+
telemetry = await Telemetry.create(session, config, telemetryConfig);
141141

142142
config.telemetry = "enabled";
143143
});
@@ -247,7 +247,7 @@ describe("Telemetry", () => {
247247
it("should handle machine ID resolution failure", async () => {
248248
const loggerSpy = jest.spyOn(logger, "debug");
249249

250-
telemetry = Telemetry.create(session, config, {
250+
telemetry = await Telemetry.create(session, config, {
251251
...telemetryConfig,
252252
getRawMachineId: () => Promise.reject(new Error("Failed to get device ID")),
253253
});
@@ -266,7 +266,7 @@ describe("Telemetry", () => {
266266
it("should timeout if machine ID resolution takes too long", async () => {
267267
const loggerSpy = jest.spyOn(logger, "debug");
268268

269-
telemetry = Telemetry.create(session, config, {
269+
telemetry = await Telemetry.create(session, config, {
270270
...telemetryConfig,
271271
getRawMachineId: () => new Promise(() => {}), // Never resolves
272272
});

0 commit comments

Comments
 (0)