-
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
Open
akkomar
wants to merge
1
commit into
main
Choose a base branch
from
mcp_telemetry
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,757
−101
Open
Changes from all commits
Commits
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pip 26 no longer bundles setuptools so I needed to clean this up a bit to make the build work.