Skip to content

Commit 10f95c6

Browse files
authored
chore: add server events (#115)
1 parent af779e1 commit 10f95c6

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

src/server.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { mongoLogId } from "mongodb-log-writer";
88
import { ObjectId } from "mongodb";
99
import { Telemetry } from "./telemetry/telemetry.js";
1010
import { UserConfig } from "./config.js";
11+
import { type ServerEvent } from "./telemetry/types.js";
12+
import { type ServerCommand } from "./telemetry/types.js";
1113
import { CallToolRequestSchema, CallToolResult } from "@modelcontextprotocol/sdk/types.js";
1214
import assert from "assert";
1315

@@ -22,8 +24,10 @@ export class Server {
2224
private readonly mcpServer: McpServer;
2325
private readonly telemetry: Telemetry;
2426
private readonly userConfig: UserConfig;
27+
private readonly startTime: number;
2528

2629
constructor({ session, mcpServer, userConfig }: ServerOptions) {
30+
this.startTime = Date.now();
2731
this.session = session;
2832
this.telemetry = new Telemetry(session);
2933
this.mcpServer = mcpServer;
@@ -71,6 +75,18 @@ export class Server {
7175
"server",
7276
`Server started with transport ${transport.constructor.name} and agent runner ${this.session.agentRunner?.name}`
7377
);
78+
79+
this.emitServerEvent("start", Date.now() - this.startTime);
80+
};
81+
82+
this.mcpServer.server.onclose = () => {
83+
const closeTime = Date.now();
84+
this.emitServerEvent("stop", Date.now() - closeTime);
85+
};
86+
87+
this.mcpServer.server.onerror = (error: Error) => {
88+
const closeTime = Date.now();
89+
this.emitServerEvent("stop", Date.now() - closeTime, error);
7490
};
7591
}
7692

@@ -79,6 +95,39 @@ export class Server {
7995
await this.mcpServer.close();
8096
}
8197

98+
/**
99+
* Emits a server event
100+
* @param command - The server command (e.g., "start", "stop", "register", "deregister")
101+
* @param additionalProperties - Additional properties specific to the event
102+
*/
103+
emitServerEvent(command: ServerCommand, commandDuration: number, error?: Error) {
104+
const event: ServerEvent = {
105+
timestamp: new Date().toISOString(),
106+
source: "mdbmcp",
107+
properties: {
108+
...this.telemetry.getCommonProperties(),
109+
result: "success",
110+
duration_ms: commandDuration,
111+
component: "server",
112+
category: "other",
113+
command: command,
114+
},
115+
};
116+
117+
if (command === "start") {
118+
event.properties.startup_time_ms = commandDuration;
119+
}
120+
if (command === "stop") {
121+
event.properties.runtime_duration_ms = Date.now() - this.startTime;
122+
if (error) {
123+
event.properties.result = "failure";
124+
event.properties.reason = error.message;
125+
}
126+
}
127+
128+
this.telemetry.emitEvents([event]).catch(() => {});
129+
}
130+
82131
private registerTools() {
83132
for (const tool of [...AtlasTools, ...MongoDbTools]) {
84133
new tool(this.session, this.userConfig, this.telemetry).register(this.mcpServer);

src/telemetry/telemetry.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ export class Telemetry {
6969
public async emitEvents(events: BaseEvent[]): Promise<void> {
7070
try {
7171
if (!Telemetry.isTelemetryEnabled()) {
72-
logger.debug(mongoLogId(1_000_000), "telemetry", "Telemetry is disabled, skipping events.");
7372
return;
7473
}
7574

src/telemetry/types.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Result type constants for telemetry events
33
*/
44
export type TelemetryResult = "success" | "failure";
5+
export type ServerCommand = "start" | "stop";
56

67
/**
78
* Base interface for all events
@@ -45,3 +46,15 @@ export interface ToolEvent extends BaseEvent {
4546
is_atlas?: boolean;
4647
} & BaseEvent["properties"];
4748
}
49+
50+
/**
51+
* Interface for server events
52+
*/
53+
export interface ServerEvent extends BaseEvent {
54+
properties: {
55+
command: ServerCommand;
56+
reason?: string;
57+
startup_time_ms?: number;
58+
runtime_duration_ms?: number;
59+
} & BaseEvent["properties"];
60+
}

0 commit comments

Comments
 (0)