-
Notifications
You must be signed in to change notification settings - Fork 48
Add Glean telemetry to MCP server #2324
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
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d87f5f6
Add Glean telemetry to MCP server
akkomar a525180
Merge branch 'main' into mcp_telemetry
akkomar 04b8020
Log telemetry submission errors
akkomar cd9fcfe
Fix botched merge
akkomar e0d7b82
Clean up misleading mock usage in telemetry test
akkomar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| /** | ||
| * Lightweight Glean telemetry for the MCP server. | ||
| * | ||
| * We can't use the Glean JS SDK here because @mozilla/glean only ships | ||
| * browser bundles — there's no Node.js entry point. Since this runs in | ||
| * a Netlify Function (Node.js), we handcraft a minimal events ping and | ||
| * POST it directly to the ingestion endpoint. | ||
| */ | ||
|
|
||
| const { randomUUID } = require("crypto"); | ||
|
|
||
| const DEFAULT_APP_ID = "glean-dictionary-dev"; | ||
| const DEFAULT_INGESTION_URL = "https://incoming.telemetry.mozilla.org"; | ||
|
|
||
| /** | ||
| * Map Netlify CONTEXT env var to app_channel. | ||
| */ | ||
| function getAppChannel() { | ||
| const context = process.env.CONTEXT; | ||
| if (context === "production") return "production"; | ||
| if (context === "deploy-preview" || context === "branch-deploy") | ||
| return "deploy-preview"; | ||
| return "development"; | ||
| } | ||
|
|
||
| /** | ||
| * Build a Glean events ping payload. | ||
| * | ||
| * @param {Array<Object>} events - Array of event objects with category, name, extra | ||
| * @returns {Object} A valid glean.1 ping payload | ||
| */ | ||
| function buildPing(events) { | ||
| const now = new Date().toISOString(); | ||
|
|
||
| return { | ||
| ping_info: { | ||
| seq: 0, | ||
| start_time: now, | ||
| end_time: now, | ||
| }, | ||
| client_info: { | ||
| app_build: "Unknown", | ||
| app_display_version: "1.0.0", | ||
| app_channel: getAppChannel(), | ||
| architecture: "n/a", | ||
| os: "n/a", | ||
| os_version: "n/a", | ||
| telemetry_sdk_build: "glean-mcp/1.0.0", | ||
| first_run_date: now, | ||
| locale: "und", | ||
| }, | ||
| events: events.map((event, index) => ({ | ||
| category: event.category, | ||
| name: event.name, | ||
| timestamp: index, | ||
| extra: event.extra, | ||
| })), | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Submit a telemetry event to the Glean ingestion endpoint. | ||
| * | ||
| * @param {string} category - Event category (e.g. "mcp") | ||
| * @param {string} name - Event name (e.g. "tool_call") | ||
| * @param {Object} extra - Extra key-value pairs (all string values) | ||
| * @returns {Promise<void>} Resolves silently; never throws. | ||
| */ | ||
| async function submitTelemetryEvent(category, name, extra = {}) { | ||
| try { | ||
| const appId = process.env.GLEAN_APPLICATION_ID || DEFAULT_APP_ID; | ||
| const ingestionUrl = | ||
| process.env.GLEAN_INGESTION_URL || DEFAULT_INGESTION_URL; | ||
| const debugTag = process.env.GLEAN_DEBUG_VIEW_TAG; | ||
|
|
||
| // Filter out undefined/null extra values | ||
| const cleanExtra = {}; | ||
| for (const [k, v] of Object.entries(extra)) { | ||
| if (v != null) { | ||
| cleanExtra[k] = String(v); | ||
| } | ||
| } | ||
|
|
||
| const ping = buildPing([{ category, name, extra: cleanExtra }]); | ||
| const uuid = randomUUID(); | ||
| const url = `${ingestionUrl}/submit/${appId}/events/1/${uuid}`; | ||
|
|
||
| const headers = { | ||
| "Content-Type": "application/json; charset=utf-8", | ||
| Date: new Date().toUTCString(), | ||
| "X-Telemetry-Agent": "Glean/handcrafted", | ||
| }; | ||
|
|
||
| if (debugTag) { | ||
| headers["X-Debug-ID"] = debugTag; | ||
| } | ||
|
|
||
| await fetch(url, { | ||
| method: "POST", | ||
| headers, | ||
| body: JSON.stringify(ping), | ||
| }); | ||
| } catch { | ||
| // Telemetry must never break MCP responses — silently ignore all errors. | ||
| } | ||
| } | ||
|
|
||
| module.exports = { submitTelemetryEvent, buildPing }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.