export const TELEMETRY_ATTRIBUTE_ALLOWLIST = Object.freeze([ "app_version", "build_id", "release_id", "config_schema_version", "api_contract_version", "route_id", "operation_id", "error_kind", "http_status_group", "attempt_count_bucket", "duration_bucket", "component_boundary", "active_release_id", "mismatch_kind", "reason", "queue_size_bucket", ]); export const TELEMETRY_FORBIDDEN_ATTRIBUTES = Object.freeze([ "access_token", "refresh_token", "authorization_header", "cookie", "email", "user_name", "raw_user_id", "raw_url", "query_string", "request_body", "response_body", "storage_value", "stack_in_user_message", ]); /** * @typedef {{ * eventName: string, * trigger: string, * requiredAttributes: readonly string[], * optionalAttributes: readonly string[], * forbiddenAttributes: readonly string[], * sampling: string, * delivery: string * }} TelemetryDefinition */ /** * @param {string} eventName * @param {string} trigger * @param {string[]} requiredAttributes * @param {string[]} [optionalAttributes] * @param {string} [sampling] * @returns {Readonly} */ const event = ( eventName, trigger, requiredAttributes, optionalAttributes = [], sampling = "all", ) => Object.freeze({ eventName, trigger, requiredAttributes: Object.freeze(requiredAttributes), optionalAttributes: Object.freeze(optionalAttributes), forbiddenAttributes: TELEMETRY_FORBIDDEN_ATTRIBUTES, sampling, delivery: "best-effort", }); export const TELEMETRY_REGISTRY = Object.freeze({ "app.boot.failed": event("app.boot.failed", "boot validation failure", [ "error_kind", "build_id", "config_schema_version", ]), "api.request.failed": event("api.request.failed", "terminal API failure", [ "error_kind", "http_status_group", "attempt_count_bucket", "route_id", ]), "ui.render.failed": event("ui.render.failed", "React boundary catch", [ "route_id", "build_id", "component_boundary", ]), "release.mismatch.detected": event( "release.mismatch.detected", "release tuple mismatch", ["build_id", "active_release_id", "mismatch_kind"], ), "telemetry.delivery.dropped": event( "telemetry.delivery.dropped", "queue or sink failure", ["reason", "queue_size_bucket"], [], "internal-counter", ), }); /** * @param {string} eventName * @param {Record} attributes */ export function projectTelemetryEvent(eventName, attributes) { const registry = /** @type {Record} */ ( TELEMETRY_REGISTRY ); const definition = registry[eventName]; if (!definition) { return { success: /** @type {false} */ (false), reason: "unregistered-event", }; } const projected = Object.fromEntries( Object.entries(attributes).filter( ([key]) => TELEMETRY_ATTRIBUTE_ALLOWLIST.includes(key) && !TELEMETRY_FORBIDDEN_ATTRIBUTES.includes(key), ), ); const missing = definition.requiredAttributes.filter( (key) => projected[key] === undefined, ); if (missing.length > 0) { return { success: /** @type {false} */ (false), reason: "missing-required-attributes", }; } return { success: /** @type {true} */ (true), event: Object.freeze({ eventName, attributes: Object.freeze(projected), }), }; }