UNPKG

@sentry/core

Version:
82 lines (78 loc) • 2.27 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); const is = require('./is.js'); const timer = require('./timer.js'); const DEFAULT_FLUSH_INTERVAL = 5e3; function setupWeightBasedFlushing(client, afterCaptureHook, flushHook, estimateSizeFn, flushFn) { let weight = 0; let flushTimeout; let isTimerActive = false; client.on(flushHook, () => { weight = 0; clearTimeout(flushTimeout); isTimerActive = false; }); client.on(afterCaptureHook, (item) => { weight += estimateSizeFn(item); if (weight >= 8e5) { flushFn(client); } else if (!isTimerActive) { const flushInterval = client.getOptions()._flushInterval ?? DEFAULT_FLUSH_INTERVAL; if (flushInterval > 0) { isTimerActive = true; flushTimeout = timer.safeUnref( setTimeout(() => { flushFn(client); }, flushInterval) ); } } }); client.on("flush", () => { flushFn(client); }); } function estimateMetricSizeInBytes(metric) { let weight = 0; if (metric.name) { weight += metric.name.length * 2; } weight += 8; return weight + estimateAttributesSizeInBytes(metric.attributes); } function estimateLogSizeInBytes(log) { let weight = 0; if (log.message) { weight += log.message.length * 2; } return weight + estimateAttributesSizeInBytes(log.attributes); } function estimateAttributesSizeInBytes(attributes) { if (!attributes) { return 0; } let weight = 0; Object.values(attributes).forEach((value) => { if (Array.isArray(value)) { weight += value.length * estimatePrimitiveSizeInBytes(value[0]); } else if (is.isPrimitive(value)) { weight += estimatePrimitiveSizeInBytes(value); } else { weight += 100; } }); return weight; } function estimatePrimitiveSizeInBytes(value) { if (typeof value === "string") { return value.length * 2; } else if (typeof value === "number") { return 8; } else if (typeof value === "boolean") { return 4; } return 0; } exports.estimateLogSizeInBytes = estimateLogSizeInBytes; exports.estimateMetricSizeInBytes = estimateMetricSizeInBytes; exports.setupWeightBasedFlushing = setupWeightBasedFlushing; //# sourceMappingURL=weightBasedFlushing.js.map