@themoltnet/agent-daemon 0.14.5 → 0.15.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.
Files changed (2) hide show
  1. package/dist/main.js +85 -0
  2. package/package.json +11 -4
package/dist/main.js CHANGED
@@ -1,5 +1,12 @@
1
1
  #!/usr/bin/env node
2
2
  import { createRequire } from "node:module";
3
+ import { registerInstrumentations } from "@opentelemetry/instrumentation";
4
+ import { DnsInstrumentation } from "@opentelemetry/instrumentation-dns";
5
+ import { HttpInstrumentation } from "@opentelemetry/instrumentation-http";
6
+ import { NetInstrumentation } from "@opentelemetry/instrumentation-net";
7
+ import { PgInstrumentation } from "@opentelemetry/instrumentation-pg";
8
+ import { PinoInstrumentation } from "@opentelemetry/instrumentation-pino";
9
+ import { UndiciInstrumentation } from "@opentelemetry/instrumentation-undici";
3
10
  import crypto$1, { createHash } from "crypto";
4
11
  import { createHash as createHash$1 } from "node:crypto";
5
12
  import path, { dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
@@ -55,6 +62,84 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
55
62
  var __toCommonJS = (mod) => __hasOwnProp.call(mod, "module.exports") ? mod["module.exports"] : __copyProps(__defProp({}, "__esModule", { value: true }), mod);
56
63
  var __require = /* @__PURE__ */ createRequire(import.meta.url);
57
64
  //#endregion
65
+ //#region ../../libs/observability/src/instrumentation.ts
66
+ /**
67
+ * Register OTel auto-instrumentation for common Node.js modules.
68
+ *
69
+ * MUST be called before any other imports that load pg, http, net, dns, or
70
+ * pino. In ESM, place this in a dedicated side-effect module that is the
71
+ * first import in the app entrypoint:
72
+ *
73
+ * ```ts
74
+ * // instrumentation.ts (app-level)
75
+ * import { initInstrumentation } from '@moltnet/observability';
76
+ * initInstrumentation({ pg: true, httpIgnoreIncomingPaths: ['/health'] });
77
+ * ```
78
+ *
79
+ * ```ts
80
+ * // main.ts
81
+ * import './instrumentation.js'; // ← MUST be first
82
+ * import { bootstrap } from './bootstrap.js';
83
+ * ```
84
+ *
85
+ * The registered instrumentations pick up the global TracerProvider once
86
+ * it is set by `initObservability()`. Call this function first, then call
87
+ * `initObservability()`.
88
+ *
89
+ * Returns the registered instrumentation instances so callers (and tests) can
90
+ * introspect what was wired — e.g. assert that pino/undici correlation is
91
+ * enabled. The return value is otherwise inert; callers may ignore it.
92
+ */
93
+ function initInstrumentation(config) {
94
+ const { http = true, dns = true, net = true, pg = true, pino = true, httpIgnoreIncomingPaths = [] } = config;
95
+ const instrumentations = [];
96
+ if (http) instrumentations.push(new HttpInstrumentation({ ignoreIncomingRequestHook: httpIgnoreIncomingPaths.length > 0 ? (req) => {
97
+ const url = req.url ?? "";
98
+ return httpIgnoreIncomingPaths.some((path) => url.startsWith(path));
99
+ } : void 0 }), new UndiciInstrumentation());
100
+ if (dns) instrumentations.push(new DnsInstrumentation());
101
+ if (net) instrumentations.push(new NetInstrumentation());
102
+ if (pg) instrumentations.push(new PgInstrumentation());
103
+ if (pino) instrumentations.push(new PinoInstrumentation());
104
+ registerInstrumentations({ instrumentations });
105
+ return instrumentations;
106
+ }
107
+ //#endregion
108
+ //#region src/instrumentation.ts
109
+ /**
110
+ * OTel auto-instrumentation registration for the agent daemon.
111
+ *
112
+ * This file MUST be imported first in main.ts — before any module that
113
+ * transitively imports http, net, dns, undici (global fetch), or pino. In
114
+ * ESM, imports execute in declaration order, so placing this first guarantees
115
+ * the monkey-patches land before those modules load. In particular:
116
+ *
117
+ * - UndiciInstrumentation patches `globalThis.fetch` so every outbound SDK
118
+ * call (@themoltnet/sdk uses global fetch — see libs/sdk/src/connect.ts)
119
+ * carries a W3C `traceparent` header. That is what links the daemon's
120
+ * trace to the rest-api server span: one distributed trace, end to end.
121
+ * - PinoInstrumentation injects `trace_id` / `span_id` into every pino log
122
+ * record, so logs correlate with the active span in Axiom. It patches
123
+ * pino at import time — hence the strict ordering requirement. It does
124
+ * NOT add a transport, so the logger's pino-pretty shutdown dance
125
+ * (lib/logger.ts, issue #1107) stays valid.
126
+ *
127
+ * The registered instrumentations bind to the global TracerProvider once it
128
+ * is registered by `initWorkerOtel()` (lib/otel.ts, via provider.register()).
129
+ * Registration order is fine: instrumentations created here pick up the
130
+ * provider whenever it is set; spans only flow once it is registered.
131
+ *
132
+ * No `pg` instrumentation — the daemon has no direct database access. No
133
+ * `runtime-node` — that is server-side process metrics, out of scope here.
134
+ */
135
+ initInstrumentation({
136
+ http: true,
137
+ dns: true,
138
+ net: true,
139
+ pino: true,
140
+ pg: false
141
+ });
142
+ //#endregion
58
143
  //#region ../../node_modules/.pnpm/typebox@1.2.8/node_modules/typebox/build/format/date.mjs
59
144
  var DAYS = [
60
145
  0,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@themoltnet/agent-daemon",
3
- "version": "0.14.5",
3
+ "version": "0.15.0",
4
4
  "license": "AGPL-3.0-only",
5
5
  "type": "module",
6
6
  "description": "MoltNet agent daemon — claims and executes tasks (fulfill_brief, assess_brief) from the MoltNet task-service via Pi-headless. CLI: moltnet-agent.",
@@ -32,6 +32,12 @@
32
32
  "@earendil-works/pi-coding-agent": "^0.74.0",
33
33
  "@opentelemetry/api": "^1.9.0",
34
34
  "@opentelemetry/exporter-trace-otlp-proto": "^0.212.0",
35
+ "@opentelemetry/instrumentation": "^0.212.0",
36
+ "@opentelemetry/instrumentation-dns": "^0.55.0",
37
+ "@opentelemetry/instrumentation-http": "^0.212.0",
38
+ "@opentelemetry/instrumentation-net": "^0.56.0",
39
+ "@opentelemetry/instrumentation-pino": "^0.58.0",
40
+ "@opentelemetry/instrumentation-undici": "^0.22.0",
35
41
  "@opentelemetry/resources": "^2.5.1",
36
42
  "@opentelemetry/sdk-trace-base": "^2.5.1",
37
43
  "@opentelemetry/sdk-trace-node": "^2.5.1",
@@ -39,9 +45,9 @@
39
45
  "pino": "^10.3.1",
40
46
  "pino-pretty": "^13.1.3",
41
47
  "@themoltnet/agent-daemon-state": "0.2.0",
42
- "@themoltnet/pi-extension": "0.22.4",
43
48
  "@themoltnet/agent-runtime": "0.22.4",
44
- "@themoltnet/sdk": "0.106.0"
49
+ "@themoltnet/sdk": "0.106.0",
50
+ "@themoltnet/pi-extension": "0.22.5"
45
51
  },
46
52
  "devDependencies": {
47
53
  "tsx": "^4.7.0",
@@ -50,7 +56,8 @@
50
56
  "vitest": "^3.0.0",
51
57
  "@moltnet/bootstrap": "0.1.0",
52
58
  "@moltnet/crypto-service": "0.1.0",
53
- "@moltnet/tasks": "0.1.0"
59
+ "@moltnet/tasks": "0.1.0",
60
+ "@moltnet/observability": "0.1.0"
54
61
  },
55
62
  "nx": {
56
63
  "tags": [