Skip to content

chore: add server events #115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 25, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { mongoLogId } from "mongodb-log-writer";
import { ObjectId } from "mongodb";
import { Telemetry } from "./telemetry/telemetry.js";
import { UserConfig } from "./config.js";
import { type ServerEvent } from "./telemetry/types.js";
import { type ServerCommand } from "./telemetry/types.js";

export interface ServerOptions {
session: Session;
Expand All @@ -20,8 +22,10 @@ export class Server {
private readonly mcpServer: McpServer;
private readonly telemetry: Telemetry;
private readonly userConfig: UserConfig;
private readonly startTime: number;

constructor({ session, mcpServer, userConfig }: ServerOptions) {
this.startTime = Date.now();
this.session = session;
this.telemetry = new Telemetry(session);
this.mcpServer = mcpServer;
Expand All @@ -46,6 +50,18 @@ export class Server {
"server",
`Server started with transport ${transport.constructor.name} and agent runner ${this.session.agentRunner?.name}`
);

this.emitServerEvent("start", Date.now() - this.startTime);
};

this.mcpServer.server.onclose = () => {
const closeTime = Date.now();
this.emitServerEvent("stop", Date.now() - closeTime);
};

this.mcpServer.server.onerror = (error: Error) => {
const closeTime = Date.now();
this.emitServerEvent("stop", Date.now() - closeTime, error);
};
}

Expand All @@ -54,6 +70,39 @@ export class Server {
await this.mcpServer.close();
}

/**
* Emits a server event
* @param command - The server command (e.g., "start", "stop", "register", "deregister")
* @param additionalProperties - Additional properties specific to the event
*/
emitServerEvent(command: ServerCommand, commandDuration: number, error?: Error) {
const event: ServerEvent = {
timestamp: new Date().toISOString(),
source: "mdbmcp",
properties: {
...this.telemetry.getCommonProperties(),
result: "success",
duration_ms: commandDuration,
component: "server",
category: "other",
command: command,
},
};

if (command === "start") {
event.properties.startup_time_ms = commandDuration;
}
if (command === "stop") {
event.properties.runtime_duration_ms = Date.now() - this.startTime;
if (error) {
event.properties.result = "failure";
event.properties.reason = error.message;
}
}

this.telemetry.emitEvents([event]).catch(() => {});
}

private registerTools() {
for (const tool of [...AtlasTools, ...MongoDbTools]) {
new tool(this.session, this.userConfig, this.telemetry).register(this.mcpServer);
Expand Down
1 change: 0 additions & 1 deletion src/telemetry/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ export class Telemetry {
public async emitEvents(events: BaseEvent[]): Promise<void> {
try {
if (!Telemetry.isTelemetryEnabled()) {
logger.debug(mongoLogId(1_000_000), "telemetry", "Telemetry is disabled, skipping events.");
return;
}

Expand Down
13 changes: 13 additions & 0 deletions src/telemetry/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Result type constants for telemetry events
*/
export type TelemetryResult = "success" | "failure";
export type ServerCommand = "start" | "stop";

/**
* Base interface for all events
Expand Down Expand Up @@ -45,3 +46,15 @@ export interface ToolEvent extends BaseEvent {
is_atlas?: boolean;
} & BaseEvent["properties"];
}

/**
* Interface for server events
*/
export interface ServerEvent extends BaseEvent {
properties: {
command: ServerCommand;
reason?: string;
startup_time_ms?: number;
runtime_duration_ms?: number;
} & BaseEvent["properties"];
}
Loading