@sentry/core
Version:
Base implementation for all Sentry JavaScript SDKs
84 lines (83 loc) • 3.52 kB
JavaScript
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 = 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(["integrations", "serverName"]);
function buildConfigAttributes(client) {
const { sdk, optionsSetByUser = [] } = client.getSdkMetadata() || {};
const clientOptions = client.getOptions();
const { release, environment, dist } = 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 (Array.isArray(options.defaultIntegrations)) options.defaultIntegrations = options.defaultIntegrations.map((integration) => ({ name: integration.name }));
const payload = JSON.stringify({
version: 2,
sdk: serialize(sdk),
runtime: serialize(runtime),
release,
environment,
dist,
integrations: client.getIntegrationNames().map((name) => ({ name })),
options: serialize(options)
});
return {
"sentry.sdk_config.hash": fnv1a32(payload),
"sentry.sdk_config.payload": payload
};
}
export { experimentalReportSdkConfigIntegration };
//# sourceMappingURL=reportSdkConfig.js.map