• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mongodb-js / mongodb-mcp-server / 14671718729

25 Apr 2025 07:06PM UTC coverage: 82.257% (+0.6%) from 81.696%
14671718729

Pull #132

github

nirinchev
enable dependabot for gha
Pull Request #132: Add dependabot config

134 of 210 branches covered (63.81%)

Branch coverage included in aggregate %.

719 of 827 relevant lines covered (86.94%)

44.48 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

40.74
/src/telemetry/telemetry.ts
1
import { Session } from "../session.js";
2
import { BaseEvent } from "./types.js";
3
import { config } from "../config.js";
31✔
4
import logger from "../logger.js";
31✔
5
import { mongoLogId } from "mongodb-log-writer";
31✔
6
import { ApiClient } from "../common/atlas/apiClient.js";
7
import { MACHINE_METADATA } from "./constants.js";
31✔
8
import { EventCache } from "./eventCache.js";
31✔
9

10
type EventResult = {
11
    success: boolean;
12
    error?: Error;
13
};
14

15
type CommonProperties = {
31✔
16
    device_id?: string;
17
    mcp_server_version: string;
18
    mcp_server_name: string;
19
    mcp_client_version?: string;
27✔
20
    mcp_client_name?: string;
27✔
21
    platform: string;
22
    arch: string;
27✔
23
    os_type: string;
24
    os_version?: string;
25
    session_id?: string;
26
};
27

28
export class Telemetry {
29
    private readonly commonProperties: CommonProperties;
30

31
    constructor(
32
        private readonly session: Session,
33
        private readonly eventCache: EventCache = EventCache.getInstance()
34
    ) {
35
        this.commonProperties = {
36
            ...MACHINE_METADATA,
283✔
37
        };
283✔
38
    }
39

40
    /**
×
41
     * Checks if telemetry is currently enabled
×
42
     * This is a method rather than a constant to capture runtime config changes
×
43
     *
44
     * Follows the Console Do Not Track standard (https://consoledonottrack.com/)
×
45
     * by respecting the DO_NOT_TRACK environment variable
×
46
     */
47
    private static isTelemetryEnabled(): boolean {
48
        // Check if telemetry is explicitly disabled in config
49
        if (config.telemetry === "disabled") {
×
50
            return false;
51
        }
52

53
        const doNotTrack = process.env.DO_NOT_TRACK;
54
        if (doNotTrack) {
55
            const value = doNotTrack.toLowerCase();
56
            // Telemetry should be disabled if DO_NOT_TRACK is "1", "true", or "yes"
57
            if (value === "1" || value === "true" || value === "yes") {
283✔
58
                return false;
283✔
59
            }
283✔
60
        }
61

62
        return true;
×
63
    }
64

×
65
    /**
66
     * Emits events through the telemetry pipeline
67
     * @param events - The events to emit
68
     */
69
    public async emitEvents(events: BaseEvent[]): Promise<void> {
70
        try {
71
            if (!Telemetry.isTelemetryEnabled()) {
72
                return;
73
            }
283✔
74

75
            await this.emit(events);
76
        } catch {
77
            logger.debug(mongoLogId(1_000_002), "telemetry", `Error emitting telemetry events.`);
78
        }
283✔
79
    }
283✔
80

81
    /**
82
     * Gets the common properties for events
83
     * @returns Object containing common properties for all events
84
     */
85
    public getCommonProperties(): CommonProperties {
86
        return {
87
            ...this.commonProperties,
88
            mcp_client_version: this.session.agentRunner?.version,
×
89
            mcp_client_name: this.session.agentRunner?.name,
×
90
            session_id: this.session.sessionId,
91
        };
×
92
    }
93

94
    /**
95
     * Attempts to emit events through authenticated and unauthenticated clients
96
     * Falls back to caching if both attempts fail
97
     */
×
98
    private async emit(events: BaseEvent[]): Promise<void> {
×
99
        const cachedEvents = this.eventCache.getEvents();
×
100
        const allEvents = [...cachedEvents, ...events];
×
101

×
102
        logger.debug(
103
            mongoLogId(1_000_003),
104
            "telemetry",
×
105
            `Attempting to send ${allEvents.length} events (${cachedEvents.length} cached)`
106
        );
107

×
108
        const result = await this.sendEvents(this.session.apiClient, allEvents);
109
        if (result.success) {
×
110
            this.eventCache.clearEvents();
111
            logger.debug(mongoLogId(1_000_004), "telemetry", `Sent ${allEvents.length} events successfully`);
112
            return;
113
        }
114

115
        logger.warning(
116
            mongoLogId(1_000_005),
×
117
            "telemetry",
×
118
            `Error sending event to client: ${result.error instanceof Error ? result.error.message : String(result.error)}`
×
119
        );
120
        this.eventCache.appendEvents(events);
×
121
    }
122

×
123
    /**
124
     * Attempts to send events through the provided API client
125
     */
126
    private async sendEvents(client: ApiClient, events: BaseEvent[]): Promise<EventResult> {
127
        try {
128
            await client.sendEvents(events);
129
            return { success: true };
130
        } catch (error) {
131
            return {
132
                success: false,
133
                error: error instanceof Error ? error : new Error(String(error)),
134
            };
135
        }
136
    }
137
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc