@wardx/core 0.9.1 → 0.9.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wardx/core",
3
- "version": "0.9.1",
3
+ "version": "0.9.2",
4
4
  "description": "Runtime-agnostic Wardx engine for metrics, events, logs, Remote Config, and experiments.",
5
5
  "keywords": [
6
6
  "wardx",
@@ -88,6 +88,7 @@ export class FrameBuilder {
88
88
  logs: 0
89
89
  };
90
90
  let current = emptyFrame(frame.seq, frame.from, frame.to);
91
+ let currentBytes = measure(current).bytes;
91
92
 
92
93
  const finishCurrent = () => {
93
94
  const measured = measure(current);
@@ -97,55 +98,44 @@ export class FrameBuilder {
97
98
  frames.push(current);
98
99
  jsons.push(measured.json);
99
100
  current = emptyFrame(frame.seq + frames.length, frame.from, frame.to);
101
+ currentBytes = measure(current).bytes;
100
102
  };
101
103
 
102
- const addRow = (collection, row, countDrop = true) => {
103
- collection(current).push(row);
104
- if (measure(current).bytes <= maxFrameBytes) return true;
105
- collection(current).pop();
106
- if (collection.kind === 'distincts' && current.metrics.distincts.length === 0) {
107
- delete current.metrics.distincts;
108
- }
104
+ const tryAddRow = (kind, row, rowBytes) => {
105
+ const parent = kind === 'events' || kind === 'logs' ? current : current.metrics;
106
+ const rows = parent[kind];
107
+ // The first distinct row also introduces the optional JSON property.
108
+ const addedBytes = rowBytes + (rows?.length ? 1 : 0) +
109
+ (kind === 'distincts' && !rows ? ',"distincts":[]'.length : 0);
110
+ if (currentBytes + addedBytes > maxFrameBytes) return false;
111
+ if (rows) rows.push(row);
112
+ else parent[kind] = [row];
113
+ currentBytes += addedBytes;
114
+ return true;
115
+ };
116
+
117
+ const addRow = (kind, row, countDrop = true) => {
118
+ const rowBytes = Buffer.byteLength(JSON.stringify(row), 'utf8');
119
+ if (tryAddRow(kind, row, rowBytes)) return true;
109
120
  if (rowCount(current) > 0) {
110
121
  finishCurrent();
111
- collection(current).push(row);
112
- if (measure(current).bytes <= maxFrameBytes) return true;
113
- collection(current).pop();
114
- if (collection.kind === 'distincts' && current.metrics.distincts.length === 0) {
115
- delete current.metrics.distincts;
116
- }
122
+ if (tryAddRow(kind, row, rowBytes)) return true;
117
123
  }
118
- if (countDrop) dropped[collection.kind] += 1;
124
+ if (countDrop) dropped[kind] += 1;
119
125
  return false;
120
126
  };
121
127
 
122
- const counters = (candidate) => candidate.metrics.counters;
123
- counters.kind = 'counters';
124
- const gauges = (candidate) => candidate.metrics.gauges;
125
- gauges.kind = 'gauges';
126
- const histograms = (candidate) => candidate.metrics.histograms;
127
- histograms.kind = 'histograms';
128
- const distincts = (candidate) => {
129
- if (!candidate.metrics.distincts) candidate.metrics.distincts = [];
130
- return candidate.metrics.distincts;
131
- };
132
- distincts.kind = 'distincts';
133
- const events = (candidate) => candidate.events;
134
- events.kind = 'events';
135
- const logs = (candidate) => candidate.logs;
136
- logs.kind = 'logs';
137
-
138
- for (const row of frame.metrics.counters) addRow(counters, row);
139
- for (const row of frame.metrics.gauges) addRow(gauges, row);
140
- for (const row of frame.metrics.histograms) addRow(histograms, row);
141
- for (const row of frame.metrics.distincts || []) addRow(distincts, row);
142
- for (const row of frame.events) addRow(events, row);
143
- for (const row of frame.logs) addRow(logs, row);
128
+ for (const row of frame.metrics.counters) addRow('counters', row);
129
+ for (const row of frame.metrics.gauges) addRow('gauges', row);
130
+ for (const row of frame.metrics.histograms) addRow('histograms', row);
131
+ for (const row of frame.metrics.distincts || []) addRow('distincts', row);
132
+ for (const row of frame.events) addRow('events', row);
133
+ for (const row of frame.logs) addRow('logs', row);
144
134
 
145
135
  const droppedRows = Object.values(dropped).reduce((sum, value) => sum + value, 0);
146
136
  if (
147
137
  droppedRows > 0 &&
148
- !addRow(counters, [INTERNAL.frameRowsDropped, null, droppedRows], false)
138
+ !addRow('counters', [INTERNAL.frameRowsDropped, null, droppedRows], false)
149
139
  ) {
150
140
  throw new Error('maxFrameBytes cannot contain the frame drop metric');
151
141
  }