UNPKG

@sentry/core

Version:
100 lines (99 loc) • 3.64 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 { timestampInSeconds } from "../utils/time.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 { const attributes = buildConfigAttributes(client); count("sdk.config", 1, { attributes, scope }); } catch (error) { DEBUG_BUILD && debug.warn("Failed to report SDK configuration", error); } }; const timer = safeUnref(setTimeout(report, 5e3)); client.on("flush", () => { report(); _INTERNAL_flushMetricsBuffer(client); }); } })); /** * Reports effective SDK configuration once after initialization. * * @experimental This integration may change or be removed without notice. */ const experimentalReportSdkConfigIntegration = 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; } function buildConfigAttributes(client) { const { runtime, ...options } = Object.fromEntries(Object.entries(client.getOptions()).filter(([key]) => ![ "integrations", "defaultIntegrations", "_metadata" ].includes(key))); const config = { version: 1, sdk: client.getSdkMetadata()?.sdk, runtime, options: serialize({ ...options, dataCollection: client.getDataCollectionOptions() }), options_set_by_user: client.getSdkMetadata()?.optionsSetByUser, integrations: client.getIntegrationNames().map((name) => ({ name })) }; const metadata = { timestamp: timestampInSeconds(), release: options.release, environment: options.environment, dist: options.dist }; const hashExclusions = /* @__PURE__ */ new Set(["serverName", ...Object.keys(metadata)]); const hashInput = { ...config, options: Object.fromEntries(Object.entries(config.options).filter(([key]) => !hashExclusions.has(key))) }; const payload = { ...config, ...metadata }; return { "sentry.sdk_config.hash": fnv1a32(JSON.stringify(hashInput)), "sentry.sdk_config.payload": JSON.stringify(payload) }; } export { experimentalReportSdkConfigIntegration }; //# sourceMappingURL=reportSdkConfig.js.map