Skip to content

Commit 994b698

Browse files
committed
address comment: move tool emission to tool.ts
1 parent 8301e43 commit 994b698

File tree

3 files changed

+57
-38
lines changed

3 files changed

+57
-38
lines changed

src/telemetry/telemetry.ts

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -52,44 +52,27 @@ export class Telemetry {
5252
}
5353

5454
/**
55-
* Emits a tool event with timing and error information
56-
* @param command - The command being executed
57-
* @param category - Category of the command
58-
* @param startTime - Start time in milliseconds
59-
* @param result - Whether the command succeeded or failed
60-
* @param error - Optional error if the command failed
55+
* Emits events through the telemetry pipeline
56+
* @param events - The events to emit
6157
*/
62-
public async emitToolEvent(
63-
command: string,
64-
category: string,
65-
startTime: number,
66-
result: "success" | "failure",
67-
error?: Error
68-
): Promise<void> {
58+
public async emitEvents(events: BaseEvent[]): Promise<void> {
6959
if (!TELEMETRY_ENABLED) {
70-
logger.debug(mongoLogId(1_000_000), "telemetry", "Telemetry is disabled, skipping event.");
60+
logger.debug(mongoLogId(1_000_000), "telemetry", "Telemetry is disabled, skipping events.");
7161
return;
7262
}
7363

74-
const duration = Date.now() - startTime;
75-
const event: ToolEvent = {
76-
timestamp: new Date().toISOString(),
77-
source: "mdbmcp",
78-
properties: {
79-
...this.commonProperties,
80-
command,
81-
category,
82-
duration_ms: duration,
83-
session_id: this.session.sessionId,
84-
result,
85-
...(error && {
86-
error_type: error.name,
87-
error_code: error.message,
88-
}),
89-
},
90-
};
64+
await this.emit(events);
65+
}
9166

92-
await this.emit([event]);
67+
/**
68+
* Gets the common properties for events
69+
* @returns Object containing common properties for all events
70+
*/
71+
public getCommonProperties(): CommonProperties {
72+
return {
73+
...this.commonProperties,
74+
session_id: this.session.sessionId,
75+
};
9376
}
9477

9578
/**

src/telemetry/types.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
/**
2+
* Result type constants for telemetry events
3+
*/
4+
export const TELEMETRY_RESULT = {
5+
SUCCESS: "success" as const,
6+
FAILURE: "failure" as const,
7+
};
8+
9+
export type TelemetryResult = (typeof TELEMETRY_RESULT)[keyof typeof TELEMETRY_RESULT];
10+
111
/**
212
* Base interface for all events
313
*/
@@ -30,7 +40,7 @@ export interface ToolEvent extends BaseEvent {
3040
command: string;
3141
category: string;
3242
duration_ms: number;
33-
result: "success" | "failure";
43+
result: TelemetryResult;
3444
error_code?: string;
3545
error_type?: string;
3646
project_id?: string;

src/tools/tool.ts

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import logger from "../logger.js";
66
import { mongoLogId } from "mongodb-log-writer";
77
import config from "../config.js";
88
import { Telemetry } from "../telemetry/telemetry.js";
9+
import { type ToolEvent, TELEMETRY_RESULT, type TelemetryResult } from "../telemetry/types.js";
910

1011
export type ToolArgs<Args extends ZodRawShape> = z.objectOutputType<Args, ZodNever>;
1112

@@ -31,6 +32,33 @@ export abstract class ToolBase {
3132
this.telemetry = new Telemetry(session);
3233
}
3334

35+
/**
36+
* Creates and emits a tool telemetry event
37+
* @param startTime - Start time in milliseconds
38+
* @param result - Whether the command succeeded or failed
39+
* @param error - Optional error if the command failed
40+
*/
41+
private async emitToolEvent(startTime: number, result: TelemetryResult, error?: Error): Promise<void> {
42+
const duration = Date.now() - startTime;
43+
const event: ToolEvent = {
44+
timestamp: new Date().toISOString(),
45+
source: "mdbmcp",
46+
properties: {
47+
...this.telemetry.getCommonProperties(),
48+
command: this.name,
49+
category: this.category,
50+
duration_ms: duration,
51+
result,
52+
...(error && {
53+
error_type: error.name,
54+
error_code: error.message,
55+
}),
56+
},
57+
};
58+
59+
await this.telemetry.emitEvents([event]);
60+
}
61+
3462
public register(server: McpServer): void {
3563
if (!this.verifyAllowed()) {
3664
return;
@@ -46,16 +74,14 @@ export abstract class ToolBase {
4674
);
4775

4876
const result = await this.execute(...args);
49-
await this.telemetry.emitToolEvent(this.name, this.category, startTime, "success");
77+
await this.emitToolEvent(startTime, TELEMETRY_RESULT.SUCCESS);
5078
return result;
5179
} catch (error: unknown) {
5280
logger.error(mongoLogId(1_000_000), "tool", `Error executing ${this.name}: ${error as string}`);
5381

54-
await this.telemetry.emitToolEvent(
55-
this.name,
56-
this.category,
82+
await this.emitToolEvent(
5783
startTime,
58-
"failure",
84+
TELEMETRY_RESULT.FAILURE,
5985
error instanceof Error ? error : new Error(String(error))
6086
);
6187

0 commit comments

Comments
 (0)