UNPKG

unleash-server

Version:

Unleash is an enterprise ready feature flag service. It provides different strategies for handling feature flags.

87 lines 2.85 kB
export class BatchHistogram { constructor(config) { this.labelNames = []; this.bucketBoundaries = new Set(); // Store accumulated data for each label combination this.store = new Map(); this.name = config.name; this.help = config.help; this.registry = config.registry; this.labelNames = config.labelNames || []; this.registry.registerMetric(this); } recordBatch(labels, data) { const labelKey = this.createLabelKey(labels); let entry = this.store.get(labelKey); if (!entry) { entry = { count: 0, sum: 0, buckets: new Map(), }; this.store.set(labelKey, entry); } entry.count += data.count; entry.sum += data.sum; for (const bucket of data.buckets) { const current = entry.buckets.get(bucket.le) || 0; entry.buckets.set(bucket.le, current + bucket.count); this.bucketBoundaries.add(bucket.le); } } createLabelKey(labels) { const sortedKeys = Object.keys(labels).sort(); return JSON.stringify(sortedKeys.map((key) => [key, labels[key]])); } reset() { this.store.clear(); this.bucketBoundaries.clear(); } get() { const values = []; for (const [labelKey, data] of this.store) { const labels = {}; if (labelKey) { const parsedLabels = JSON.parse(labelKey); parsedLabels.forEach(([key, value]) => { labels[key] = value; }); } for (const [le, cumulativeCount] of Array.from(data.buckets.entries()).sort((a, b) => { // Sort buckets: numbers first (ascending), then '+Inf' last if (a[0] === '+Inf') return 1; if (b[0] === '+Inf') return -1; return a[0] - b[0]; })) { values.push({ value: cumulativeCount, labels: { ...labels, le: le.toString(), }, metricName: `${this.name}_bucket`, }); } values.push({ value: data.sum, labels, metricName: `${this.name}_sum`, }); values.push({ value: data.count, labels, metricName: `${this.name}_count`, }); } return { name: this.name, help: this.help, type: 'histogram', values, aggregator: 'sum', }; } } //# sourceMappingURL=batch-histogram.js.map