@tracelumo/node 0.6.0 → 0.6.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/metrics.cjs +23 -3
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tracelumo/node",
3
- "version": "0.6.0",
3
+ "version": "0.6.2",
4
4
  "description": "Connect Node.js apps to TraceLumo: automatic traces, logs, errors and system metrics",
5
5
  "type": "commonjs",
6
6
  "main": "./src/index.cjs",
package/src/metrics.cjs CHANGED
@@ -122,7 +122,8 @@ function createSampler({ service, cwd = process.cwd(), cgroup } = {}) {
122
122
  }
123
123
  let last = { at: performance.now(), cpu: process.cpuUsage(), system: cpuTimes(), elu: performance.eventLoopUtilization(), net: networkBytes() };
124
124
 
125
- function sample() {
125
+ /** One sample. `paused`: the process was suspended (serverless between requests), so delay readings are void. */
126
+ function sample({ paused = false } = {}) {
126
127
  const now = { at: performance.now(), cpu: process.cpuUsage(), system: cpuTimes(), elu: performance.eventLoopUtilization(), net: networkBytes() };
127
128
  const elapsedMs = Math.max(1, now.at - last.at);
128
129
  const cores = info.cpus || 1;
@@ -163,6 +164,13 @@ function createSampler({ service, cwd = process.cwd(), cgroup } = {}) {
163
164
  values.network_rx = round(Math.max(0, now.net.rx - last.net.rx) / (elapsedMs / 1000), 0);
164
165
  values.network_tx = round(Math.max(0, now.net.tx - last.net.tx) / (elapsedMs / 1000), 0);
165
166
  }
167
+ // A paused process (Vercel and Lambda freeze functions between requests) records the whole pause as event loop
168
+ // delay when it resumes. That's not a delay the app had, so leave those readings out.
169
+ if (paused) {
170
+ delete values.event_loop_p50;
171
+ delete values.event_loop_p99;
172
+ delete values.event_loop_max;
173
+ }
166
174
  for (const key of Object.keys(values)) if (values[key] === undefined) delete values[key];
167
175
  delay.reset();
168
176
  gcCount = 0;
@@ -212,13 +220,19 @@ function startHeartbeat({ heartbeatUrl, token }, { instance, sampler, seconds, s
212
220
  const agent = new transport.Agent({ keepAlive: true, maxSockets: 1 });
213
221
  let busy = false;
214
222
  let stopped = false;
223
+ let beats = 0;
215
224
  const send = () => {
216
225
  if (busy || stopped) return;
217
226
  let body;
218
227
  try {
228
+ // The first beat (and every 30th) carries the instance's details, so even a function that lives only a few
229
+ // seconds shows its platform and runtime.
230
+ const withInfo = beats % 30 === 0;
231
+ beats += 1;
219
232
  body = JSON.stringify({
220
233
  instance, timestamp: new Date().toISOString(), every: seconds,
221
234
  service: sampler.info.service, hostname: sampler.info.hostname, values: sampler.beat(),
235
+ ...(withInfo ? { info: sampler.info } : {}),
222
236
  });
223
237
  } catch {
224
238
  return;
@@ -255,14 +269,20 @@ function startMetrics({ metricsUrl, heartbeatUrl, token, service, metricsInterva
255
269
  if (details) Object.assign(sampler.info, defined(details));
256
270
  }).catch(() => {});
257
271
  const queue = createQueue({ url: metricsUrl, token, maxBatch: 100, kind: 'metrics' }, options);
272
+ // Serverless functions may live only seconds, so their first full sample comes after one second.
273
+ const firstSample = NOT_CONTAINERS.has(sampler.info.runtime) && sampler.info.runtime !== 'host' ? 1000 : Math.min(FIRST_SAMPLE, metricsInterval * 1000);
274
+ // When the sample timer fires far later than planned, the process was paused rather than busy.
275
+ let due = Date.now() + firstSample;
258
276
  const record = () => {
259
277
  try {
260
- queue.push({ instance, timestamp: new Date().toISOString(), info: sampler.info, values: sampler.sample() });
278
+ const late = Date.now() - due;
279
+ due = Date.now() + metricsInterval * 1000;
280
+ queue.push({ instance, timestamp: new Date().toISOString(), info: sampler.info, values: sampler.sample({ paused: late > Math.max(2000, metricsInterval * 500) }) });
261
281
  } catch {
262
282
  // Metrics must never break the application.
263
283
  }
264
284
  };
265
- const first = setTimeout(record, Math.min(FIRST_SAMPLE, metricsInterval * 1000));
285
+ const first = setTimeout(record, firstSample);
266
286
  first.unref();
267
287
  const timer = setInterval(record, metricsInterval * 1000);
268
288
  timer.unref();