-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathglean.js
More file actions
56 lines (50 loc) · 1.62 KB
/
glean.js
File metadata and controls
56 lines (50 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import Glean from "@mozilla/glean/node";
import { captureException } from "@sentry/node";
/**
* @import EventMetricType from "@mozilla/glean/private/metrics/event";
* @import { ExtraMap } from "@mozilla/glean/private/metrics/events_database/recorded_event";
* @import { RequestHandlerExtra } from "@modelcontextprotocol/sdk/shared/protocol.js";
* @import { Request, Notification } from "@modelcontextprotocol/sdk/types.js";
*/
const uploadEnabled = process.env.GLEAN_ENABLED === "true";
const GLEAN_CHANNEL = process.env.GLEAN_CHANNEL || "dev";
const GLEAN_DEBUG = process.env.GLEAN_DEBUG === "true";
/* node:coverage disable */
if (GLEAN_DEBUG) {
Glean.setDebugViewTag("mdn-dev");
Glean.setLogPings(true);
}
/* node:coverage enable */
Glean.initialize("mdn-mcp", uploadEnabled, {
channel: GLEAN_CHANNEL,
});
/**
* @template {ExtraMap} E
* @template {Request} R
* @template {Notification} N
* @param {EventMetricType<E>} event
* @param {RequestHandlerExtra<R, N>} [request]
* @param {E} [extra]
*/
export function submitEvent(event, request, extra) {
try {
const optOut =
request?.requestInfo?.headers?.["x-moz-1st-party-data-opt-out"] === "1";
if (optOut) return;
const user_agent =
request?.requestInfo?.headers?.["user-agent"]?.toString();
event.record(
/** @type {E} */ ({
...extra,
...(user_agent ? { user_agent } : undefined),
}),
);
/* node:coverage disable */
} catch (error) {
// we don't want to throw, the request should work
// even if glean fails in some way
console.error(error);
captureException(error);
}
/* node:coverage enable */
}