UNPKG

@sentry/core

Version:
113 lines (112 loc) • 4.54 kB
import { DEBUG_BUILD } from "../debug-build.js"; import { defineIntegration } from "../integration.js"; import { _INTERNAL_flushMetricsBuffer } from "../metrics/internal.js"; import { count } from "../metrics/public-api.js"; import { Scope } from "../scope.js"; import { debug } from "../utils/debug-logger.js"; import { fnv1a32 } from "../utils/hash.js"; import { isPlainObject } from "../utils/is.js"; import { safeUnref } from "../utils/timer.js"; // src/integrations/reportSdkConfig.ts const _experimentalReportSdkConfigIntegration = (() => ({ name: "ExperimentalReportSdkConfig", afterAllSetup(client) { const scope = new Scope(); scope.setClient(client); let reported = false; const report = () => { if (reported) return; reported = true; clearTimeout(timer); try { count("sdk.config", 1, { attributes: buildConfigAttributes(client), scope }); } catch (error) { DEBUG_BUILD && debug.warn("Failed to report SDK configuration", error); } }; const timer = safeUnref(setTimeout(report, 0)); client.on("flush", () => { report(); _INTERNAL_flushMetricsBuffer(client); }); } })); /** * Reports the SDK options the user passed to `init()` on the next tick after client setup, or on an earlier flush. * * @experimental This integration may change or be removed without notice. */ const experimentalReportSdkConfigIntegration = /* @__PURE__ */ defineIntegration(_experimentalReportSdkConfigIntegration); function serialize(value, ancestors = /* @__PURE__ */ new Set()) { if (typeof value === "function") return `[Function:${fnv1a32(Function.prototype.toString.call(value))}]`; if (typeof value === "bigint" || typeof value === "symbol") return `[${typeof value}:${fnv1a32(String(value))}]`; if (value === null || typeof value !== "object") return value; if (value instanceof RegExp) return value.toString(); if (ancestors.has(value)) return "[Circular]"; if (!Array.isArray(value) && !isPlainObject(value)) return `[${String(Object.prototype.toString.call(value)).slice(8, -1)}:${fnv1a32(String(value))}]`; const nextAncestors = new Set(ancestors).add(value); if (Array.isArray(value)) return value.map((item) => serialize(item, nextAncestors) ?? null); const result = {}; for (const key of Object.keys(value).sort()) { const item = value[key]; result[key] = item === void 0 ? "[undefined]" : serialize(item, nextAncestors); } return result; } const EXCLUDED_OPTIONS = /* @__PURE__ */ new Set([ "dist", "dsn", "integrations", "release", "serverName" ]); function buildConfigAttributes(client) { const { sdk, optionsSetByUser = [], integrationsSetByUser, removedDefaultIntegrations } = client.getSdkMetadata() || {}; const clientOptions = client.getOptions(); const { environment } = clientOptions; const runtime = "runtime" in clientOptions ? clientOptions.runtime : void 0; const options = Object.fromEntries(Object.entries(clientOptions).filter(([key]) => optionsSetByUser.includes(key) && !EXCLUDED_OPTIONS.has(key))); if (integrationsSetByUser) options.integrations = integrationsSetByUser.map((name) => { const integration = client.getIntegrationByName(name); return describeIntegration(name, integration?.isDefaultInstance ? void 0 : integration?._options); }); if (Array.isArray(options.defaultIntegrations)) options.defaultIntegrations = options.defaultIntegrations.map((integration) => describeIntegration(integration.name, integration._options)); const payload = JSON.stringify({ version: 3, sdk: serialize(sdk), runtime: serialize(runtime), environment, ...removedDefaultIntegrations && { removed_default_integrations: removedDefaultIntegrations }, options: serialize(options) }); return { "sentry.sdk_config.hash": fnv1a32(payload), "sentry.sdk_config.payload": payload }; } function describeIntegration(name, options) { if (options === void 0) return { name }; if (!isPlainObject(options)) return { name, options: describeOptionValue(options) }; const keys = Object.keys(options); if (!keys.length) return { name }; const described = {}; for (const key of keys) described[key] = describeOptionValue(options[key]); return { name, options: described }; } function describeOptionValue(value) { return Array.isArray(value) ? value.map(redactObject) : redactObject(value); } function redactObject(value) { return value === null || typeof value !== "object" || value instanceof RegExp ? value : "[Object]"; } export { experimentalReportSdkConfigIntegration }; //# sourceMappingURL=reportSdkConfig.js.map