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

mongodb-js / mongodb-mcp-server / 14667012576

25 Apr 2025 02:31PM UTC coverage: 81.696%. Remained the same
14667012576

Pull #125

github

fmenezes
fix: duplicated v on version bump
Pull Request #125: fix: duplicated v on version bump

128 of 208 branches covered (61.54%)

Branch coverage included in aggregate %.

720 of 830 relevant lines covered (86.75%)

44.18 hits per line

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

36.0
/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 = {
16
    device_id?: string;
17
    mcp_server_version: string;
18
    mcp_server_name: string;
19
    mcp_client_version?: string;
20
    mcp_client_name?: string;
21
    platform: string;
22
    arch: string;
23
    os_type: string;
24
    os_version?: string;
25
    session_id?: string;
26
};
27

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

31
    constructor(
32
        private readonly session: Session,
27✔
33
        private readonly eventCache: EventCache = EventCache.getInstance()
27✔
34
    ) {
35
        this.commonProperties = {
27✔
36
            ...MACHINE_METADATA,
37
        };
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") {
283✔
50
            return false;
283✔
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") {
×
58
                return false;
×
59
            }
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 {
283✔
71
            if (!Telemetry.isTelemetryEnabled()) {
283✔
72
                return;
283✔
73
            }
74

75
            await this.emit(events);
×
76
        } catch {
77
            logger.debug(mongoLogId(1_000_002), "telemetry", `Error emitting telemetry events.`);
×
78
        }
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 {
283✔
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