toolscope 0.1.0 → 0.1.1

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.
@@ -6,7 +6,11 @@ export declare class Heartbeat {
6
6
  private readonly session;
7
7
  private readonly config;
8
8
  private timer;
9
+ private lastCpuTimes;
9
10
  constructor(queue: EventQueue, session: Session, config: Config);
11
+ private getCpuTimes;
12
+ private getCpuUsagePercent;
13
+ private sendHeartbeat;
10
14
  start(): void;
11
15
  stop(): void;
12
16
  }
package/dist/heartbeat.js CHANGED
@@ -1,27 +1,72 @@
1
+ import os from "node:os";
1
2
  export class Heartbeat {
2
3
  queue;
3
4
  session;
4
5
  config;
5
6
  timer = null;
7
+ lastCpuTimes = { idle: 0, total: 0 };
6
8
  constructor(queue, session, config) {
7
9
  this.queue = queue;
8
10
  this.session = session;
9
11
  this.config = config;
10
12
  }
13
+ getCpuTimes() {
14
+ const cpus = os.cpus();
15
+ if (!cpus || cpus.length === 0)
16
+ return { idle: 0, total: 0 };
17
+ let idle = 0;
18
+ let total = 0;
19
+ for (const cpu of cpus) {
20
+ const times = cpu.times;
21
+ idle += times.idle;
22
+ total += times.user + times.nice + times.sys + times.idle + times.irq;
23
+ }
24
+ return { idle, total };
25
+ }
26
+ getCpuUsagePercent() {
27
+ const current = this.getCpuTimes();
28
+ const idleDiff = current.idle - this.lastCpuTimes.idle;
29
+ const totalDiff = current.total - this.lastCpuTimes.total;
30
+ this.lastCpuTimes = current;
31
+ if (totalDiff === 0)
32
+ return 0;
33
+ return Math.round((1 - idleDiff / totalDiff) * 100);
34
+ }
35
+ sendHeartbeat() {
36
+ let cpuPercent = 0;
37
+ let memoryPercent = 0;
38
+ try {
39
+ cpuPercent = this.getCpuUsagePercent();
40
+ const totalMem = os.totalmem();
41
+ const freeMem = os.freemem();
42
+ if (totalMem > 0) {
43
+ memoryPercent = Math.round((1 - freeMem / totalMem) * 100);
44
+ }
45
+ }
46
+ catch (err) {
47
+ // Fallback or ignore
48
+ }
49
+ const event = {
50
+ type: "heartbeat",
51
+ timestamp: new Date().toISOString(),
52
+ session_id: this.session.sessionId,
53
+ data: {
54
+ queue_size: this.queue.size(),
55
+ dropped_events: this.queue.dropped,
56
+ cpu_percent: cpuPercent,
57
+ memory_percent: memoryPercent,
58
+ },
59
+ };
60
+ this.queue.enqueue(event);
61
+ }
11
62
  start() {
12
63
  if (this.timer !== null)
13
64
  return;
65
+ this.lastCpuTimes = this.getCpuTimes();
66
+ // Send initial heartbeat immediately
67
+ this.sendHeartbeat();
14
68
  this.timer = setInterval(() => {
15
- const event = {
16
- type: "heartbeat",
17
- timestamp: new Date().toISOString(),
18
- session_id: this.session.sessionId,
19
- data: {
20
- queue_size: this.queue.size(),
21
- dropped_events: this.queue.dropped,
22
- },
23
- };
24
- this.queue.enqueue(event);
69
+ this.sendHeartbeat();
25
70
  }, this.config.heartbeatIntervalSeconds * 1000);
26
71
  if (this.timer.unref) {
27
72
  this.timer.unref();
@@ -1 +1 @@
1
- {"version":3,"file":"heartbeat.js","sourceRoot":"","sources":["../src/heartbeat.ts"],"names":[],"mappings":"AAKA,MAAM,OAAO,SAAS;IACH,KAAK,CAAa;IAClB,OAAO,CAAU;IACjB,MAAM,CAAS;IACxB,KAAK,GAA0C,IAAI,CAAC;IAE5D,YAAY,KAAiB,EAAE,OAAgB,EAAE,MAAc;QAC7D,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;IAED,KAAK;QACH,IAAI,IAAI,CAAC,KAAK,KAAK,IAAI;YAAE,OAAO;QAEhC,IAAI,CAAC,KAAK,GAAG,WAAW,CAAC,GAAG,EAAE;YAC5B,MAAM,KAAK,GAAmB;gBAC5B,IAAI,EAAE,WAAW;gBACjB,SAAS,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;gBACnC,UAAU,EAAE,IAAI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AI,CAAC;QACpB,CAAC;IACH,CAAC;CACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "toolscope",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Production observability for your applications",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",