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

mongodb-js / mongodb-mcp-server / 14705884283

28 Apr 2025 10:45AM UTC coverage: 82.547% (+0.9%) from 81.696%
14705884283

Pull #131

github

fmenezes
fix: make ip changes smooth
Pull Request #131: feat: add atlas-connect-cluster tool

147 of 229 branches covered (64.19%)

Branch coverage included in aggregate %.

26 of 31 new or added lines in 4 files covered. (83.87%)

60 existing lines in 8 files now uncovered.

780 of 894 relevant lines covered (87.25%)

49.5 hits per line

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

39.62
/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";
6
import { ApiClient } from "../common/atlas/apiClient.js";
31✔
7
import { MACHINE_METADATA } from "./constants.js";
31✔
8
import { EventCache } from "./eventCache.js";
9

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

31✔
15
type CommonProperties = {
16
    device_id?: string;
17
    mcp_server_version: string;
18
    mcp_server_name: string;
29✔
19
    mcp_client_version?: string;
29✔
20
    mcp_client_name?: string;
21
    platform: string;
29✔
22
    arch: string;
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 = {
285✔
36
            ...MACHINE_METADATA,
285✔
37
        };
38
    }
UNCOV
39

×
UNCOV
40
    /**
×
UNCOV
41
     * Checks if telemetry is currently enabled
×
42
     * This is a method rather than a constant to capture runtime config changes
UNCOV
43
     *
×
UNCOV
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 {
UNCOV
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"
285✔
57
            if (value === "1" || value === "true" || value === "yes") {
285✔
58
                return false;
285✔
59
            }
60
        }
UNCOV
61

×
62
        return true;
UNCOV
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;
285✔
73
            }
74

75
            await this.emit(events);
76
        } catch {
77
            logger.debug(mongoLogId(1_000_002), "telemetry", `Error emitting telemetry events.`);
285✔
78
        }
285✔
79
    }
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 {
UNCOV
87
            ...this.commonProperties,
×
UNCOV
88
            mcp_client_version: this.session.agentRunner?.version,
×
89
            mcp_client_name: this.session.agentRunner?.name,
UNCOV
90
            session_id: this.session.sessionId,
×
91
        };
92
    }
93

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

102
        logger.debug(
UNCOV
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(
×
UNCOV
116
            mongoLogId(1_000_005),
×
UNCOV
117
            "telemetry",
×
118
            `Error sending event to client: ${result.error instanceof Error ? result.error.message : String(result.error)}`
UNCOV
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