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

mongodb-js / mongodb-mcp-server / 14672998392

25 Apr 2025 08:31PM UTC coverage: 82.446%. Remained the same
14672998392

Pull #137

github

nirinchev
Update name for fork workflow
Pull Request #137: chore: revamp gha workflows

135 of 212 branches covered (63.68%)

Branch coverage included in aggregate %.

748 of 859 relevant lines covered (87.08%)

48.14 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, CommonProperties } from "./types.js";
3
import { config } from "../config.js";
31✔
4
import logger, { LogId } from "../logger.js";
31✔
5
import { ApiClient } from "../common/atlas/apiClient.js";
6
import { MACHINE_METADATA } from "./constants.js";
31✔
7
import { EventCache } from "./eventCache.js";
31✔
8

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

14
export class Telemetry {
31✔
15
    private readonly commonProperties: CommonProperties;
16

17
    constructor(
18
        private readonly session: Session,
28✔
19
        private readonly eventCache: EventCache = EventCache.getInstance()
28✔
20
    ) {
21
        this.commonProperties = {
28✔
22
            ...MACHINE_METADATA,
23
        };
24
    }
25

26
    /**
27
     * Checks if telemetry is currently enabled
28
     * This is a method rather than a constant to capture runtime config changes
29
     *
30
     * Follows the Console Do Not Track standard (https://consoledonottrack.com/)
31
     * by respecting the DO_NOT_TRACK environment variable
32
     */
33
    private static isTelemetryEnabled(): boolean {
34
        // Check if telemetry is explicitly disabled in config
35
        if (config.telemetry === "disabled") {
282✔
36
            return false;
282✔
37
        }
38

39
        const doNotTrack = process.env.DO_NOT_TRACK;
×
40
        if (doNotTrack) {
×
41
            const value = doNotTrack.toLowerCase();
×
42
            // Telemetry should be disabled if DO_NOT_TRACK is "1", "true", or "yes"
43
            if (value === "1" || value === "true" || value === "yes") {
×
44
                return false;
×
45
            }
46
        }
47

48
        return true;
×
49
    }
50

51
    /**
52
     * Emits events through the telemetry pipeline
53
     * @param events - The events to emit
54
     */
55
    public async emitEvents(events: BaseEvent[]): Promise<void> {
56
        try {
282✔
57
            if (!Telemetry.isTelemetryEnabled()) {
282✔
58
                return;
282✔
59
            }
60

61
            await this.emit(events);
×
62
        } catch {
63
            logger.debug(LogId.telemetryEmitFailure, "telemetry", `Error emitting telemetry events.`);
×
64
        }
65
    }
66

67
    /**
68
     * Gets the common properties for events
69
     * @returns Object containing common properties for all events
70
     */
71
    public getCommonProperties(): CommonProperties {
72
        return {
282✔
73
            ...this.commonProperties,
74
            mcp_client_version: this.session.agentRunner?.version,
75
            mcp_client_name: this.session.agentRunner?.name,
76
            session_id: this.session.sessionId,
77
            config_atlas_auth: this.session.apiClient.hasCredentials() ? "true" : "false",
282✔
78
            config_connection_string: config.connectionString ? "true" : "false",
282✔
79
        };
80
    }
81

82
    /**
83
     * Attempts to emit events through authenticated and unauthenticated clients
84
     * Falls back to caching if both attempts fail
85
     */
86
    private async emit(events: BaseEvent[]): Promise<void> {
87
        const cachedEvents = this.eventCache.getEvents();
×
88
        const allEvents = [...cachedEvents, ...events];
×
89

90
        logger.debug(
×
91
            LogId.telemetryEmitStart,
92
            "telemetry",
93
            `Attempting to send ${allEvents.length} events (${cachedEvents.length} cached)`
94
        );
95

96
        const result = await this.sendEvents(this.session.apiClient, allEvents);
×
97
        if (result.success) {
×
98
            this.eventCache.clearEvents();
×
99
            logger.debug(LogId.telemetryEmitSuccess, "telemetry", `Sent ${allEvents.length} events successfully`);
×
100
            return;
×
101
        }
102

103
        logger.debug(
×
104
            LogId.telemetryEmitFailure,
105
            "telemetry",
106
            `Error sending event to client: ${result.error instanceof Error ? result.error.message : String(result.error)}`
×
107
        );
108
        this.eventCache.appendEvents(events);
×
109
    }
110

111
    /**
112
     * Attempts to send events through the provided API client
113
     */
114
    private async sendEvents(client: ApiClient, events: BaseEvent[]): Promise<EventResult> {
115
        try {
×
116
            await client.sendEvents(events);
×
117
            return { success: true };
×
118
        } catch (error) {
119
            return {
×
120
                success: false,
121
                error: error instanceof Error ? error : new Error(String(error)),
×
122
            };
123
        }
124
    }
125
}
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