@openclaw/diagnostics-otel 2026.7.1-beta.2 → 2026.7.1-beta.5

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 (91) hide show
  1. package/dist/index.js +1 -2404
  2. package/dist/runtime-api-BUlvn2ET.js +2589 -0
  3. package/dist/runtime-api.js +2 -0
  4. package/node_modules/@types/node/README.md +1 -1
  5. package/node_modules/@types/node/assert/strict.d.ts +10 -1
  6. package/node_modules/@types/node/assert.d.ts +144 -17
  7. package/node_modules/@types/node/async_hooks.d.ts +9 -9
  8. package/node_modules/@types/node/buffer.d.ts +64 -30
  9. package/node_modules/@types/node/child_process.d.ts +17 -21
  10. package/node_modules/@types/node/cluster.d.ts +13 -13
  11. package/node_modules/@types/node/console.d.ts +1 -1
  12. package/node_modules/@types/node/crypto.d.ts +471 -575
  13. package/node_modules/@types/node/diagnostics_channel.d.ts +274 -32
  14. package/node_modules/@types/node/dns.d.ts +15 -15
  15. package/node_modules/@types/node/events.d.ts +3 -3
  16. package/node_modules/@types/node/ffi.d.ts +486 -0
  17. package/node_modules/@types/node/fs/promises.d.ts +7 -4
  18. package/node_modules/@types/node/fs.d.ts +24 -18
  19. package/node_modules/@types/node/globals.d.ts +4 -4
  20. package/node_modules/@types/node/http.d.ts +30 -5
  21. package/node_modules/@types/node/http2.d.ts +5 -1
  22. package/node_modules/@types/node/index.d.ts +1 -3
  23. package/node_modules/@types/node/inspector.d.ts +1 -1
  24. package/node_modules/@types/node/module.d.ts +16 -45
  25. package/node_modules/@types/node/net.d.ts +1 -1
  26. package/node_modules/@types/node/os.d.ts +2 -2
  27. package/node_modules/@types/node/package.json +4 -4
  28. package/node_modules/@types/node/perf_hooks.d.ts +1 -9
  29. package/node_modules/@types/node/process.d.ts +25 -12
  30. package/node_modules/@types/node/quic.d.ts +92 -11
  31. package/node_modules/@types/node/readline.d.ts +12 -11
  32. package/node_modules/@types/node/repl.d.ts +10 -10
  33. package/node_modules/@types/node/sqlite.d.ts +56 -1
  34. package/node_modules/@types/node/stream/iter.d.ts +150 -0
  35. package/node_modules/@types/node/stream/promises.d.ts +2 -2
  36. package/node_modules/@types/node/stream/web.d.ts +1 -1
  37. package/node_modules/@types/node/stream.d.ts +37 -5
  38. package/node_modules/@types/node/test/reporters.d.ts +1 -1
  39. package/node_modules/@types/node/test.d.ts +120 -10
  40. package/node_modules/@types/node/tls.d.ts +1 -1
  41. package/node_modules/@types/node/ts5.6/index.d.ts +2 -4
  42. package/node_modules/@types/node/ts5.7/index.d.ts +1 -3
  43. package/node_modules/@types/node/url.d.ts +3 -3
  44. package/node_modules/@types/node/util/types.d.ts +1 -1
  45. package/node_modules/@types/node/util.d.ts +22 -22
  46. package/node_modules/@types/node/v8.d.ts +85 -3
  47. package/node_modules/@types/node/vm.d.ts +9 -9
  48. package/node_modules/@types/node/web-globals/domexception.d.ts +15 -0
  49. package/node_modules/@types/node/worker_threads.d.ts +10 -12
  50. package/node_modules/@types/node/zlib.d.ts +3 -3
  51. package/node_modules/acorn/CHANGELOG.md +14 -0
  52. package/node_modules/acorn/README.md +3 -0
  53. package/node_modules/acorn/dist/acorn.d.mts +6 -0
  54. package/node_modules/acorn/dist/acorn.d.ts +6 -0
  55. package/node_modules/acorn/dist/acorn.js +87 -49
  56. package/node_modules/acorn/dist/acorn.mjs +87 -49
  57. package/node_modules/acorn/package.json +2 -2
  58. package/node_modules/import-in-the-middle/CHANGELOG.md +21 -0
  59. package/node_modules/import-in-the-middle/README.md +60 -0
  60. package/node_modules/import-in-the-middle/create-hook.mjs +375 -146
  61. package/node_modules/import-in-the-middle/lib/get-exports.mjs +88 -52
  62. package/node_modules/import-in-the-middle/lib/io.mjs +78 -0
  63. package/node_modules/import-in-the-middle/package.json +1 -1
  64. package/node_modules/import-in-the-middle/register-hooks.d.ts +35 -0
  65. package/node_modules/import-in-the-middle/register-hooks.mjs +63 -0
  66. package/node_modules/undici-types/agent.d.ts +0 -2
  67. package/node_modules/undici-types/client.d.ts +25 -19
  68. package/node_modules/undici-types/dispatcher.d.ts +7 -33
  69. package/node_modules/undici-types/dispatcher1-wrapper.d.ts +7 -0
  70. package/node_modules/undici-types/fetch.d.ts +24 -4
  71. package/node_modules/undici-types/h2c-client.d.ts +6 -6
  72. package/node_modules/undici-types/handlers.d.ts +1 -2
  73. package/node_modules/undici-types/header.d.ts +5 -0
  74. package/node_modules/undici-types/index.d.ts +3 -1
  75. package/node_modules/undici-types/interceptors.d.ts +1 -1
  76. package/node_modules/undici-types/package.json +1 -1
  77. package/node_modules/undici-types/pool.d.ts +0 -2
  78. package/node_modules/undici-types/proxy-agent.d.ts +2 -2
  79. package/node_modules/undici-types/round-robin-pool.d.ts +0 -2
  80. package/node_modules/undici-types/snapshot-agent.d.ts +4 -0
  81. package/node_modules/undici-types/socks5-proxy-agent.d.ts +2 -2
  82. package/node_modules/undici-types/webidl.d.ts +0 -1
  83. package/node_modules/yargs/CHANGELOG.md +330 -0
  84. package/node_modules/yargs/build/index.cjs +1 -1
  85. package/node_modules/yargs/package.json +6 -4
  86. package/node_modules/yargs/yargs +5 -9
  87. package/node_modules/yargs/yargs.cjs +15 -0
  88. package/npm-shrinkwrap.json +18 -18
  89. package/package.json +4 -4
  90. package/node_modules/@types/node/compatibility/iterators.d.ts +0 -21
  91. package/node_modules/yargs/browser.d.ts +0 -5
@@ -0,0 +1,2589 @@
1
+ import { isValidDiagnosticSpanId, isValidDiagnosticTraceFlags, isValidDiagnosticTraceId, redactSensitiveText } from "./api.js";
2
+ import { readFileSync } from "node:fs";
3
+ import nodePath from "node:path";
4
+ import { SpanKind, SpanStatusCode, TraceFlags, context, metrics, trace } from "@opentelemetry/api";
5
+ import { OTLPLogExporter } from "@opentelemetry/exporter-logs-otlp-proto";
6
+ import { OTLPMetricExporter } from "@opentelemetry/exporter-metrics-otlp-proto";
7
+ import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-proto";
8
+ import { resourceFromAttributes } from "@opentelemetry/resources";
9
+ import { BatchLogRecordProcessor, LoggerProvider } from "@opentelemetry/sdk-logs";
10
+ import { PeriodicExportingMetricReader } from "@opentelemetry/sdk-metrics";
11
+ import { NodeSDK } from "@opentelemetry/sdk-node";
12
+ import { BatchSpanProcessor, ParentBasedSampler, TraceIdRatioBasedSampler } from "@opentelemetry/sdk-trace-base";
13
+ import { ATTR_SERVICE_NAME } from "@opentelemetry/semantic-conventions";
14
+ import { ATTR_GEN_AI_INPUT_MESSAGES, ATTR_GEN_AI_OUTPUT_MESSAGES, ATTR_GEN_AI_SYSTEM_INSTRUCTIONS, ATTR_GEN_AI_TOOL_CALL_ARGUMENTS, ATTR_GEN_AI_TOOL_CALL_ID, ATTR_GEN_AI_TOOL_CALL_RESULT, ATTR_GEN_AI_TOOL_DEFINITIONS, GEN_AI_OPERATION_NAME_VALUE_EXECUTE_TOOL } from "@opentelemetry/semantic-conventions/incubating";
15
+ import { waitForDiagnosticEventsDrained } from "openclaw/plugin-sdk/diagnostic-runtime";
16
+ import { createNodeProxyAgent } from "openclaw/plugin-sdk/fetch-runtime";
17
+ import { registerUnhandledRejectionHandler } from "openclaw/plugin-sdk/runtime-env";
18
+ import { isRecord } from "openclaw/plugin-sdk/string-coerce-runtime";
19
+ import { truncateUtf16Safe } from "openclaw/plugin-sdk/text-utility-runtime";
20
+ //#region extensions/diagnostics-otel/src/service.ts
21
+ const DEFAULT_SERVICE_NAME = "openclaw";
22
+ const DROPPED_OTEL_ATTRIBUTE_KEYS = /* @__PURE__ */ new Set([
23
+ "openclaw.callId",
24
+ "openclaw.call_id",
25
+ "openclaw.chatId",
26
+ "openclaw.chat_id",
27
+ "openclaw.messageId",
28
+ "openclaw.message_id",
29
+ "openclaw.parentSpanId",
30
+ "openclaw.parent_span_id",
31
+ "openclaw.runId",
32
+ "openclaw.run_id",
33
+ "openclaw.sessionId",
34
+ "openclaw.session_id",
35
+ "openclaw.sessionKey",
36
+ "openclaw.session_key",
37
+ "openclaw.spanId",
38
+ "openclaw.span_id",
39
+ "openclaw.toolCallId",
40
+ "openclaw.tool_call_id",
41
+ "openclaw.traceId",
42
+ "openclaw.trace_id"
43
+ ]);
44
+ const LOW_CARDINALITY_VALUE_RE = /^[A-Za-z0-9_.:-]{1,120}$/u;
45
+ const SECURITY_TARGET_NAME_VALUE_RE = /^[A-Za-z0-9@/_.:-]{1,256}$/u;
46
+ const MAX_OTEL_CONTENT_ATTRIBUTE_CHARS = 128 * 1024;
47
+ const MAX_OTEL_CONTENT_ARRAY_ITEMS = 200;
48
+ const MAX_OTEL_LOG_BODY_CHARS = 4 * 1024;
49
+ const MAX_OTEL_LOG_ATTRIBUTE_COUNT = 64;
50
+ const MAX_OTEL_LOG_ATTRIBUTE_VALUE_CHARS = 4 * 1024;
51
+ const MAX_OTEL_ERROR_MESSAGE_CHARS = 4 * 1024;
52
+ const LOG_RECORD_EXPORT_FAILURE_REPORT_INTERVAL_MS = 6e4;
53
+ const OTEL_LOG_RAW_ATTRIBUTE_KEY_RE = /^[A-Za-z0-9_.:-]{1,64}$/u;
54
+ const OTEL_LOG_ATTRIBUTE_KEY_RE = /^[A-Za-z0-9_.:-]{1,96}$/u;
55
+ const BLOCKED_OTEL_LOG_ATTRIBUTE_KEYS = /* @__PURE__ */ new Set([
56
+ "__proto__",
57
+ "prototype",
58
+ "constructor"
59
+ ]);
60
+ const PRELOADED_OTEL_SDK_ENV = "OPENCLAW_OTEL_PRELOADED";
61
+ const OTEL_EXPORTER_OTLP_ENDPOINT_ENV = "OTEL_EXPORTER_OTLP_ENDPOINT";
62
+ const OTEL_EXPORTER_OTLP_TRACES_ENDPOINT_ENV = "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT";
63
+ const OTEL_EXPORTER_OTLP_METRICS_ENDPOINT_ENV = "OTEL_EXPORTER_OTLP_METRICS_ENDPOINT";
64
+ const OTEL_EXPORTER_OTLP_LOGS_ENDPOINT_ENV = "OTEL_EXPORTER_OTLP_LOGS_ENDPOINT";
65
+ const OTEL_EXPORTER_OTLP_CERTIFICATE_ENV = "OTEL_EXPORTER_OTLP_CERTIFICATE";
66
+ const OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE_ENV = "OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE";
67
+ const OTEL_EXPORTER_OTLP_CLIENT_KEY_ENV = "OTEL_EXPORTER_OTLP_CLIENT_KEY";
68
+ const OTEL_SEMCONV_STABILITY_OPT_IN_ENV = "OTEL_SEMCONV_STABILITY_OPT_IN";
69
+ const GEN_AI_LATEST_EXPERIMENTAL_OPT_IN = "gen_ai_latest_experimental";
70
+ const GEN_AI_TOKEN_USAGE_BUCKETS = [
71
+ 1,
72
+ 4,
73
+ 16,
74
+ 64,
75
+ 256,
76
+ 1024,
77
+ 4096,
78
+ 16384,
79
+ 65536,
80
+ 262144,
81
+ 1048576,
82
+ 4194304,
83
+ 16777216,
84
+ 67108864
85
+ ];
86
+ const GEN_AI_OPERATION_DURATION_BUCKETS = [
87
+ .01,
88
+ .02,
89
+ .04,
90
+ .08,
91
+ .16,
92
+ .32,
93
+ .64,
94
+ 1.28,
95
+ 2.56,
96
+ 5.12,
97
+ 10.24,
98
+ 20.48,
99
+ 40.96,
100
+ 81.92
101
+ ];
102
+ const OTEL_DEFAULT_HISTOGRAM_BUCKETS = [
103
+ 0,
104
+ 5,
105
+ 10,
106
+ 25,
107
+ 50,
108
+ 75,
109
+ 100,
110
+ 250,
111
+ 500,
112
+ 750,
113
+ 1e3,
114
+ 2500,
115
+ 5e3,
116
+ 7500,
117
+ 1e4
118
+ ];
119
+ const AGENT_DURATION_MS_BUCKETS = [
120
+ ...OTEL_DEFAULT_HISTOGRAM_BUCKETS,
121
+ 15e3,
122
+ 2e4,
123
+ 3e4,
124
+ 45e3,
125
+ 6e4,
126
+ 12e4,
127
+ 18e4,
128
+ 24e4,
129
+ 3e5,
130
+ 6e5,
131
+ 9e5,
132
+ 18e5,
133
+ 36e5
134
+ ];
135
+ const CONTEXT_TOKENS_BUCKETS = [
136
+ ...OTEL_DEFAULT_HISTOGRAM_BUCKETS,
137
+ 16e3,
138
+ 32e3,
139
+ 64e3,
140
+ 128e3,
141
+ 2e5,
142
+ 4e5,
143
+ 1e6,
144
+ 2e6
145
+ ];
146
+ const MAX_RETAINED_TRUSTED_SPAN_CONTEXTS = 1024;
147
+ const RETAINED_TRUSTED_SPAN_CONTEXT_TIMEOUT_MS = 5e3;
148
+ const NO_CONTENT_CAPTURE = {
149
+ inputMessages: false,
150
+ outputMessages: false,
151
+ toolInputs: false,
152
+ toolOutputs: false,
153
+ systemPrompt: false,
154
+ toolDefinitions: false,
155
+ logBodies: false
156
+ };
157
+ function normalizeEndpoint(endpoint) {
158
+ const trimmed = endpoint?.trim();
159
+ return trimmed ? trimmed.replace(/\/+$/, "") : void 0;
160
+ }
161
+ function resolveOtelUrl(endpoint, path) {
162
+ if (!endpoint) return;
163
+ const endpointWithoutQueryOrFragment = endpoint.split(/[?#]/, 1)[0] ?? endpoint;
164
+ if (/\/v1\/(?:traces|metrics|logs)$/i.test(endpointWithoutQueryOrFragment)) return endpoint;
165
+ if (/[?#]/u.test(endpoint)) try {
166
+ const url = new URL(endpoint);
167
+ url.pathname = `${url.pathname.replace(/\/+$/u, "")}/${path}`;
168
+ return url.toString();
169
+ } catch {}
170
+ return `${endpoint}/${path}`;
171
+ }
172
+ function resolveSignalOtelUrl(params) {
173
+ return resolveOtelUrl(normalizeEndpoint(params.signalEndpoint ?? params.signalEnvEndpoint) ?? params.endpoint, params.path);
174
+ }
175
+ function readOtelEnvFile(params) {
176
+ const signalEnvName = `OTEL_EXPORTER_OTLP_${params.signalIdentifier}_${params.signalSuffix}`;
177
+ const filePath = normalizeOtelEnvValue(process.env[signalEnvName]) ?? normalizeOtelEnvValue(process.env[params.sharedEnvName]);
178
+ if (!filePath) return;
179
+ try {
180
+ return readFileSync(nodePath.resolve(process.cwd(), filePath));
181
+ } catch {
182
+ params.logger.warn(`diagnostics-otel: ${params.warning}`);
183
+ return;
184
+ }
185
+ }
186
+ function normalizeOtelEnvValue(value) {
187
+ const trimmed = value?.trim();
188
+ return trimmed ? trimmed : void 0;
189
+ }
190
+ function resolveOtelHttpAgentOptions(params) {
191
+ const { url, signalIdentifier, logger } = params;
192
+ if (!url) return;
193
+ const ca = readOtelEnvFile({
194
+ signalIdentifier,
195
+ signalSuffix: "CERTIFICATE",
196
+ sharedEnvName: OTEL_EXPORTER_OTLP_CERTIFICATE_ENV,
197
+ logger,
198
+ warning: "failed to read root certificate file"
199
+ });
200
+ const cert = readOtelEnvFile({
201
+ signalIdentifier,
202
+ signalSuffix: "CLIENT_CERTIFICATE",
203
+ sharedEnvName: OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE_ENV,
204
+ logger,
205
+ warning: "failed to read client certificate chain file"
206
+ });
207
+ const key = readOtelEnvFile({
208
+ signalIdentifier,
209
+ signalSuffix: "CLIENT_KEY",
210
+ sharedEnvName: OTEL_EXPORTER_OTLP_CLIENT_KEY_ENV,
211
+ logger,
212
+ warning: "failed to read client certificate private key file"
213
+ });
214
+ const agentOptions = {
215
+ keepAlive: true,
216
+ ...ca !== void 0 ? { ca } : {},
217
+ ...cert !== void 0 ? { cert } : {},
218
+ ...key !== void 0 ? { key } : {}
219
+ };
220
+ try {
221
+ const agent = createNodeProxyAgent({
222
+ mode: "env",
223
+ targetUrl: url,
224
+ agentOptions
225
+ });
226
+ return agent ? () => agent : void 0;
227
+ } catch {
228
+ logger.warn(`diagnostics-otel: env proxy agent unavailable for OTLP ${signalIdentifier.toLowerCase()} exporter; falling back to default Node agent`);
229
+ return;
230
+ }
231
+ }
232
+ function resolveSampleRate(value) {
233
+ if (typeof value !== "number" || !Number.isFinite(value)) return;
234
+ if (value < 0 || value > 1) return;
235
+ return value;
236
+ }
237
+ function formatError(err) {
238
+ if (err instanceof Error) return err.stack ?? err.message;
239
+ if (typeof err === "string") return err;
240
+ try {
241
+ return JSON.stringify(err);
242
+ } catch {
243
+ return String(err);
244
+ }
245
+ }
246
+ function errorCategory(err) {
247
+ try {
248
+ if (err instanceof Error && typeof err.name === "string" && err.name.trim()) return lowCardinalityAttr(err.name, "Error");
249
+ return lowCardinalityAttr(typeof err, "unknown");
250
+ } catch {
251
+ return "unknown";
252
+ }
253
+ }
254
+ function collectNestedErrorCandidates(err) {
255
+ const queue = [err];
256
+ const seen = /* @__PURE__ */ new Set();
257
+ const candidates = [];
258
+ while (queue.length > 0) {
259
+ const current = queue.shift();
260
+ if (current == null || seen.has(current)) continue;
261
+ seen.add(current);
262
+ candidates.push(current);
263
+ if (Array.isArray(current)) {
264
+ for (const item of current) if (item != null && !seen.has(item)) queue.push(item);
265
+ continue;
266
+ }
267
+ if (typeof current !== "object") continue;
268
+ const record = current;
269
+ for (const nested of [
270
+ record.cause,
271
+ record.reason,
272
+ record.original,
273
+ record.error
274
+ ]) if (nested != null && !seen.has(nested)) queue.push(nested);
275
+ if (Array.isArray(record.errors)) {
276
+ for (const nested of record.errors) if (nested != null && !seen.has(nested)) queue.push(nested);
277
+ }
278
+ }
279
+ return candidates;
280
+ }
281
+ function readErrorName(err) {
282
+ if (!err || typeof err !== "object") return;
283
+ const name = err.name;
284
+ return typeof name === "string" && name.trim() ? name : void 0;
285
+ }
286
+ function readErrorCode(err) {
287
+ if (!err || typeof err !== "object") return;
288
+ const code = err.code;
289
+ return typeof code === "string" || typeof code === "number" ? code : void 0;
290
+ }
291
+ function findOtlpExporterError(reason) {
292
+ for (const candidate of collectNestedErrorCandidates(reason)) if (readErrorName(candidate) === "OTLPExporterError" && candidate && typeof candidate === "object") return candidate;
293
+ }
294
+ function redactOtelAttributes(attributes) {
295
+ const redactedAttributes = {};
296
+ for (const [key, value] of Object.entries(attributes)) {
297
+ if (DROPPED_OTEL_ATTRIBUTE_KEYS.has(key)) continue;
298
+ redactedAttributes[key] = typeof value === "string" ? redactSensitiveText(value) : value;
299
+ }
300
+ return redactedAttributes;
301
+ }
302
+ function lowCardinalityAttr(value, fallback = "unknown") {
303
+ if (!value) return fallback;
304
+ const redacted = redactSensitiveText(value.trim());
305
+ const redactedLower = redacted.toLowerCase();
306
+ if (redactedLower.startsWith("agent:") || redactedLower.includes(":agent:")) return fallback;
307
+ return LOW_CARDINALITY_VALUE_RE.test(redacted) ? redacted : fallback;
308
+ }
309
+ function securityTargetNameAttr(value, fallback = "unknown") {
310
+ if (!value) return fallback;
311
+ const redacted = redactSensitiveText(value.trim());
312
+ const redactedLower = redacted.toLowerCase();
313
+ if (redactedLower.startsWith("agent:") || redactedLower.includes(":agent:")) return fallback;
314
+ return SECURITY_TARGET_NAME_VALUE_RE.test(redacted) ? redacted : fallback;
315
+ }
316
+ function lowCardinalityQueueLaneAttr(value, fallback = "unknown") {
317
+ if (!value) return fallback;
318
+ const redacted = redactSensitiveText(value.trim());
319
+ if (redacted.toLowerCase().startsWith("agent:")) return fallback;
320
+ const scopedLaneIndex = redacted.indexOf(":");
321
+ const lane = scopedLaneIndex >= 0 ? redacted.slice(0, scopedLaneIndex) : redacted;
322
+ return LOW_CARDINALITY_VALUE_RE.test(lane) ? lane : fallback;
323
+ }
324
+ function shouldCaptureOtelLogBody(policy) {
325
+ return policy.logBodies;
326
+ }
327
+ function otelLogTimestampIso(timestamp) {
328
+ if (timestamp instanceof Date) return timestamp.toISOString();
329
+ if (typeof timestamp === "number" && Number.isFinite(timestamp)) return new Date(timestamp).toISOString();
330
+ if (Array.isArray(timestamp)) {
331
+ const [seconds, nanoseconds] = timestamp;
332
+ if (Number.isFinite(seconds) && Number.isFinite(nanoseconds)) return new Date(seconds * 1e3 + Math.trunc(nanoseconds / 1e6)).toISOString();
333
+ }
334
+ return (/* @__PURE__ */ new Date()).toISOString();
335
+ }
336
+ function writeStdoutDiagnosticLogRecord(params) {
337
+ const { logRecord, serviceName, traceContext } = params;
338
+ const line = {
339
+ ts: otelLogTimestampIso(logRecord.timestamp),
340
+ signal: "openclaw.diagnostic.log",
341
+ "service.name": serviceName,
342
+ severityText: logRecord.severityText,
343
+ severityNumber: logRecord.severityNumber,
344
+ body: logRecord.body,
345
+ attributes: logRecord.attributes ?? {},
346
+ ...traceContext?.traceId ? { trace_id: traceContext.traceId } : {},
347
+ ...traceContext?.spanId ? { span_id: traceContext.spanId } : {},
348
+ ...traceContext?.traceFlags ? { trace_flags: traceContext.traceFlags } : {}
349
+ };
350
+ process.stdout.write(`${JSON.stringify(line)}\n`);
351
+ }
352
+ function hasOtelSemconvOptIn(value, optIn) {
353
+ return value?.split(",").map((part) => part.trim()).includes(optIn) ?? false;
354
+ }
355
+ function emitLatestGenAiSemconv() {
356
+ return hasOtelSemconvOptIn(process.env[OTEL_SEMCONV_STABILITY_OPT_IN_ENV], GEN_AI_LATEST_EXPERIMENTAL_OPT_IN);
357
+ }
358
+ function genAiOperationName(api) {
359
+ const normalized = api?.trim().toLowerCase();
360
+ if (!normalized) return "chat";
361
+ if (normalized === "completions" || normalized.endsWith("-completions")) return "text_completion";
362
+ if (normalized === "generate_content" || normalized.includes("generative-ai")) return "generate_content";
363
+ return "chat";
364
+ }
365
+ function positiveFiniteNumber(value) {
366
+ return typeof value === "number" && Number.isFinite(value) && value > 0 ? value : void 0;
367
+ }
368
+ function assignPositiveNumberAttr(attrs, key, value) {
369
+ const normalized = positiveFiniteNumber(value);
370
+ if (normalized !== void 0) attrs[key] = normalized;
371
+ }
372
+ function assignModelCallSizeTimingAttrs(attrs, evt) {
373
+ assignPositiveNumberAttr(attrs, "openclaw.model_call.request_bytes", evt.requestPayloadBytes);
374
+ assignPositiveNumberAttr(attrs, "openclaw.model_call.response_bytes", evt.responseStreamBytes);
375
+ assignPositiveNumberAttr(attrs, "openclaw.model_call.time_to_first_byte_ms", evt.timeToFirstByteMs);
376
+ }
377
+ function assignNumberAttr(attrs, key, value) {
378
+ if (typeof value === "number" && Number.isFinite(value)) attrs[key] = value;
379
+ }
380
+ function modelCallPromptTokens(usage) {
381
+ if (typeof usage.promptTokens === "number" && Number.isFinite(usage.promptTokens)) return usage.promptTokens;
382
+ const input = usage.input ?? 0;
383
+ const cacheRead = usage.cacheRead ?? 0;
384
+ const cacheWrite = usage.cacheWrite ?? 0;
385
+ const total = input + cacheRead + cacheWrite;
386
+ return total > 0 ? total : void 0;
387
+ }
388
+ function assignModelCallPromptStatsAttrs(attrs, evt) {
389
+ const stats = evt.promptStats;
390
+ if (!stats) return;
391
+ for (const [key, value] of [
392
+ ["openclaw.model_call.prompt.input_messages_count", stats.inputMessagesCount],
393
+ ["openclaw.model_call.prompt.input_messages_chars", stats.inputMessagesChars],
394
+ ["openclaw.model_call.prompt.system_prompt_chars", stats.systemPromptChars],
395
+ ["openclaw.model_call.prompt.tool_definitions_count", stats.toolDefinitionsCount],
396
+ ["openclaw.model_call.prompt.tool_definitions_chars", stats.toolDefinitionsChars],
397
+ ["openclaw.model_call.prompt.total_chars", stats.totalChars]
398
+ ]) assignNumberAttr(attrs, key, value);
399
+ }
400
+ function assignModelCallUsageAttrs(attrs, evt) {
401
+ const usage = evt.usage;
402
+ if (!usage) return;
403
+ const promptTokens = modelCallPromptTokens(usage);
404
+ for (const [key, value] of [
405
+ ["openclaw.model_call.usage.input_tokens", usage.input],
406
+ ["openclaw.model_call.usage.output_tokens", usage.output],
407
+ ["openclaw.model_call.usage.cache_read_input_tokens", usage.cacheRead],
408
+ ["openclaw.model_call.usage.cache_creation_input_tokens", usage.cacheWrite],
409
+ ["openclaw.model_call.usage.reasoning_output_tokens", usage.reasoningTokens],
410
+ ["openclaw.model_call.usage.prompt_tokens", promptTokens],
411
+ ["openclaw.model_call.usage.total_tokens", usage.total],
412
+ ["gen_ai.usage.input_tokens", promptTokens],
413
+ ["gen_ai.usage.output_tokens", usage.output],
414
+ ["gen_ai.usage.cache_read.input_tokens", usage.cacheRead],
415
+ ["gen_ai.usage.cache_creation.input_tokens", usage.cacheWrite]
416
+ ]) assignPositiveNumberAttr(attrs, key, value);
417
+ }
418
+ function assignGenAiSpanIdentityAttrs(attrs, input) {
419
+ if (emitLatestGenAiSemconv()) attrs["gen_ai.provider.name"] = lowCardinalityAttr(input.provider);
420
+ else attrs["gen_ai.system"] = lowCardinalityAttr(input.provider);
421
+ if (input.model) attrs["gen_ai.request.model"] = redactSensitiveText(input.model.trim());
422
+ attrs["gen_ai.operation.name"] = genAiOperationName(input.api);
423
+ }
424
+ function assignGenAiModelCallAttrs(attrs, evt) {
425
+ assignGenAiSpanIdentityAttrs(attrs, evt);
426
+ }
427
+ function modelCallSpanName(evt) {
428
+ if (!emitLatestGenAiSemconv()) return "openclaw.model.call";
429
+ return `${genAiOperationName(evt.api)} ${lowCardinalityAttr(evt.model)}`;
430
+ }
431
+ function modelCallSpanKind() {
432
+ return emitLatestGenAiSemconv() ? SpanKind.CLIENT : void 0;
433
+ }
434
+ function addUpstreamRequestIdSpanEvent(span, upstreamRequestIdHash) {
435
+ if (!upstreamRequestIdHash) return;
436
+ const boundedHash = lowCardinalityAttr(upstreamRequestIdHash);
437
+ if (boundedHash === "unknown") return;
438
+ span.addEvent?.("openclaw.provider.request", { "openclaw.upstreamRequestIdHash": boundedHash });
439
+ }
440
+ function clampOtelLogText(value, maxChars) {
441
+ return value.length > maxChars ? `${truncateUtf16Safe(value, maxChars)}...(truncated)` : value;
442
+ }
443
+ function normalizeOtelLogString(value, maxChars) {
444
+ return clampOtelLogText(redactSensitiveText(value), maxChars);
445
+ }
446
+ function normalizeOtelErrorMessage(value) {
447
+ if (!value) return;
448
+ return normalizeOtelLogString(value.trim(), MAX_OTEL_ERROR_MESSAGE_CHARS) || void 0;
449
+ }
450
+ function resolveContentCapturePolicy(value) {
451
+ if (value === true) return {
452
+ inputMessages: true,
453
+ outputMessages: true,
454
+ toolInputs: true,
455
+ toolOutputs: true,
456
+ systemPrompt: false,
457
+ toolDefinitions: true,
458
+ logBodies: true
459
+ };
460
+ if (!value || typeof value !== "object" || Array.isArray(value)) return NO_CONTENT_CAPTURE;
461
+ const config = value;
462
+ if (config.enabled !== true) return NO_CONTENT_CAPTURE;
463
+ return {
464
+ inputMessages: config.inputMessages === true,
465
+ outputMessages: config.outputMessages === true,
466
+ toolInputs: config.toolInputs === true,
467
+ toolOutputs: config.toolOutputs === true,
468
+ systemPrompt: config.systemPrompt === true,
469
+ toolDefinitions: config.toolDefinitions === true,
470
+ logBodies: false
471
+ };
472
+ }
473
+ function hasPreloadedOtelSdk() {
474
+ return process.env[PRELOADED_OTEL_SDK_ENV] === "1";
475
+ }
476
+ function normalizeOtelContentValue(value) {
477
+ if (typeof value === "string") return normalizeOtelLogString(value, MAX_OTEL_CONTENT_ATTRIBUTE_CHARS);
478
+ if (Array.isArray(value)) {
479
+ const items = [];
480
+ for (const item of value.slice(0, MAX_OTEL_CONTENT_ARRAY_ITEMS)) if (typeof item === "string") items.push(item);
481
+ if (items.length > 0) return normalizeOtelLogString(items.join("\n"), MAX_OTEL_CONTENT_ATTRIBUTE_CHARS);
482
+ }
483
+ const json = safeJsonString(value, MAX_OTEL_CONTENT_ATTRIBUTE_CHARS);
484
+ if (json) return json;
485
+ }
486
+ const TRUNCATED_JSON_TEXT_SUFFIX = "...(truncated)";
487
+ const JSON_TRUNCATION_STRING_BUDGETS = [
488
+ 8192,
489
+ 4096,
490
+ 2048,
491
+ 1024,
492
+ 512,
493
+ 256,
494
+ 128,
495
+ 64,
496
+ 32
497
+ ];
498
+ const JSON_TRUNCATION_ARRAY_ITEM_BUDGETS = [
499
+ MAX_OTEL_CONTENT_ARRAY_ITEMS,
500
+ 100,
501
+ 50,
502
+ 25,
503
+ 10,
504
+ 5,
505
+ 1
506
+ ];
507
+ const JSON_TRUNCATION_MAX_OBJECT_FIELDS = 64;
508
+ const JSON_TRUNCATION_MAX_DEPTH = 8;
509
+ function safeJsonString(value, maxChars) {
510
+ if (value === void 0 || typeof value === "function" || typeof value === "symbol") return;
511
+ const exact = stringifyJsonForOtelAttribute(value);
512
+ if (exact && exact.length <= maxChars) return exact;
513
+ for (const maxArrayItems of JSON_TRUNCATION_ARRAY_ITEM_BUDGETS) for (const maxStringChars of JSON_TRUNCATION_STRING_BUDGETS) {
514
+ const json = stringifyJsonForOtelAttribute(truncateJsonValueForOtelAttribute(value, {
515
+ maxArrayItems,
516
+ maxDepth: JSON_TRUNCATION_MAX_DEPTH,
517
+ maxObjectFields: JSON_TRUNCATION_MAX_OBJECT_FIELDS,
518
+ maxStringChars,
519
+ seen: /* @__PURE__ */ new WeakSet()
520
+ }));
521
+ if (json && json.length <= maxChars) return json;
522
+ }
523
+ const summary = stringifyJsonForOtelAttribute({
524
+ truncated: true,
525
+ reason: exact ? "max_attribute_size" : "unserializable_value",
526
+ type: describeJsonValue(value)
527
+ });
528
+ return summary && summary.length <= maxChars ? summary : void 0;
529
+ }
530
+ function stringifyJsonForOtelAttribute(value) {
531
+ try {
532
+ const json = JSON.stringify(value);
533
+ if (!json) return;
534
+ return redactSensitiveText(json);
535
+ } catch {
536
+ return;
537
+ }
538
+ }
539
+ function truncateJsonValueForOtelAttribute(value, options) {
540
+ if (typeof value === "string") return truncateJsonTextForOtelAttribute(value, options.maxStringChars);
541
+ if (typeof value === "number" || typeof value === "boolean" || value === null) return value;
542
+ if (typeof value === "bigint") return truncateJsonTextForOtelAttribute(String(value), options.maxStringChars);
543
+ if (value === void 0 || typeof value === "function" || typeof value === "symbol") return;
544
+ if (options.maxDepth <= 0) return {
545
+ truncated: true,
546
+ reason: "max_depth"
547
+ };
548
+ if (Array.isArray(value)) return truncateJsonArrayForOtelAttribute(value, options);
549
+ if (typeof value === "object") return truncateJsonObjectForOtelAttribute(value, options);
550
+ }
551
+ function truncateJsonArrayForOtelAttribute(value, options) {
552
+ if (options.seen.has(value)) return [{
553
+ truncated: true,
554
+ reason: "circular_reference"
555
+ }];
556
+ options.seen.add(value);
557
+ const nextOptions = {
558
+ ...options,
559
+ maxDepth: options.maxDepth - 1
560
+ };
561
+ const items = value.slice(0, options.maxArrayItems).map((item) => truncateJsonValueForOtelAttribute(item, nextOptions));
562
+ if (value.length > items.length) items.push({
563
+ truncated: true,
564
+ omittedItems: value.length - items.length
565
+ });
566
+ options.seen.delete(value);
567
+ return items;
568
+ }
569
+ function truncateJsonObjectForOtelAttribute(value, options) {
570
+ if (options.seen.has(value)) return {
571
+ truncated: true,
572
+ reason: "circular_reference"
573
+ };
574
+ options.seen.add(value);
575
+ const nextOptions = {
576
+ ...options,
577
+ maxDepth: options.maxDepth - 1
578
+ };
579
+ const result = {};
580
+ const entries = Object.entries(value).filter(([, field]) => field !== void 0 && typeof field !== "function" && typeof field !== "symbol");
581
+ for (const [key, field] of entries.slice(0, options.maxObjectFields)) result[key] = truncateJsonValueForOtelAttribute(field, nextOptions);
582
+ if (entries.length > options.maxObjectFields) {
583
+ result.truncated = true;
584
+ result.omittedFields = entries.length - options.maxObjectFields;
585
+ }
586
+ options.seen.delete(value);
587
+ return result;
588
+ }
589
+ function truncateJsonTextForOtelAttribute(value, maxChars) {
590
+ const redacted = redactSensitiveText(value);
591
+ if (redacted.length <= maxChars) return redacted;
592
+ const suffixBudget = Math.min(14, maxChars);
593
+ const prefixBudget = Math.max(0, maxChars - suffixBudget);
594
+ return `${redacted.slice(0, prefixBudget)}${TRUNCATED_JSON_TEXT_SUFFIX.slice(14 - suffixBudget)}`;
595
+ }
596
+ function describeJsonValue(value) {
597
+ if (Array.isArray(value)) return "array";
598
+ if (value === null) return "null";
599
+ return typeof value;
600
+ }
601
+ function textPart(content) {
602
+ return {
603
+ type: "text",
604
+ content
605
+ };
606
+ }
607
+ function textPartContent(part) {
608
+ if (part.type !== "text") return;
609
+ if (typeof part.text === "string") return part.text;
610
+ return typeof part.content === "string" ? part.content : void 0;
611
+ }
612
+ function toolCallResponseValue(value) {
613
+ if (!Array.isArray(value)) return value;
614
+ const textItems = [];
615
+ for (const item of value) {
616
+ const text = typeof item === "string" ? item : isRecord(item) ? textPartContent(item) : void 0;
617
+ if (typeof text !== "string") return value;
618
+ textItems.push(text);
619
+ }
620
+ const kept = textItems.slice(0, MAX_OTEL_CONTENT_ARRAY_ITEMS);
621
+ const joined = kept.filter((text) => text.length > 0).join("\n");
622
+ if (joined.length === 0) return value;
623
+ const omitted = textItems.length - kept.length;
624
+ return omitted > 0 ? `${joined}\n...(${omitted} more text parts omitted)` : joined;
625
+ }
626
+ function toolCallResponsePart(part) {
627
+ return {
628
+ type: "tool_call_response",
629
+ ...typeof part.id === "string" ? { id: part.id } : {},
630
+ response: toolCallResponseValue(part.response ?? part.result ?? part.content ?? part.details ?? "")
631
+ };
632
+ }
633
+ function contentParts(value) {
634
+ if (typeof value === "string") return value.length > 0 ? [textPart(value)] : [];
635
+ if (!Array.isArray(value)) {
636
+ if (value === void 0 || value === null) return [];
637
+ if (typeof value === "number" || typeof value === "boolean" || typeof value === "bigint") return [textPart(String(value))];
638
+ const json = safeJsonString(value, MAX_OTEL_CONTENT_ATTRIBUTE_CHARS);
639
+ return json ? [textPart(json)] : [];
640
+ }
641
+ const parts = [];
642
+ for (const part of value) {
643
+ if (typeof part === "string") {
644
+ if (part.length > 0) parts.push(textPart(part));
645
+ continue;
646
+ }
647
+ if (!isRecord(part)) continue;
648
+ const text = textPartContent(part);
649
+ if (text !== void 0) parts.push(textPart(text));
650
+ else if (part.type === "thinking" && typeof part.thinking === "string") parts.push({
651
+ type: "reasoning",
652
+ content: part.thinking
653
+ });
654
+ else if (part.type === "toolCall" && typeof part.name === "string") parts.push({
655
+ type: "tool_call",
656
+ name: part.name,
657
+ ...typeof part.id === "string" ? { id: part.id } : {},
658
+ ...part.arguments !== void 0 ? { arguments: part.arguments } : {}
659
+ });
660
+ else if (part.type === "tool_call" && typeof part.name === "string") parts.push({
661
+ type: "tool_call",
662
+ name: part.name,
663
+ ...typeof part.id === "string" ? { id: part.id } : {},
664
+ ...part.arguments !== void 0 ? { arguments: part.arguments } : {}
665
+ });
666
+ else if (part.type === "tool_call_response") parts.push(toolCallResponsePart(part));
667
+ else if (part.type === "image") {
668
+ const data = typeof part.data === "string" ? part.data : void 0;
669
+ parts.push({
670
+ type: "blob",
671
+ modality: "image",
672
+ ...typeof part.mimeType === "string" ? { mime_type: part.mimeType } : {},
673
+ ...typeof part.mime_type === "string" ? { mime_type: part.mime_type } : {},
674
+ ...data ? { content: data } : {}
675
+ });
676
+ }
677
+ }
678
+ return parts;
679
+ }
680
+ function normalizeGenAiMessage(value, fallbackRole = "user") {
681
+ if (typeof value === "string") return {
682
+ role: fallbackRole,
683
+ parts: [textPart(value)]
684
+ };
685
+ if (!isRecord(value)) return;
686
+ const rawRole = typeof value.role === "string" ? value.role : fallbackRole;
687
+ const role = rawRole === "toolResult" ? "tool" : rawRole;
688
+ let parts;
689
+ if (role === "tool") {
690
+ const explicitParts = contentParts(value.parts);
691
+ parts = explicitParts.length > 0 ? explicitParts : [toolCallResponsePart({
692
+ id: value.toolCallId,
693
+ response: value.content ?? value.details ?? ""
694
+ })];
695
+ } else parts = contentParts(value.parts ?? value.content);
696
+ if (parts.length === 0) return;
697
+ return {
698
+ role,
699
+ parts,
700
+ ...typeof value.name === "string" ? { name: value.name } : {},
701
+ ...typeof value.finish_reason === "string" ? { finish_reason: value.finish_reason } : {},
702
+ ...typeof value.stopReason === "string" ? { finish_reason: value.stopReason } : {}
703
+ };
704
+ }
705
+ function normalizeGenAiMessages(value, fallbackRole) {
706
+ const source = Array.isArray(value) ? value : value === void 0 ? [] : [value];
707
+ const messages = [];
708
+ for (const item of source.slice(0, MAX_OTEL_CONTENT_ARRAY_ITEMS)) {
709
+ const message = normalizeGenAiMessage(item, fallbackRole);
710
+ if (message) messages.push(message);
711
+ }
712
+ return messages;
713
+ }
714
+ function normalizeGenAiToolDefinition(value) {
715
+ if (!isRecord(value) || typeof value.name !== "string" || value.name.trim().length === 0) return;
716
+ return {
717
+ type: typeof value.type === "string" ? value.type : "function",
718
+ name: value.name,
719
+ ...typeof value.description === "string" ? { description: value.description } : {},
720
+ ...value.parameters !== void 0 ? { parameters: value.parameters } : {}
721
+ };
722
+ }
723
+ function normalizeGenAiToolDefinitions(value) {
724
+ if (!Array.isArray(value)) return [];
725
+ const definitions = [];
726
+ for (const item of value.slice(0, MAX_OTEL_CONTENT_ARRAY_ITEMS)) {
727
+ const definition = normalizeGenAiToolDefinition(item);
728
+ if (definition) definitions.push(definition);
729
+ }
730
+ return definitions;
731
+ }
732
+ function assignJsonAttribute(attributes, key, value) {
733
+ const json = safeJsonString(value, MAX_OTEL_CONTENT_ATTRIBUTE_CHARS);
734
+ if (json) attributes[key] = json;
735
+ }
736
+ function assignGenAiModelContentAttributes(attributes, content, policy) {
737
+ if (policy.systemPrompt && typeof content?.systemPrompt === "string") assignJsonAttribute(attributes, ATTR_GEN_AI_SYSTEM_INSTRUCTIONS, [textPart(content.systemPrompt)]);
738
+ if (policy.inputMessages) {
739
+ const inputMessages = normalizeGenAiMessages(content?.inputMessages, "user");
740
+ if (inputMessages.length > 0) {
741
+ assignJsonAttribute(attributes, ATTR_GEN_AI_INPUT_MESSAGES, inputMessages);
742
+ assignJsonAttribute(attributes, "input.value", inputMessages);
743
+ attributes["input.mime_type"] = "application/json";
744
+ }
745
+ }
746
+ if (policy.toolDefinitions) {
747
+ const toolDefinitions = normalizeGenAiToolDefinitions(content?.toolDefinitions);
748
+ if (toolDefinitions.length > 0) assignJsonAttribute(attributes, ATTR_GEN_AI_TOOL_DEFINITIONS, toolDefinitions);
749
+ }
750
+ if (policy.outputMessages) {
751
+ const outputMessages = normalizeGenAiMessages(content?.outputMessages, "assistant");
752
+ if (outputMessages.length > 0) {
753
+ assignJsonAttribute(attributes, ATTR_GEN_AI_OUTPUT_MESSAGES, outputMessages);
754
+ assignJsonAttribute(attributes, "output.value", outputMessages);
755
+ attributes["output.mime_type"] = "application/json";
756
+ }
757
+ }
758
+ }
759
+ function assignOtelContentAttribute(attributes, key, value) {
760
+ const normalized = normalizeOtelContentValue(value);
761
+ if (normalized) attributes[key] = normalized;
762
+ }
763
+ function assignOtelToolIdentityAttributes(attributes, evt) {
764
+ attributes["gen_ai.operation.name"] = GEN_AI_OPERATION_NAME_VALUE_EXECUTE_TOOL;
765
+ const toolCallId = evt.toolCallId?.trim();
766
+ if (toolCallId) attributes[ATTR_GEN_AI_TOOL_CALL_ID] = toolCallId;
767
+ }
768
+ function assignOtelModelContentAttributes(attributes, content, policy) {
769
+ assignGenAiModelContentAttributes(attributes, content, policy);
770
+ if (policy.inputMessages) assignOtelContentAttribute(attributes, "openclaw.content.input_messages", content?.inputMessages);
771
+ if (policy.toolDefinitions) assignOtelContentAttribute(attributes, "openclaw.content.tool_definitions", content?.toolDefinitions);
772
+ if (policy.outputMessages) assignOtelContentAttribute(attributes, "openclaw.content.output_messages", content?.outputMessages);
773
+ if (policy.systemPrompt) assignOtelContentAttribute(attributes, "openclaw.content.system_prompt", content?.systemPrompt);
774
+ }
775
+ function assignOtelToolContentAttributes(attributes, content, policy) {
776
+ if (policy.toolInputs) {
777
+ const toolInput = normalizeOtelContentValue(content?.toolInput);
778
+ if (toolInput) {
779
+ attributes[ATTR_GEN_AI_TOOL_CALL_ARGUMENTS] = toolInput;
780
+ attributes["openclaw.content.tool_input"] = toolInput;
781
+ }
782
+ }
783
+ if (policy.toolOutputs) {
784
+ const toolOutput = normalizeOtelContentValue(content?.toolOutput);
785
+ if (toolOutput) {
786
+ attributes[ATTR_GEN_AI_TOOL_CALL_RESULT] = toolOutput;
787
+ attributes["openclaw.content.tool_output"] = toolOutput;
788
+ }
789
+ }
790
+ }
791
+ function assignOtelLogAttribute(attributes, key, value) {
792
+ if (Object.keys(attributes).length >= MAX_OTEL_LOG_ATTRIBUTE_COUNT) return;
793
+ if (BLOCKED_OTEL_LOG_ATTRIBUTE_KEYS.has(key)) return;
794
+ if (redactSensitiveText(key) !== key) return;
795
+ if (!OTEL_LOG_ATTRIBUTE_KEY_RE.test(key)) return;
796
+ if (typeof value === "string") {
797
+ attributes[key] = normalizeOtelLogString(value, MAX_OTEL_LOG_ATTRIBUTE_VALUE_CHARS);
798
+ return;
799
+ }
800
+ if (typeof value === "number" && Number.isFinite(value)) {
801
+ attributes[key] = value;
802
+ return;
803
+ }
804
+ if (typeof value === "boolean") attributes[key] = value;
805
+ }
806
+ function normalizeTraceContext(value) {
807
+ if (!value || typeof value !== "object" || Array.isArray(value)) return;
808
+ const candidate = value;
809
+ if (!isValidDiagnosticTraceId(candidate.traceId)) return;
810
+ if (candidate.spanId !== void 0 && !isValidDiagnosticSpanId(candidate.spanId)) return;
811
+ if (candidate.parentSpanId !== void 0 && !isValidDiagnosticSpanId(candidate.parentSpanId)) return;
812
+ if (candidate.traceFlags !== void 0 && !isValidDiagnosticTraceFlags(candidate.traceFlags)) return;
813
+ return {
814
+ traceId: candidate.traceId,
815
+ ...candidate.spanId ? { spanId: candidate.spanId } : {},
816
+ ...candidate.parentSpanId ? { parentSpanId: candidate.parentSpanId } : {},
817
+ ...candidate.traceFlags ? { traceFlags: candidate.traceFlags } : {}
818
+ };
819
+ }
820
+ function assignOtelLogEventAttributes(attributes, eventAttributes) {
821
+ if (!eventAttributes) return;
822
+ for (const rawKey in eventAttributes) {
823
+ if (Object.keys(attributes).length >= MAX_OTEL_LOG_ATTRIBUTE_COUNT) break;
824
+ if (!Object.hasOwn(eventAttributes, rawKey)) continue;
825
+ const key = rawKey.trim();
826
+ if (BLOCKED_OTEL_LOG_ATTRIBUTE_KEYS.has(key)) continue;
827
+ if (redactSensitiveText(key) !== key) continue;
828
+ if (!OTEL_LOG_RAW_ATTRIBUTE_KEY_RE.test(key)) continue;
829
+ assignOtelLogAttribute(attributes, `openclaw.${key}`, eventAttributes[rawKey]);
830
+ }
831
+ }
832
+ function assignOtelSecurityEventAttributes(attributes, eventAttributes) {
833
+ if (!eventAttributes) return;
834
+ for (const rawKey in eventAttributes) {
835
+ if (Object.keys(attributes).length >= MAX_OTEL_LOG_ATTRIBUTE_COUNT) break;
836
+ if (!Object.hasOwn(eventAttributes, rawKey)) continue;
837
+ const key = rawKey.trim();
838
+ if (BLOCKED_OTEL_LOG_ATTRIBUTE_KEYS.has(key)) continue;
839
+ if (redactSensitiveText(key) !== key) continue;
840
+ if (!OTEL_LOG_RAW_ATTRIBUTE_KEY_RE.test(key)) continue;
841
+ const value = eventAttributes[rawKey];
842
+ assignOtelLogAttribute(attributes, `openclaw.security.attribute.${key}`, typeof value === "string" ? lowCardinalityAttr(value) : value);
843
+ }
844
+ }
845
+ function securitySeverityText(severity) {
846
+ switch (severity) {
847
+ case "critical": return "FATAL";
848
+ case "high": return "ERROR";
849
+ case "medium": return "WARN";
850
+ case "info":
851
+ case "low": return "INFO";
852
+ }
853
+ return severity;
854
+ }
855
+ function assignOtelSecurityAttributes(attributes, evt) {
856
+ assignOtelLogAttribute(attributes, "openclaw.security.event_id", evt.eventId);
857
+ assignOtelLogAttribute(attributes, "openclaw.security.category", evt.category);
858
+ assignOtelLogAttribute(attributes, "openclaw.security.action", lowCardinalityAttr(evt.action));
859
+ assignOtelLogAttribute(attributes, "openclaw.security.outcome", evt.outcome);
860
+ assignOtelLogAttribute(attributes, "openclaw.security.severity", evt.severity);
861
+ if (evt.reason) assignOtelLogAttribute(attributes, "openclaw.security.reason", lowCardinalityAttr(evt.reason));
862
+ if (evt.actor) {
863
+ assignOtelLogAttribute(attributes, "openclaw.security.actor.kind", evt.actor.kind);
864
+ if (evt.actor.idHash) assignOtelLogAttribute(attributes, "openclaw.security.actor.id_hash", lowCardinalityAttr(evt.actor.idHash));
865
+ if (evt.actor.deviceIdHash) assignOtelLogAttribute(attributes, "openclaw.security.actor.device_id_hash", lowCardinalityAttr(evt.actor.deviceIdHash));
866
+ if (evt.actor.channel) assignOtelLogAttribute(attributes, "openclaw.security.actor.channel", lowCardinalityAttr(evt.actor.channel));
867
+ if (evt.actor.role) assignOtelLogAttribute(attributes, "openclaw.security.actor.role", lowCardinalityAttr(evt.actor.role));
868
+ if (evt.actor.scopes?.length) assignOtelLogAttribute(attributes, "openclaw.security.actor.scopes", evt.actor.scopes.map((scope) => lowCardinalityAttr(scope)).join(","));
869
+ }
870
+ if (evt.target) {
871
+ assignOtelLogAttribute(attributes, "openclaw.security.target.kind", evt.target.kind);
872
+ if (evt.target.idHash) assignOtelLogAttribute(attributes, "openclaw.security.target.id_hash", lowCardinalityAttr(evt.target.idHash));
873
+ if (evt.target.name) assignOtelLogAttribute(attributes, "openclaw.security.target.name", securityTargetNameAttr(evt.target.name));
874
+ if (evt.target.owner) assignOtelLogAttribute(attributes, "openclaw.security.target.owner", lowCardinalityAttr(evt.target.owner));
875
+ }
876
+ if (evt.policy) {
877
+ if (evt.policy.id) assignOtelLogAttribute(attributes, "openclaw.security.policy.id", lowCardinalityAttr(evt.policy.id));
878
+ if (evt.policy.decision) assignOtelLogAttribute(attributes, "openclaw.security.policy.decision", evt.policy.decision);
879
+ if (evt.policy.reason) assignOtelLogAttribute(attributes, "openclaw.security.policy.reason", lowCardinalityAttr(evt.policy.reason));
880
+ }
881
+ if (evt.control) {
882
+ if (evt.control.id) assignOtelLogAttribute(attributes, "openclaw.security.control.id", lowCardinalityAttr(evt.control.id));
883
+ if (evt.control.family) assignOtelLogAttribute(attributes, "openclaw.security.control.family", evt.control.family);
884
+ }
885
+ assignOtelSecurityEventAttributes(attributes, evt.attributes);
886
+ }
887
+ function traceFlagsToOtel(traceFlags) {
888
+ return (Number.parseInt(traceFlags ?? "00", 16) & TraceFlags.SAMPLED) !== 0 ? TraceFlags.SAMPLED : TraceFlags.NONE;
889
+ }
890
+ function contextForTraceContext(traceContext) {
891
+ const normalized = normalizeTraceContext(traceContext);
892
+ if (!normalized?.spanId) return;
893
+ return trace.setSpanContext(context.active(), {
894
+ traceId: normalized.traceId,
895
+ spanId: normalized.spanId,
896
+ traceFlags: traceFlagsToOtel(normalized.traceFlags),
897
+ isRemote: true
898
+ });
899
+ }
900
+ function contextForTrustedTraceContext(evt, metadata) {
901
+ return metadata.trusted || metadata.trustedTraceContext === true ? contextForTraceContext(evt.trace) : void 0;
902
+ }
903
+ function normalizedTrustedTraceContext(evt, metadata) {
904
+ return metadata.trusted || metadata.trustedTraceContext === true ? normalizeTraceContext(evt.trace) : void 0;
905
+ }
906
+ function addTraceAttributes(attributes, traceContext) {
907
+ const normalized = normalizeTraceContext(traceContext);
908
+ if (!normalized) return;
909
+ attributes["openclaw.traceId"] = normalized.traceId;
910
+ if (normalized.spanId) attributes["openclaw.spanId"] = normalized.spanId;
911
+ if (normalized.parentSpanId) attributes["openclaw.parentSpanId"] = normalized.parentSpanId;
912
+ if (normalized.traceFlags) attributes["openclaw.traceFlags"] = normalized.traceFlags;
913
+ }
914
+ function createDiagnosticsOtelService() {
915
+ let sdk = null;
916
+ let logProvider = null;
917
+ let unsubscribe = null;
918
+ let stopActiveTrustedSpans = null;
919
+ let unregisterUnhandledRejectionHandler = null;
920
+ const stopStarted = async () => {
921
+ const currentUnsubscribe = unsubscribe;
922
+ const currentLogProvider = logProvider;
923
+ const currentSdk = sdk;
924
+ const currentStopActiveTrustedSpans = stopActiveTrustedSpans;
925
+ const currentUnregisterUnhandledRejectionHandler = unregisterUnhandledRejectionHandler;
926
+ unsubscribe = null;
927
+ logProvider = null;
928
+ sdk = null;
929
+ stopActiveTrustedSpans = null;
930
+ unregisterUnhandledRejectionHandler = null;
931
+ currentUnregisterUnhandledRejectionHandler?.();
932
+ currentUnsubscribe?.();
933
+ currentStopActiveTrustedSpans?.();
934
+ if (currentLogProvider) await currentLogProvider.shutdown().catch(() => void 0);
935
+ if (currentSdk) await currentSdk.shutdown().catch(() => void 0);
936
+ };
937
+ return {
938
+ id: "diagnostics-otel",
939
+ async start(ctx) {
940
+ await stopStarted();
941
+ const cfg = ctx.config.diagnostics;
942
+ const otel = cfg?.otel;
943
+ if (!cfg || cfg.enabled === false || !otel?.enabled) return;
944
+ const emitExporterEvent = (event) => {
945
+ try {
946
+ ctx.internalDiagnostics?.emit({
947
+ type: "telemetry.exporter",
948
+ ...event
949
+ });
950
+ } catch {}
951
+ };
952
+ const emitForSignals = (signals, event) => {
953
+ for (const signal of signals) emitExporterEvent({
954
+ signal,
955
+ ...event
956
+ });
957
+ };
958
+ const tracesEnabled = otel.traces !== false;
959
+ const metricsEnabled = otel.metrics !== false;
960
+ const logsEnabled = otel.logs === true;
961
+ const logsExporter = otel.logsExporter ?? "otlp";
962
+ const logsToOtlp = logsEnabled && (logsExporter === "otlp" || logsExporter === "both");
963
+ const logsToStdout = logsEnabled && (logsExporter === "stdout" || logsExporter === "both");
964
+ const otlpSignals = [
965
+ ...tracesEnabled ? ["traces"] : [],
966
+ ...metricsEnabled ? ["metrics"] : [],
967
+ ...logsToOtlp ? ["logs"] : []
968
+ ];
969
+ const enabledSignals = [
970
+ ...tracesEnabled ? ["traces"] : [],
971
+ ...metricsEnabled ? ["metrics"] : [],
972
+ ...logsEnabled ? ["logs"] : []
973
+ ];
974
+ if (enabledSignals.length === 0) return;
975
+ const protocol = otel.protocol ?? process.env.OTEL_EXPORTER_OTLP_PROTOCOL ?? "http/protobuf";
976
+ if (otlpSignals.length > 0 && protocol !== "http/protobuf") {
977
+ emitForSignals(otlpSignals, {
978
+ exporter: "diagnostics-otel",
979
+ status: "failure",
980
+ reason: "unsupported_protocol"
981
+ });
982
+ ctx.logger.warn(`diagnostics-otel: unsupported protocol ${protocol}`);
983
+ return;
984
+ }
985
+ const endpoint = normalizeEndpoint(otel.endpoint ?? process.env[OTEL_EXPORTER_OTLP_ENDPOINT_ENV]);
986
+ const headers = otel.headers ?? void 0;
987
+ const serviceName = otel.serviceName?.trim() || process.env.OTEL_SERVICE_NAME || DEFAULT_SERVICE_NAME;
988
+ const sampleRate = resolveSampleRate(otel.sampleRate);
989
+ const contentCapturePolicy = resolveContentCapturePolicy(otel.captureContent);
990
+ const sdkPreloaded = hasPreloadedOtelSdk();
991
+ const resource = resourceFromAttributes({ [ATTR_SERVICE_NAME]: serviceName });
992
+ const logUrl = resolveSignalOtelUrl({
993
+ signalEndpoint: otel.logsEndpoint,
994
+ signalEnvEndpoint: process.env[OTEL_EXPORTER_OTLP_LOGS_ENDPOINT_ENV],
995
+ endpoint,
996
+ path: "v1/logs"
997
+ });
998
+ if (!sdkPreloaded && (tracesEnabled || metricsEnabled)) {
999
+ const traceUrl = resolveSignalOtelUrl({
1000
+ signalEndpoint: otel.tracesEndpoint,
1001
+ signalEnvEndpoint: process.env[OTEL_EXPORTER_OTLP_TRACES_ENDPOINT_ENV],
1002
+ endpoint,
1003
+ path: "v1/traces"
1004
+ });
1005
+ const metricUrl = resolveSignalOtelUrl({
1006
+ signalEndpoint: otel.metricsEndpoint,
1007
+ signalEnvEndpoint: process.env[OTEL_EXPORTER_OTLP_METRICS_ENDPOINT_ENV],
1008
+ endpoint,
1009
+ path: "v1/metrics"
1010
+ });
1011
+ const traceHttpAgentOptions = resolveOtelHttpAgentOptions({
1012
+ url: traceUrl,
1013
+ signalIdentifier: "TRACES",
1014
+ logger: ctx.logger
1015
+ });
1016
+ const metricHttpAgentOptions = resolveOtelHttpAgentOptions({
1017
+ url: metricUrl,
1018
+ signalIdentifier: "METRICS",
1019
+ logger: ctx.logger
1020
+ });
1021
+ const traceExporter = tracesEnabled ? new OTLPTraceExporter({
1022
+ ...traceUrl ? { url: traceUrl } : {},
1023
+ ...headers ? { headers } : {},
1024
+ ...traceHttpAgentOptions ? { httpAgentOptions: traceHttpAgentOptions } : {}
1025
+ }) : void 0;
1026
+ const spanProcessors = traceExporter && typeof otel.flushIntervalMs === "number" ? [new BatchSpanProcessor(traceExporter, { scheduledDelayMillis: Math.max(1e3, otel.flushIntervalMs) })] : void 0;
1027
+ const metricExporter = metricsEnabled ? new OTLPMetricExporter({
1028
+ ...metricUrl ? { url: metricUrl } : {},
1029
+ ...headers ? { headers } : {},
1030
+ ...metricHttpAgentOptions ? { httpAgentOptions: metricHttpAgentOptions } : {}
1031
+ }) : void 0;
1032
+ const metricReader = metricExporter ? new PeriodicExportingMetricReader({
1033
+ exporter: metricExporter,
1034
+ ...typeof otel.flushIntervalMs === "number" ? { exportIntervalMillis: Math.max(1e3, otel.flushIntervalMs) } : {}
1035
+ }) : void 0;
1036
+ sdk = new NodeSDK({
1037
+ resource,
1038
+ ...spanProcessors ? { spanProcessors } : traceExporter ? { traceExporter } : {},
1039
+ ...metricReader ? { metricReader } : {},
1040
+ ...sampleRate !== void 0 ? { sampler: new ParentBasedSampler({ root: new TraceIdRatioBasedSampler(sampleRate) }) } : {}
1041
+ });
1042
+ try {
1043
+ sdk.start();
1044
+ } catch (err) {
1045
+ emitForSignals([...tracesEnabled ? ["traces"] : [], ...metricsEnabled ? ["metrics"] : []], {
1046
+ exporter: "diagnostics-otel",
1047
+ status: "failure",
1048
+ reason: "start_failed",
1049
+ errorCategory: errorCategory(err)
1050
+ });
1051
+ await stopStarted();
1052
+ ctx.logger.error(`diagnostics-otel: failed to start SDK: ${formatError(err)}`);
1053
+ throw err;
1054
+ }
1055
+ } else if (sdkPreloaded && (tracesEnabled || metricsEnabled)) ctx.logger.info("diagnostics-otel: using preloaded OpenTelemetry SDK");
1056
+ const logSeverityMap = {
1057
+ TRACE: 1,
1058
+ DEBUG: 5,
1059
+ INFO: 9,
1060
+ WARN: 13,
1061
+ ERROR: 17,
1062
+ FATAL: 21
1063
+ };
1064
+ const meter = metrics.getMeter("openclaw");
1065
+ const tracer = trace.getTracer("openclaw");
1066
+ const activeTrustedSpans = /* @__PURE__ */ new Map();
1067
+ const activeTrustedSpanAliases = /* @__PURE__ */ new Map();
1068
+ const retainedTrustedSpanContexts = /* @__PURE__ */ new Map();
1069
+ const retainedTrustedSpanContextCleanupTimers = /* @__PURE__ */ new Set();
1070
+ stopActiveTrustedSpans = () => {
1071
+ const stopAt = Date.now();
1072
+ for (const handle of retainedTrustedSpanContextCleanupTimers) clearTimeout(handle);
1073
+ retainedTrustedSpanContextCleanupTimers.clear();
1074
+ retainedTrustedSpanContexts.clear();
1075
+ for (const span of /* @__PURE__ */ new Set([...activeTrustedSpans.values(), ...Array.from(activeTrustedSpanAliases.values(), (entry) => entry.span)])) span.end(stopAt);
1076
+ activeTrustedSpans.clear();
1077
+ activeTrustedSpanAliases.clear();
1078
+ };
1079
+ const tokensCounter = meter.createCounter("openclaw.tokens", {
1080
+ unit: "1",
1081
+ description: "Token usage by type"
1082
+ });
1083
+ const genAiTokenUsageHistogram = meter.createHistogram("gen_ai.client.token.usage", {
1084
+ unit: "{token}",
1085
+ description: "Number of input and output tokens used by GenAI client operations",
1086
+ advice: { explicitBucketBoundaries: GEN_AI_TOKEN_USAGE_BUCKETS }
1087
+ });
1088
+ const genAiOperationDurationHistogram = meter.createHistogram("gen_ai.client.operation.duration", {
1089
+ unit: "s",
1090
+ description: "GenAI client operation duration",
1091
+ advice: { explicitBucketBoundaries: GEN_AI_OPERATION_DURATION_BUCKETS }
1092
+ });
1093
+ const costCounter = meter.createCounter("openclaw.cost.usd", {
1094
+ unit: "1",
1095
+ description: "Estimated model cost (USD)"
1096
+ });
1097
+ const durationHistogram = meter.createHistogram("openclaw.run.duration_ms", {
1098
+ unit: "ms",
1099
+ description: "Agent run duration",
1100
+ advice: { explicitBucketBoundaries: AGENT_DURATION_MS_BUCKETS }
1101
+ });
1102
+ const harnessDurationHistogram = meter.createHistogram("openclaw.harness.duration_ms", {
1103
+ unit: "ms",
1104
+ description: "Agent harness lifecycle duration",
1105
+ advice: { explicitBucketBoundaries: AGENT_DURATION_MS_BUCKETS }
1106
+ });
1107
+ const contextHistogram = meter.createHistogram("openclaw.context.tokens", {
1108
+ unit: "1",
1109
+ description: "Context window size and usage",
1110
+ advice: { explicitBucketBoundaries: CONTEXT_TOKENS_BUCKETS }
1111
+ });
1112
+ const webhookReceivedCounter = meter.createCounter("openclaw.webhook.received", {
1113
+ unit: "1",
1114
+ description: "Webhook requests received"
1115
+ });
1116
+ const webhookErrorCounter = meter.createCounter("openclaw.webhook.error", {
1117
+ unit: "1",
1118
+ description: "Webhook processing errors"
1119
+ });
1120
+ const webhookDurationHistogram = meter.createHistogram("openclaw.webhook.duration_ms", {
1121
+ unit: "ms",
1122
+ description: "Webhook processing duration"
1123
+ });
1124
+ const messageQueuedCounter = meter.createCounter("openclaw.message.queued", {
1125
+ unit: "1",
1126
+ description: "Messages queued for processing"
1127
+ });
1128
+ const messageReceivedCounter = meter.createCounter("openclaw.message.received", {
1129
+ unit: "1",
1130
+ description: "Inbound messages received"
1131
+ });
1132
+ const messageDispatchStartedCounter = meter.createCounter("openclaw.message.dispatch.started", {
1133
+ unit: "1",
1134
+ description: "Inbound message dispatch attempts started"
1135
+ });
1136
+ const messageDispatchCompletedCounter = meter.createCounter("openclaw.message.dispatch.completed", {
1137
+ unit: "1",
1138
+ description: "Inbound message dispatch attempts completed"
1139
+ });
1140
+ const messageDispatchDurationHistogram = meter.createHistogram("openclaw.message.dispatch.duration_ms", {
1141
+ unit: "ms",
1142
+ description: "Inbound message dispatch duration"
1143
+ });
1144
+ const messageProcessedCounter = meter.createCounter("openclaw.message.processed", {
1145
+ unit: "1",
1146
+ description: "Messages processed by outcome"
1147
+ });
1148
+ const messageDurationHistogram = meter.createHistogram("openclaw.message.duration_ms", {
1149
+ unit: "ms",
1150
+ description: "Message processing duration"
1151
+ });
1152
+ const messageDeliveryStartedCounter = meter.createCounter("openclaw.message.delivery.started", {
1153
+ unit: "1",
1154
+ description: "Outbound message delivery attempts started"
1155
+ });
1156
+ const messageDeliveryDurationHistogram = meter.createHistogram("openclaw.message.delivery.duration_ms", {
1157
+ unit: "ms",
1158
+ description: "Outbound message delivery duration"
1159
+ });
1160
+ const queueDepthHistogram = meter.createHistogram("openclaw.queue.depth", {
1161
+ unit: "1",
1162
+ description: "Queue depth on enqueue/dequeue"
1163
+ });
1164
+ const queueWaitHistogram = meter.createHistogram("openclaw.queue.wait_ms", {
1165
+ unit: "ms",
1166
+ description: "Queue wait time before execution"
1167
+ });
1168
+ const laneEnqueueCounter = meter.createCounter("openclaw.queue.lane.enqueue", {
1169
+ unit: "1",
1170
+ description: "Command queue lane enqueue events"
1171
+ });
1172
+ const laneDequeueCounter = meter.createCounter("openclaw.queue.lane.dequeue", {
1173
+ unit: "1",
1174
+ description: "Command queue lane dequeue events"
1175
+ });
1176
+ const sessionStateCounter = meter.createCounter("openclaw.session.state", {
1177
+ unit: "1",
1178
+ description: "Session state transitions"
1179
+ });
1180
+ const sessionTurnCreatedCounter = meter.createCounter("openclaw.session.turn.created", {
1181
+ unit: "1",
1182
+ description: "Agent session turns created"
1183
+ });
1184
+ const sessionStuckCounter = meter.createCounter("openclaw.session.stuck", {
1185
+ unit: "1",
1186
+ description: "Sessions stuck in processing"
1187
+ });
1188
+ const sessionStuckAgeHistogram = meter.createHistogram("openclaw.session.stuck_age_ms", {
1189
+ unit: "ms",
1190
+ description: "Age of stuck sessions"
1191
+ });
1192
+ const sessionRecoveryRequestedCounter = meter.createCounter("openclaw.session.recovery.requested", {
1193
+ unit: "1",
1194
+ description: "Session recovery attempts requested"
1195
+ });
1196
+ const sessionRecoveryCompletedCounter = meter.createCounter("openclaw.session.recovery.completed", {
1197
+ unit: "1",
1198
+ description: "Session recovery attempts completed"
1199
+ });
1200
+ const sessionRecoveryAgeHistogram = meter.createHistogram("openclaw.session.recovery.age_ms", {
1201
+ unit: "ms",
1202
+ description: "Age of sessions selected for recovery"
1203
+ });
1204
+ const talkEventCounter = meter.createCounter("openclaw.talk.event", {
1205
+ unit: "1",
1206
+ description: "Talk events emitted by type"
1207
+ });
1208
+ const talkEventDurationHistogram = meter.createHistogram("openclaw.talk.event.duration_ms", {
1209
+ unit: "ms",
1210
+ description: "Talk event duration when reported"
1211
+ });
1212
+ const talkAudioBytesHistogram = meter.createHistogram("openclaw.talk.audio.bytes", {
1213
+ unit: "By",
1214
+ description: "Talk audio frame byte lengths"
1215
+ });
1216
+ const runAttemptCounter = meter.createCounter("openclaw.run.attempt", {
1217
+ unit: "1",
1218
+ description: "Run attempts"
1219
+ });
1220
+ const toolLoopCounter = meter.createCounter("openclaw.tool.loop", {
1221
+ unit: "1",
1222
+ description: "Detected repetitive tool-call loop events"
1223
+ });
1224
+ const skillUsedCounter = meter.createCounter("openclaw.skill.used", {
1225
+ unit: "1",
1226
+ description: "Skills used by agent runs"
1227
+ });
1228
+ const modelCallDurationHistogram = meter.createHistogram("openclaw.model_call.duration_ms", {
1229
+ unit: "ms",
1230
+ description: "Model call duration"
1231
+ });
1232
+ const modelCallRequestBytesHistogram = meter.createHistogram("openclaw.model_call.request_bytes", {
1233
+ unit: "By",
1234
+ description: "UTF-8 byte size of sanitized model request payloads"
1235
+ });
1236
+ const modelCallResponseBytesHistogram = meter.createHistogram("openclaw.model_call.response_bytes", {
1237
+ unit: "By",
1238
+ description: "UTF-8 byte size of bounded streamed model response payloads"
1239
+ });
1240
+ const modelCallTimeToFirstByteHistogram = meter.createHistogram("openclaw.model_call.time_to_first_byte_ms", {
1241
+ unit: "ms",
1242
+ description: "Elapsed time before the first streamed model response event"
1243
+ });
1244
+ const modelFailoverCounter = meter.createCounter("openclaw.model.failover", {
1245
+ unit: "1",
1246
+ description: "Model failovers by source, destination, lane, and reason"
1247
+ });
1248
+ const toolExecutionDurationHistogram = meter.createHistogram("openclaw.tool.execution.duration_ms", {
1249
+ unit: "ms",
1250
+ description: "Tool execution duration"
1251
+ });
1252
+ const toolExecutionBlockedCounter = meter.createCounter("openclaw.tool.execution.blocked", {
1253
+ unit: "1",
1254
+ description: "Tool executions blocked by policy or sandbox diagnostics"
1255
+ });
1256
+ const execProcessDurationHistogram = meter.createHistogram("openclaw.exec.duration_ms", {
1257
+ unit: "ms",
1258
+ description: "Exec process duration"
1259
+ });
1260
+ const memoryRssHistogram = meter.createHistogram("openclaw.memory.rss_bytes", {
1261
+ unit: "By",
1262
+ description: "Resident set size reported by diagnostic memory samples"
1263
+ });
1264
+ const memoryHeapUsedHistogram = meter.createHistogram("openclaw.memory.heap_used_bytes", {
1265
+ unit: "By",
1266
+ description: "Heap used bytes reported by diagnostic memory samples"
1267
+ });
1268
+ const memoryHeapTotalHistogram = meter.createHistogram("openclaw.memory.heap_total_bytes", {
1269
+ unit: "By",
1270
+ description: "Heap total bytes reported by diagnostic memory samples"
1271
+ });
1272
+ const memoryExternalHistogram = meter.createHistogram("openclaw.memory.external_bytes", {
1273
+ unit: "By",
1274
+ description: "External memory bytes reported by diagnostic memory samples"
1275
+ });
1276
+ const memoryArrayBuffersHistogram = meter.createHistogram("openclaw.memory.array_buffers_bytes", {
1277
+ unit: "By",
1278
+ description: "ArrayBuffer bytes reported by diagnostic memory samples"
1279
+ });
1280
+ const memoryPressureCounter = meter.createCounter("openclaw.memory.pressure", {
1281
+ unit: "1",
1282
+ description: "Diagnostic memory pressure events"
1283
+ });
1284
+ const asyncQueueDroppedCounter = meter.createCounter("openclaw.diagnostic.async_queue.dropped", {
1285
+ unit: "1",
1286
+ description: "Async diagnostic queue drops by dropped event class"
1287
+ });
1288
+ const payloadLargeCounter = meter.createCounter("openclaw.payload.large", {
1289
+ unit: "1",
1290
+ description: "Oversized payload diagnostics by surface and action"
1291
+ });
1292
+ const payloadLargeBytesHistogram = meter.createHistogram("openclaw.payload.large_bytes", {
1293
+ unit: "By",
1294
+ description: "Oversized payload byte sizes by surface and action"
1295
+ });
1296
+ const livenessWarningCounter = meter.createCounter("openclaw.liveness.warning", {
1297
+ unit: "1",
1298
+ description: "Diagnostic liveness warning events"
1299
+ });
1300
+ const livenessEventLoopDelayP99Histogram = meter.createHistogram("openclaw.liveness.event_loop_delay_p99_ms", {
1301
+ unit: "ms",
1302
+ description: "P99 event-loop delay reported by diagnostic liveness warnings"
1303
+ });
1304
+ const livenessEventLoopDelayMaxHistogram = meter.createHistogram("openclaw.liveness.event_loop_delay_max_ms", {
1305
+ unit: "ms",
1306
+ description: "Maximum event-loop delay reported by diagnostic liveness warnings"
1307
+ });
1308
+ const livenessEventLoopUtilizationHistogram = meter.createHistogram("openclaw.liveness.event_loop_utilization", {
1309
+ unit: "1",
1310
+ description: "Event-loop utilization reported by diagnostic liveness warnings"
1311
+ });
1312
+ const livenessCpuCoreRatioHistogram = meter.createHistogram("openclaw.liveness.cpu_core_ratio", {
1313
+ unit: "1",
1314
+ description: "CPU core ratio reported by diagnostic liveness warnings"
1315
+ });
1316
+ const telemetryExporterCounter = meter.createCounter("openclaw.telemetry.exporter.events", {
1317
+ unit: "1",
1318
+ description: "Diagnostic telemetry exporter lifecycle and failure events"
1319
+ });
1320
+ let recordLogRecord;
1321
+ let recordSecurityEvent;
1322
+ if (logsEnabled) {
1323
+ let logRecordExportFailureLastReportedAt = Number.NEGATIVE_INFINITY;
1324
+ let otelLogger;
1325
+ if (logsToOtlp) {
1326
+ const logHttpAgentOptions = resolveOtelHttpAgentOptions({
1327
+ url: logUrl,
1328
+ signalIdentifier: "LOGS",
1329
+ logger: ctx.logger
1330
+ });
1331
+ logProvider = new LoggerProvider({
1332
+ resource,
1333
+ processors: [new BatchLogRecordProcessor(new OTLPLogExporter({
1334
+ ...logUrl ? { url: logUrl } : {},
1335
+ ...headers ? { headers } : {},
1336
+ ...logHttpAgentOptions ? { httpAgentOptions: logHttpAgentOptions } : {}
1337
+ }), typeof otel.flushIntervalMs === "number" ? { scheduledDelayMillis: Math.max(1e3, otel.flushIntervalMs) } : {})]
1338
+ });
1339
+ otelLogger = logProvider.getLogger("openclaw");
1340
+ }
1341
+ const reportLogExportFailure = (err, label) => {
1342
+ emitExporterEvent({
1343
+ exporter: "diagnostics-otel",
1344
+ signal: "logs",
1345
+ status: "failure",
1346
+ reason: "emit_failed",
1347
+ errorCategory: errorCategory(err)
1348
+ });
1349
+ const now = Date.now();
1350
+ if (now - logRecordExportFailureLastReportedAt >= LOG_RECORD_EXPORT_FAILURE_REPORT_INTERVAL_MS) {
1351
+ logRecordExportFailureLastReportedAt = now;
1352
+ ctx.logger.error(`diagnostics-otel: ${label} export failed: ${formatError(err)}`);
1353
+ }
1354
+ };
1355
+ const emitLogRecord = ({ logRecord, traceContext }) => {
1356
+ if (logsToOtlp) otelLogger?.emit(logRecord);
1357
+ if (logsToStdout) writeStdoutDiagnosticLogRecord({
1358
+ logRecord,
1359
+ serviceName,
1360
+ ...traceContext ? { traceContext } : {}
1361
+ });
1362
+ };
1363
+ const buildDiagnosticLogRecord = (evt, metadata) => {
1364
+ const logLevelName = evt.level || "INFO";
1365
+ const severityNumber = logSeverityMap[logLevelName] ?? 9;
1366
+ const body = shouldCaptureOtelLogBody(contentCapturePolicy) ? normalizeOtelLogString(evt.message || "log", MAX_OTEL_LOG_BODY_CHARS) : "log";
1367
+ const attributes = Object.create(null);
1368
+ assignOtelLogAttribute(attributes, "openclaw.log.level", logLevelName);
1369
+ if (evt.loggerName) assignOtelLogAttribute(attributes, "openclaw.logger", evt.loggerName);
1370
+ if (evt.loggerParents?.length) assignOtelLogAttribute(attributes, "openclaw.logger.parents", evt.loggerParents.join("."));
1371
+ assignOtelLogEventAttributes(attributes, evt.attributes);
1372
+ if (evt.code?.line) assignOtelLogAttribute(attributes, "code.lineno", evt.code.line);
1373
+ if (evt.code?.functionName) assignOtelLogAttribute(attributes, "code.function", evt.code.functionName);
1374
+ const traceContext = normalizedTrustedTraceContext(evt, metadata);
1375
+ addTraceAttributes(attributes, traceContext);
1376
+ const logRecord = {
1377
+ body,
1378
+ severityText: logLevelName,
1379
+ severityNumber,
1380
+ attributes: redactOtelAttributes(attributes),
1381
+ timestamp: evt.ts
1382
+ };
1383
+ const logContext = contextForTrustedTraceContext(evt, metadata);
1384
+ if (logContext) logRecord.context = logContext;
1385
+ return {
1386
+ logRecord,
1387
+ ...traceContext ? { traceContext } : {}
1388
+ };
1389
+ };
1390
+ const buildSecurityLogRecord = (evt, metadata) => {
1391
+ const severityText = securitySeverityText(evt.severity);
1392
+ const attributes = Object.create(null);
1393
+ assignOtelSecurityAttributes(attributes, evt);
1394
+ const traceContext = normalizedTrustedTraceContext(evt, metadata);
1395
+ const logRecord = {
1396
+ body: "openclaw.security.event",
1397
+ severityText,
1398
+ severityNumber: logSeverityMap[severityText] ?? 9,
1399
+ attributes: redactOtelAttributes(attributes),
1400
+ timestamp: evt.ts
1401
+ };
1402
+ const logContext = contextForTrustedTraceContext(evt, metadata);
1403
+ if (logContext) logRecord.context = logContext;
1404
+ return {
1405
+ logRecord,
1406
+ ...traceContext ? { traceContext } : {}
1407
+ };
1408
+ };
1409
+ recordLogRecord = (evt, metadata) => {
1410
+ try {
1411
+ emitLogRecord(buildDiagnosticLogRecord(evt, metadata));
1412
+ } catch (err) {
1413
+ reportLogExportFailure(err, "log record");
1414
+ }
1415
+ };
1416
+ recordSecurityEvent = (evt, metadata) => {
1417
+ if (!metadata.trusted) return;
1418
+ try {
1419
+ emitLogRecord(buildSecurityLogRecord(evt, metadata));
1420
+ } catch (err) {
1421
+ reportLogExportFailure(err, "security event");
1422
+ }
1423
+ };
1424
+ }
1425
+ const spanWithDuration = (name, attributes, durationMs, options = {}) => {
1426
+ const endTimeMs = options.endTimeMs ?? Date.now();
1427
+ const startTime = typeof options.startTimeMs === "number" ? options.startTimeMs : typeof durationMs === "number" && durationMs >= 0 ? endTimeMs - durationMs : void 0;
1428
+ const parentContext = "parentContext" in options ? options.parentContext ?? void 0 : void 0;
1429
+ return tracer.startSpan(name, {
1430
+ attributes: redactOtelAttributes(attributes),
1431
+ ...options.kind !== void 0 ? { kind: options.kind } : {},
1432
+ ...startTime !== void 0 ? { startTime } : {}
1433
+ }, parentContext);
1434
+ };
1435
+ const trustedTraceContext = (evt, metadata) => metadata.trusted ? normalizeTraceContext(evt.trace) : void 0;
1436
+ const internalOrTrustedTraceContext = (evt, metadata) => metadata.trusted || metadata.internal ? normalizeTraceContext(evt.trace) : void 0;
1437
+ const trustedSpanAliasOwner = (evt) => {
1438
+ if ("runId" in evt && evt.runId) return {
1439
+ kind: "run",
1440
+ id: evt.runId
1441
+ };
1442
+ };
1443
+ const sameTrustedSpanAliasOwner = (left, right) => Boolean(left && right && left.kind === right.kind && left.id === right.id);
1444
+ const trustedSpanAliasKey = (spanId, owner) => `${spanId}:${owner.kind}:${owner.id}`;
1445
+ const retainedTrustedSpanContextKey = (traceId, spanId, owner) => `${traceId}:${owner ? trustedSpanAliasKey(spanId, owner) : spanId}`;
1446
+ const retainedTrustedSpanContext = (traceContext, spanId, owner) => {
1447
+ if (!traceContext?.traceId || !spanId) return;
1448
+ const retained = (owner ? retainedTrustedSpanContexts.get(retainedTrustedSpanContextKey(traceContext.traceId, spanId, owner)) : void 0) ?? retainedTrustedSpanContexts.get(retainedTrustedSpanContextKey(traceContext.traceId, spanId));
1449
+ if (retained?.spanContext.traceId !== traceContext.traceId) return;
1450
+ if (retained.owner && !sameTrustedSpanAliasOwner(retained.owner, owner)) return;
1451
+ return retained.spanContext;
1452
+ };
1453
+ const activeTrustedSpanAlias = (spanId, owner) => {
1454
+ if (!owner) return;
1455
+ const alias = activeTrustedSpanAliases.get(trustedSpanAliasKey(spanId, owner));
1456
+ if (!alias || !sameTrustedSpanAliasOwner(alias.owner, owner)) return;
1457
+ return alias.span;
1458
+ };
1459
+ const internalOrTrustedParentContext = (evt, metadata) => {
1460
+ const traceContext = internalOrTrustedTraceContext(evt, metadata);
1461
+ const parentSpanId = traceContext?.parentSpanId ?? traceContext?.spanId;
1462
+ if (!traceContext || !parentSpanId) return;
1463
+ return contextForTraceContext({
1464
+ ...traceContext,
1465
+ spanId: parentSpanId
1466
+ });
1467
+ };
1468
+ const internalOrTrustedExplicitParentContext = (evt, metadata) => {
1469
+ const traceContext = internalOrTrustedTraceContext(evt, metadata);
1470
+ if (!traceContext?.parentSpanId) return;
1471
+ return contextForTraceContext({
1472
+ ...traceContext,
1473
+ spanId: traceContext.parentSpanId
1474
+ });
1475
+ };
1476
+ const activeTrustedParentContext = (evt, metadata) => {
1477
+ const traceContext = trustedTraceContext(evt, metadata);
1478
+ const parentSpanId = traceContext?.parentSpanId;
1479
+ if (!parentSpanId) return;
1480
+ const owner = trustedSpanAliasOwner(evt);
1481
+ const spanContext = (activeTrustedSpans.get(parentSpanId) ?? activeTrustedSpanAlias(parentSpanId, owner))?.spanContext() ?? retainedTrustedSpanContext(traceContext, parentSpanId, owner);
1482
+ if (!spanContext) return;
1483
+ return trace.setSpanContext(context.active(), spanContext);
1484
+ };
1485
+ const activeInternalOrTrustedContext = (evt, metadata) => {
1486
+ const traceContext = internalOrTrustedTraceContext(evt, metadata);
1487
+ if (!traceContext) return;
1488
+ const owner = trustedSpanAliasOwner(evt);
1489
+ const activeSpan = (traceContext.spanId ? activeTrustedSpans.get(traceContext.spanId) ?? activeTrustedSpanAlias(traceContext.spanId, owner) : void 0) ?? (traceContext.parentSpanId ? activeTrustedSpans.get(traceContext.parentSpanId) ?? activeTrustedSpanAlias(traceContext.parentSpanId, owner) : void 0);
1490
+ if (activeSpan) return trace.setSpanContext(context.active(), activeSpan.spanContext());
1491
+ const retainedSpanContext = retainedTrustedSpanContext(traceContext, traceContext.spanId, owner) ?? retainedTrustedSpanContext(traceContext, traceContext.parentSpanId, owner);
1492
+ if (retainedSpanContext) return trace.setSpanContext(context.active(), retainedSpanContext);
1493
+ return internalOrTrustedParentContext(evt, metadata);
1494
+ };
1495
+ const trackTrustedSpan = (evt, metadata, span) => {
1496
+ const spanId = trustedTraceContext(evt, metadata)?.spanId;
1497
+ if (spanId) activeTrustedSpans.set(spanId, span);
1498
+ return span;
1499
+ };
1500
+ const trackInternalOrTrustedSpan = (evt, metadata, span) => {
1501
+ const spanId = internalOrTrustedTraceContext(evt, metadata)?.spanId;
1502
+ if (spanId) activeTrustedSpans.set(spanId, span);
1503
+ return span;
1504
+ };
1505
+ const takeTrackedTrustedSpan = (evt, metadata) => {
1506
+ const spanId = trustedTraceContext(evt, metadata)?.spanId;
1507
+ if (!spanId) return;
1508
+ const span = activeTrustedSpans.get(spanId);
1509
+ if (span) activeTrustedSpans.delete(spanId);
1510
+ return span;
1511
+ };
1512
+ const getTrackedInternalOrTrustedSpan = (evt, metadata) => {
1513
+ const spanId = internalOrTrustedTraceContext(evt, metadata)?.spanId;
1514
+ if (!spanId) return;
1515
+ return activeTrustedSpans.get(spanId);
1516
+ };
1517
+ const setSpanAttrs = (span, attributes) => {
1518
+ span.setAttributes?.(redactOtelAttributes(attributes));
1519
+ };
1520
+ const retainTrustedSpanContext = (traceId, spanId, spanContext, token, owner) => {
1521
+ retainedTrustedSpanContexts.set(retainedTrustedSpanContextKey(traceId, spanId, owner), {
1522
+ spanContext,
1523
+ token,
1524
+ ...owner ? { owner } : {}
1525
+ });
1526
+ while (retainedTrustedSpanContexts.size > MAX_RETAINED_TRUSTED_SPAN_CONTEXTS) {
1527
+ const oldestKey = retainedTrustedSpanContexts.keys().next().value;
1528
+ if (!oldestKey) break;
1529
+ retainedTrustedSpanContexts.delete(oldestKey);
1530
+ }
1531
+ };
1532
+ const scheduleRetainedTrustedSpanContextCleanup = (token) => {
1533
+ let drainHandle;
1534
+ let timeoutHandle;
1535
+ const cleanup = () => {
1536
+ if (drainHandle) {
1537
+ clearTimeout(drainHandle);
1538
+ retainedTrustedSpanContextCleanupTimers.delete(drainHandle);
1539
+ drainHandle = void 0;
1540
+ }
1541
+ if (timeoutHandle) {
1542
+ clearTimeout(timeoutHandle);
1543
+ retainedTrustedSpanContextCleanupTimers.delete(timeoutHandle);
1544
+ timeoutHandle = void 0;
1545
+ }
1546
+ for (const [key, retained] of retainedTrustedSpanContexts) if (retained.token === token) retainedTrustedSpanContexts.delete(key);
1547
+ };
1548
+ drainHandle = setTimeout(() => {
1549
+ if (drainHandle) {
1550
+ retainedTrustedSpanContextCleanupTimers.delete(drainHandle);
1551
+ drainHandle = void 0;
1552
+ }
1553
+ waitForDiagnosticEventsDrained().then(cleanup, cleanup);
1554
+ }, 0);
1555
+ drainHandle.unref?.();
1556
+ retainedTrustedSpanContextCleanupTimers.add(drainHandle);
1557
+ timeoutHandle = setTimeout(cleanup, RETAINED_TRUSTED_SPAN_CONTEXT_TIMEOUT_MS);
1558
+ timeoutHandle.unref?.();
1559
+ retainedTrustedSpanContextCleanupTimers.add(timeoutHandle);
1560
+ };
1561
+ const completeTrackedLifecycleSpan = (spanId, span, endTimeMs) => {
1562
+ const spanContext = span.spanContext();
1563
+ const retainedKeys = [{ spanId }];
1564
+ const retainedAliasKeys = [];
1565
+ for (const [aliasKey, alias] of activeTrustedSpanAliases) if (alias.span === span) {
1566
+ retainedKeys.push({
1567
+ spanId: alias.spanId,
1568
+ owner: alias.owner
1569
+ });
1570
+ retainedAliasKeys.push(aliasKey);
1571
+ }
1572
+ if (activeTrustedSpans.get(spanId) === span) activeTrustedSpans.delete(spanId);
1573
+ for (const aliasKey of retainedAliasKeys) if (activeTrustedSpanAliases.get(aliasKey)?.span === span) activeTrustedSpanAliases.delete(aliasKey);
1574
+ span.end(endTimeMs);
1575
+ const token = Symbol("retainedTrustedSpanContext");
1576
+ for (const retainedKey of retainedKeys) retainTrustedSpanContext(spanContext.traceId, retainedKey.spanId, spanContext, token, retainedKey.owner);
1577
+ scheduleRetainedTrustedSpanContextCleanup(token);
1578
+ };
1579
+ const addRunAttrs = (spanAttrs, evt) => {
1580
+ if (evt.provider) spanAttrs["openclaw.provider"] = evt.provider;
1581
+ if (evt.model) spanAttrs["openclaw.model"] = evt.model;
1582
+ if (evt.channel) spanAttrs["openclaw.channel"] = evt.channel;
1583
+ if (evt.trigger) spanAttrs["openclaw.trigger"] = evt.trigger;
1584
+ };
1585
+ const paramsSummaryAttrs = (summary) => {
1586
+ if (!summary) return {};
1587
+ return {
1588
+ "openclaw.tool.params.kind": summary.kind,
1589
+ ..."length" in summary ? { "openclaw.tool.params.length": summary.length } : {}
1590
+ };
1591
+ };
1592
+ const recordModelUsage = (evt, metadata) => {
1593
+ const attrs = {
1594
+ "openclaw.channel": evt.channel ?? "unknown",
1595
+ "openclaw.agent": lowCardinalityAttr(evt.agentId),
1596
+ "openclaw.provider": evt.provider ?? "unknown",
1597
+ "openclaw.model": evt.model ?? "unknown"
1598
+ };
1599
+ const genAiAttrs = {
1600
+ "gen_ai.operation.name": "chat",
1601
+ "gen_ai.provider.name": lowCardinalityAttr(evt.provider),
1602
+ "gen_ai.request.model": lowCardinalityAttr(evt.model)
1603
+ };
1604
+ const usage = evt.usage;
1605
+ if (usage.input) {
1606
+ tokensCounter.add(usage.input, {
1607
+ ...attrs,
1608
+ "openclaw.token": "input"
1609
+ });
1610
+ genAiTokenUsageHistogram.record(usage.input, {
1611
+ ...genAiAttrs,
1612
+ "gen_ai.token.type": "input"
1613
+ });
1614
+ }
1615
+ if (usage.output) {
1616
+ tokensCounter.add(usage.output, {
1617
+ ...attrs,
1618
+ "openclaw.token": "output"
1619
+ });
1620
+ genAiTokenUsageHistogram.record(usage.output, {
1621
+ ...genAiAttrs,
1622
+ "gen_ai.token.type": "output"
1623
+ });
1624
+ }
1625
+ if (usage.cacheRead) tokensCounter.add(usage.cacheRead, {
1626
+ ...attrs,
1627
+ "openclaw.token": "cache_read"
1628
+ });
1629
+ if (usage.cacheWrite) tokensCounter.add(usage.cacheWrite, {
1630
+ ...attrs,
1631
+ "openclaw.token": "cache_write"
1632
+ });
1633
+ if (usage.promptTokens) tokensCounter.add(usage.promptTokens, {
1634
+ ...attrs,
1635
+ "openclaw.token": "prompt"
1636
+ });
1637
+ if (usage.total) tokensCounter.add(usage.total, {
1638
+ ...attrs,
1639
+ "openclaw.token": "total"
1640
+ });
1641
+ if (evt.costUsd) costCounter.add(evt.costUsd, attrs);
1642
+ if (evt.durationMs) durationHistogram.record(evt.durationMs, attrs);
1643
+ if (evt.context?.limit) contextHistogram.record(evt.context.limit, {
1644
+ ...attrs,
1645
+ "openclaw.context": "limit"
1646
+ });
1647
+ if (evt.context?.used) contextHistogram.record(evt.context.used, {
1648
+ ...attrs,
1649
+ "openclaw.context": "used"
1650
+ });
1651
+ if (!tracesEnabled) return;
1652
+ const genAiInputTokens = usage.promptTokens ?? (usage.input ?? 0) + (usage.cacheRead ?? 0) + (usage.cacheWrite ?? 0);
1653
+ const spanAttrs = {
1654
+ ...attrs,
1655
+ "openclaw.tokens.input": usage.input ?? 0,
1656
+ "openclaw.tokens.output": usage.output ?? 0,
1657
+ "openclaw.tokens.cache_read": usage.cacheRead ?? 0,
1658
+ "openclaw.tokens.cache_write": usage.cacheWrite ?? 0,
1659
+ "openclaw.tokens.total": usage.total ?? 0
1660
+ };
1661
+ assignGenAiSpanIdentityAttrs(spanAttrs, evt);
1662
+ assignPositiveNumberAttr(spanAttrs, "gen_ai.usage.input_tokens", genAiInputTokens);
1663
+ assignPositiveNumberAttr(spanAttrs, "gen_ai.usage.output_tokens", usage.output);
1664
+ assignPositiveNumberAttr(spanAttrs, "gen_ai.usage.cache_read.input_tokens", usage.cacheRead);
1665
+ assignPositiveNumberAttr(spanAttrs, "gen_ai.usage.cache_creation.input_tokens", usage.cacheWrite);
1666
+ spanWithDuration("openclaw.model.usage", spanAttrs, evt.durationMs, {
1667
+ parentContext: activeTrustedParentContext(evt, metadata),
1668
+ endTimeMs: evt.ts
1669
+ }).end(evt.ts);
1670
+ };
1671
+ const recordWebhookReceived = (evt) => {
1672
+ const attrs = {
1673
+ "openclaw.channel": evt.channel ?? "unknown",
1674
+ "openclaw.webhook": evt.updateType ?? "unknown"
1675
+ };
1676
+ webhookReceivedCounter.add(1, attrs);
1677
+ };
1678
+ const recordWebhookProcessed = (evt) => {
1679
+ const attrs = {
1680
+ "openclaw.channel": lowCardinalityAttr(evt.channel),
1681
+ "openclaw.webhook": lowCardinalityAttr(evt.updateType)
1682
+ };
1683
+ if (typeof evt.durationMs === "number") webhookDurationHistogram.record(evt.durationMs, attrs);
1684
+ if (!tracesEnabled) return;
1685
+ const spanAttrs = { ...attrs };
1686
+ spanWithDuration("openclaw.webhook.processed", spanAttrs, evt.durationMs).end();
1687
+ };
1688
+ const recordWebhookError = (evt) => {
1689
+ const attrs = {
1690
+ "openclaw.channel": lowCardinalityAttr(evt.channel),
1691
+ "openclaw.webhook": lowCardinalityAttr(evt.updateType)
1692
+ };
1693
+ webhookErrorCounter.add(1, attrs);
1694
+ if (!tracesEnabled) return;
1695
+ const redactedError = redactSensitiveText(evt.error);
1696
+ const spanAttrs = {
1697
+ ...attrs,
1698
+ "openclaw.error": redactedError
1699
+ };
1700
+ const span = tracer.startSpan("openclaw.webhook.error", { attributes: spanAttrs });
1701
+ span.setStatus({
1702
+ code: SpanStatusCode.ERROR,
1703
+ message: redactedError
1704
+ });
1705
+ span.end();
1706
+ };
1707
+ const recordMessageQueued = (evt) => {
1708
+ const attrs = {
1709
+ "openclaw.channel": lowCardinalityAttr(evt.channel),
1710
+ "openclaw.source": lowCardinalityAttr(evt.source)
1711
+ };
1712
+ messageQueuedCounter.add(1, attrs);
1713
+ if (typeof evt.queueDepth === "number") queueDepthHistogram.record(evt.queueDepth, attrs);
1714
+ };
1715
+ const recordMessageReceived = (evt) => {
1716
+ messageReceivedCounter.add(1, {
1717
+ "openclaw.channel": lowCardinalityAttr(evt.channel),
1718
+ "openclaw.source": lowCardinalityAttr(evt.source)
1719
+ });
1720
+ };
1721
+ const recordMessageDispatchStarted = (evt, metadata) => {
1722
+ const attrs = {
1723
+ "openclaw.channel": lowCardinalityAttr(evt.channel),
1724
+ "openclaw.source": lowCardinalityAttr(evt.source)
1725
+ };
1726
+ messageDispatchStartedCounter.add(1, attrs);
1727
+ if (!tracesEnabled) return;
1728
+ const traceContext = internalOrTrustedTraceContext(evt, metadata);
1729
+ if (!traceContext?.spanId || activeTrustedSpans.has(traceContext.spanId)) return;
1730
+ trackInternalOrTrustedSpan(evt, metadata, spanWithDuration("openclaw.message.processed", attrs, void 0, {
1731
+ parentContext: internalOrTrustedExplicitParentContext(evt, metadata),
1732
+ startTimeMs: evt.ts
1733
+ }));
1734
+ };
1735
+ const recordMessageDispatchCompleted = (evt) => {
1736
+ const attrs = {
1737
+ "openclaw.channel": lowCardinalityAttr(evt.channel),
1738
+ "openclaw.outcome": evt.outcome,
1739
+ "openclaw.reason": lowCardinalityAttr(evt.reason, "none"),
1740
+ "openclaw.source": lowCardinalityAttr(evt.source)
1741
+ };
1742
+ messageDispatchCompletedCounter.add(1, attrs);
1743
+ messageDispatchDurationHistogram.record(evt.durationMs, attrs);
1744
+ };
1745
+ const recordMessageProcessed = (evt, metadata) => {
1746
+ const attrs = {
1747
+ "openclaw.channel": lowCardinalityAttr(evt.channel),
1748
+ "openclaw.outcome": evt.outcome ?? "unknown"
1749
+ };
1750
+ messageProcessedCounter.add(1, attrs);
1751
+ if (typeof evt.durationMs === "number") messageDurationHistogram.record(evt.durationMs, attrs);
1752
+ if (!tracesEnabled) return;
1753
+ const spanAttrs = { ...attrs };
1754
+ if (evt.reason) spanAttrs["openclaw.reason"] = lowCardinalityAttr(evt.reason, "unknown");
1755
+ const trackedSpan = getTrackedInternalOrTrustedSpan(evt, metadata);
1756
+ const span = trackedSpan ?? spanWithDuration("openclaw.message.processed", spanAttrs, evt.durationMs, {
1757
+ parentContext: internalOrTrustedExplicitParentContext(evt, metadata),
1758
+ endTimeMs: evt.ts
1759
+ });
1760
+ setSpanAttrs(span, spanAttrs);
1761
+ if (evt.outcome === "error" && evt.error) span.setStatus({
1762
+ code: SpanStatusCode.ERROR,
1763
+ message: redactSensitiveText(evt.error)
1764
+ });
1765
+ const traceContext = internalOrTrustedTraceContext(evt, metadata);
1766
+ if (trackedSpan && traceContext?.spanId) {
1767
+ completeTrackedLifecycleSpan(traceContext.spanId, trackedSpan, evt.ts);
1768
+ return;
1769
+ }
1770
+ span.end(evt.ts);
1771
+ };
1772
+ const messageDeliveryAttrs = (evt) => ({
1773
+ "openclaw.channel": lowCardinalityAttr(evt.channel),
1774
+ "openclaw.delivery.kind": lowCardinalityAttr(evt.deliveryKind, "other")
1775
+ });
1776
+ const recordMessageDeliveryStarted = (evt) => {
1777
+ messageDeliveryStartedCounter.add(1, messageDeliveryAttrs(evt));
1778
+ };
1779
+ const recordMessageDeliveryCompleted = (evt, metadata) => {
1780
+ const attrs = {
1781
+ ...messageDeliveryAttrs(evt),
1782
+ "openclaw.outcome": "completed"
1783
+ };
1784
+ messageDeliveryDurationHistogram.record(evt.durationMs, attrs);
1785
+ if (!tracesEnabled) return;
1786
+ spanWithDuration("openclaw.message.delivery", {
1787
+ ...attrs,
1788
+ "openclaw.delivery.result_count": evt.resultCount
1789
+ }, evt.durationMs, {
1790
+ parentContext: activeInternalOrTrustedContext(evt, metadata),
1791
+ endTimeMs: evt.ts
1792
+ }).end(evt.ts);
1793
+ };
1794
+ const recordMessageDeliveryError = (evt, metadata) => {
1795
+ const attrs = {
1796
+ ...messageDeliveryAttrs(evt),
1797
+ "openclaw.outcome": "error",
1798
+ "openclaw.errorCategory": lowCardinalityAttr(evt.errorCategory, "other")
1799
+ };
1800
+ messageDeliveryDurationHistogram.record(evt.durationMs, attrs);
1801
+ if (!tracesEnabled) return;
1802
+ const span = spanWithDuration("openclaw.message.delivery", attrs, evt.durationMs, {
1803
+ parentContext: activeInternalOrTrustedContext(evt, metadata),
1804
+ endTimeMs: evt.ts
1805
+ });
1806
+ span.setStatus({
1807
+ code: SpanStatusCode.ERROR,
1808
+ message: redactSensitiveText(evt.errorCategory)
1809
+ });
1810
+ span.end(evt.ts);
1811
+ };
1812
+ const recordRunStarted = (evt, metadata) => {
1813
+ if (!tracesEnabled || !metadata.trusted) return;
1814
+ const spanAttrs = {};
1815
+ addRunAttrs(spanAttrs, evt);
1816
+ const span = trackTrustedSpan(evt, metadata, spanWithDuration("openclaw.run", spanAttrs, void 0, {
1817
+ parentContext: activeTrustedParentContext(evt, metadata),
1818
+ startTimeMs: evt.ts
1819
+ }));
1820
+ const parentSpanId = trustedTraceContext(evt, metadata)?.parentSpanId;
1821
+ if (parentSpanId && !activeTrustedSpans.has(parentSpanId)) {
1822
+ const owner = {
1823
+ kind: "run",
1824
+ id: evt.runId
1825
+ };
1826
+ activeTrustedSpanAliases.set(trustedSpanAliasKey(parentSpanId, owner), {
1827
+ span,
1828
+ spanId: parentSpanId,
1829
+ owner
1830
+ });
1831
+ }
1832
+ };
1833
+ const recordLaneEnqueue = (evt) => {
1834
+ const attrs = { "openclaw.lane": lowCardinalityQueueLaneAttr(evt.lane) };
1835
+ laneEnqueueCounter.add(1, attrs);
1836
+ queueDepthHistogram.record(evt.queueSize, attrs);
1837
+ };
1838
+ const recordLaneDequeue = (evt) => {
1839
+ const attrs = { "openclaw.lane": lowCardinalityQueueLaneAttr(evt.lane) };
1840
+ laneDequeueCounter.add(1, attrs);
1841
+ queueDepthHistogram.record(evt.queueSize, attrs);
1842
+ if (typeof evt.waitMs === "number") queueWaitHistogram.record(evt.waitMs, attrs);
1843
+ };
1844
+ const recordSessionState = (evt) => {
1845
+ const attrs = { "openclaw.state": evt.state };
1846
+ if (evt.reason) attrs["openclaw.reason"] = redactSensitiveText(evt.reason);
1847
+ sessionStateCounter.add(1, attrs);
1848
+ };
1849
+ const recordSessionTurnCreated = (evt) => {
1850
+ sessionTurnCreatedCounter.add(1, {
1851
+ "openclaw.agent": lowCardinalityAttr(evt.agentId, "unknown"),
1852
+ "openclaw.channel": lowCardinalityAttr(evt.channel, "unknown"),
1853
+ "openclaw.trigger": evt.trigger
1854
+ });
1855
+ };
1856
+ const recordSessionStuck = (evt) => {
1857
+ const attrs = { "openclaw.state": evt.state };
1858
+ sessionStuckCounter.add(1, attrs);
1859
+ if (typeof evt.ageMs === "number") sessionStuckAgeHistogram.record(evt.ageMs, attrs);
1860
+ if (!tracesEnabled) return;
1861
+ const spanAttrs = { ...attrs };
1862
+ spanAttrs["openclaw.queueDepth"] = evt.queueDepth ?? 0;
1863
+ spanAttrs["openclaw.ageMs"] = evt.ageMs;
1864
+ const span = tracer.startSpan("openclaw.session.stuck", { attributes: spanAttrs });
1865
+ span.setStatus({
1866
+ code: SpanStatusCode.ERROR,
1867
+ message: "session stuck"
1868
+ });
1869
+ span.end();
1870
+ };
1871
+ const sessionRecoveryAttrs = (evt) => {
1872
+ const attrs = { "openclaw.state": evt.state };
1873
+ if (evt.reason) attrs["openclaw.reason"] = redactSensitiveText(evt.reason);
1874
+ if (evt.activeWorkKind) attrs["openclaw.active_work_kind"] = evt.activeWorkKind;
1875
+ return attrs;
1876
+ };
1877
+ const recordSessionRecoveryRequested = (evt) => {
1878
+ const attrs = sessionRecoveryAttrs(evt);
1879
+ attrs["openclaw.action"] = evt.allowActiveAbort ? "abort" : "recover";
1880
+ sessionRecoveryRequestedCounter.add(1, attrs);
1881
+ sessionRecoveryAgeHistogram.record(evt.ageMs, attrs);
1882
+ };
1883
+ const recordSessionRecoveryCompleted = (evt) => {
1884
+ const attrs = sessionRecoveryAttrs(evt);
1885
+ attrs["openclaw.status"] = evt.status;
1886
+ attrs["openclaw.action"] = lowCardinalityAttr(evt.action, "unknown");
1887
+ if (evt.outcomeReason) attrs["openclaw.reason"] = redactSensitiveText(evt.outcomeReason);
1888
+ sessionRecoveryCompletedCounter.add(1, attrs);
1889
+ sessionRecoveryAgeHistogram.record(evt.ageMs, attrs);
1890
+ };
1891
+ const talkEventAttrs = (evt) => ({
1892
+ "openclaw.talk.brain": lowCardinalityAttr(evt.brain),
1893
+ "openclaw.talk.event_type": lowCardinalityAttr(evt.talkEventType),
1894
+ "openclaw.talk.mode": lowCardinalityAttr(evt.mode),
1895
+ "openclaw.talk.provider": lowCardinalityAttr(evt.provider),
1896
+ "openclaw.talk.transport": lowCardinalityAttr(evt.transport)
1897
+ });
1898
+ const recordTalkEvent = (evt, metadata) => {
1899
+ if (!metadata.trusted) return;
1900
+ const attrs = talkEventAttrs(evt);
1901
+ talkEventCounter.add(1, attrs);
1902
+ if (typeof evt.durationMs === "number") talkEventDurationHistogram.record(evt.durationMs, attrs);
1903
+ if (typeof evt.byteLength === "number") talkAudioBytesHistogram.record(evt.byteLength, attrs);
1904
+ };
1905
+ const recordRunAttempt = (evt) => {
1906
+ runAttemptCounter.add(1, { "openclaw.attempt": evt.attempt });
1907
+ };
1908
+ const toolLoopAttrs = (evt) => ({
1909
+ "openclaw.toolName": lowCardinalityAttr(evt.toolName, "tool"),
1910
+ "openclaw.loop.level": evt.level,
1911
+ "openclaw.loop.action": evt.action,
1912
+ "openclaw.loop.detector": evt.detector,
1913
+ "openclaw.loop.count": evt.count,
1914
+ ...evt.pairedToolName ? { "openclaw.loop.paired_tool": lowCardinalityAttr(evt.pairedToolName, "tool") } : {}
1915
+ });
1916
+ const recordToolLoop = (evt) => {
1917
+ const attrs = toolLoopAttrs(evt);
1918
+ toolLoopCounter.add(1, attrs);
1919
+ if (!tracesEnabled) return;
1920
+ const span = spanWithDuration("openclaw.tool.loop", attrs, 0, { endTimeMs: evt.ts });
1921
+ if (evt.level === "critical" || evt.action === "block") span.setStatus({
1922
+ code: SpanStatusCode.ERROR,
1923
+ message: `${evt.detector}:${evt.action}`
1924
+ });
1925
+ span.end(evt.ts);
1926
+ };
1927
+ const recordMemoryUsageMetrics = (evt, attrs = {}) => {
1928
+ memoryRssHistogram.record(evt.memory.rssBytes, attrs);
1929
+ memoryHeapUsedHistogram.record(evt.memory.heapUsedBytes, attrs);
1930
+ memoryHeapTotalHistogram.record(evt.memory.heapTotalBytes, attrs);
1931
+ memoryExternalHistogram.record(evt.memory.externalBytes, attrs);
1932
+ memoryArrayBuffersHistogram.record(evt.memory.arrayBuffersBytes, attrs);
1933
+ };
1934
+ const recordMemorySample = (evt) => {
1935
+ recordMemoryUsageMetrics(evt);
1936
+ };
1937
+ const recordMemoryPressure = (evt) => {
1938
+ const attrs = {
1939
+ "openclaw.memory.level": evt.level,
1940
+ "openclaw.memory.reason": evt.reason
1941
+ };
1942
+ memoryPressureCounter.add(1, attrs);
1943
+ recordMemoryUsageMetrics(evt, attrs);
1944
+ if (!tracesEnabled) return;
1945
+ const spanAttrs = {
1946
+ ...attrs,
1947
+ "openclaw.memory.rss_bytes": evt.memory.rssBytes,
1948
+ "openclaw.memory.heap_used_bytes": evt.memory.heapUsedBytes,
1949
+ "openclaw.memory.heap_total_bytes": evt.memory.heapTotalBytes,
1950
+ "openclaw.memory.external_bytes": evt.memory.externalBytes,
1951
+ "openclaw.memory.array_buffers_bytes": evt.memory.arrayBuffersBytes,
1952
+ ...evt.thresholdBytes !== void 0 ? { "openclaw.memory.threshold_bytes": evt.thresholdBytes } : {},
1953
+ ...evt.rssGrowthBytes !== void 0 ? { "openclaw.memory.rss_growth_bytes": evt.rssGrowthBytes } : {},
1954
+ ...evt.windowMs !== void 0 ? { "openclaw.memory.window_ms": evt.windowMs } : {}
1955
+ };
1956
+ const span = spanWithDuration("openclaw.memory.pressure", spanAttrs, 0, { endTimeMs: evt.ts });
1957
+ if (evt.level === "critical") span.setStatus({
1958
+ code: SpanStatusCode.ERROR,
1959
+ message: evt.reason
1960
+ });
1961
+ span.end(evt.ts);
1962
+ };
1963
+ const recordAsyncQueueDropped = (evt) => {
1964
+ asyncQueueDroppedCounter.add(evt.droppedEvents, { "openclaw.diagnostic.async_queue.drop_class": "total" });
1965
+ if (evt.droppedTrustedEvents !== void 0) asyncQueueDroppedCounter.add(evt.droppedTrustedEvents, { "openclaw.diagnostic.async_queue.drop_class": "trusted" });
1966
+ if (evt.droppedUntrustedEvents !== void 0) asyncQueueDroppedCounter.add(evt.droppedUntrustedEvents, { "openclaw.diagnostic.async_queue.drop_class": "untrusted" });
1967
+ if (evt.droppedPriorityEvents !== void 0) asyncQueueDroppedCounter.add(evt.droppedPriorityEvents, { "openclaw.diagnostic.async_queue.drop_class": "priority" });
1968
+ };
1969
+ const recordRunCompleted = (evt, metadata, privateData) => {
1970
+ const attrs = {
1971
+ "openclaw.outcome": evt.outcome,
1972
+ "openclaw.provider": evt.provider ?? "unknown",
1973
+ "openclaw.model": evt.model ?? "unknown"
1974
+ };
1975
+ if (evt.channel) attrs["openclaw.channel"] = evt.channel;
1976
+ if (evt.blockedBy) attrs["openclaw.blocked_by"] = lowCardinalityAttr(evt.blockedBy, "unknown");
1977
+ durationHistogram.record(evt.durationMs, attrs);
1978
+ if (!tracesEnabled) return;
1979
+ const spanAttrs = { "openclaw.outcome": evt.outcome };
1980
+ addRunAttrs(spanAttrs, evt);
1981
+ if (evt.blockedBy) spanAttrs["openclaw.blocked_by"] = lowCardinalityAttr(evt.blockedBy, "unknown");
1982
+ if (evt.errorCategory) spanAttrs["openclaw.errorCategory"] = lowCardinalityAttr(evt.errorCategory, "other");
1983
+ const redactedError = normalizeOtelErrorMessage(privateData.errorMessage);
1984
+ if (redactedError) spanAttrs["openclaw.error"] = redactedError;
1985
+ const trustedTrace = trustedTraceContext(evt, metadata);
1986
+ const trackedSpan = trustedTrace?.spanId ? activeTrustedSpans.get(trustedTrace.spanId) : void 0;
1987
+ const span = trackedSpan ?? spanWithDuration("openclaw.run", spanAttrs, evt.durationMs, {
1988
+ parentContext: activeTrustedParentContext(evt, metadata),
1989
+ endTimeMs: evt.ts
1990
+ });
1991
+ setSpanAttrs(span, spanAttrs);
1992
+ if (evt.outcome === "error") {
1993
+ const message = redactedError ?? (evt.errorCategory ? redactSensitiveText(evt.errorCategory) : void 0);
1994
+ span.setStatus({
1995
+ code: SpanStatusCode.ERROR,
1996
+ ...message ? { message } : {}
1997
+ });
1998
+ }
1999
+ if (trackedSpan && trustedTrace?.spanId) {
2000
+ completeTrackedLifecycleSpan(trustedTrace.spanId, trackedSpan, evt.ts);
2001
+ return;
2002
+ }
2003
+ span.end(evt.ts);
2004
+ };
2005
+ const harnessRunMetricAttrs = (evt) => ({
2006
+ "openclaw.harness.id": lowCardinalityAttr(evt.harnessId, "unknown"),
2007
+ "openclaw.harness.plugin": lowCardinalityAttr(evt.pluginId),
2008
+ ...evt.type === "harness.run.started" ? {} : { "openclaw.outcome": evt.type === "harness.run.error" ? "error" : evt.outcome },
2009
+ "openclaw.provider": lowCardinalityAttr(evt.provider, "unknown"),
2010
+ "openclaw.model": lowCardinalityAttr(evt.model, "unknown"),
2011
+ ...evt.channel ? { "openclaw.channel": lowCardinalityAttr(evt.channel) } : {}
2012
+ });
2013
+ const recordHarnessRunStarted = (evt, metadata) => {
2014
+ if (!tracesEnabled || !metadata.trusted) return;
2015
+ trackTrustedSpan(evt, metadata, spanWithDuration("openclaw.harness.run", harnessRunMetricAttrs(evt), void 0, {
2016
+ parentContext: activeTrustedParentContext(evt, metadata),
2017
+ startTimeMs: evt.ts
2018
+ }));
2019
+ };
2020
+ const recordHarnessRunCompleted = (evt, metadata, privateData) => {
2021
+ harnessDurationHistogram.record(evt.durationMs, harnessRunMetricAttrs(evt));
2022
+ if (!tracesEnabled) return;
2023
+ const spanAttrs = { ...harnessRunMetricAttrs(evt) };
2024
+ if (evt.resultClassification) spanAttrs["openclaw.harness.result_classification"] = lowCardinalityAttr(evt.resultClassification);
2025
+ if (typeof evt.yieldDetected === "boolean") spanAttrs["openclaw.harness.yield_detected"] = evt.yieldDetected;
2026
+ if (evt.itemLifecycle) {
2027
+ spanAttrs["openclaw.harness.items.started"] = evt.itemLifecycle.startedCount;
2028
+ spanAttrs["openclaw.harness.items.completed"] = evt.itemLifecycle.completedCount;
2029
+ spanAttrs["openclaw.harness.items.active"] = evt.itemLifecycle.activeCount;
2030
+ }
2031
+ const redactedError = normalizeOtelErrorMessage(privateData.errorMessage);
2032
+ if (redactedError) spanAttrs["openclaw.error"] = redactedError;
2033
+ const trustedTrace = trustedTraceContext(evt, metadata);
2034
+ const trackedSpan = trustedTrace?.spanId ? activeTrustedSpans.get(trustedTrace.spanId) : void 0;
2035
+ const span = trackedSpan ?? spanWithDuration("openclaw.harness.run", spanAttrs, evt.durationMs, {
2036
+ parentContext: activeTrustedParentContext(evt, metadata),
2037
+ endTimeMs: evt.ts
2038
+ });
2039
+ setSpanAttrs(span, spanAttrs);
2040
+ if (evt.outcome === "error") span.setStatus({
2041
+ code: SpanStatusCode.ERROR,
2042
+ message: redactedError ?? "error"
2043
+ });
2044
+ if (trackedSpan && trustedTrace?.spanId) {
2045
+ completeTrackedLifecycleSpan(trustedTrace.spanId, trackedSpan, evt.ts);
2046
+ return;
2047
+ }
2048
+ span.end(evt.ts);
2049
+ };
2050
+ const recordHarnessRunError = (evt, metadata, privateData) => {
2051
+ const errorType = lowCardinalityAttr(evt.errorCategory, "other");
2052
+ const attrs = {
2053
+ ...harnessRunMetricAttrs(evt),
2054
+ "openclaw.harness.phase": evt.phase,
2055
+ "openclaw.errorCategory": errorType
2056
+ };
2057
+ harnessDurationHistogram.record(evt.durationMs, attrs);
2058
+ if (!tracesEnabled) return;
2059
+ const redactedError = normalizeOtelErrorMessage(privateData.errorMessage);
2060
+ const spanAttrs = {
2061
+ ...attrs,
2062
+ "error.type": errorType,
2063
+ ...redactedError ? { "openclaw.error": redactedError } : {},
2064
+ ...evt.cleanupFailed ? { "openclaw.harness.cleanup_failed": true } : {}
2065
+ };
2066
+ const span = takeTrackedTrustedSpan(evt, metadata) ?? spanWithDuration("openclaw.harness.run", spanAttrs, evt.durationMs, {
2067
+ parentContext: activeTrustedParentContext(evt, metadata),
2068
+ endTimeMs: evt.ts
2069
+ });
2070
+ setSpanAttrs(span, spanAttrs);
2071
+ span.setStatus({
2072
+ code: SpanStatusCode.ERROR,
2073
+ message: redactedError ?? errorType
2074
+ });
2075
+ span.end(evt.ts);
2076
+ };
2077
+ const recordContextAssembled = (evt, metadata) => {
2078
+ if (!tracesEnabled) return;
2079
+ const spanAttrs = {
2080
+ "openclaw.context.message_count": evt.messageCount,
2081
+ "openclaw.context.history_text_chars": evt.historyTextChars,
2082
+ "openclaw.context.history_image_blocks": evt.historyImageBlocks,
2083
+ "openclaw.context.max_message_text_chars": evt.maxMessageTextChars,
2084
+ "openclaw.context.system_prompt_chars": evt.systemPromptChars,
2085
+ "openclaw.context.prompt_chars": evt.promptChars,
2086
+ "openclaw.context.prompt_images": evt.promptImages
2087
+ };
2088
+ addRunAttrs(spanAttrs, evt);
2089
+ if (evt.contextTokenBudget !== void 0) spanAttrs["openclaw.context.token_budget"] = evt.contextTokenBudget;
2090
+ if (evt.reserveTokens !== void 0) spanAttrs["openclaw.context.reserve_tokens"] = evt.reserveTokens;
2091
+ spanWithDuration("openclaw.context.assembled", spanAttrs, 0, {
2092
+ parentContext: activeTrustedParentContext(evt, metadata),
2093
+ endTimeMs: evt.ts
2094
+ }).end(evt.ts);
2095
+ };
2096
+ const recordModelFailover = (evt, metadata) => {
2097
+ const metricAttrs = {
2098
+ "openclaw.failover.reason": lowCardinalityAttr(evt.reason, "unknown"),
2099
+ "openclaw.failover.suspended": evt.suspended === void 0 ? "unknown" : String(evt.suspended),
2100
+ "openclaw.lane": lowCardinalityQueueLaneAttr(evt.lane, "unknown"),
2101
+ "openclaw.model": lowCardinalityAttr(evt.fromModel),
2102
+ "openclaw.provider": lowCardinalityAttr(evt.fromProvider),
2103
+ "openclaw.failover.to_model": lowCardinalityAttr(evt.toModel),
2104
+ "openclaw.failover.to_provider": lowCardinalityAttr(evt.toProvider)
2105
+ };
2106
+ modelFailoverCounter.add(1, metricAttrs);
2107
+ if (!tracesEnabled) return;
2108
+ const spanAttrs = { "openclaw.failover.reason": lowCardinalityAttr(evt.reason, "unknown") };
2109
+ if (evt.fromProvider) spanAttrs["openclaw.provider"] = evt.fromProvider;
2110
+ if (evt.fromModel) spanAttrs["openclaw.model"] = evt.fromModel;
2111
+ if (evt.toProvider) spanAttrs["openclaw.failover.to_provider"] = evt.toProvider;
2112
+ if (evt.toModel) spanAttrs["openclaw.failover.to_model"] = evt.toModel;
2113
+ if (evt.lane) spanAttrs["openclaw.lane"] = lowCardinalityQueueLaneAttr(evt.lane, "unknown");
2114
+ if (evt.suspended !== void 0) spanAttrs["openclaw.failover.suspended"] = evt.suspended;
2115
+ if (evt.cascadeDepth !== void 0) spanAttrs["openclaw.failover.cascade_depth"] = evt.cascadeDepth;
2116
+ spanWithDuration("openclaw.model.failover", spanAttrs, 0, {
2117
+ parentContext: activeTrustedParentContext(evt, metadata),
2118
+ endTimeMs: evt.ts
2119
+ }).end(evt.ts);
2120
+ };
2121
+ const modelCallMetricAttrs = (evt) => ({
2122
+ "openclaw.provider": evt.provider,
2123
+ "openclaw.model": evt.model,
2124
+ "openclaw.api": lowCardinalityAttr(evt.api),
2125
+ "openclaw.transport": lowCardinalityAttr(evt.transport)
2126
+ });
2127
+ const genAiModelCallMetricAttrs = (evt, errorType) => ({
2128
+ "gen_ai.operation.name": genAiOperationName(evt.api),
2129
+ "gen_ai.provider.name": lowCardinalityAttr(evt.provider),
2130
+ "gen_ai.request.model": lowCardinalityAttr(evt.model),
2131
+ ...errorType ? { "error.type": errorType } : {}
2132
+ });
2133
+ const recordModelCallSizeTimingMetrics = (evt, attrs) => {
2134
+ const requestPayloadBytes = positiveFiniteNumber(evt.requestPayloadBytes);
2135
+ if (requestPayloadBytes !== void 0) modelCallRequestBytesHistogram.record(requestPayloadBytes, attrs);
2136
+ const responseStreamBytes = positiveFiniteNumber(evt.responseStreamBytes);
2137
+ if (responseStreamBytes !== void 0) modelCallResponseBytesHistogram.record(responseStreamBytes, attrs);
2138
+ const timeToFirstByteMs = positiveFiniteNumber(evt.timeToFirstByteMs);
2139
+ if (timeToFirstByteMs !== void 0) modelCallTimeToFirstByteHistogram.record(timeToFirstByteMs, attrs);
2140
+ };
2141
+ const recordModelCallStarted = (evt, metadata) => {
2142
+ if (!tracesEnabled || !metadata.trusted) return;
2143
+ const spanAttrs = {
2144
+ "openclaw.provider": evt.provider,
2145
+ "openclaw.model": evt.model
2146
+ };
2147
+ assignGenAiModelCallAttrs(spanAttrs, evt);
2148
+ if (evt.api) spanAttrs["openclaw.api"] = evt.api;
2149
+ if (evt.transport) spanAttrs["openclaw.transport"] = evt.transport;
2150
+ assignModelCallPromptStatsAttrs(spanAttrs, evt);
2151
+ trackTrustedSpan(evt, metadata, spanWithDuration(modelCallSpanName(evt), spanAttrs, void 0, {
2152
+ kind: modelCallSpanKind(),
2153
+ parentContext: activeTrustedParentContext(evt, metadata),
2154
+ startTimeMs: evt.ts
2155
+ }));
2156
+ };
2157
+ const recordModelCallCompleted = (evt, metadata, modelContent) => {
2158
+ const metricAttrs = modelCallMetricAttrs(evt);
2159
+ modelCallDurationHistogram.record(evt.durationMs, metricAttrs);
2160
+ recordModelCallSizeTimingMetrics(evt, metricAttrs);
2161
+ genAiOperationDurationHistogram.record(evt.durationMs / 1e3, genAiModelCallMetricAttrs(evt));
2162
+ if (!tracesEnabled) return;
2163
+ const spanAttrs = {
2164
+ "openclaw.provider": evt.provider,
2165
+ "openclaw.model": evt.model
2166
+ };
2167
+ assignGenAiModelCallAttrs(spanAttrs, evt);
2168
+ if (evt.api) spanAttrs["openclaw.api"] = evt.api;
2169
+ if (evt.transport) spanAttrs["openclaw.transport"] = evt.transport;
2170
+ assignModelCallSizeTimingAttrs(spanAttrs, evt);
2171
+ assignModelCallPromptStatsAttrs(spanAttrs, evt);
2172
+ assignModelCallUsageAttrs(spanAttrs, evt);
2173
+ assignOtelModelContentAttributes(spanAttrs, modelContent, contentCapturePolicy);
2174
+ const span = takeTrackedTrustedSpan(evt, metadata) ?? spanWithDuration(modelCallSpanName(evt), spanAttrs, evt.durationMs, {
2175
+ kind: modelCallSpanKind(),
2176
+ parentContext: activeTrustedParentContext(evt, metadata),
2177
+ endTimeMs: evt.ts
2178
+ });
2179
+ setSpanAttrs(span, spanAttrs);
2180
+ addUpstreamRequestIdSpanEvent(span, evt.upstreamRequestIdHash);
2181
+ span.end(evt.ts);
2182
+ };
2183
+ const recordModelCallError = (evt, metadata, modelContent) => {
2184
+ const errorType = lowCardinalityAttr(evt.errorCategory, "other");
2185
+ const metricAttrs = {
2186
+ ...modelCallMetricAttrs(evt),
2187
+ "openclaw.errorCategory": errorType,
2188
+ ...evt.failureKind ? { "openclaw.failureKind": lowCardinalityAttr(evt.failureKind, "other") } : {}
2189
+ };
2190
+ modelCallDurationHistogram.record(evt.durationMs, metricAttrs);
2191
+ recordModelCallSizeTimingMetrics(evt, metricAttrs);
2192
+ genAiOperationDurationHistogram.record(evt.durationMs / 1e3, genAiModelCallMetricAttrs(evt, errorType));
2193
+ if (!tracesEnabled) return;
2194
+ const spanAttrs = {
2195
+ "openclaw.provider": evt.provider,
2196
+ "openclaw.model": evt.model,
2197
+ "openclaw.errorCategory": errorType,
2198
+ "error.type": errorType
2199
+ };
2200
+ if (evt.failureKind) spanAttrs["openclaw.failureKind"] = lowCardinalityAttr(evt.failureKind, "other");
2201
+ assignGenAiModelCallAttrs(spanAttrs, evt);
2202
+ if (evt.api) spanAttrs["openclaw.api"] = evt.api;
2203
+ if (evt.transport) spanAttrs["openclaw.transport"] = evt.transport;
2204
+ assignModelCallSizeTimingAttrs(spanAttrs, evt);
2205
+ assignModelCallPromptStatsAttrs(spanAttrs, evt);
2206
+ assignModelCallUsageAttrs(spanAttrs, evt);
2207
+ assignOtelModelContentAttributes(spanAttrs, modelContent, contentCapturePolicy);
2208
+ const span = takeTrackedTrustedSpan(evt, metadata) ?? spanWithDuration(modelCallSpanName(evt), spanAttrs, evt.durationMs, {
2209
+ kind: modelCallSpanKind(),
2210
+ parentContext: activeTrustedParentContext(evt, metadata),
2211
+ endTimeMs: evt.ts
2212
+ });
2213
+ setSpanAttrs(span, spanAttrs);
2214
+ addUpstreamRequestIdSpanEvent(span, evt.upstreamRequestIdHash);
2215
+ span.setStatus({
2216
+ code: SpanStatusCode.ERROR,
2217
+ message: redactSensitiveText(evt.errorCategory)
2218
+ });
2219
+ span.end(evt.ts);
2220
+ };
2221
+ const toolExecutionBaseAttrs = (evt) => ({
2222
+ "openclaw.toolName": evt.toolName,
2223
+ "openclaw.tool.source": lowCardinalityAttr(evt.toolSource, "core"),
2224
+ "gen_ai.tool.name": evt.toolName,
2225
+ ...evt.toolOwner ? { "openclaw.tool.owner": lowCardinalityAttr(evt.toolOwner) } : {},
2226
+ ...paramsSummaryAttrs(evt.paramsSummary)
2227
+ });
2228
+ const skillUsedAttrs = (evt) => ({
2229
+ "openclaw.skill.name": lowCardinalityAttr(evt.skillName, "skill"),
2230
+ "openclaw.skill.source": lowCardinalityAttr(evt.skillSource),
2231
+ "openclaw.skill.activation": lowCardinalityAttr(evt.activation),
2232
+ ...evt.agentId ? { "openclaw.agent": lowCardinalityAttr(evt.agentId) } : {},
2233
+ ...evt.toolName ? { "openclaw.toolName": lowCardinalityAttr(evt.toolName, "tool") } : {}
2234
+ });
2235
+ const recordSkillUsed = (evt, metadata) => {
2236
+ if (!metadata.trusted) return;
2237
+ const attrs = skillUsedAttrs(evt);
2238
+ skillUsedCounter.add(1, attrs);
2239
+ if (!tracesEnabled) return;
2240
+ const spanAttrs = { ...attrs };
2241
+ addRunAttrs(spanAttrs, evt);
2242
+ const span = spanWithDuration("openclaw.skill.used", spanAttrs, 0, {
2243
+ parentContext: activeTrustedParentContext(evt, metadata),
2244
+ endTimeMs: evt.ts
2245
+ });
2246
+ setSpanAttrs(span, spanAttrs);
2247
+ span.end(evt.ts);
2248
+ };
2249
+ const recordToolExecutionStarted = (evt, metadata) => {
2250
+ if (!tracesEnabled || !metadata.trusted) return;
2251
+ const spanAttrs = toolExecutionBaseAttrs(evt);
2252
+ assignOtelToolIdentityAttributes(spanAttrs, evt);
2253
+ trackTrustedSpan(evt, metadata, spanWithDuration("openclaw.tool.execution", spanAttrs, void 0, {
2254
+ parentContext: activeTrustedParentContext(evt, metadata),
2255
+ startTimeMs: evt.ts
2256
+ }));
2257
+ };
2258
+ const recordToolExecutionCompleted = (evt, metadata, toolContent) => {
2259
+ const attrs = toolExecutionBaseAttrs(evt);
2260
+ toolExecutionDurationHistogram.record(evt.durationMs, attrs);
2261
+ if (!tracesEnabled) return;
2262
+ const spanAttrs = { ...attrs };
2263
+ addRunAttrs(spanAttrs, evt);
2264
+ assignOtelToolIdentityAttributes(spanAttrs, evt);
2265
+ assignOtelToolContentAttributes(spanAttrs, toolContent, contentCapturePolicy);
2266
+ const span = takeTrackedTrustedSpan(evt, metadata) ?? spanWithDuration("openclaw.tool.execution", spanAttrs, evt.durationMs, {
2267
+ parentContext: activeTrustedParentContext(evt, metadata),
2268
+ endTimeMs: evt.ts
2269
+ });
2270
+ setSpanAttrs(span, spanAttrs);
2271
+ span.end(evt.ts);
2272
+ };
2273
+ const recordToolExecutionError = (evt, metadata, toolContent) => {
2274
+ const attrs = {
2275
+ ...toolExecutionBaseAttrs(evt),
2276
+ "openclaw.errorCategory": lowCardinalityAttr(evt.errorCategory, "other")
2277
+ };
2278
+ toolExecutionDurationHistogram.record(evt.durationMs, attrs);
2279
+ if (!tracesEnabled) return;
2280
+ const spanAttrs = { ...attrs };
2281
+ addRunAttrs(spanAttrs, evt);
2282
+ assignOtelToolIdentityAttributes(spanAttrs, evt);
2283
+ if (evt.errorCode) spanAttrs["openclaw.errorCode"] = lowCardinalityAttr(evt.errorCode, "other");
2284
+ assignOtelToolContentAttributes(spanAttrs, toolContent, contentCapturePolicy);
2285
+ const span = takeTrackedTrustedSpan(evt, metadata) ?? spanWithDuration("openclaw.tool.execution", spanAttrs, evt.durationMs, {
2286
+ parentContext: activeTrustedParentContext(evt, metadata),
2287
+ endTimeMs: evt.ts
2288
+ });
2289
+ setSpanAttrs(span, spanAttrs);
2290
+ span.setStatus({
2291
+ code: SpanStatusCode.ERROR,
2292
+ message: redactSensitiveText(evt.errorCategory)
2293
+ });
2294
+ span.end(evt.ts);
2295
+ };
2296
+ const recordToolExecutionBlocked = (evt, metadata) => {
2297
+ toolExecutionBlockedCounter.add(1, {
2298
+ ...toolExecutionBaseAttrs(evt),
2299
+ "openclaw.deniedReason": lowCardinalityAttr(evt.deniedReason, "other")
2300
+ });
2301
+ if (!tracesEnabled) return;
2302
+ const spanAttrs = {
2303
+ ...toolExecutionBaseAttrs(evt),
2304
+ "openclaw.outcome": "blocked",
2305
+ "openclaw.deniedReason": lowCardinalityAttr(evt.deniedReason, "other")
2306
+ };
2307
+ addRunAttrs(spanAttrs, evt);
2308
+ assignOtelToolIdentityAttributes(spanAttrs, evt);
2309
+ const span = spanWithDuration("openclaw.tool.execution", spanAttrs, 0, {
2310
+ parentContext: activeTrustedParentContext(evt, metadata),
2311
+ endTimeMs: evt.ts
2312
+ });
2313
+ setSpanAttrs(span, spanAttrs);
2314
+ span.end(evt.ts);
2315
+ };
2316
+ const recordPayloadLarge = (evt) => {
2317
+ const attrs = {
2318
+ "openclaw.payload.action": evt.action,
2319
+ "openclaw.payload.surface": lowCardinalityAttr(evt.surface, "unknown"),
2320
+ "openclaw.channel": lowCardinalityAttr(evt.channel, "none"),
2321
+ "openclaw.plugin": lowCardinalityAttr(evt.pluginId, "none"),
2322
+ "openclaw.reason": lowCardinalityAttr(evt.reason, "none")
2323
+ };
2324
+ payloadLargeCounter.add(1, attrs);
2325
+ const bytes = positiveFiniteNumber(evt.bytes);
2326
+ if (bytes !== void 0) payloadLargeBytesHistogram.record(bytes, attrs);
2327
+ };
2328
+ const recordExecProcessCompleted = (evt) => {
2329
+ const attrs = {
2330
+ "openclaw.exec.target": evt.target,
2331
+ "openclaw.exec.mode": evt.mode,
2332
+ "openclaw.outcome": evt.outcome
2333
+ };
2334
+ if (evt.failureKind) attrs["openclaw.failureKind"] = evt.failureKind;
2335
+ execProcessDurationHistogram.record(evt.durationMs, attrs);
2336
+ if (!tracesEnabled) return;
2337
+ const spanAttrs = {
2338
+ ...attrs,
2339
+ "openclaw.exec.command_length": evt.commandLength
2340
+ };
2341
+ if (typeof evt.exitCode === "number") spanAttrs["openclaw.exec.exit_code"] = evt.exitCode;
2342
+ if (evt.exitSignal) spanAttrs["openclaw.exec.exit_signal"] = lowCardinalityAttr(evt.exitSignal, "other");
2343
+ if (evt.timedOut !== void 0) spanAttrs["openclaw.exec.timed_out"] = evt.timedOut;
2344
+ const span = spanWithDuration("openclaw.exec", spanAttrs, evt.durationMs, { endTimeMs: evt.ts });
2345
+ if (evt.outcome === "failed") span.setStatus({
2346
+ code: SpanStatusCode.ERROR,
2347
+ ...evt.failureKind ? { message: evt.failureKind } : {}
2348
+ });
2349
+ span.end(evt.ts);
2350
+ };
2351
+ const recordHeartbeat = (evt) => {
2352
+ queueDepthHistogram.record(evt.queued, { "openclaw.channel": "heartbeat" });
2353
+ };
2354
+ const recordLivenessWarning = (evt) => {
2355
+ const reason = evt.reasons.join(":");
2356
+ const attrs = { "openclaw.liveness.reason": lowCardinalityAttr(reason, "unknown") };
2357
+ livenessWarningCounter.add(1, attrs);
2358
+ queueDepthHistogram.record(evt.queued, { "openclaw.channel": "liveness" });
2359
+ if (evt.eventLoopDelayP99Ms !== void 0) livenessEventLoopDelayP99Histogram.record(evt.eventLoopDelayP99Ms, attrs);
2360
+ if (evt.eventLoopDelayMaxMs !== void 0) livenessEventLoopDelayMaxHistogram.record(evt.eventLoopDelayMaxMs, attrs);
2361
+ if (evt.eventLoopUtilization !== void 0) livenessEventLoopUtilizationHistogram.record(evt.eventLoopUtilization, attrs);
2362
+ if (evt.cpuCoreRatio !== void 0) livenessCpuCoreRatioHistogram.record(evt.cpuCoreRatio, attrs);
2363
+ if (!tracesEnabled) return;
2364
+ const spanAttrs = {
2365
+ ...attrs,
2366
+ "openclaw.liveness.active": evt.active,
2367
+ "openclaw.liveness.waiting": evt.waiting,
2368
+ "openclaw.liveness.queued": evt.queued,
2369
+ "openclaw.liveness.interval_ms": evt.intervalMs,
2370
+ ...evt.eventLoopDelayP99Ms !== void 0 ? { "openclaw.liveness.event_loop_delay_p99_ms": evt.eventLoopDelayP99Ms } : {},
2371
+ ...evt.eventLoopDelayMaxMs !== void 0 ? { "openclaw.liveness.event_loop_delay_max_ms": evt.eventLoopDelayMaxMs } : {},
2372
+ ...evt.eventLoopUtilization !== void 0 ? { "openclaw.liveness.event_loop_utilization": evt.eventLoopUtilization } : {},
2373
+ ...evt.cpuUserMs !== void 0 ? { "openclaw.liveness.cpu_user_ms": evt.cpuUserMs } : {},
2374
+ ...evt.cpuSystemMs !== void 0 ? { "openclaw.liveness.cpu_system_ms": evt.cpuSystemMs } : {},
2375
+ ...evt.cpuTotalMs !== void 0 ? { "openclaw.liveness.cpu_total_ms": evt.cpuTotalMs } : {},
2376
+ ...evt.cpuCoreRatio !== void 0 ? { "openclaw.liveness.cpu_core_ratio": evt.cpuCoreRatio } : {}
2377
+ };
2378
+ const span = spanWithDuration("openclaw.liveness.warning", spanAttrs, 0, { endTimeMs: evt.ts });
2379
+ span.setStatus({
2380
+ code: SpanStatusCode.ERROR,
2381
+ message: reason
2382
+ });
2383
+ span.end(evt.ts);
2384
+ };
2385
+ const recordDiagnosticPhaseCompleted = (evt) => {
2386
+ if (!tracesEnabled) return;
2387
+ const spanAttrs = {
2388
+ "openclaw.phase": lowCardinalityAttr(evt.name, "unknown"),
2389
+ ...evt.cpuUserMs !== void 0 ? { "openclaw.phase.cpu_user_ms": evt.cpuUserMs } : {},
2390
+ ...evt.cpuSystemMs !== void 0 ? { "openclaw.phase.cpu_system_ms": evt.cpuSystemMs } : {},
2391
+ ...evt.cpuTotalMs !== void 0 ? { "openclaw.phase.cpu_total_ms": evt.cpuTotalMs } : {},
2392
+ ...evt.cpuCoreRatio !== void 0 ? { "openclaw.phase.cpu_core_ratio": evt.cpuCoreRatio } : {}
2393
+ };
2394
+ for (const [key, value] of Object.entries(evt.details ?? {})) spanAttrs[`openclaw.phase.detail.${key}`] = typeof value === "boolean" ? String(value) : value;
2395
+ spanWithDuration("openclaw.diagnostic.phase", spanAttrs, evt.durationMs, { endTimeMs: evt.ts }).end(evt.ts);
2396
+ };
2397
+ const recordTelemetryExporter = (evt, metadata) => {
2398
+ if (!metadata.trusted) return;
2399
+ telemetryExporterCounter.add(1, {
2400
+ "openclaw.exporter": lowCardinalityAttr(evt.exporter, "unknown"),
2401
+ "openclaw.signal": evt.signal,
2402
+ "openclaw.status": evt.status,
2403
+ ...evt.reason ? { "openclaw.reason": evt.reason } : {},
2404
+ ...evt.errorCategory ? { "openclaw.errorCategory": lowCardinalityAttr(evt.errorCategory, "other") } : {}
2405
+ });
2406
+ };
2407
+ const subscribe = ctx.internalDiagnostics?.onEvent;
2408
+ if (!subscribe) {
2409
+ ctx.logger.error("diagnostics-otel: internal diagnostics capability unavailable");
2410
+ return;
2411
+ }
2412
+ unsubscribe = subscribe((evt, metadata, privateData) => {
2413
+ try {
2414
+ switch (evt.type) {
2415
+ case "model.usage":
2416
+ recordModelUsage(evt, metadata);
2417
+ return;
2418
+ case "webhook.received":
2419
+ recordWebhookReceived(evt);
2420
+ return;
2421
+ case "webhook.processed":
2422
+ recordWebhookProcessed(evt);
2423
+ return;
2424
+ case "webhook.error":
2425
+ recordWebhookError(evt);
2426
+ return;
2427
+ case "message.queued":
2428
+ recordMessageQueued(evt);
2429
+ return;
2430
+ case "message.received":
2431
+ recordMessageReceived(evt);
2432
+ return;
2433
+ case "message.dispatch.started":
2434
+ recordMessageDispatchStarted(evt, metadata);
2435
+ return;
2436
+ case "message.dispatch.completed":
2437
+ recordMessageDispatchCompleted(evt);
2438
+ return;
2439
+ case "message.processed":
2440
+ recordMessageProcessed(evt, metadata);
2441
+ return;
2442
+ case "message.delivery.started":
2443
+ recordMessageDeliveryStarted(evt);
2444
+ return;
2445
+ case "message.delivery.completed":
2446
+ recordMessageDeliveryCompleted(evt, metadata);
2447
+ return;
2448
+ case "message.delivery.error":
2449
+ recordMessageDeliveryError(evt, metadata);
2450
+ return;
2451
+ case "talk.event":
2452
+ recordTalkEvent(evt, metadata);
2453
+ return;
2454
+ case "queue.lane.enqueue":
2455
+ recordLaneEnqueue(evt);
2456
+ return;
2457
+ case "queue.lane.dequeue":
2458
+ recordLaneDequeue(evt);
2459
+ return;
2460
+ case "session.state":
2461
+ recordSessionState(evt);
2462
+ return;
2463
+ case "session.long_running":
2464
+ case "session.stalled": return;
2465
+ case "session.turn.created":
2466
+ recordSessionTurnCreated(evt);
2467
+ return;
2468
+ case "session.stuck":
2469
+ recordSessionStuck(evt);
2470
+ return;
2471
+ case "session.recovery.requested":
2472
+ recordSessionRecoveryRequested(evt);
2473
+ return;
2474
+ case "session.recovery.completed":
2475
+ recordSessionRecoveryCompleted(evt);
2476
+ return;
2477
+ case "run.attempt":
2478
+ recordRunAttempt(evt);
2479
+ return;
2480
+ case "run.progress": return;
2481
+ case "diagnostic.heartbeat":
2482
+ recordHeartbeat(evt);
2483
+ return;
2484
+ case "diagnostic.liveness.warning":
2485
+ recordLivenessWarning(evt);
2486
+ return;
2487
+ case "diagnostic.phase.completed":
2488
+ recordDiagnosticPhaseCompleted(evt);
2489
+ return;
2490
+ case "run.started":
2491
+ recordRunStarted(evt, metadata);
2492
+ return;
2493
+ case "run.completed":
2494
+ recordRunCompleted(evt, metadata, privateData);
2495
+ return;
2496
+ case "harness.run.started":
2497
+ recordHarnessRunStarted(evt, metadata);
2498
+ return;
2499
+ case "harness.run.completed":
2500
+ recordHarnessRunCompleted(evt, metadata, privateData);
2501
+ return;
2502
+ case "harness.run.error":
2503
+ recordHarnessRunError(evt, metadata, privateData);
2504
+ return;
2505
+ case "context.assembled":
2506
+ recordContextAssembled(evt, metadata);
2507
+ return;
2508
+ case "model.call.started":
2509
+ recordModelCallStarted(evt, metadata);
2510
+ return;
2511
+ case "model.call.completed":
2512
+ recordModelCallCompleted(evt, metadata, privateData.modelContent);
2513
+ return;
2514
+ case "model.call.error":
2515
+ recordModelCallError(evt, metadata, privateData.modelContent);
2516
+ return;
2517
+ case "tool.execution.started":
2518
+ recordToolExecutionStarted(evt, metadata);
2519
+ return;
2520
+ case "tool.execution.completed":
2521
+ recordToolExecutionCompleted(evt, metadata, privateData.toolContent);
2522
+ return;
2523
+ case "tool.execution.error":
2524
+ recordToolExecutionError(evt, metadata, privateData.toolContent);
2525
+ return;
2526
+ case "tool.execution.blocked":
2527
+ recordToolExecutionBlocked(evt, metadata);
2528
+ return;
2529
+ case "skill.used":
2530
+ recordSkillUsed(evt, metadata);
2531
+ return;
2532
+ case "exec.process.completed":
2533
+ recordExecProcessCompleted(evt);
2534
+ return;
2535
+ case "exec.approval.followup_suppressed": return;
2536
+ case "log.record":
2537
+ recordLogRecord?.(evt, metadata);
2538
+ return;
2539
+ case "security.event":
2540
+ recordSecurityEvent?.(evt, metadata);
2541
+ return;
2542
+ case "tool.loop":
2543
+ recordToolLoop(evt);
2544
+ return;
2545
+ case "diagnostic.memory.sample":
2546
+ recordMemorySample(evt);
2547
+ return;
2548
+ case "diagnostic.memory.pressure":
2549
+ recordMemoryPressure(evt);
2550
+ return;
2551
+ case "diagnostic.async_queue.dropped":
2552
+ recordAsyncQueueDropped(evt);
2553
+ return;
2554
+ case "telemetry.exporter":
2555
+ recordTelemetryExporter(evt, metadata);
2556
+ return;
2557
+ case "payload.large":
2558
+ recordPayloadLarge(evt);
2559
+ return;
2560
+ case "model.failover": recordModelFailover(evt, metadata);
2561
+ }
2562
+ } catch (err) {
2563
+ ctx.logger.error(`diagnostics-otel: event handler failed (${evt.type}): ${formatError(err)}`);
2564
+ }
2565
+ });
2566
+ unregisterUnhandledRejectionHandler = registerUnhandledRejectionHandler((reason) => {
2567
+ const otlpError = findOtlpExporterError(reason);
2568
+ if (!otlpError) return false;
2569
+ const code = readErrorCode(otlpError) ?? "unknown";
2570
+ ctx.logger.warn(`diagnostics-otel: suppressed OTLP exporter unhandled rejection (code=${String(code)})`);
2571
+ return true;
2572
+ });
2573
+ emitForSignals(enabledSignals, {
2574
+ exporter: "diagnostics-otel",
2575
+ status: "started",
2576
+ reason: "configured"
2577
+ });
2578
+ if (logsEnabled) {
2579
+ const label = logsExporter === "both" ? "OTLP/Protobuf + stdout JSONL" : logsExporter === "stdout" ? "stdout JSONL" : "OTLP/Protobuf";
2580
+ ctx.logger.info(`diagnostics-otel: logs exporter enabled (${label})`);
2581
+ }
2582
+ },
2583
+ async stop() {
2584
+ await stopStarted();
2585
+ }
2586
+ };
2587
+ }
2588
+ //#endregion
2589
+ export { createDiagnosticsOtelService as t };