@tracelumo/node 0.2.0 → 0.5.0

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/README.md CHANGED
@@ -1,13 +1,13 @@
1
1
  # TraceLumo for Node.js
2
2
 
3
- `@tracelumo/node` connects a Node.js app to TraceLumo. It traces Node HTTP and HTTPS, Express and built-in `fetch`, sends `console` output as logs linked to the active trace, and announces the service when it starts. Requires Node.js 22.15 or later.
3
+ `@tracelumo/node` connects a Node.js app to TraceLumo. It traces Node HTTP and HTTPS, Express and built-in `fetch`, sends `console` output as logs linked to the active trace, reports errors to error tracking, sends system and runtime metrics, and announces the service when it starts. No separate agent is needed. Requires Node.js 22.15 or later.
4
4
 
5
5
  ## Quick setup
6
6
 
7
7
  Create a token on your project's **Agent tokens** page, then run this in your app's folder:
8
8
 
9
9
  ```sh
10
- npx @tracelumo/node init --endpoint http://127.0.0.1:911 --token tl_agent_your_project_token
10
+ npx @tracelumo/node init --endpoint https://your-tracelumo.vercel.app --token tl_agent_your_project_token
11
11
  ```
12
12
 
13
13
  `init` installs the SDK, saves the endpoint and token to `.env.local` (and makes sure Git ignores it), and hooks the SDK into the app's normal start-up:
@@ -26,7 +26,7 @@ npm install @tracelumo/node
26
26
  Set the endpoint and token in the app's environment or `.env.local`:
27
27
 
28
28
  ```dotenv
29
- TRACELUMO_OTLP_ENDPOINT=http://127.0.0.1:911
29
+ TRACELUMO_OTLP_ENDPOINT=https://your-tracelumo.vercel.app
30
30
  TRACELUMO_AGENT_TOKEN=tl_agent_your_project_token
31
31
  ```
32
32
 
@@ -52,10 +52,50 @@ NODE_OPTIONS="--require @tracelumo/node/register" npm run dev
52
52
  | `TRACELUMO_OTLP_ENDPOINT` | — | API address. Remote endpoints must use HTTPS. |
53
53
  | `TRACELUMO_AGENT_TOKEN` | — | Project agent token. |
54
54
  | `TRACELUMO_SERVICE_NAME` | `name` in package.json | Service name shown in TraceLumo. |
55
- | `TRACELUMO_LOGS` | `true` | Set to `false` to stop sending console output as logs. |
55
+ | `TRACELUMO_LOGS` | `true` | Set to `false` to stop sending console output as logs and errors. |
56
+ | `TRACELUMO_METRICS` | `true` | Set to `false` to stop sending system metrics. |
57
+ | `TRACELUMO_METRICS_INTERVAL` | `15` | Seconds between metric samples (5 to 300). |
56
58
  | `TRACELUMO_ENABLED` | `true` | Set to `false` to turn the SDK off. |
57
59
 
58
- Logs are batched (up to 100 per request, sent every second) and capped at 8 KiB per line. The SDK never traces or logs its own requests. Use a single OpenTelemetry setup per app: if you already register a tracer, replace it with this package.
60
+ Logs are batched (up to 100 per request, sent every second) and capped at 8 KiB per line. Node.js process warnings are sent as `warn`, not `error`.
61
+
62
+ **Errors:** uncaught exceptions, unhandled promise rejections and any `Error` passed to `console.error` are sent to error tracking, where the same error from the same place is grouped into one issue. After an uncaught exception the SDK keeps Node's default behaviour — print the error and exit with code 1 — once the report is sent (at most 2 seconds), unless your app has its own `uncaughtException` handler. The SDK never traces or logs its own requests. Use a single OpenTelemetry setup per app: if you already register a tracer, replace it with this package.
63
+
64
+ **System metrics:** every 15 seconds the SDK records the process's CPU, memory (RSS, heap), event loop delay and
65
+ utilization, garbage collection pauses and open handles, plus the machine's CPU, memory, load average, disk and (on
66
+ Linux) network. It also records the host name, OS, CPU model, memory limit and Node.js version. Everything uses Node.js
67
+ built-ins inside your app: no agent or extra process. The first sample is taken 5 seconds after start and each sample is
68
+ sent right away, so serverless functions (Vercel, AWS Lambda), which pause between requests, report while they run. The
69
+ timers never keep your process alive.
70
+
71
+ **Containers (Docker, Kubernetes, ECS, Fargate):** inside a container, Node.js reports the host's CPUs and memory, so
72
+ the SDK also reads the container's own cgroup (v2, or v1 on older nodes). It reports:
73
+ - CPU against the container's limit, and CPU throttling.
74
+ - Working-set memory against the memory limit, out-of-memory kills, and the number of processes.
75
+
76
+ It also detects where it runs:
77
+ - Docker and Podman.
78
+ - Kubernetes on EKS (including EKS on Fargate), AKS and GKE, with namespace, pod and container ID.
79
+ - ECS on EC2 or Fargate, with cluster, task, task definition and availability zone, from the ECS task metadata endpoint.
80
+ - Cloud Run, Azure Container Apps and App Service.
81
+
82
+ Kubernetes doesn't tell a pod its cluster or node name. To show them, add these to the container's `env`:
83
+
84
+ ```yaml
85
+ env:
86
+ - name: CLUSTER_NAME
87
+ value: production
88
+ - name: NODE_NAME
89
+ valueFrom: { fieldRef: { fieldPath: spec.nodeName } }
90
+ ```
91
+
92
+ The SDK measures the process it runs in and its container. Containers that don't run the SDK, and cluster-wide
93
+ numbers such as every node's capacity, need a cluster-level collector, because nothing inside one container can
94
+ see them.
95
+
96
+ **Service name on serverless:** Vercel and similar platforms usually deploy without `package.json`, so the SDK names the
97
+ service after the deployment (for example `studio.naar.io`). Set `TRACELUMO_SERVICE_NAME` in the platform's environment
98
+ variables to choose the name yourself. They appear on the project's **Infrastructure** page, one row per running instance.
59
99
 
60
100
  ## Lifecycle hooks
61
101
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tracelumo/node",
3
- "version": "0.2.0",
4
- "description": "Connect Node.js apps to TraceLumo: automatic traces and logs",
3
+ "version": "0.5.0",
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",
7
7
  "bin": {
package/src/config.cjs CHANGED
@@ -13,9 +13,11 @@ function readIfPresent(path) {
13
13
  }
14
14
  }
15
15
 
16
- function appName(cwd) {
16
+ function appName(cwd, environment = process.env) {
17
17
  const manifest = readIfPresent(resolve(cwd, 'package.json'));
18
- if (!manifest) return 'node-app';
18
+ // Serverless platforms often deploy without package.json; name the service after the deployment instead.
19
+ const deployed = environment.VERCEL_PROJECT_PRODUCTION_URL || environment.AWS_LAMBDA_FUNCTION_NAME || environment.K_SERVICE;
20
+ if (!manifest) return typeof deployed === 'string' && deployed.trim() ? deployed.trim().slice(0, 128) : 'node-app';
19
21
  const name = JSON.parse(manifest)?.name;
20
22
  return typeof name === 'string' && name.trim() ? name : 'node-app';
21
23
  }
@@ -49,7 +51,7 @@ function configuration(options = {}, cwd = process.cwd(), environment = process.
49
51
 
50
52
  const endpoint = options.endpoint ?? setting('TRACELUMO_OTLP_ENDPOINT');
51
53
  const token = (options.token ?? setting('TRACELUMO_AGENT_TOKEN') ?? '').trim();
52
- const service = (options.serviceName ?? setting('TRACELUMO_SERVICE_NAME') ?? appName(cwd)).trim();
54
+ const service = (options.serviceName ?? setting('TRACELUMO_SERVICE_NAME') ?? appName(cwd, environment)).trim();
53
55
  if (!endpoint || !token) throw new Error('Set TRACELUMO_OTLP_ENDPOINT and TRACELUMO_AGENT_TOKEN.');
54
56
  if (!/^tl_agent_[A-Za-z0-9_-]{23,119}$/.test(token)) {
55
57
  throw new Error('TRACELUMO_AGENT_TOKEN must be an active project agent token.');
@@ -59,7 +61,15 @@ function configuration(options = {}, cwd = process.cwd(), environment = process.
59
61
  }
60
62
  const url = traceURL(endpoint);
61
63
  const logs = options.logs ?? setting('TRACELUMO_LOGS') !== 'false';
62
- return { url, logsUrl: url.replace(/\/v1\/traces$/, '/v1/ingest/logs'), token, service, logs };
64
+ const metrics = options.metrics ?? setting('TRACELUMO_METRICS') !== 'false';
65
+ // Seconds between metric samples: 15 by default, 5 to 300.
66
+ const requested = Number(options.metricsInterval ?? setting('TRACELUMO_METRICS_INTERVAL') ?? 15);
67
+ const metricsInterval = Number.isFinite(requested) ? Math.min(300, Math.max(5, Math.round(requested))) : 15;
68
+ const base = url.replace(/\/v1\/traces$/, '');
69
+ return {
70
+ url, logsUrl: `${base}/v1/ingest/logs`, errorsUrl: `${base}/v1/ingest/errors`, metricsUrl: `${base}/v1/ingest/metrics`,
71
+ token, service, logs, metrics, metricsInterval,
72
+ };
63
73
  }
64
74
 
65
75
  module.exports = { configuration, traceURL };
@@ -0,0 +1,243 @@
1
+ 'use strict';
2
+
3
+ const { existsSync, readFileSync } = require('node:fs');
4
+ const http = require('node:http');
5
+ const { join } = require('node:path');
6
+
7
+ /**
8
+ * Container awareness, from inside the container with no agent:
9
+ * - cgroup v2 (and v1 on older nodes) for the container's own CPU and memory use, its limits, CPU throttling,
10
+ * out-of-memory kills and process count. Inside a container, Node's `os` module reports the host, so these are
11
+ * the numbers that matter.
12
+ * - Where it runs: Docker, Kubernetes (EKS, AKS, GKE, EKS on Fargate), ECS (EC2 or Fargate), Cloud Run, Azure
13
+ * Container Apps and App Service, with cluster, namespace, pod, node, task and container ID when available.
14
+ */
15
+
16
+ const read = (path) => {
17
+ try {
18
+ return readFileSync(path, 'utf8').trim();
19
+ } catch {
20
+ return null;
21
+ }
22
+ };
23
+ const int = (text) => {
24
+ const value = Number(text);
25
+ return Number.isFinite(value) ? value : null;
26
+ };
27
+ /** `key value` lines, as in cpu.stat and memory.stat. */
28
+ function stats(text) {
29
+ const result = {};
30
+ for (const line of (text ?? '').split('\n')) {
31
+ const [key, value] = line.trim().split(/\s+/);
32
+ if (key && value !== undefined && Number.isFinite(Number(value))) result[key] = Number(value);
33
+ }
34
+ return result;
35
+ }
36
+ // Anything at or above this is "no limit" in cgroup v1.
37
+ const UNLIMITED = 2 ** 60;
38
+
39
+ /**
40
+ * Finds this process's cgroup. Returns a reader for the raw counters, or null when there is none (not Linux).
41
+ * `root` and `self` can point at a copy of /sys/fs/cgroup and /proc/self/cgroup for tests.
42
+ */
43
+ function createCgroup({ root = '/sys/fs/cgroup', self = '/proc/self/cgroup' } = {}) {
44
+ const lines = (read(self) ?? '').split('\n').filter(Boolean).map((line) => {
45
+ const [, controllers = '', path = '/'] = line.match(/^\d+:([^:]*):(.*)$/) ?? [];
46
+ return { controllers: controllers.split(','), path };
47
+ });
48
+ if (lines.length === 0) return null;
49
+ // The process's own directory when visible, otherwise the mount root (with a cgroup namespace they are the same).
50
+ const pick = (base, path, probe) => [join(base, path), base].find((dir) => existsSync(join(dir, probe))) ?? null;
51
+
52
+ const unified = lines.find((line) => line.controllers.length === 1 && line.controllers[0] === '');
53
+ if (unified && existsSync(join(root, 'cgroup.controllers'))) {
54
+ const dir = pick(root, unified.path, 'cpu.stat');
55
+ if (!dir) return null;
56
+ return {
57
+ version: 2,
58
+ read() {
59
+ const cpu = stats(read(join(dir, 'cpu.stat')));
60
+ const [quota, period] = (read(join(dir, 'cpu.max')) ?? 'max').split(/\s+/);
61
+ const memoryMax = read(join(dir, 'memory.max'));
62
+ const memory = stats(read(join(dir, 'memory.stat')));
63
+ const current = int(read(join(dir, 'memory.current')));
64
+ return {
65
+ cpuUsec: cpu.usage_usec ?? null,
66
+ periods: cpu.nr_periods ?? null,
67
+ throttled: cpu.nr_throttled ?? null,
68
+ throttledUsec: cpu.throttled_usec ?? null,
69
+ cpuLimit: quota && quota !== 'max' && int(quota) > 0 && int(period) > 0 ? int(quota) / int(period) : null,
70
+ memory: current,
71
+ // Working set, as Kubernetes counts it: usage minus inactive file cache, which the kernel can reclaim.
72
+ workingSet: current === null ? null : Math.max(0, current - (memory.inactive_file ?? 0)),
73
+ memoryLimit: memoryMax && memoryMax !== 'max' ? int(memoryMax) : null,
74
+ oomKills: stats(read(join(dir, 'memory.events'))).oom_kill ?? null,
75
+ pids: int(read(join(dir, 'pids.current'))),
76
+ };
77
+ },
78
+ };
79
+ }
80
+
81
+ const controller = (name) => lines.find((line) => line.controllers.includes(name));
82
+ const dirFor = (name, mounts, probe) => {
83
+ const line = controller(name);
84
+ if (!line) return null;
85
+ for (const mount of mounts) {
86
+ const dir = pick(join(root, mount), line.path, probe);
87
+ if (dir) return dir;
88
+ }
89
+ return null;
90
+ };
91
+ const cpuDir = dirFor('cpu', ['cpu,cpuacct', 'cpu', 'cpuacct,cpu'], 'cpu.cfs_period_us');
92
+ const acctDir = dirFor('cpuacct', ['cpu,cpuacct', 'cpuacct', 'cpuacct,cpu'], 'cpuacct.usage');
93
+ const memDir = dirFor('memory', ['memory'], 'memory.usage_in_bytes');
94
+ const pidsDir = dirFor('pids', ['pids'], 'pids.current');
95
+ if (!cpuDir && !acctDir && !memDir) return null;
96
+ return {
97
+ version: 1,
98
+ read() {
99
+ const cpu = cpuDir ? stats(read(join(cpuDir, 'cpu.stat'))) : {};
100
+ const quota = cpuDir ? int(read(join(cpuDir, 'cpu.cfs_quota_us'))) : null;
101
+ const period = cpuDir ? int(read(join(cpuDir, 'cpu.cfs_period_us'))) : null;
102
+ const usageNs = acctDir ? int(read(join(acctDir, 'cpuacct.usage'))) : null;
103
+ const usage = memDir ? int(read(join(memDir, 'memory.usage_in_bytes'))) : null;
104
+ const limit = memDir ? int(read(join(memDir, 'memory.limit_in_bytes'))) : null;
105
+ const memory = memDir ? stats(read(join(memDir, 'memory.stat'))) : {};
106
+ const oom = memDir ? stats(read(join(memDir, 'memory.oom_control'))) : {};
107
+ return {
108
+ cpuUsec: usageNs === null ? null : usageNs / 1000,
109
+ periods: cpu.nr_periods ?? null,
110
+ throttled: cpu.nr_throttled ?? null,
111
+ throttledUsec: cpu.throttled_time === undefined ? null : cpu.throttled_time / 1000,
112
+ cpuLimit: quota !== null && quota > 0 && period ? quota / period : null,
113
+ memory: usage,
114
+ workingSet: usage === null ? null : Math.max(0, usage - (memory.total_inactive_file ?? memory.inactive_file ?? 0)),
115
+ memoryLimit: limit !== null && limit < UNLIMITED ? limit : null,
116
+ oomKills: oom.oom_kill ?? null,
117
+ pids: pidsDir ? int(read(join(pidsDir, 'pids.current'))) : null,
118
+ };
119
+ },
120
+ };
121
+ }
122
+
123
+ /** Container metrics for one interval, from two cgroup readings `elapsedMs` apart. */
124
+ function containerValues(previous, current, elapsedMs, hostCpus) {
125
+ if (!current) return {};
126
+ const values = {};
127
+ const limit = current.cpuLimit ?? hostCpus;
128
+ if (previous?.cpuUsec != null && current.cpuUsec != null) {
129
+ const cores = Math.max(0, current.cpuUsec - previous.cpuUsec) / 1000 / elapsedMs;
130
+ values.container_cpu_cores = Number(cores.toFixed(3));
131
+ if (limit) values.container_cpu = Number(Math.min(100, (cores / limit) * 100).toFixed(2));
132
+ }
133
+ if (previous?.periods != null && current.periods != null && current.throttled != null && previous.throttled != null) {
134
+ const periods = current.periods - previous.periods;
135
+ values.cpu_throttled = periods > 0 ? Number((((current.throttled - previous.throttled) / periods) * 100).toFixed(2)) : 0;
136
+ }
137
+ if (previous?.throttledUsec != null && current.throttledUsec != null) {
138
+ values.cpu_throttled_time = Number((Math.max(0, current.throttledUsec - previous.throttledUsec) / 1000).toFixed(1));
139
+ }
140
+ if (current.workingSet != null) values.container_memory = current.workingSet;
141
+ if (current.memoryLimit != null && current.workingSet != null) {
142
+ values.container_memory_pct = Number(((current.workingSet / current.memoryLimit) * 100).toFixed(2));
143
+ }
144
+ if (previous?.oomKills != null && current.oomKills != null) values.oom_kills = Math.max(0, current.oomKills - previous.oomKills);
145
+ if (current.pids != null) values.pids = current.pids;
146
+ return values;
147
+ }
148
+
149
+ /** The container ID (12 characters), from the cgroup path or the mount table. */
150
+ function containerId({ self = '/proc/self/cgroup', mountinfo = '/proc/self/mountinfo' } = {}) {
151
+ const fromCgroup = (read(self) ?? '').match(/[0-9a-f]{64}/);
152
+ if (fromCgroup) return fromCgroup[0].slice(0, 12);
153
+ // cgroup v2 with a namespace hides the path; Docker and containerd still mount /etc/hostname from the container's folder.
154
+ const fromMounts = (read(mountinfo) ?? '').match(/\/(?:containers|sandboxes)\/([0-9a-f]{64})\//);
155
+ return fromMounts ? fromMounts[1].slice(0, 12) : undefined;
156
+ }
157
+
158
+ /** Which cloud the machine belongs to, from its firmware (readable inside most containers). */
159
+ function cloudVendor(dmi = '/sys/class/dmi/id') {
160
+ const text = `${read(join(dmi, 'sys_vendor')) ?? ''} ${read(join(dmi, 'board_vendor')) ?? ''} ${read(join(dmi, 'product_name')) ?? ''}`.toLowerCase();
161
+ if (/amazon|ec2/.test(text)) return 'aws';
162
+ if (/microsoft/.test(text)) return 'azure';
163
+ if (/google/.test(text)) return 'gcp';
164
+ return undefined;
165
+ }
166
+
167
+ /** Where this process runs, with orchestration details. Synchronous; ECS details are added by `ecsDetails`. */
168
+ function platform({ env = process.env, files = {} } = {}) {
169
+ const exists = (path) => existsSync(files[path] ?? path);
170
+ const text = (path) => read(files[path] ?? path);
171
+ const cloud = env.AWS_REGION || env.AWS_DEFAULT_REGION || env.AWS_EXECUTION_ENV ? 'aws'
172
+ : cloudVendor(files.dmi);
173
+ const region = env.AWS_REGION || env.AWS_DEFAULT_REGION || env.REGION_NAME || env.AZURE_REGION || undefined;
174
+ const id = containerId({ self: files['/proc/self/cgroup'], mountinfo: files['/proc/self/mountinfo'] });
175
+
176
+ if (env.VERCEL) return { runtime: 'vercel', cloud: 'vercel', region: env.VERCEL_REGION };
177
+ if (env.AWS_LAMBDA_FUNCTION_NAME) return { runtime: 'aws-lambda', cloud: 'aws', region };
178
+ if (env.ECS_CONTAINER_METADATA_URI_V4 || env.ECS_CONTAINER_METADATA_URI) {
179
+ // Fargate or EC2 is known once the task metadata answers; Fargate sets AWS_EXECUTION_ENV.
180
+ const fargate = env.AWS_EXECUTION_ENV === 'AWS_ECS_FARGATE';
181
+ return { runtime: fargate ? 'ecs-fargate' : 'ecs', cloud: 'aws', region, container_id: id };
182
+ }
183
+ if (env.KUBERNETES_SERVICE_HOST) {
184
+ const node = env.NODE_NAME || env.K8S_NODE_NAME || env.KUBE_NODE_NAME || env.MY_NODE_NAME || undefined;
185
+ const provider = cloud === 'aws' ? 'eks' : cloud === 'azure' ? 'aks' : cloud === 'gcp' ? 'gke' : 'kubernetes';
186
+ return {
187
+ runtime: provider === 'eks' && node?.startsWith('fargate-') ? 'eks-fargate' : provider,
188
+ cloud, region,
189
+ cluster: env.CLUSTER_NAME || env.TRACELUMO_CLUSTER || env.EKS_CLUSTER_NAME || env.AKS_CLUSTER_NAME || undefined,
190
+ namespace: env.POD_NAMESPACE || text('/var/run/secrets/kubernetes.io/serviceaccount/namespace') || undefined,
191
+ pod: env.POD_NAME || env.HOSTNAME || undefined,
192
+ // "node" is the Node.js version, so the Kubernetes node is k8s_node.
193
+ k8s_node: node,
194
+ container_id: id,
195
+ };
196
+ }
197
+ if (env.K_SERVICE) return { runtime: 'cloud-run', cloud: 'gcp', service_revision: env.K_REVISION, container_id: id };
198
+ if (env.CONTAINER_APP_NAME) return { runtime: 'azure-container-apps', cloud: 'azure', region, container_id: id };
199
+ if (env.WEBSITE_SITE_NAME) return { runtime: 'azure-app-service', cloud: 'azure', region: env.REGION_NAME, container_id: id };
200
+ if (exists('/run/.containerenv')) return { runtime: 'podman', cloud, region, container_id: id };
201
+ if (exists('/.dockerenv') || id) return { runtime: 'docker', cloud, region, container_id: id };
202
+ return { runtime: 'host', cloud, region };
203
+ }
204
+
205
+ /** ECS task details from the task metadata endpoint (local to the task, answered in milliseconds). */
206
+ function ecsDetails(env = process.env, suppress = (fn) => fn()) {
207
+ const base = env.ECS_CONTAINER_METADATA_URI_V4 || env.ECS_CONTAINER_METADATA_URI;
208
+ if (!base) return Promise.resolve(null);
209
+ const get = (url) => new Promise((resolve) => {
210
+ suppress(() => {
211
+ const request = http.get(url, { timeout: 2000 }, (response) => {
212
+ let body = '';
213
+ response.setEncoding('utf8');
214
+ response.on('data', (chunk) => { body += chunk; });
215
+ response.on('end', () => {
216
+ try {
217
+ resolve(response.statusCode === 200 ? JSON.parse(body) : null);
218
+ } catch {
219
+ resolve(null);
220
+ }
221
+ });
222
+ });
223
+ request.on('timeout', () => request.destroy());
224
+ request.on('error', () => resolve(null));
225
+ });
226
+ });
227
+ return Promise.all([get(`${base}/task`), get(base)]).then(([task, container]) => {
228
+ if (!task) return null;
229
+ const arn = String(task.TaskARN ?? '');
230
+ const cluster = String(task.Cluster ?? '').split('/').pop();
231
+ return {
232
+ runtime: task.LaunchType === 'FARGATE' ? 'ecs-fargate' : 'ecs',
233
+ cluster: cluster || undefined,
234
+ task: arn.split('/').pop() || undefined,
235
+ task_family: task.Family ? `${task.Family}:${task.Revision ?? ''}`.replace(/:$/, '') : undefined,
236
+ zone: task.AvailabilityZone || undefined,
237
+ container: container?.Name || undefined,
238
+ container_id: container?.DockerId ? String(container.DockerId).slice(0, 12) : undefined,
239
+ };
240
+ });
241
+ }
242
+
243
+ module.exports = { createCgroup, containerValues, containerId, cloudVendor, platform, ecsDetails, stats };
package/src/index.cjs CHANGED
@@ -61,24 +61,35 @@ function start(options) {
61
61
  syncBuiltinESMExports();
62
62
 
63
63
  let logs = null;
64
+ let errors = null;
64
65
  let restoreConsole = () => {};
66
+ let restoreCrashes = () => {};
65
67
  if (config.logs) {
66
- const { captureConsole, createLogShipper } = require('./logs.cjs');
67
- logs = createLogShipper(config, { suppress: (fn) => context.with(suppressTracing(context.active()), fn) });
68
- restoreConsole = captureConsole(logs, () => {
68
+ const { captureConsole, captureCrashes, createErrorShipper, createLogShipper } = require('./logs.cjs');
69
+ const options = { suppress: (fn) => context.with(suppressTracing(context.active()), fn) };
70
+ const activeTrace = () => {
69
71
  const span = trace.getSpanContext(context.active());
70
72
  return span && isSpanContextValid(span) ? span.traceId : undefined;
71
- });
73
+ };
74
+ logs = createLogShipper(config, options);
75
+ errors = createErrorShipper(config, options);
76
+ restoreConsole = captureConsole(logs, activeTrace, errors);
77
+ restoreCrashes = captureCrashes(errors, activeTrace);
72
78
  // Announce the service as soon as it starts, so the project shows as connected before any request.
73
79
  logs.add('info', `TraceLumo connected: ${config.service} started (Node.js ${process.version}, pid ${process.pid})`);
74
80
  setImmediate(logs.flush);
75
81
  }
76
- globalThis[instance] = { provider, stopInstrumentation, logs, restoreConsole };
82
+ let metrics = null;
83
+ if (config.metrics) {
84
+ const { startMetrics } = require('./metrics.cjs');
85
+ metrics = startMetrics(config, { suppress: (fn) => context.with(suppressTracing(context.active()), fn) });
86
+ }
87
+ globalThis[instance] = { provider, stopInstrumentation, logs, errors, metrics, restoreConsole, restoreCrashes };
77
88
  }
78
89
 
79
90
  async function flush() {
80
91
  const sdk = globalThis[instance];
81
- if (sdk) await Promise.all([sdk.stopping ?? sdk.provider.forceFlush(), sdk.logs?.flush()]);
92
+ if (sdk) await Promise.all([sdk.stopping ?? sdk.provider.forceFlush(), sdk.logs?.flush(), sdk.errors?.flush(), sdk.metrics?.flush()]);
82
93
  }
83
94
 
84
95
  async function shutdown() {
@@ -87,8 +98,9 @@ async function shutdown() {
87
98
  if (!sdk.stopping) {
88
99
  sdk.stopInstrumentation();
89
100
  sdk.restoreConsole();
101
+ sdk.restoreCrashes();
90
102
  syncBuiltinESMExports();
91
- sdk.stopping = Promise.all([sdk.provider.shutdown(), sdk.logs?.close()]).then(() => {});
103
+ sdk.stopping = Promise.all([sdk.provider.shutdown(), sdk.logs?.close(), sdk.errors?.close(), sdk.metrics?.close()]).then(() => {});
92
104
  }
93
105
  await sdk.stopping;
94
106
  }
package/src/index.d.cts CHANGED
@@ -5,6 +5,10 @@ export interface TracingOptions {
5
5
  enabled?: boolean;
6
6
  /** Send console output as logs. Defaults to true; TRACELUMO_LOGS=false turns it off. */
7
7
  logs?: boolean;
8
+ /** Send system and runtime metrics (CPU, memory, event loop, GC, disk). Defaults to true; TRACELUMO_METRICS=false turns it off. */
9
+ metrics?: boolean;
10
+ /** Seconds between metric samples, 5 to 300. Defaults to 15 (TRACELUMO_METRICS_INTERVAL). */
11
+ metricsInterval?: number;
8
12
  }
9
13
 
10
14
  export function start(options?: TracingOptions): void;
package/src/init.cjs CHANGED
@@ -20,7 +20,7 @@ const usage = `Connect this app to TraceLumo.
20
20
  Usage: npx @tracelumo/node init --endpoint <url> --token <tl_agent_…> [options]
21
21
 
22
22
  Options:
23
- --endpoint <url> TraceLumo API address, e.g. http://127.0.0.1:911
23
+ --endpoint <url> TraceLumo address, e.g. https://tracelumo.vercel.app
24
24
  --token <token> Project agent token from the Agent tokens page
25
25
  --service <name> Service name shown in TraceLumo (default: package.json name)
26
26
  --package <spec> Install the SDK from this spec instead of npm (e.g. a local .tgz)
package/src/logs.cjs CHANGED
@@ -3,7 +3,7 @@
3
3
  const { randomUUID } = require('node:crypto');
4
4
  const http = require('node:http');
5
5
  const https = require('node:https');
6
- const { format } = require('node:util');
6
+ const { format, inspect } = require('node:util');
7
7
  const { gzipSync } = require('node:zlib');
8
8
 
9
9
  // Limits match the TraceLumo log ingest API.
@@ -20,12 +20,12 @@ function clip(text) {
20
20
  }
21
21
 
22
22
  /**
23
- * Batches log events and sends them to the ingest API. Requests run with tracing suppressed so
24
- * the SDK never traces or logs its own traffic. Transient failures are retried on the next tick;
25
- * a rejected token is reported once and its events are dropped.
23
+ * A batching queue that sends JSON events to one ingest API. Requests run with tracing suppressed so the SDK
24
+ * never traces or logs its own traffic. Transient failures are retried on the next tick; a rejected token is
25
+ * reported once and its events are dropped.
26
26
  */
27
- function createLogShipper({ logsUrl, token, service }, { suppress }) {
28
- const target = new URL(logsUrl);
27
+ function createQueue({ url, token, maxBatch, kind, interval = INTERVAL }, { suppress }) {
28
+ const target = new URL(url);
29
29
  const transport = target.protocol === 'https:' ? https : http;
30
30
  let queue = [];
31
31
  let inflight = null;
@@ -58,7 +58,7 @@ function createLogShipper({ logsUrl, token, service }, { suppress }) {
58
58
 
59
59
  async function drain() {
60
60
  while (queue.length) {
61
- const batch = queue.splice(0, MAX_BATCH);
61
+ const batch = queue.splice(0, maxBatch);
62
62
  const status = await post(batch);
63
63
  if (status >= 200 && status < 300) continue;
64
64
  if (status === 0 || status === 429 || status >= 500) {
@@ -67,7 +67,7 @@ function createLogShipper({ logsUrl, token, service }, { suppress }) {
67
67
  }
68
68
  if (!warned) {
69
69
  warned = true;
70
- process.emitWarning(`TraceLumo rejected a log batch (HTTP ${status}). Check TRACELUMO_AGENT_TOKEN.`, { code: 'TRACELUMO_LOGS' });
70
+ process.emitWarning(`TraceLumo rejected a ${kind} batch (HTTP ${status}). Check TRACELUMO_AGENT_TOKEN.`, { code: 'TRACELUMO_LOGS' });
71
71
  }
72
72
  }
73
73
  }
@@ -77,20 +77,18 @@ function createLogShipper({ logsUrl, token, service }, { suppress }) {
77
77
  return inflight;
78
78
  }
79
79
 
80
- function add(severity, body, traceId) {
80
+ function push(event) {
81
81
  if (queue.length >= MAX_QUEUE) queue.shift();
82
- const event = { event_id: randomUUID(), timestamp: new Date().toISOString(), service, severity, body: clip(body) };
83
- if (traceId) event.trace_id = traceId;
84
82
  queue.push(event);
85
- if (queue.length >= MAX_BATCH) setImmediate(flush);
83
+ if (queue.length >= maxBatch) setImmediate(flush);
86
84
  }
87
85
 
88
- const timer = setInterval(flush, INTERVAL);
86
+ const timer = setInterval(flush, interval);
89
87
  timer.unref();
90
88
  process.once('beforeExit', () => { void flush(); });
91
89
 
92
90
  return {
93
- add,
91
+ push,
94
92
  flush,
95
93
  async close() {
96
94
  clearInterval(timer);
@@ -99,8 +97,56 @@ function createLogShipper({ logsUrl, token, service }, { suppress }) {
99
97
  };
100
98
  }
101
99
 
100
+ /** Sends console output as log events. */
101
+ function createLogShipper({ logsUrl, token, service }, options) {
102
+ const queue = createQueue({ url: logsUrl, token, maxBatch: MAX_BATCH, kind: 'log' }, options);
103
+ return {
104
+ add(severity, body, traceId) {
105
+ const event = { event_id: randomUUID(), timestamp: new Date().toISOString(), service, severity, body: clip(body) };
106
+ if (traceId) event.trace_id = traceId;
107
+ queue.push(event);
108
+ },
109
+ flush: queue.flush,
110
+ close: queue.close,
111
+ };
112
+ }
113
+
114
+ /** Sends exceptions to error tracking, where they are grouped into issues. */
115
+ function createErrorShipper({ errorsUrl, token, service }, options) {
116
+ const queue = createQueue({ url: errorsUrl, token, maxBatch: 50, kind: 'error' }, options);
117
+ return {
118
+ add(error, { handled, traceId }) {
119
+ const isError = error instanceof Error;
120
+ const name = isError ? (error.name || error.constructor?.name || 'Error') : 'UnhandledRejection';
121
+ const event = {
122
+ event_id: randomUUID(),
123
+ timestamp: new Date().toISOString(),
124
+ service,
125
+ type: String(name).slice(0, 200),
126
+ message: String(isError ? error.message : inspect(error)).slice(0, 4000),
127
+ stack: isError && typeof error.stack === 'string' ? error.stack.slice(0, 32000) : '',
128
+ handled,
129
+ };
130
+ if (!event.message && !event.type) return;
131
+ if (traceId) event.trace_id = traceId;
132
+ queue.push(event);
133
+ },
134
+ flush: queue.flush,
135
+ close: queue.close,
136
+ };
137
+ }
138
+
139
+ // Node.js prints its own process warnings through console.error, e.g.
140
+ // "(node:7) ExperimentalWarning: …" or "(node:7) [DEP0040] DeprecationWarning: …". They are warnings, not errors.
141
+ const NODE_WARNING = /^\(node:\d+\)\s+(?:\[[A-Z0-9]+\]\s+)?[A-Za-z]*Warning:/;
142
+
143
+ /** The severity for a console call, correcting Node.js process warnings that arrive through console.error. */
144
+ function severityOf(severity, body) {
145
+ return severity === 'error' && NODE_WARNING.test(body) ? 'warn' : severity;
146
+ }
147
+
102
148
  /** Mirrors console output to the shipper, tagged with the active trace. Returns a restore function. */
103
- function captureConsole(shipper, traceId) {
149
+ function captureConsole(shipper, traceId, errors) {
104
150
  const originals = {};
105
151
  let busy = false;
106
152
  for (const [method, severity] of Object.entries(METHODS)) {
@@ -114,7 +160,13 @@ function captureConsole(shipper, traceId) {
114
160
  try {
115
161
  let body = format(...args).replace(ANSI, '').trim();
116
162
  if (method === 'trace') body = `Trace: ${body}`;
117
- if (body) shipper.add(severity, body, traceId());
163
+ const trace = traceId();
164
+ if (body) shipper.add(severityOf(severity, body), body, trace);
165
+ // An Error passed to console.error (how frameworks such as Next.js report request errors) is also an error event.
166
+ if (errors && method === 'error') {
167
+ const error = args.find((arg) => arg instanceof Error);
168
+ if (error) errors.add(error, { handled: true, traceId: trace });
169
+ }
118
170
  } catch {
119
171
  // Logging must never break the application.
120
172
  } finally {
@@ -127,4 +179,21 @@ function captureConsole(shipper, traceId) {
127
179
  };
128
180
  }
129
181
 
130
- module.exports = { createLogShipper, captureConsole, clip };
182
+ /**
183
+ * Reports uncaught exceptions and unhandled rejections. When TraceLumo's handler is the only one, it keeps
184
+ * Node's default behaviour — print the error and exit with code 1 — after giving the report up to 2 seconds.
185
+ */
186
+ function captureCrashes(errors, traceId) {
187
+ async function onCrash(error, origin) {
188
+ errors.add(error, { handled: false, traceId: traceId() });
189
+ if (process.listenerCount('uncaughtException') > 1) return;
190
+ await Promise.race([errors.flush(), new Promise((resolve) => setTimeout(resolve, 2000).unref())]);
191
+ const label = origin === 'unhandledRejection' ? 'Unhandled rejection' : 'Uncaught exception';
192
+ process.stderr.write(`${label}: ${error instanceof Error ? error.stack ?? error.message : inspect(error)}\n`);
193
+ process.exit(1);
194
+ }
195
+ process.on('uncaughtException', onCrash);
196
+ return () => process.off('uncaughtException', onCrash);
197
+ }
198
+
199
+ module.exports = { createQueue, createLogShipper, createErrorShipper, captureConsole, captureCrashes, clip, severityOf };
@@ -0,0 +1,207 @@
1
+ 'use strict';
2
+
3
+ const { randomUUID } = require('node:crypto');
4
+ const { readFileSync, statfsSync } = require('node:fs');
5
+ const os = require('node:os');
6
+ const { monitorEventLoopDelay, performance, PerformanceObserver } = require('node:perf_hooks');
7
+ const { containerValues, createCgroup, ecsDetails, platform } = require('./container.cjs');
8
+ const { createQueue } = require('./logs.cjs');
9
+
10
+ /**
11
+ * System and runtime metrics, collected inside the application with Node.js built-ins only — no agent, no extra
12
+ * process, no dependencies. Every interval it records one sample: process CPU and memory, heap, event loop delay
13
+ * and utilization, garbage collection, open handles, and the machine's CPU, memory, load, disk and (on Linux)
14
+ * network. Each sample is sent within a second, so serverless functions (which pause between requests) report
15
+ * while they run. The timers never keep the process alive.
16
+ */
17
+
18
+ // The first sample comes soon after start, so short-lived and serverless processes report too.
19
+ const FIRST_SAMPLE = 5_000;
20
+ const round = (value, digits = 2) => (Number.isFinite(value) ? Number(value.toFixed(digits)) : undefined);
21
+ const ms = (ns) => ns / 1e6;
22
+
23
+ /** Where the process runs (see container.cjs), so the dashboard can explain what the machine numbers mean. */
24
+ function runtime(env = process.env) {
25
+ return platform({ env }).runtime;
26
+ }
27
+
28
+ // Platforms where the process has its own cgroup with its own limits.
29
+ const NOT_CONTAINERS = new Set(['host', 'vercel', 'aws-lambda']);
30
+ const defined = (object) => Object.fromEntries(Object.entries(object).filter(([, value]) => value !== undefined && value !== null && value !== ''));
31
+
32
+ /** Facts about the machine and process that rarely change. */
33
+ function hostInfo(service) {
34
+ const cpus = os.cpus();
35
+ const limit = typeof process.constrainedMemory === 'function' ? process.constrainedMemory() : 0;
36
+ return {
37
+ hostname: os.hostname().slice(0, 128),
38
+ service,
39
+ os: `${os.type()} ${os.release()}`.slice(0, 128),
40
+ platform: process.platform,
41
+ arch: process.arch,
42
+ cpu_model: (cpus[0]?.model ?? '').trim().slice(0, 128),
43
+ cpus: os.availableParallelism?.() ?? cpus.length,
44
+ memory_total: os.totalmem(),
45
+ // A container or cgroup memory limit, when one is set below the machine's memory.
46
+ ...(limit > 0 && limit < os.totalmem() ? { memory_limit: limit } : {}),
47
+ node: process.version,
48
+ pid: process.pid,
49
+ ...defined(platform()),
50
+ started: new Date(Date.now() - process.uptime() * 1000).toISOString(),
51
+ };
52
+ }
53
+
54
+ function cpuTimes() {
55
+ let idle = 0;
56
+ let total = 0;
57
+ for (const cpu of os.cpus()) {
58
+ const { user, nice, sys, irq, idle: free } = cpu.times;
59
+ idle += free;
60
+ total += user + nice + sys + irq + free;
61
+ }
62
+ return { idle, total };
63
+ }
64
+
65
+ /** Network bytes received and sent by all non-loopback interfaces (Linux only). */
66
+ function networkBytes() {
67
+ if (process.platform !== 'linux') return null;
68
+ try {
69
+ let rx = 0;
70
+ let tx = 0;
71
+ for (const line of readFileSync('/proc/net/dev', 'utf8').split('\n').slice(2)) {
72
+ const [name, data] = line.split(':');
73
+ if (!data || name.trim() === 'lo') continue;
74
+ const fields = data.trim().split(/\s+/).map(Number);
75
+ rx += fields[0];
76
+ tx += fields[8];
77
+ }
78
+ return { rx, tx };
79
+ } catch {
80
+ return null;
81
+ }
82
+ }
83
+
84
+ function disk(path) {
85
+ try {
86
+ const stats = statfsSync(path);
87
+ const total = stats.blocks * stats.bsize;
88
+ return total > 0 ? { total, free: stats.bavail * stats.bsize } : null;
89
+ } catch {
90
+ return null;
91
+ }
92
+ }
93
+
94
+ /** Measures one process. `sample()` returns the values since the previous call. */
95
+ function createSampler({ service, cwd = process.cwd(), cgroup } = {}) {
96
+ const info = hostInfo(service);
97
+ // Inside a container, the container's own CPU and memory (and their limits) come from its cgroup.
98
+ const group = cgroup !== undefined ? cgroup : NOT_CONTAINERS.has(info.runtime) ? null : createCgroup();
99
+ let lastGroup = group?.read() ?? null;
100
+ const limits = (reading) => {
101
+ if (reading?.cpuLimit) info.cpu_limit = Number(reading.cpuLimit.toFixed(3));
102
+ if (reading?.memoryLimit) info.memory_limit = reading.memoryLimit;
103
+ };
104
+ limits(lastGroup);
105
+ const delay = monitorEventLoopDelay({ resolution: 20 });
106
+ delay.enable();
107
+ let gcCount = 0;
108
+ let gcPause = 0;
109
+ let gc = null;
110
+ try {
111
+ gc = new PerformanceObserver((list) => {
112
+ for (const entry of list.getEntries()) {
113
+ gcCount += 1;
114
+ gcPause += entry.duration;
115
+ }
116
+ });
117
+ gc.observe({ entryTypes: ['gc'] });
118
+ } catch {
119
+ gc = null;
120
+ }
121
+ let last = { at: performance.now(), cpu: process.cpuUsage(), system: cpuTimes(), elu: performance.eventLoopUtilization(), net: networkBytes() };
122
+
123
+ function sample() {
124
+ const now = { at: performance.now(), cpu: process.cpuUsage(), system: cpuTimes(), elu: performance.eventLoopUtilization(), net: networkBytes() };
125
+ const elapsedMs = Math.max(1, now.at - last.at);
126
+ const cores = info.cpus || 1;
127
+ const processCpu = ((now.cpu.user - last.cpu.user + now.cpu.system - last.cpu.system) / 1000 / elapsedMs / cores) * 100;
128
+ const systemTotal = now.system.total - last.system.total;
129
+ const systemCpu = systemTotal > 0 ? (1 - (now.system.idle - last.system.idle) / systemTotal) * 100 : undefined;
130
+ const elu = performance.eventLoopUtilization(now.elu, last.elu).utilization * 100;
131
+ const memory = process.memoryUsage();
132
+ const free = os.freemem();
133
+ const [load1, load5, load15] = os.loadavg();
134
+ const space = disk(cwd);
135
+ const values = {
136
+ process_cpu: round(Math.min(100, Math.max(0, processCpu))),
137
+ system_cpu: round(systemCpu),
138
+ rss: memory.rss,
139
+ heap_used: memory.heapUsed,
140
+ heap_total: memory.heapTotal,
141
+ external: memory.external,
142
+ system_memory_used: os.totalmem() - free,
143
+ event_loop_p50: round(ms(delay.percentile(50))),
144
+ event_loop_p99: round(ms(delay.percentile(99))),
145
+ event_loop_max: round(ms(delay.max)),
146
+ event_loop_utilization: round(elu),
147
+ gc_count: gcCount,
148
+ gc_pause: round(gcPause),
149
+ handles: typeof process.getActiveResourcesInfo === 'function' ? process.getActiveResourcesInfo().length : undefined,
150
+ ...(info.platform === 'win32' ? {} : { load_1: round(load1), load_5: round(load5), load_15: round(load15) }),
151
+ ...(space ? { disk_total: space.total, disk_used: space.total - space.free } : {}),
152
+ uptime: Math.round(process.uptime()),
153
+ };
154
+ if (group) {
155
+ const reading = group.read();
156
+ Object.assign(values, containerValues(lastGroup, reading, elapsedMs, info.cpus));
157
+ limits(reading);
158
+ lastGroup = reading;
159
+ }
160
+ if (now.net && last.net) {
161
+ values.network_rx = round(Math.max(0, now.net.rx - last.net.rx) / (elapsedMs / 1000), 0);
162
+ values.network_tx = round(Math.max(0, now.net.tx - last.net.tx) / (elapsedMs / 1000), 0);
163
+ }
164
+ for (const key of Object.keys(values)) if (values[key] === undefined) delete values[key];
165
+ delay.reset();
166
+ gcCount = 0;
167
+ gcPause = 0;
168
+ last = now;
169
+ return values;
170
+ }
171
+
172
+ return { info, sample, stop: () => { delay.disable(); gc?.disconnect(); } };
173
+ }
174
+
175
+ /** Samples every `interval` seconds and sends the samples to TraceLumo in batches. */
176
+ function startMetrics({ metricsUrl, token, service, metricsInterval }, options) {
177
+ const sampler = createSampler({ service });
178
+ const instance = randomUUID();
179
+ // On ECS, the task metadata endpoint adds the cluster, task, launch type (EC2 or Fargate) and zone.
180
+ ecsDetails(process.env, options.suppress).then((details) => {
181
+ if (details) Object.assign(sampler.info, defined(details));
182
+ }).catch(() => {});
183
+ const queue = createQueue({ url: metricsUrl, token, maxBatch: 100, kind: 'metrics' }, options);
184
+ const record = () => {
185
+ try {
186
+ queue.push({ instance, timestamp: new Date().toISOString(), info: sampler.info, values: sampler.sample() });
187
+ } catch {
188
+ // Metrics must never break the application.
189
+ }
190
+ };
191
+ const first = setTimeout(record, Math.min(FIRST_SAMPLE, metricsInterval * 1000));
192
+ first.unref();
193
+ const timer = setInterval(record, metricsInterval * 1000);
194
+ timer.unref();
195
+ return {
196
+ flush: queue.flush,
197
+ async close() {
198
+ clearTimeout(first);
199
+ clearInterval(timer);
200
+ record();
201
+ sampler.stop();
202
+ await queue.close();
203
+ },
204
+ };
205
+ }
206
+
207
+ module.exports = { createSampler, startMetrics, hostInfo, runtime, defined };