@semiont/observability 0.5.29 → 0.5.31
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +115 -2
- package/dist/index.js +158 -1
- package/dist/index.js.map +1 -1
- package/dist/node.d.ts +9 -1
- package/dist/node.js +39 -2
- package/dist/node.js.map +1 -1
- package/dist/process-logger.js.map +1 -1
- package/package.json +9 -9
package/dist/index.d.ts
CHANGED
|
@@ -110,12 +110,67 @@ interface JobQueueSnapshot {
|
|
|
110
110
|
failed: number;
|
|
111
111
|
cancelled: number;
|
|
112
112
|
}
|
|
113
|
+
/**
|
|
114
|
+
* A correlated reply was withheld from a subscriber that does not own its
|
|
115
|
+
* correlationId (CORRELATED-REPLY-ROUTING P5).
|
|
116
|
+
*
|
|
117
|
+
* Counts ONLY that case. A frame with no correlationId is a shape violation
|
|
118
|
+
* (warned, not counted), and a cid nobody claimed is the structural in-process
|
|
119
|
+
* case that fires constantly — counting either would drown the signal this
|
|
120
|
+
* metric exists to show: the fan-out amplification the delivery filter removes.
|
|
121
|
+
*/
|
|
122
|
+
declare function recordReplySuppressed(channel: string): void;
|
|
123
|
+
/**
|
|
124
|
+
* An SSE resume could not be served and the client was told to fall back to
|
|
125
|
+
* cache. This degradation is CORRECT by design and therefore silent — which is
|
|
126
|
+
* exactly why it needs a number. A rising rate means clients are losing
|
|
127
|
+
* history, and nothing else in the stack says so.
|
|
128
|
+
*/
|
|
129
|
+
declare function recordResumeGap(reason: string): void;
|
|
130
|
+
/**
|
|
131
|
+
* A request-shaped emit reached no subscriber, so the gateway synthesized its
|
|
132
|
+
* mapped failure (ARCHIVIST-STAYS-UP P3). By channel, this is the absence rate
|
|
133
|
+
* of the service that answers it — the difference between "it went down once"
|
|
134
|
+
* and "it is flapping."
|
|
135
|
+
*/
|
|
136
|
+
declare function recordUnanswerableRequest(channel: string): void;
|
|
137
|
+
/** Claims held and reply payloads retained by a gateway's correlation registry. */
|
|
138
|
+
interface CorrelationRegistrySnapshot {
|
|
139
|
+
claims: number;
|
|
140
|
+
retainedReplies: number;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Register a callback returning the gateway's correlation-registry occupancy.
|
|
144
|
+
*
|
|
145
|
+
* COUNTS, not bytes: retention is count-budgeted today (byte-budgeting is a
|
|
146
|
+
* known limit in CORRELATED-REPLY-ROUTING), so `retainedReplies` is a proxy for
|
|
147
|
+
* heap, not a measure of it. It is still the closest observable to the question
|
|
148
|
+
* two OOM investigations keep asking — a browse result can be 1-2 MB, and up to
|
|
149
|
+
* REPLY_RETENTION_MAX of them are held at once.
|
|
150
|
+
*/
|
|
151
|
+
declare function registerCorrelationRegistryProvider(provider: () => CorrelationRegistrySnapshot): void;
|
|
113
152
|
/** Increment the bus-emit counter. Called at every transport `emit` site. */
|
|
114
153
|
declare function recordBusEmit(channel: string, scope?: string): void;
|
|
115
154
|
/** Record an in-process actor handler's duration. */
|
|
116
155
|
declare function recordHandlerDuration(actor: string, channel: string, durationMs: number): void;
|
|
117
156
|
/** Record a worker job's outcome and duration. */
|
|
118
157
|
declare function recordJobOutcome(jobType: string, outcome: 'completed' | 'failed', durationMs: number): void;
|
|
158
|
+
/**
|
|
159
|
+
* Record one stage of `EventStore.appendEvent` (ARCHIVIST-STAYS-UP P7).
|
|
160
|
+
*
|
|
161
|
+
* The append path is the one operation only the Archivist can perform, and it
|
|
162
|
+
* was entirely dark: reads had `recordHandlerDuration` and the bus had its own
|
|
163
|
+
* counters, while writes had nothing. Stage-labeled because the useful
|
|
164
|
+
* question is never "was the append slow" but WHICH PART — and `materialize`
|
|
165
|
+
* in particular does work proportional to a resource's annotation count, so it
|
|
166
|
+
* degrades with history rather than with load.
|
|
167
|
+
*/
|
|
168
|
+
declare function recordAppendStage(stage: 'persist' | 'materialize' | 'enrich' | 'publish', durationMs: number): void;
|
|
169
|
+
/**
|
|
170
|
+
* Record a git invocation. Read the `add` count against events appended: one
|
|
171
|
+
* per event means deferred staging has stopped deduping.
|
|
172
|
+
*/
|
|
173
|
+
declare function recordGitCommand(command: string, durationMs: number): void;
|
|
119
174
|
/**
|
|
120
175
|
* Record a gather degraded by a projection read barrier: `'vectors'` — the
|
|
121
176
|
* Smelter settle barrier timed out (semanticContext shipped absent);
|
|
@@ -142,6 +197,18 @@ declare function registerJobQueueProvider(provider: () => Promise<JobQueueSnapsh
|
|
|
142
197
|
* (point count). Async to allow remote queries (Qdrant). Polled at
|
|
143
198
|
* the metric-collection interval.
|
|
144
199
|
*/
|
|
200
|
+
/**
|
|
201
|
+
* Register the Archivist's fact-pump backlog — facts appended to the record
|
|
202
|
+
* but not yet republished onto the bus.
|
|
203
|
+
*
|
|
204
|
+
* At rest this is zero. A value that climbs and does not come back means the
|
|
205
|
+
* pump is outrunning its transport, which is the leading hypothesis for the
|
|
206
|
+
* load-correlated heap growth in `bugs/absent-archivist-wedges-browse.md`
|
|
207
|
+
* (ARCHIVIST-STAYS-UP P5). The backlog is deliberately unbounded today, so
|
|
208
|
+
* this number is the only thing standing between "the pump is behind" and an
|
|
209
|
+
* OOM whose cause is inferred from RSS after the fact.
|
|
210
|
+
*/
|
|
211
|
+
declare function registerFactPumpDepthProvider(provider: () => number): void;
|
|
145
212
|
declare function registerVectorIndexSizeProvider(provider: () => Promise<number> | number): void;
|
|
146
213
|
/**
|
|
147
214
|
* Record an inference call. Token counts are optional — providers that
|
|
@@ -156,6 +223,52 @@ declare function recordInferenceUsage(opts: {
|
|
|
156
223
|
inputTokens?: number;
|
|
157
224
|
outputTokens?: number;
|
|
158
225
|
}): void;
|
|
226
|
+
/**
|
|
227
|
+
* Record one detection model call (DETECTION-QUALITY-THROUGHPUT P1).
|
|
228
|
+
*
|
|
229
|
+
* The adapters already record provider/model/duration/tokens for every
|
|
230
|
+
* inference call. What they cannot know is the detection shape around it:
|
|
231
|
+
* which motivation asked, how big the piece was, how many annotations came
|
|
232
|
+
* back, how deep subdivision had descended, and whether this was the floor
|
|
233
|
+
* re-roll. Those are the facts that distinguish a healthy call from a
|
|
234
|
+
* expensive descent, and without them a slow detection run is one
|
|
235
|
+
* undifferentiated number.
|
|
236
|
+
*
|
|
237
|
+
* FAILED attempts are recorded too, and that is the point: the calls paid for
|
|
238
|
+
* and thrown away during a descent are exactly the cost later phases exist to
|
|
239
|
+
* avoid, so a record only of successes would hide the thing being optimized.
|
|
240
|
+
*
|
|
241
|
+
* Tokens are the PROVIDER's counts, passed through — never estimated. Absent
|
|
242
|
+
* means the provider did not report them.
|
|
243
|
+
*/
|
|
244
|
+
declare function recordDetectionCall(opts: {
|
|
245
|
+
label: string;
|
|
246
|
+
pieceChars: number;
|
|
247
|
+
durationMs: number;
|
|
248
|
+
items: number;
|
|
249
|
+
depth: number;
|
|
250
|
+
reroll: boolean;
|
|
251
|
+
outcome: 'success' | 'truncated' | 'timeout' | 'collapsed' | 'error';
|
|
252
|
+
inputTokens?: number;
|
|
253
|
+
outputTokens?: number;
|
|
254
|
+
}): void;
|
|
255
|
+
/**
|
|
256
|
+
* Record how one annotation got anchored (DETECTION-QUALITY-THROUGHPUT P5).
|
|
257
|
+
*
|
|
258
|
+
* The selector-vs-source check is already a WRITE-TIME INVARIANT — both
|
|
259
|
+
* `buildTextAnnotation` and `buildPdfAnnotation` throw on a selector that does
|
|
260
|
+
* not match its source — so mechanical correctness is guaranteed rather than
|
|
261
|
+
* sampled, and auditing it would measure a constant.
|
|
262
|
+
*
|
|
263
|
+
* What is genuinely uncertain is which anchoring METHOD got there. An `exact`
|
|
264
|
+
* the model quoted verbatim and that appears once is certain; one resolved by
|
|
265
|
+
* `first-of-many` (several occurrences, no usable context) or `fuzzy-match`
|
|
266
|
+
* picked a plausible occurrence and may have picked wrong. Those were visible
|
|
267
|
+
* only as log warnings — countable by a human reading worker output, which is
|
|
268
|
+
* how 47 of them went unreviewed. As a rate they are the precision number that
|
|
269
|
+
* sits beside the yield numbers.
|
|
270
|
+
*/
|
|
271
|
+
declare function recordAnchorOutcome(label: string, method: string): void;
|
|
159
272
|
|
|
160
|
-
export { extractTraceparent, getActiveTraceparent, getLogTraceContext, injectTraceparent, recordBusEmit, recordGatherDegrade, recordHandlerDuration, recordInferenceUsage, recordJobOutcome, recordSubscriberConnect, recordSubscriberDisconnect, registerJobQueueProvider, registerVectorIndexSizeProvider, withActorSpan, withSpan, withTraceparent };
|
|
161
|
-
export type { JobQueueSnapshot, TraceCarrier };
|
|
273
|
+
export { extractTraceparent, getActiveTraceparent, getLogTraceContext, injectTraceparent, recordAnchorOutcome, recordAppendStage, recordBusEmit, recordDetectionCall, recordGatherDegrade, recordGitCommand, recordHandlerDuration, recordInferenceUsage, recordJobOutcome, recordReplySuppressed, recordResumeGap, recordSubscriberConnect, recordSubscriberDisconnect, recordUnanswerableRequest, registerCorrelationRegistryProvider, registerFactPumpDepthProvider, registerJobQueueProvider, registerVectorIndexSizeProvider, withActorSpan, withSpan, withTraceparent };
|
|
274
|
+
export type { CorrelationRegistrySnapshot, JobQueueSnapshot, TraceCarrier };
|
package/dist/index.js
CHANGED
|
@@ -85,6 +85,11 @@ function getLogTraceContext() {
|
|
|
85
85
|
var METER_NAME = "semiont";
|
|
86
86
|
var meter = () => metrics.getMeter(METER_NAME);
|
|
87
87
|
var _busEmitCounter;
|
|
88
|
+
var _replySuppressedCounter;
|
|
89
|
+
var _resumeGapCounter;
|
|
90
|
+
var _unanswerableCounter;
|
|
91
|
+
var _correlationRegistryGauge;
|
|
92
|
+
var _correlationRegistryProvider;
|
|
88
93
|
var _handlerDurationHistogram;
|
|
89
94
|
var _jobOutcomeCounter;
|
|
90
95
|
var _jobDurationHistogram;
|
|
@@ -96,6 +101,8 @@ var _sseSubscribers;
|
|
|
96
101
|
var _jobQueueGauge;
|
|
97
102
|
var _jobQueueProvider;
|
|
98
103
|
var _vectorIndexSizeGauge;
|
|
104
|
+
var _factPumpDepthGauge;
|
|
105
|
+
var _factPumpDepthProvider;
|
|
99
106
|
var _vectorIndexSizeProvider;
|
|
100
107
|
function busEmitCounter() {
|
|
101
108
|
if (!_busEmitCounter) {
|
|
@@ -164,6 +171,53 @@ function sseSubscribersCounter() {
|
|
|
164
171
|
}
|
|
165
172
|
return _sseSubscribers;
|
|
166
173
|
}
|
|
174
|
+
function replySuppressedCounter() {
|
|
175
|
+
if (!_replySuppressedCounter) {
|
|
176
|
+
_replySuppressedCounter = meter().createCounter("semiont.bus.reply.suppressed", {
|
|
177
|
+
description: "Correlated replies withheld from a non-owning subscriber"
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
return _replySuppressedCounter;
|
|
181
|
+
}
|
|
182
|
+
function recordReplySuppressed(channel) {
|
|
183
|
+
replySuppressedCounter().add(1, { "bus.channel": channel });
|
|
184
|
+
}
|
|
185
|
+
function resumeGapCounter() {
|
|
186
|
+
if (!_resumeGapCounter) {
|
|
187
|
+
_resumeGapCounter = meter().createCounter("semiont.bus.resume_gap", {
|
|
188
|
+
description: "SSE resumes that degraded to a gap because replay was unavailable"
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
return _resumeGapCounter;
|
|
192
|
+
}
|
|
193
|
+
function recordResumeGap(reason) {
|
|
194
|
+
resumeGapCounter().add(1, { "bus.resume_gap.reason": reason });
|
|
195
|
+
}
|
|
196
|
+
function unanswerableCounter() {
|
|
197
|
+
if (!_unanswerableCounter) {
|
|
198
|
+
_unanswerableCounter = meter().createCounter("semiont.bus.unanswerable", {
|
|
199
|
+
description: "Request emits that reached zero subscribers and were failed at the gateway"
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
return _unanswerableCounter;
|
|
203
|
+
}
|
|
204
|
+
function recordUnanswerableRequest(channel) {
|
|
205
|
+
unanswerableCounter().add(1, { "bus.channel": channel });
|
|
206
|
+
}
|
|
207
|
+
function registerCorrelationRegistryProvider(provider) {
|
|
208
|
+
_correlationRegistryProvider = provider;
|
|
209
|
+
if (!_correlationRegistryGauge) {
|
|
210
|
+
_correlationRegistryGauge = meter().createObservableGauge("semiont.bus.correlation.size", {
|
|
211
|
+
description: "Correlation registry occupancy: live claims and retained reply payloads"
|
|
212
|
+
});
|
|
213
|
+
_correlationRegistryGauge.addCallback((observer) => {
|
|
214
|
+
if (!_correlationRegistryProvider) return;
|
|
215
|
+
const snap = _correlationRegistryProvider();
|
|
216
|
+
observer.observe(snap.claims, { "correlation.kind": "claims" });
|
|
217
|
+
observer.observe(snap.retainedReplies, { "correlation.kind": "retained_replies" });
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
}
|
|
167
221
|
function recordBusEmit(channel, scope) {
|
|
168
222
|
busEmitCounter().add(1, {
|
|
169
223
|
"bus.channel": channel,
|
|
@@ -180,6 +234,32 @@ function recordJobOutcome(jobType, outcome, durationMs) {
|
|
|
180
234
|
jobOutcomeCounter().add(1, { "job.type": jobType, "job.outcome": outcome });
|
|
181
235
|
jobDurationHistogram().record(durationMs, { "job.type": jobType, "job.outcome": outcome });
|
|
182
236
|
}
|
|
237
|
+
var _appendStageHistogram;
|
|
238
|
+
function appendStageHistogram() {
|
|
239
|
+
if (!_appendStageHistogram) {
|
|
240
|
+
_appendStageHistogram = meter().createHistogram("semiont.record.append.duration", {
|
|
241
|
+
description: "Time spent in one stage of appending an event to the record, labeled by stage: persist (JSONL write + git), materialize (view rebuild), enrich, publish. The Archivist's core write path.",
|
|
242
|
+
unit: "ms"
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
return _appendStageHistogram;
|
|
246
|
+
}
|
|
247
|
+
function recordAppendStage(stage, durationMs) {
|
|
248
|
+
appendStageHistogram().record(durationMs, { "record.stage": stage });
|
|
249
|
+
}
|
|
250
|
+
var _gitCommandHistogram;
|
|
251
|
+
function gitCommandHistogram() {
|
|
252
|
+
if (!_gitCommandHistogram) {
|
|
253
|
+
_gitCommandHistogram = meter().createHistogram("semiont.git.duration", {
|
|
254
|
+
description: "Wall time of a git subprocess. Async \u2014 this is latency, not event-loop blockage. Staging is deduped, so the `add` count is far below the number of appended events.",
|
|
255
|
+
unit: "ms"
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
return _gitCommandHistogram;
|
|
259
|
+
}
|
|
260
|
+
function recordGitCommand(command, durationMs) {
|
|
261
|
+
gitCommandHistogram().record(durationMs, { "git.command": command });
|
|
262
|
+
}
|
|
183
263
|
function gatherDegradeCounter() {
|
|
184
264
|
if (!_gatherDegradeCounter) {
|
|
185
265
|
_gatherDegradeCounter = meter().createCounter("semiont.gather.degraded", {
|
|
@@ -214,6 +294,17 @@ function registerJobQueueProvider(provider) {
|
|
|
214
294
|
});
|
|
215
295
|
}
|
|
216
296
|
}
|
|
297
|
+
function registerFactPumpDepthProvider(provider) {
|
|
298
|
+
_factPumpDepthProvider = provider;
|
|
299
|
+
if (!_factPumpDepthGauge) {
|
|
300
|
+
_factPumpDepthGauge = meter().createObservableGauge("semiont.archivist.fact_pump.depth", {
|
|
301
|
+
description: "Facts appended to the record but not yet published to the bus. Zero at rest; a rising floor means the pump is behind its transport."
|
|
302
|
+
});
|
|
303
|
+
_factPumpDepthGauge.addCallback((observer) => {
|
|
304
|
+
if (_factPumpDepthProvider) observer.observe(_factPumpDepthProvider());
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
}
|
|
217
308
|
function registerVectorIndexSizeProvider(provider) {
|
|
218
309
|
_vectorIndexSizeProvider = provider;
|
|
219
310
|
if (!_vectorIndexSizeGauge) {
|
|
@@ -251,7 +342,73 @@ function recordInferenceUsage(opts) {
|
|
|
251
342
|
});
|
|
252
343
|
}
|
|
253
344
|
}
|
|
345
|
+
var _detectionCallCounter;
|
|
346
|
+
var _detectionDurationHistogram;
|
|
347
|
+
var _detectionItemsHistogram;
|
|
348
|
+
var _detectionTokensHistogram;
|
|
349
|
+
function detectionCallCounter() {
|
|
350
|
+
if (!_detectionCallCounter) {
|
|
351
|
+
_detectionCallCounter = meter().createCounter("semiont.detection.calls", {
|
|
352
|
+
description: "Detection model calls, labeled by motivation, outcome, subdivision depth and whether this was the floor re-roll."
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
return _detectionCallCounter;
|
|
356
|
+
}
|
|
357
|
+
function detectionDurationHistogram() {
|
|
358
|
+
if (!_detectionDurationHistogram) {
|
|
359
|
+
_detectionDurationHistogram = meter().createHistogram("semiont.detection.call.duration", {
|
|
360
|
+
description: "Wall time of one detection model call, including the attempts that failed and were retried smaller.",
|
|
361
|
+
unit: "ms"
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
return _detectionDurationHistogram;
|
|
365
|
+
}
|
|
366
|
+
function detectionItemsHistogram() {
|
|
367
|
+
if (!_detectionItemsHistogram) {
|
|
368
|
+
_detectionItemsHistogram = meter().createHistogram("semiont.detection.call.items", {
|
|
369
|
+
description: "Annotations returned by one detection call. Against the input size on the same record, this is yield."
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
return _detectionItemsHistogram;
|
|
373
|
+
}
|
|
374
|
+
function detectionTokensHistogram() {
|
|
375
|
+
if (!_detectionTokensHistogram) {
|
|
376
|
+
_detectionTokensHistogram = meter().createHistogram("semiont.detection.call.tokens", {
|
|
377
|
+
description: "Provider-reported tokens for one detection call, by direction. Deliberately separate from semiont.inference.tokens: that series is the authoritative total but carries no subdivision depth, and 'what does a depth-2 call cost' is the question every sizing decision asks."
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
return _detectionTokensHistogram;
|
|
381
|
+
}
|
|
382
|
+
function recordDetectionCall(opts) {
|
|
383
|
+
const attrs = {
|
|
384
|
+
"detection.label": opts.label,
|
|
385
|
+
"detection.outcome": opts.outcome,
|
|
386
|
+
"detection.depth": opts.depth,
|
|
387
|
+
"detection.reroll": opts.reroll
|
|
388
|
+
};
|
|
389
|
+
detectionCallCounter().add(1, attrs);
|
|
390
|
+
detectionDurationHistogram().record(opts.durationMs, attrs);
|
|
391
|
+
detectionItemsHistogram().record(opts.items, attrs);
|
|
392
|
+
if (opts.inputTokens !== void 0) {
|
|
393
|
+
detectionTokensHistogram().record(opts.inputTokens, { ...attrs, "detection.direction": "input" });
|
|
394
|
+
}
|
|
395
|
+
if (opts.outputTokens !== void 0) {
|
|
396
|
+
detectionTokensHistogram().record(opts.outputTokens, { ...attrs, "detection.direction": "output" });
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
var _anchorOutcomeCounter;
|
|
400
|
+
function anchorOutcomeCounter() {
|
|
401
|
+
if (!_anchorOutcomeCounter) {
|
|
402
|
+
_anchorOutcomeCounter = meter().createCounter("semiont.detection.anchors", {
|
|
403
|
+
description: "Every annotation anchoring, labeled by the method that resolved it. EVERY outcome is counted, not just the risky ones, because a bare count of degraded anchors has no denominator \u2014 the rate is the precision signal."
|
|
404
|
+
});
|
|
405
|
+
}
|
|
406
|
+
return _anchorOutcomeCounter;
|
|
407
|
+
}
|
|
408
|
+
function recordAnchorOutcome(label, method) {
|
|
409
|
+
anchorOutcomeCounter().add(1, { "detection.label": label, "anchor.method": method });
|
|
410
|
+
}
|
|
254
411
|
|
|
255
|
-
export { extractTraceparent, getActiveTraceparent, getLogTraceContext, injectTraceparent, recordBusEmit, recordGatherDegrade, recordHandlerDuration, recordInferenceUsage, recordJobOutcome, recordSubscriberConnect, recordSubscriberDisconnect, registerJobQueueProvider, registerVectorIndexSizeProvider, withActorSpan, withSpan, withTraceparent };
|
|
412
|
+
export { extractTraceparent, getActiveTraceparent, getLogTraceContext, injectTraceparent, recordAnchorOutcome, recordAppendStage, recordBusEmit, recordDetectionCall, recordGatherDegrade, recordGitCommand, recordHandlerDuration, recordInferenceUsage, recordJobOutcome, recordReplySuppressed, recordResumeGap, recordSubscriberConnect, recordSubscriberDisconnect, recordUnanswerableRequest, registerCorrelationRegistryProvider, registerFactPumpDepthProvider, registerJobQueueProvider, registerVectorIndexSizeProvider, withActorSpan, withSpan, withTraceparent };
|
|
256
413
|
//# sourceMappingURL=index.js.map
|
|
257
414
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"s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IAAA,CAAK,YAAA,IAAgB,IAAA,IAAQ,IAAA,CAAK,eAAe,CAAA,EAAG;AACtD,IAAA,sBAAA,EAAuB,CAAE,GAAA,CAAI,IAAA,CAAK,YAAA,EAAc;AAAA,MAC9C,sBAAsB,IAAA,CAAK,QAAA;AAAA,MAC3B,mBAAmB,IAAA,CAAK,KAAA;AAAA,MACxB,qBAAA,EAAuB;AAAA,KACxB,CAAA;AAAA,EACH;AACF","file":"index.js","sourcesContent":["/**\n * @semiont/observability — public API.\n *\n * Universal surface (works in Node + browser). For SDK *initialization*,\n * import from `@semiont/observability/node` or `/web` at the process entry\n * point. Everything else uses this module.\n *\n * Tier 2 of `.plans/OBSERVABILITY.md`. The public surface:\n *\n * - `withSpan(name, fn, options?)` — wrap an async block in a span;\n * `options` carries `kind` and `attrs`.\n * - `withActorSpan(actor, channel, fn, extraAttrs?)` — consumer-span\n * wrapper for bus-event handlers, with handler-duration recording.\n * - `injectTraceparent(payload)` / `extractTraceparent(payload)` — W3C\n * trace-context propagation across the SSE channel (the bus payload\n * gets a `_trace?: { traceparent }` sibling to `correlationId`).\n * - `withTraceparent(carrier, fn)` — run `fn` with the incoming\n * traceparent as the parent context.\n * - `getActiveTraceparent()` — read the active span's traceparent for\n * manual propagation (e.g. attaching to a fetch header or SSE field).\n * - `getLogTraceContext()` — active `trace_id` / `span_id` for log-line\n * correlation.\n * - Metric recorders (`recordBusEmit`, `recordHandlerDuration`,\n * `recordJobOutcome`, `recordSubscriberConnect` / `Disconnect`,\n * `recordInferenceUsage`) and gauge providers\n * (`registerJobQueueProvider`, `registerVectorIndexSizeProvider`).\n *\n * No-op when no exporter is configured: `@opentelemetry/api`'s default\n * tracer is a no-op, so `withSpan` is essentially free until\n * `initObservability*()` runs.\n */\n\nimport {\n context,\n isSpanContextValid,\n metrics,\n propagation,\n SpanKind,\n SpanStatusCode,\n trace,\n type Attributes,\n type Counter,\n type Histogram,\n type ObservableGauge,\n type Span,\n type UpDownCounter,\n} from '@opentelemetry/api';\nimport { setBusLogTraceIdProvider } from '@semiont/core';\n\n// Wire `busLog`'s trace-id provider once at module load. When an OTel\n// SDK is initialized (and a span is active when `busLog` fires), the\n// emitted line gets a `trace=<8hex>` suffix that correlates the\n// grep-timeline with the trace UI. No-op when no SDK is active.\nsetBusLogTraceIdProvider(() => {\n const span = trace.getActiveSpan();\n if (!span) return undefined;\n const ctx = span.spanContext();\n if (!isSpanContextValid(ctx)) return undefined;\n return ctx.traceId;\n});\n\nconst TRACER_NAME = 'semiont';\n\nconst tracer = () => trace.getTracer(TRACER_NAME);\n\n// ── withSpan ───────────────────────────────────────────────────────────\n\n/**\n * Wrap an async block in a span. The span is started before `fn` runs and\n * ended after it resolves or rejects; exceptions are recorded and the span\n * status is set to ERROR. `kind` defaults to INTERNAL.\n */\nexport async function withSpan<T>(\n name: string,\n fn: (span: Span) => Promise<T> | T,\n options?: { kind?: SpanKind; attrs?: Attributes },\n): Promise<T> {\n const span = tracer().startSpan(name, {\n kind: options?.kind ?? SpanKind.INTERNAL,\n ...(options?.attrs ? { attributes: options.attrs } : {}),\n });\n try {\n return await context.with(trace.setSpan(context.active(), span), () => fn(span));\n } catch (err) {\n span.recordException(err as Error);\n span.setStatus({\n code: SpanStatusCode.ERROR,\n message: err instanceof Error ? err.message : String(err),\n });\n throw err;\n } finally {\n span.end();\n }\n}\n\n// ── Traceparent on bus payloads ────────────────────────────────────────\n\nconst TRACE_FIELD = '_trace';\n\n/**\n * Sibling of `correlationId` on bus payloads. Lives on the SSE event body\n * because SSE has no header trailer; the SDK strips it before delivering\n * the payload to subscribers. Additive — payloads without `_trace` parse\n * unchanged.\n */\nexport interface TraceCarrier {\n /** W3C `traceparent` header value (`00-<traceId>-<spanId>-<flags>`). */\n traceparent: string;\n /** W3C `tracestate` header value (vendor-specific extensions). */\n tracestate?: string;\n}\n\n/**\n * Read the active span's W3C traceparent (and tracestate). Returns\n * `undefined` if no span is active.\n */\nexport function getActiveTraceparent(): TraceCarrier | undefined {\n const carrier: Record<string, string> = {};\n propagation.inject(context.active(), carrier);\n const traceparent = carrier['traceparent'];\n if (!traceparent) return undefined;\n return carrier['tracestate']\n ? { traceparent, tracestate: carrier['tracestate'] }\n : { traceparent };\n}\n\n/**\n * Attach the active span's trace-context to a payload object as\n * `_trace`. No-op when no span is active. Returns the same object\n * reference for chaining.\n */\nexport function injectTraceparent<T extends Record<string, unknown>>(payload: T): T {\n const carrier = getActiveTraceparent();\n if (carrier) {\n (payload as Record<string, unknown>)[TRACE_FIELD] = carrier;\n }\n return payload;\n}\n\n/**\n * Strip and return the `_trace` field from a payload. Mutates `payload`.\n * The field is internal plumbing and should not be visible to subscribers.\n */\nexport function extractTraceparent<T extends Record<string, unknown>>(\n payload: T,\n): TraceCarrier | undefined {\n const carrier = (payload as Record<string, unknown>)[TRACE_FIELD] as\n | TraceCarrier\n | undefined;\n if (carrier !== undefined) {\n delete (payload as Record<string, unknown>)[TRACE_FIELD];\n }\n if (!carrier || typeof carrier.traceparent !== 'string') return undefined;\n return carrier;\n}\n\n/**\n * Run `fn` with the given W3C traceparent set as the parent context.\n * Any spans started inside `fn` will be children of the incoming trace.\n * No-op if `carrier` is undefined.\n */\nexport function withTraceparent<T>(\n carrier: TraceCarrier | undefined,\n fn: () => T,\n): T {\n if (!carrier) return fn();\n const carrierObj: Record<string, string> = { traceparent: carrier.traceparent };\n if (carrier.tracestate) carrierObj['tracestate'] = carrier.tracestate;\n const ctx = propagation.extract(context.active(), carrierObj);\n return context.with(ctx, fn);\n}\n\n// ── Actor handler convenience ──────────────────────────────────────────\n\n/**\n * Wrap a bus-event handler in an `actor.<name>:<channel>` consumer span.\n * Used at every `eventBus.get(channel).subscribe(handler)` site inside\n * an actor (Stower, Gatherer, Matcher, Browser, Smelter), to attribute\n * each in-process subscriber's work to a span without scattering manual\n * `withSpan` calls across handler bodies.\n *\n * The span's parent is the active context at the time the handler\n * fires — which is the `bus.dispatch:<channel>` span on the gateway\n * (Subject.next runs synchronously inside the dispatch span), or the\n * `bus.emit:<channel>` span when an actor emits to itself.\n */\nexport async function withActorSpan<T>(\n actor: string,\n channel: string,\n fn: (span: Span) => Promise<T> | T,\n extraAttrs?: Attributes,\n): Promise<T> {\n const start = performance.now();\n try {\n return await withSpan(`actor.${actor}:${channel}`, fn, {\n kind: SpanKind.CONSUMER,\n attrs: {\n actor,\n 'bus.channel': channel,\n ...(extraAttrs ?? {}),\n },\n });\n } finally {\n recordHandlerDuration(actor, channel, performance.now() - start);\n }\n}\n\n// ── Log correlation ────────────────────────────────────────────────────\n\n/**\n * Read the active span's `trace_id` / `span_id` for log-line correlation.\n * Tier 3 of `.plans/OBSERVABILITY.md`. Each structured log line gets\n * tagged with these so a log query in CloudWatch / Loki / Datadog can\n * jump to the trace in Tempo / Jaeger / X-Ray.\n *\n * Returns `undefined` if no span is active, or if the active span's\n * context is invalid (uninitialized SDK, no-op tracer).\n */\nexport function getLogTraceContext(): { trace_id: string; span_id: string } | undefined {\n const span = trace.getActiveSpan();\n if (!span) return undefined;\n const ctx = span.spanContext();\n if (!isSpanContextValid(ctx)) return undefined;\n return { trace_id: ctx.traceId, span_id: ctx.spanId };\n}\n\n// ── Metrics — Tier 3 ───────────────────────────────────────────────────\n\nconst METER_NAME = 'semiont';\n\nconst meter = () => metrics.getMeter(METER_NAME);\n\nlet _busEmitCounter: Counter | undefined;\nlet _handlerDurationHistogram: Histogram | undefined;\nlet _jobOutcomeCounter: Counter | undefined;\nlet _jobDurationHistogram: Histogram | undefined;\nlet _gatherDegradeCounter: Counter | undefined;\nlet _inferenceCallsCounter: Counter | undefined;\nlet _inferenceTokensCounter: Counter | undefined;\nlet _inferenceDurationHistogram: Histogram | undefined;\nlet _sseSubscribers: UpDownCounter | undefined;\nlet _jobQueueGauge: ObservableGauge | undefined;\nlet _jobQueueProvider: (() => Promise<JobQueueSnapshot> | JobQueueSnapshot) | undefined;\nlet _vectorIndexSizeGauge: ObservableGauge | undefined;\nlet _vectorIndexSizeProvider: (() => Promise<number> | number) | undefined;\n\n/** Snapshot of job-queue contents by status. Match `JobQueue.getStats()`. */\nexport interface JobQueueSnapshot {\n pending: number;\n running: number;\n complete: number;\n failed: number;\n cancelled: number;\n}\n\nfunction busEmitCounter(): Counter {\n if (!_busEmitCounter) {\n _busEmitCounter = meter().createCounter('semiont.bus.emit', {\n description: 'Bus emits by channel and scope',\n });\n }\n return _busEmitCounter;\n}\n\nfunction handlerDurationHistogram(): Histogram {\n if (!_handlerDurationHistogram) {\n _handlerDurationHistogram = meter().createHistogram('semiont.handler.duration', {\n description: 'In-process actor handler duration',\n unit: 'ms',\n });\n }\n return _handlerDurationHistogram;\n}\n\nfunction jobOutcomeCounter(): Counter {\n if (!_jobOutcomeCounter) {\n _jobOutcomeCounter = meter().createCounter('semiont.job.outcome', {\n description: 'Worker job completions by type and outcome',\n });\n }\n return _jobOutcomeCounter;\n}\n\nfunction jobDurationHistogram(): Histogram {\n if (!_jobDurationHistogram) {\n _jobDurationHistogram = meter().createHistogram('semiont.job.duration', {\n description: 'Worker job duration by type',\n unit: 'ms',\n });\n }\n return _jobDurationHistogram;\n}\n\nfunction inferenceCallsCounter(): Counter {\n if (!_inferenceCallsCounter) {\n _inferenceCallsCounter = meter().createCounter('semiont.inference.calls', {\n description: 'Inference API calls by provider, model, and outcome',\n });\n }\n return _inferenceCallsCounter;\n}\n\nfunction inferenceTokensCounter(): Counter {\n if (!_inferenceTokensCounter) {\n _inferenceTokensCounter = meter().createCounter('semiont.inference.tokens', {\n description: 'Inference token usage by provider, model, and direction',\n });\n }\n return _inferenceTokensCounter;\n}\n\nfunction inferenceDurationHistogram(): Histogram {\n if (!_inferenceDurationHistogram) {\n _inferenceDurationHistogram = meter().createHistogram('semiont.inference.duration', {\n description: 'Inference call duration by provider, model, and outcome',\n unit: 'ms',\n });\n }\n return _inferenceDurationHistogram;\n}\n\nfunction sseSubscribersCounter(): UpDownCounter {\n if (!_sseSubscribers) {\n _sseSubscribers = meter().createUpDownCounter('semiont.sse.subscribers', {\n description: 'Active SSE subscribers',\n });\n }\n return _sseSubscribers;\n}\n\n/** Increment the bus-emit counter. Called at every transport `emit` site. */\nexport function recordBusEmit(channel: string, scope?: string): void {\n busEmitCounter().add(1, {\n 'bus.channel': channel,\n ...(scope ? { 'bus.scope': scope } : {}),\n });\n}\n\n/** Record an in-process actor handler's duration. */\nexport function recordHandlerDuration(actor: string, channel: string, durationMs: number): void {\n handlerDurationHistogram().record(durationMs, {\n actor,\n 'bus.channel': channel,\n });\n}\n\n/** Record a worker job's outcome and duration. */\nexport function recordJobOutcome(jobType: string, outcome: 'completed' | 'failed', durationMs: number): void {\n jobOutcomeCounter().add(1, { 'job.type': jobType, 'job.outcome': outcome });\n jobDurationHistogram().record(durationMs, { 'job.type': jobType, 'job.outcome': outcome });\n}\n\nfunction gatherDegradeCounter(): Counter {\n if (!_gatherDegradeCounter) {\n _gatherDegradeCounter = meter().createCounter('semiont.gather.degraded', {\n description: 'Gathers that degraded because an eventually-consistent projection did not catch up within its read barrier (vectors: absent semanticContext; graph: projection-lag failure). Labeled by projection.',\n });\n }\n return _gatherDegradeCounter;\n}\n\n/**\n * Record a gather degraded by a projection read barrier: `'vectors'` — the\n * Smelter settle barrier timed out (semanticContext shipped absent);\n * `'graph'` — the Weaver applied barrier + poll floor exhausted (projection\n * lag surfaced as a distinct failure). Fleet-alertable counterpart of the\n * `[gather DEGRADED]` L4 breadcrumbs — a rising rate on either label means\n * that pipeline is not keeping up.\n */\nexport function recordGatherDegrade(projection: 'graph' | 'vectors'): void {\n gatherDegradeCounter().add(1, { projection });\n}\n\n/** Increment the SSE subscriber gauge — call on `/bus/subscribe` open. */\nexport function recordSubscriberConnect(): void {\n sseSubscribersCounter().add(1);\n}\n\n/** Decrement on disconnect. Pair with `recordSubscriberConnect`. */\nexport function recordSubscriberDisconnect(): void {\n sseSubscribersCounter().add(-1);\n}\n\n/**\n * Register a callback that returns the current job-queue snapshot.\n * Polled at the SDK's metric-collection interval. The single gauge\n * emits one observation per status (`pending`, `running`, …) tagged\n * with the `job.status` attribute. Idempotent — last registered\n * provider wins.\n */\nexport function registerJobQueueProvider(\n provider: () => Promise<JobQueueSnapshot> | JobQueueSnapshot,\n): void {\n _jobQueueProvider = provider;\n if (!_jobQueueGauge) {\n _jobQueueGauge = meter().createObservableGauge('semiont.job.queue.size', {\n description: 'Job queue size by status',\n });\n _jobQueueGauge.addCallback(async (observer) => {\n if (!_jobQueueProvider) return;\n const snap = await _jobQueueProvider();\n observer.observe(snap.pending, { 'job.status': 'pending' });\n observer.observe(snap.running, { 'job.status': 'running' });\n observer.observe(snap.complete, { 'job.status': 'complete' });\n observer.observe(snap.failed, { 'job.status': 'failed' });\n observer.observe(snap.cancelled, { 'job.status': 'cancelled' });\n });\n }\n}\n\n/**\n * Register a callback that returns the current vector-index size\n * (point count). Async to allow remote queries (Qdrant). Polled at\n * the metric-collection interval.\n */\nexport function registerVectorIndexSizeProvider(\n provider: () => Promise<number> | number,\n): void {\n _vectorIndexSizeProvider = provider;\n if (!_vectorIndexSizeGauge) {\n _vectorIndexSizeGauge = meter().createObservableGauge('semiont.vector.index.size', {\n description: 'Vector store point count',\n });\n _vectorIndexSizeGauge.addCallback(async (observer) => {\n if (_vectorIndexSizeProvider) {\n const value = await _vectorIndexSizeProvider();\n observer.observe(value);\n }\n });\n }\n}\n\n/**\n * Record an inference call. Token counts are optional — providers that\n * don't expose them (or fail before generating) record only call count\n * and duration.\n */\nexport function recordInferenceUsage(opts: {\n provider: string;\n model: string;\n durationMs: number;\n outcome: 'success' | 'error';\n inputTokens?: number;\n outputTokens?: number;\n}): void {\n const baseAttrs = {\n 'inference.provider': opts.provider,\n 'inference.model': opts.model,\n 'inference.outcome': opts.outcome,\n };\n inferenceCallsCounter().add(1, baseAttrs);\n inferenceDurationHistogram().record(opts.durationMs, baseAttrs);\n if (opts.inputTokens != null && opts.inputTokens > 0) {\n inferenceTokensCounter().add(opts.inputTokens, {\n 'inference.provider': opts.provider,\n 'inference.model': opts.model,\n 'inference.direction': 'input',\n });\n }\n if (opts.outputTokens != null && opts.outputTokens > 0) {\n inferenceTokensCounter().add(opts.outputTokens, {\n 'inference.provider': opts.provider,\n 'inference.model': opts.model,\n 'inference.direction': 'output',\n });\n }\n}\n\n// ── Re-exports from @opentelemetry/api ─────────────────────────────────\n\nexport { SpanKind, SpanStatusCode, type Attributes, type Span } from '@opentelemetry/api';\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";;;;;AAqDA,wBAAA,CAAyB,MAAM;AAC7B,EAAA,MAAM,IAAA,GAAO,MAAM,aAAA,EAAc;AACjC,EAAA,IAAI,CAAC,MAAM,OAAO,MAAA;AAClB,EAAA,MAAM,GAAA,GAAM,KAAK,WAAA,EAAY;AAC7B,EAAA,IAAI,CAAC,kBAAA,CAAmB,GAAG,CAAA,EAAG,OAAO,MAAA;AACrC,EAAA,OAAO,GAAA,CAAI,OAAA;AACb,CAAC,CAAA;AAED,IAAM,WAAA,GAAc,SAAA;AAEpB,IAAM,MAAA,GAAS,MAAM,KAAA,CAAM,SAAA,CAAU,WAAW,CAAA;AAShD,eAAsB,QAAA,CACpB,IAAA,EACA,EAAA,EACA,OAAA,EACY;AACZ,EAAA,MAAM,IAAA,GAAO,MAAA,EAAO,CAAE,SAAA,CAAU,IAAA,EAAM;AAAA,IACpC,IAAA,EAAM,OAAA,EAAS,IAAA,IAAQ,QAAA,CAAS,QAAA;AAAA,IAChC,GAAI,SAAS,KAAA,GAAQ,EAAE,YAAY,OAAA,CAAQ,KAAA,KAAU;AAAC,GACvD,CAAA;AACD,EAAA,IAAI;AACF,IAAA,OAAO,MAAM,OAAA,CAAQ,IAAA,CAAK,KAAA,CAAM,OAAA,CAAQ,OAAA,CAAQ,MAAA,EAAO,EAAG,IAAI,CAAA,EAAG,MAAM,EAAA,CAAG,IAAI,CAAC,CAAA;AAAA,EACjF,SAAS,GAAA,EAAK;AACZ,IAAA,IAAA,CAAK,gBAAgB,GAAY,CAAA;AACjC,IAAA,IAA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BAAgB,MAAA,EAAW;AAClC,IAAA,wBAAA,EAAyB,CAAE,OAAO,IAAA,CAAK,WAAA,EAAa,EAAE,GAAG,KAAA,EAAO,qBAAA,EAAuB,OAAA,EAAS,CAAA;AAAA,EAClG;AACA,EAAA,IAAI,IAAA,CAAK,iBAAiB,MAAA,EAAW;AACnC,IAAA,wBAAA,EAAyB,CAAE,OAAO,IAAA,CAAK,YAAA,EAAc,EAAE,GAAG,KAAA,EAAO,qBAAA,EAAuB,QAAA,EAAU,CAAA;AAAA,EACpG;AACF;AAEA,IAAI,qBAAA;AACJ,SAAS,oBAAA,GAAgC;AACvC,EAAA,IAAI,CAAC,qBAAA,EAAuB;AAC1B,IAAA,qBAAA,GAAwB,KAAA,EAAM,CAAE,aAAA,CAAc,2BAAA,EAA6B;AAAA,MACzE,WAAA,EAAa;AAAA,KACd,CAAA;AAAA,EACH;AACA,EAAA,OAAO,qBAAA;AACT;AAkBO,SAAS,mBAAA,CAAoB,OAAe,MAAA,EAAsB;AACvE,EAAA,oBAAA,EAAqB,CAAE,IAAI,CAAA,EAAG,EAAE,mBAAmB,KAAA,EAAO,eAAA,EAAiB,QAAQ,CAAA;AACrF","file":"index.js","sourcesContent":["/**\n * @semiont/observability — public API.\n *\n * Universal surface (works in Node + browser). For SDK *initialization*,\n * import from `@semiont/observability/node` or `/web` at the process entry\n * point. Everything else uses this module.\n *\n * Tier 2 of `.plans/OBSERVABILITY.md`. The public surface:\n *\n * - `withSpan(name, fn, options?)` — wrap an async block in a span;\n * `options` carries `kind` and `attrs`.\n * - `withActorSpan(actor, channel, fn, extraAttrs?)` — consumer-span\n * wrapper for bus-event handlers, with handler-duration recording.\n * - `injectTraceparent(payload)` / `extractTraceparent(payload)` — W3C\n * trace-context propagation across the SSE channel (the bus payload\n * gets a `_trace?: { traceparent }` sibling to `correlationId`).\n * - `withTraceparent(carrier, fn)` — run `fn` with the incoming\n * traceparent as the parent context.\n * - `getActiveTraceparent()` — read the active span's traceparent for\n * manual propagation (e.g. attaching to a fetch header or SSE field).\n * - `getLogTraceContext()` — active `trace_id` / `span_id` for log-line\n * correlation.\n * - Metric recorders (`recordBusEmit`, `recordHandlerDuration`,\n * `recordJobOutcome`, `recordSubscriberConnect` / `Disconnect`,\n * `recordInferenceUsage`) and gauge providers\n * (`registerJobQueueProvider`, `registerVectorIndexSizeProvider`).\n *\n * No-op when no exporter is configured: `@opentelemetry/api`'s default\n * tracer is a no-op, so `withSpan` is essentially free until\n * `initObservability*()` runs.\n */\n\nimport {\n context,\n isSpanContextValid,\n metrics,\n propagation,\n SpanKind,\n SpanStatusCode,\n trace,\n type Attributes,\n type Counter,\n type Histogram,\n type ObservableGauge,\n type Span,\n type UpDownCounter,\n} from '@opentelemetry/api';\nimport { setBusLogTraceIdProvider } from '@semiont/core';\n\n// Wire `busLog`'s trace-id provider once at module load. When an OTel\n// SDK is initialized (and a span is active when `busLog` fires), the\n// emitted line gets a `trace=<8hex>` suffix that correlates the\n// grep-timeline with the trace UI. No-op when no SDK is active.\nsetBusLogTraceIdProvider(() => {\n const span = trace.getActiveSpan();\n if (!span) return undefined;\n const ctx = span.spanContext();\n if (!isSpanContextValid(ctx)) return undefined;\n return ctx.traceId;\n});\n\nconst TRACER_NAME = 'semiont';\n\nconst tracer = () => trace.getTracer(TRACER_NAME);\n\n// ── withSpan ───────────────────────────────────────────────────────────\n\n/**\n * Wrap an async block in a span. The span is started before `fn` runs and\n * ended after it resolves or rejects; exceptions are recorded and the span\n * status is set to ERROR. `kind` defaults to INTERNAL.\n */\nexport async function withSpan<T>(\n name: string,\n fn: (span: Span) => Promise<T> | T,\n options?: { kind?: SpanKind; attrs?: Attributes },\n): Promise<T> {\n const span = tracer().startSpan(name, {\n kind: options?.kind ?? SpanKind.INTERNAL,\n ...(options?.attrs ? { attributes: options.attrs } : {}),\n });\n try {\n return await context.with(trace.setSpan(context.active(), span), () => fn(span));\n } catch (err) {\n span.recordException(err as Error);\n span.setStatus({\n code: SpanStatusCode.ERROR,\n message: err instanceof Error ? err.message : String(err),\n });\n throw err;\n } finally {\n span.end();\n }\n}\n\n// ── Traceparent on bus payloads ────────────────────────────────────────\n\nconst TRACE_FIELD = '_trace';\n\n/**\n * Sibling of `correlationId` on bus payloads. Lives on the SSE event body\n * because SSE has no header trailer; the SDK strips it before delivering\n * the payload to subscribers. Additive — payloads without `_trace` parse\n * unchanged.\n */\nexport interface TraceCarrier {\n /** W3C `traceparent` header value (`00-<traceId>-<spanId>-<flags>`). */\n traceparent: string;\n /** W3C `tracestate` header value (vendor-specific extensions). */\n tracestate?: string;\n}\n\n/**\n * Read the active span's W3C traceparent (and tracestate). Returns\n * `undefined` if no span is active.\n */\nexport function getActiveTraceparent(): TraceCarrier | undefined {\n const carrier: Record<string, string> = {};\n propagation.inject(context.active(), carrier);\n const traceparent = carrier['traceparent'];\n if (!traceparent) return undefined;\n return carrier['tracestate']\n ? { traceparent, tracestate: carrier['tracestate'] }\n : { traceparent };\n}\n\n/**\n * Attach the active span's trace-context to a payload object as\n * `_trace`. No-op when no span is active. Returns the same object\n * reference for chaining.\n */\nexport function injectTraceparent<T extends Record<string, unknown>>(payload: T): T {\n const carrier = getActiveTraceparent();\n if (carrier) {\n (payload as Record<string, unknown>)[TRACE_FIELD] = carrier;\n }\n return payload;\n}\n\n/**\n * Strip and return the `_trace` field from a payload. Mutates `payload`.\n * The field is internal plumbing and should not be visible to subscribers.\n */\nexport function extractTraceparent<T extends Record<string, unknown>>(\n payload: T,\n): TraceCarrier | undefined {\n const carrier = (payload as Record<string, unknown>)[TRACE_FIELD] as\n | TraceCarrier\n | undefined;\n if (carrier !== undefined) {\n delete (payload as Record<string, unknown>)[TRACE_FIELD];\n }\n if (!carrier || typeof carrier.traceparent !== 'string') return undefined;\n return carrier;\n}\n\n/**\n * Run `fn` with the given W3C traceparent set as the parent context.\n * Any spans started inside `fn` will be children of the incoming trace.\n * No-op if `carrier` is undefined.\n */\nexport function withTraceparent<T>(\n carrier: TraceCarrier | undefined,\n fn: () => T,\n): T {\n if (!carrier) return fn();\n const carrierObj: Record<string, string> = { traceparent: carrier.traceparent };\n if (carrier.tracestate) carrierObj['tracestate'] = carrier.tracestate;\n const ctx = propagation.extract(context.active(), carrierObj);\n return context.with(ctx, fn);\n}\n\n// ── Actor handler convenience ──────────────────────────────────────────\n\n/**\n * Wrap a bus-event handler in an `actor.<name>:<channel>` consumer span.\n * Used at every `eventBus.get(channel).subscribe(handler)` site inside\n * an actor (Stower, Gatherer, Matcher, Browser, Smelter), to attribute\n * each in-process subscriber's work to a span without scattering manual\n * `withSpan` calls across handler bodies.\n *\n * The span's parent is the active context at the time the handler\n * fires — which is the `bus.dispatch:<channel>` span on the gateway\n * (Subject.next runs synchronously inside the dispatch span), or the\n * `bus.emit:<channel>` span when an actor emits to itself.\n */\nexport async function withActorSpan<T>(\n actor: string,\n channel: string,\n fn: (span: Span) => Promise<T> | T,\n extraAttrs?: Attributes,\n): Promise<T> {\n const start = performance.now();\n try {\n return await withSpan(`actor.${actor}:${channel}`, fn, {\n kind: SpanKind.CONSUMER,\n attrs: {\n actor,\n 'bus.channel': channel,\n ...(extraAttrs ?? {}),\n },\n });\n } finally {\n recordHandlerDuration(actor, channel, performance.now() - start);\n }\n}\n\n// ── Log correlation ────────────────────────────────────────────────────\n\n/**\n * Read the active span's `trace_id` / `span_id` for log-line correlation.\n * Tier 3 of `.plans/OBSERVABILITY.md`. Each structured log line gets\n * tagged with these so a log query in CloudWatch / Loki / Datadog can\n * jump to the trace in Tempo / Jaeger / X-Ray.\n *\n * Returns `undefined` if no span is active, or if the active span's\n * context is invalid (uninitialized SDK, no-op tracer).\n */\nexport function getLogTraceContext(): { trace_id: string; span_id: string } | undefined {\n const span = trace.getActiveSpan();\n if (!span) return undefined;\n const ctx = span.spanContext();\n if (!isSpanContextValid(ctx)) return undefined;\n return { trace_id: ctx.traceId, span_id: ctx.spanId };\n}\n\n// ── Metrics — Tier 3 ───────────────────────────────────────────────────\n\nconst METER_NAME = 'semiont';\n\nconst meter = () => metrics.getMeter(METER_NAME);\n\nlet _busEmitCounter: Counter | undefined;\nlet _replySuppressedCounter: Counter | undefined;\nlet _resumeGapCounter: Counter | undefined;\nlet _unanswerableCounter: Counter | undefined;\nlet _correlationRegistryGauge: ObservableGauge | undefined;\nlet _correlationRegistryProvider: (() => CorrelationRegistrySnapshot) | undefined;\nlet _handlerDurationHistogram: Histogram | undefined;\nlet _jobOutcomeCounter: Counter | undefined;\nlet _jobDurationHistogram: Histogram | undefined;\nlet _gatherDegradeCounter: Counter | undefined;\nlet _inferenceCallsCounter: Counter | undefined;\nlet _inferenceTokensCounter: Counter | undefined;\nlet _inferenceDurationHistogram: Histogram | undefined;\nlet _sseSubscribers: UpDownCounter | undefined;\nlet _jobQueueGauge: ObservableGauge | undefined;\nlet _jobQueueProvider: (() => Promise<JobQueueSnapshot> | JobQueueSnapshot) | undefined;\nlet _vectorIndexSizeGauge: ObservableGauge | undefined;\nlet _factPumpDepthGauge: ObservableGauge | undefined;\nlet _factPumpDepthProvider: (() => number) | undefined;\nlet _vectorIndexSizeProvider: (() => Promise<number> | number) | undefined;\n\n/** Snapshot of job-queue contents by status. Match `JobQueue.getStats()`. */\nexport interface JobQueueSnapshot {\n pending: number;\n running: number;\n complete: number;\n failed: number;\n cancelled: number;\n}\n\nfunction busEmitCounter(): Counter {\n if (!_busEmitCounter) {\n _busEmitCounter = meter().createCounter('semiont.bus.emit', {\n description: 'Bus emits by channel and scope',\n });\n }\n return _busEmitCounter;\n}\n\nfunction handlerDurationHistogram(): Histogram {\n if (!_handlerDurationHistogram) {\n _handlerDurationHistogram = meter().createHistogram('semiont.handler.duration', {\n description: 'In-process actor handler duration',\n unit: 'ms',\n });\n }\n return _handlerDurationHistogram;\n}\n\nfunction jobOutcomeCounter(): Counter {\n if (!_jobOutcomeCounter) {\n _jobOutcomeCounter = meter().createCounter('semiont.job.outcome', {\n description: 'Worker job completions by type and outcome',\n });\n }\n return _jobOutcomeCounter;\n}\n\nfunction jobDurationHistogram(): Histogram {\n if (!_jobDurationHistogram) {\n _jobDurationHistogram = meter().createHistogram('semiont.job.duration', {\n description: 'Worker job duration by type',\n unit: 'ms',\n });\n }\n return _jobDurationHistogram;\n}\n\nfunction inferenceCallsCounter(): Counter {\n if (!_inferenceCallsCounter) {\n _inferenceCallsCounter = meter().createCounter('semiont.inference.calls', {\n description: 'Inference API calls by provider, model, and outcome',\n });\n }\n return _inferenceCallsCounter;\n}\n\nfunction inferenceTokensCounter(): Counter {\n if (!_inferenceTokensCounter) {\n _inferenceTokensCounter = meter().createCounter('semiont.inference.tokens', {\n description: 'Inference token usage by provider, model, and direction',\n });\n }\n return _inferenceTokensCounter;\n}\n\nfunction inferenceDurationHistogram(): Histogram {\n if (!_inferenceDurationHistogram) {\n _inferenceDurationHistogram = meter().createHistogram('semiont.inference.duration', {\n description: 'Inference call duration by provider, model, and outcome',\n unit: 'ms',\n });\n }\n return _inferenceDurationHistogram;\n}\n\nfunction sseSubscribersCounter(): UpDownCounter {\n if (!_sseSubscribers) {\n _sseSubscribers = meter().createUpDownCounter('semiont.sse.subscribers', {\n description: 'Active SSE subscribers',\n });\n }\n return _sseSubscribers;\n}\n\nfunction replySuppressedCounter(): Counter {\n if (!_replySuppressedCounter) {\n _replySuppressedCounter = meter().createCounter('semiont.bus.reply.suppressed', {\n description: 'Correlated replies withheld from a non-owning subscriber',\n });\n }\n return _replySuppressedCounter;\n}\n\n/**\n * A correlated reply was withheld from a subscriber that does not own its\n * correlationId (CORRELATED-REPLY-ROUTING P5).\n *\n * Counts ONLY that case. A frame with no correlationId is a shape violation\n * (warned, not counted), and a cid nobody claimed is the structural in-process\n * case that fires constantly — counting either would drown the signal this\n * metric exists to show: the fan-out amplification the delivery filter removes.\n */\nexport function recordReplySuppressed(channel: string): void {\n replySuppressedCounter().add(1, { 'bus.channel': channel });\n}\n\nfunction resumeGapCounter(): Counter {\n if (!_resumeGapCounter) {\n _resumeGapCounter = meter().createCounter('semiont.bus.resume_gap', {\n description: 'SSE resumes that degraded to a gap because replay was unavailable',\n });\n }\n return _resumeGapCounter;\n}\n\n/**\n * An SSE resume could not be served and the client was told to fall back to\n * cache. This degradation is CORRECT by design and therefore silent — which is\n * exactly why it needs a number. A rising rate means clients are losing\n * history, and nothing else in the stack says so.\n */\nexport function recordResumeGap(reason: string): void {\n resumeGapCounter().add(1, { 'bus.resume_gap.reason': reason });\n}\n\nfunction unanswerableCounter(): Counter {\n if (!_unanswerableCounter) {\n _unanswerableCounter = meter().createCounter('semiont.bus.unanswerable', {\n description: 'Request emits that reached zero subscribers and were failed at the gateway',\n });\n }\n return _unanswerableCounter;\n}\n\n/**\n * A request-shaped emit reached no subscriber, so the gateway synthesized its\n * mapped failure (ARCHIVIST-STAYS-UP P3). By channel, this is the absence rate\n * of the service that answers it — the difference between \"it went down once\"\n * and \"it is flapping.\"\n */\nexport function recordUnanswerableRequest(channel: string): void {\n unanswerableCounter().add(1, { 'bus.channel': channel });\n}\n\n/** Claims held and reply payloads retained by a gateway's correlation registry. */\nexport interface CorrelationRegistrySnapshot {\n claims: number;\n retainedReplies: number;\n}\n\n/**\n * Register a callback returning the gateway's correlation-registry occupancy.\n *\n * COUNTS, not bytes: retention is count-budgeted today (byte-budgeting is a\n * known limit in CORRELATED-REPLY-ROUTING), so `retainedReplies` is a proxy for\n * heap, not a measure of it. It is still the closest observable to the question\n * two OOM investigations keep asking — a browse result can be 1-2 MB, and up to\n * REPLY_RETENTION_MAX of them are held at once.\n */\nexport function registerCorrelationRegistryProvider(\n provider: () => CorrelationRegistrySnapshot,\n): void {\n _correlationRegistryProvider = provider;\n if (!_correlationRegistryGauge) {\n _correlationRegistryGauge = meter().createObservableGauge('semiont.bus.correlation.size', {\n description: 'Correlation registry occupancy: live claims and retained reply payloads',\n });\n _correlationRegistryGauge.addCallback((observer) => {\n if (!_correlationRegistryProvider) return;\n const snap = _correlationRegistryProvider();\n observer.observe(snap.claims, { 'correlation.kind': 'claims' });\n observer.observe(snap.retainedReplies, { 'correlation.kind': 'retained_replies' });\n });\n }\n}\n\n/** Increment the bus-emit counter. Called at every transport `emit` site. */\nexport function recordBusEmit(channel: string, scope?: string): void {\n busEmitCounter().add(1, {\n 'bus.channel': channel,\n ...(scope ? { 'bus.scope': scope } : {}),\n });\n}\n\n/** Record an in-process actor handler's duration. */\nexport function recordHandlerDuration(actor: string, channel: string, durationMs: number): void {\n handlerDurationHistogram().record(durationMs, {\n actor,\n 'bus.channel': channel,\n });\n}\n\n/** Record a worker job's outcome and duration. */\nexport function recordJobOutcome(jobType: string, outcome: 'completed' | 'failed', durationMs: number): void {\n jobOutcomeCounter().add(1, { 'job.type': jobType, 'job.outcome': outcome });\n jobDurationHistogram().record(durationMs, { 'job.type': jobType, 'job.outcome': outcome });\n}\n\nlet _appendStageHistogram: Histogram | undefined;\nfunction appendStageHistogram(): Histogram {\n if (!_appendStageHistogram) {\n _appendStageHistogram = meter().createHistogram('semiont.record.append.duration', {\n description: 'Time spent in one stage of appending an event to the record, labeled by stage: persist (JSONL write + git), materialize (view rebuild), enrich, publish. The Archivist\\'s core write path.',\n unit: 'ms',\n });\n }\n return _appendStageHistogram;\n}\n\n/**\n * Record one stage of `EventStore.appendEvent` (ARCHIVIST-STAYS-UP P7).\n *\n * The append path is the one operation only the Archivist can perform, and it\n * was entirely dark: reads had `recordHandlerDuration` and the bus had its own\n * counters, while writes had nothing. Stage-labeled because the useful\n * question is never \"was the append slow\" but WHICH PART — and `materialize`\n * in particular does work proportional to a resource's annotation count, so it\n * degrades with history rather than with load.\n */\nexport function recordAppendStage(\n stage: 'persist' | 'materialize' | 'enrich' | 'publish',\n durationMs: number,\n): void {\n appendStageHistogram().record(durationMs, { 'record.stage': stage });\n}\n\nlet _gitCommandHistogram: Histogram | undefined;\nfunction gitCommandHistogram(): Histogram {\n if (!_gitCommandHistogram) {\n _gitCommandHistogram = meter().createHistogram('semiont.git.duration', {\n description: 'Wall time of a git subprocess. Async — this is latency, not event-loop blockage. Staging is deduped, so the `add` count is far below the number of appended events.',\n unit: 'ms',\n });\n }\n return _gitCommandHistogram;\n}\n\n/**\n * Record a git invocation. Read the `add` count against events appended: one\n * per event means deferred staging has stopped deduping.\n */\nexport function recordGitCommand(command: string, durationMs: number): void {\n gitCommandHistogram().record(durationMs, { 'git.command': command });\n}\n\nfunction gatherDegradeCounter(): Counter {\n if (!_gatherDegradeCounter) {\n _gatherDegradeCounter = meter().createCounter('semiont.gather.degraded', {\n description: 'Gathers that degraded because an eventually-consistent projection did not catch up within its read barrier (vectors: absent semanticContext; graph: projection-lag failure). Labeled by projection.',\n });\n }\n return _gatherDegradeCounter;\n}\n\n/**\n * Record a gather degraded by a projection read barrier: `'vectors'` — the\n * Smelter settle barrier timed out (semanticContext shipped absent);\n * `'graph'` — the Weaver applied barrier + poll floor exhausted (projection\n * lag surfaced as a distinct failure). Fleet-alertable counterpart of the\n * `[gather DEGRADED]` L4 breadcrumbs — a rising rate on either label means\n * that pipeline is not keeping up.\n */\nexport function recordGatherDegrade(projection: 'graph' | 'vectors'): void {\n gatherDegradeCounter().add(1, { projection });\n}\n\n/** Increment the SSE subscriber gauge — call on `/bus/subscribe` open. */\nexport function recordSubscriberConnect(): void {\n sseSubscribersCounter().add(1);\n}\n\n/** Decrement on disconnect. Pair with `recordSubscriberConnect`. */\nexport function recordSubscriberDisconnect(): void {\n sseSubscribersCounter().add(-1);\n}\n\n/**\n * Register a callback that returns the current job-queue snapshot.\n * Polled at the SDK's metric-collection interval. The single gauge\n * emits one observation per status (`pending`, `running`, …) tagged\n * with the `job.status` attribute. Idempotent — last registered\n * provider wins.\n */\nexport function registerJobQueueProvider(\n provider: () => Promise<JobQueueSnapshot> | JobQueueSnapshot,\n): void {\n _jobQueueProvider = provider;\n if (!_jobQueueGauge) {\n _jobQueueGauge = meter().createObservableGauge('semiont.job.queue.size', {\n description: 'Job queue size by status',\n });\n _jobQueueGauge.addCallback(async (observer) => {\n if (!_jobQueueProvider) return;\n const snap = await _jobQueueProvider();\n observer.observe(snap.pending, { 'job.status': 'pending' });\n observer.observe(snap.running, { 'job.status': 'running' });\n observer.observe(snap.complete, { 'job.status': 'complete' });\n observer.observe(snap.failed, { 'job.status': 'failed' });\n observer.observe(snap.cancelled, { 'job.status': 'cancelled' });\n });\n }\n}\n\n/**\n * Register a callback that returns the current vector-index size\n * (point count). Async to allow remote queries (Qdrant). Polled at\n * the metric-collection interval.\n */\n/**\n * Register the Archivist's fact-pump backlog — facts appended to the record\n * but not yet republished onto the bus.\n *\n * At rest this is zero. A value that climbs and does not come back means the\n * pump is outrunning its transport, which is the leading hypothesis for the\n * load-correlated heap growth in `bugs/absent-archivist-wedges-browse.md`\n * (ARCHIVIST-STAYS-UP P5). The backlog is deliberately unbounded today, so\n * this number is the only thing standing between \"the pump is behind\" and an\n * OOM whose cause is inferred from RSS after the fact.\n */\nexport function registerFactPumpDepthProvider(provider: () => number): void {\n _factPumpDepthProvider = provider;\n if (!_factPumpDepthGauge) {\n _factPumpDepthGauge = meter().createObservableGauge('semiont.archivist.fact_pump.depth', {\n description: 'Facts appended to the record but not yet published to the bus. Zero at rest; a rising floor means the pump is behind its transport.',\n });\n _factPumpDepthGauge.addCallback((observer) => {\n if (_factPumpDepthProvider) observer.observe(_factPumpDepthProvider());\n });\n }\n}\n\nexport function registerVectorIndexSizeProvider(\n provider: () => Promise<number> | number,\n): void {\n _vectorIndexSizeProvider = provider;\n if (!_vectorIndexSizeGauge) {\n _vectorIndexSizeGauge = meter().createObservableGauge('semiont.vector.index.size', {\n description: 'Vector store point count',\n });\n _vectorIndexSizeGauge.addCallback(async (observer) => {\n if (_vectorIndexSizeProvider) {\n const value = await _vectorIndexSizeProvider();\n observer.observe(value);\n }\n });\n }\n}\n\n/**\n * Record an inference call. Token counts are optional — providers that\n * don't expose them (or fail before generating) record only call count\n * and duration.\n */\nexport function recordInferenceUsage(opts: {\n provider: string;\n model: string;\n durationMs: number;\n outcome: 'success' | 'error';\n inputTokens?: number;\n outputTokens?: number;\n}): void {\n const baseAttrs = {\n 'inference.provider': opts.provider,\n 'inference.model': opts.model,\n 'inference.outcome': opts.outcome,\n };\n inferenceCallsCounter().add(1, baseAttrs);\n inferenceDurationHistogram().record(opts.durationMs, baseAttrs);\n if (opts.inputTokens != null && opts.inputTokens > 0) {\n inferenceTokensCounter().add(opts.inputTokens, {\n 'inference.provider': opts.provider,\n 'inference.model': opts.model,\n 'inference.direction': 'input',\n });\n }\n if (opts.outputTokens != null && opts.outputTokens > 0) {\n inferenceTokensCounter().add(opts.outputTokens, {\n 'inference.provider': opts.provider,\n 'inference.model': opts.model,\n 'inference.direction': 'output',\n });\n }\n}\n\nlet _detectionCallCounter: Counter | undefined;\nlet _detectionDurationHistogram: Histogram | undefined;\nlet _detectionItemsHistogram: Histogram | undefined;\nlet _detectionTokensHistogram: Histogram | undefined;\n\nfunction detectionCallCounter(): Counter {\n if (!_detectionCallCounter) {\n _detectionCallCounter = meter().createCounter('semiont.detection.calls', {\n description: 'Detection model calls, labeled by motivation, outcome, subdivision depth and whether this was the floor re-roll.',\n });\n }\n return _detectionCallCounter;\n}\n\nfunction detectionDurationHistogram(): Histogram {\n if (!_detectionDurationHistogram) {\n _detectionDurationHistogram = meter().createHistogram('semiont.detection.call.duration', {\n description: 'Wall time of one detection model call, including the attempts that failed and were retried smaller.',\n unit: 'ms',\n });\n }\n return _detectionDurationHistogram;\n}\n\nfunction detectionItemsHistogram(): Histogram {\n if (!_detectionItemsHistogram) {\n _detectionItemsHistogram = meter().createHistogram('semiont.detection.call.items', {\n description: 'Annotations returned by one detection call. Against the input size on the same record, this is yield.',\n });\n }\n return _detectionItemsHistogram;\n}\n\nfunction detectionTokensHistogram(): Histogram {\n if (!_detectionTokensHistogram) {\n _detectionTokensHistogram = meter().createHistogram('semiont.detection.call.tokens', {\n description: \"Provider-reported tokens for one detection call, by direction. Deliberately separate from semiont.inference.tokens: that series is the authoritative total but carries no subdivision depth, and 'what does a depth-2 call cost' is the question every sizing decision asks.\",\n });\n }\n return _detectionTokensHistogram;\n}\n\n/**\n * Record one detection model call (DETECTION-QUALITY-THROUGHPUT P1).\n *\n * The adapters already record provider/model/duration/tokens for every\n * inference call. What they cannot know is the detection shape around it:\n * which motivation asked, how big the piece was, how many annotations came\n * back, how deep subdivision had descended, and whether this was the floor\n * re-roll. Those are the facts that distinguish a healthy call from a\n * expensive descent, and without them a slow detection run is one\n * undifferentiated number.\n *\n * FAILED attempts are recorded too, and that is the point: the calls paid for\n * and thrown away during a descent are exactly the cost later phases exist to\n * avoid, so a record only of successes would hide the thing being optimized.\n *\n * Tokens are the PROVIDER's counts, passed through — never estimated. Absent\n * means the provider did not report them.\n */\nexport function recordDetectionCall(opts: {\n label: string;\n pieceChars: number;\n durationMs: number;\n items: number;\n depth: number;\n reroll: boolean;\n outcome: 'success' | 'truncated' | 'timeout' | 'collapsed' | 'error';\n inputTokens?: number;\n outputTokens?: number;\n}): void {\n const attrs = {\n 'detection.label': opts.label,\n 'detection.outcome': opts.outcome,\n 'detection.depth': opts.depth,\n 'detection.reroll': opts.reroll,\n };\n detectionCallCounter().add(1, attrs);\n detectionDurationHistogram().record(opts.durationMs, attrs);\n detectionItemsHistogram().record(opts.items, attrs);\n if (opts.inputTokens !== undefined) {\n detectionTokensHistogram().record(opts.inputTokens, { ...attrs, 'detection.direction': 'input' });\n }\n if (opts.outputTokens !== undefined) {\n detectionTokensHistogram().record(opts.outputTokens, { ...attrs, 'detection.direction': 'output' });\n }\n}\n\nlet _anchorOutcomeCounter: Counter | undefined;\nfunction anchorOutcomeCounter(): Counter {\n if (!_anchorOutcomeCounter) {\n _anchorOutcomeCounter = meter().createCounter('semiont.detection.anchors', {\n description: 'Every annotation anchoring, labeled by the method that resolved it. EVERY outcome is counted, not just the risky ones, because a bare count of degraded anchors has no denominator — the rate is the precision signal.',\n });\n }\n return _anchorOutcomeCounter;\n}\n\n/**\n * Record how one annotation got anchored (DETECTION-QUALITY-THROUGHPUT P5).\n *\n * The selector-vs-source check is already a WRITE-TIME INVARIANT — both\n * `buildTextAnnotation` and `buildPdfAnnotation` throw on a selector that does\n * not match its source — so mechanical correctness is guaranteed rather than\n * sampled, and auditing it would measure a constant.\n *\n * What is genuinely uncertain is which anchoring METHOD got there. An `exact`\n * the model quoted verbatim and that appears once is certain; one resolved by\n * `first-of-many` (several occurrences, no usable context) or `fuzzy-match`\n * picked a plausible occurrence and may have picked wrong. Those were visible\n * only as log warnings — countable by a human reading worker output, which is\n * how 47 of them went unreviewed. As a rate they are the precision number that\n * sits beside the yield numbers.\n */\nexport function recordAnchorOutcome(label: string, method: string): void {\n anchorOutcomeCounter().add(1, { 'detection.label': label, 'anchor.method': method });\n}\n\n// ── Re-exports from @opentelemetry/api ─────────────────────────────────\n\nexport { SpanKind, SpanStatusCode, type Attributes, type Span } from '@opentelemetry/api';\n"]}
|
package/dist/node.d.ts
CHANGED
|
@@ -30,6 +30,14 @@ interface NodeObservabilityConfig {
|
|
|
30
30
|
/** Service version. Defaults to `0.0.0` if omitted. */
|
|
31
31
|
serviceVersion?: string;
|
|
32
32
|
}
|
|
33
|
+
/**
|
|
34
|
+
* Which exporter metrics use. `console` wins even with an OTLP endpoint set —
|
|
35
|
+
* the readout for a bare process or CI with no collector; traces unaffected.
|
|
36
|
+
* Any other value (incl. unrecognised) leaves the endpoint to decide, so an
|
|
37
|
+
* unknown value cannot silently disable metrics. Exported and pure so tests
|
|
38
|
+
* assert the choice without mocking the exporter modules.
|
|
39
|
+
*/
|
|
40
|
+
declare function metricsExporterKind(endpoint: string | undefined, requested: string | undefined): 'otlp' | 'console';
|
|
33
41
|
/**
|
|
34
42
|
* Initialize OTel for the current process. Wires up both tracing and
|
|
35
43
|
* metrics. Idempotent — calling twice is a no-op. Returns `true` if the
|
|
@@ -43,5 +51,5 @@ declare function initObservabilityNode(config: NodeObservabilityConfig): boolean
|
|
|
43
51
|
/** Force-flush + shutdown both SDKs. Test cleanup, not production. */
|
|
44
52
|
declare function shutdownObservabilityNode(): Promise<void>;
|
|
45
53
|
|
|
46
|
-
export { initObservabilityNode, shutdownObservabilityNode };
|
|
54
|
+
export { initObservabilityNode, metricsExporterKind, shutdownObservabilityNode };
|
|
47
55
|
export type { NodeObservabilityConfig };
|
package/dist/node.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { monitorEventLoopDelay } from 'perf_hooks';
|
|
2
|
+
import { getHeapStatistics } from 'v8';
|
|
1
3
|
import { context, trace, propagation, metrics } from '@opentelemetry/api';
|
|
2
4
|
import { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks';
|
|
3
5
|
import { W3CTraceContextPropagator } from '@opentelemetry/core';
|
|
@@ -9,9 +11,22 @@ import { ConsoleSpanExporter, BasicTracerProvider, BatchSpanProcessor } from '@o
|
|
|
9
11
|
import { ATTR_SERVICE_VERSION, ATTR_SERVICE_NAME } from '@opentelemetry/semantic-conventions';
|
|
10
12
|
|
|
11
13
|
// src/node.ts
|
|
14
|
+
function heapStats() {
|
|
15
|
+
const mem = process.memoryUsage();
|
|
16
|
+
return {
|
|
17
|
+
heapUsed: mem.heapUsed,
|
|
18
|
+
heapTotal: mem.heapTotal,
|
|
19
|
+
heapLimit: getHeapStatistics().heap_size_limit,
|
|
20
|
+
rss: mem.rss
|
|
21
|
+
};
|
|
22
|
+
}
|
|
12
23
|
var tracerProviderInstance;
|
|
13
24
|
var meterProviderInstance;
|
|
14
25
|
var DEFAULT_METRIC_EXPORT_INTERVAL_MS = 3e4;
|
|
26
|
+
function metricsExporterKind(endpoint, requested) {
|
|
27
|
+
if (requested === "console") return "console";
|
|
28
|
+
return endpoint ? "otlp" : "console";
|
|
29
|
+
}
|
|
15
30
|
function initObservabilityNode(config) {
|
|
16
31
|
if (tracerProviderInstance) return false;
|
|
17
32
|
if (process.env["OTEL_SDK_DISABLED"] === "true") return false;
|
|
@@ -30,7 +45,7 @@ function initObservabilityNode(config) {
|
|
|
30
45
|
context.setGlobalContextManager(new AsyncLocalStorageContextManager().enable());
|
|
31
46
|
trace.setGlobalTracerProvider(tracerProviderInstance);
|
|
32
47
|
propagation.setGlobalPropagator(new W3CTraceContextPropagator());
|
|
33
|
-
const metricExporter = endpoint ? new
|
|
48
|
+
const metricExporter = metricsExporterKind(endpoint, process.env["OTEL_METRICS_EXPORTER"]) === "console" ? new ConsoleMetricExporter() : new OTLPMetricExporter();
|
|
34
49
|
const intervalRaw = process.env["OTEL_METRIC_EXPORT_INTERVAL"];
|
|
35
50
|
const exportIntervalMillis = intervalRaw ? Number.parseInt(intervalRaw, 10) : DEFAULT_METRIC_EXPORT_INTERVAL_MS;
|
|
36
51
|
meterProviderInstance = new MeterProvider({
|
|
@@ -43,6 +58,28 @@ function initObservabilityNode(config) {
|
|
|
43
58
|
]
|
|
44
59
|
});
|
|
45
60
|
metrics.setGlobalMeterProvider(meterProviderInstance);
|
|
61
|
+
const loopDelay = monitorEventLoopDelay({ resolution: 10 });
|
|
62
|
+
loopDelay.enable();
|
|
63
|
+
const runtimeMeter = meterProviderInstance.getMeter("semiont-runtime");
|
|
64
|
+
runtimeMeter.createObservableGauge("semiont.runtime.event_loop.lag", {
|
|
65
|
+
description: "Event-loop delay percentiles over the last export interval. Time the process could not serve anything.",
|
|
66
|
+
unit: "ms"
|
|
67
|
+
}).addCallback((observer) => {
|
|
68
|
+
observer.observe(loopDelay.mean / 1e6, { "lag.stat": "mean" });
|
|
69
|
+
observer.observe(loopDelay.percentile(99) / 1e6, { "lag.stat": "p99" });
|
|
70
|
+
observer.observe(loopDelay.max / 1e6, { "lag.stat": "max" });
|
|
71
|
+
loopDelay.reset();
|
|
72
|
+
});
|
|
73
|
+
runtimeMeter.createObservableGauge("semiont.runtime.heap", {
|
|
74
|
+
description: "Process memory by kind. `limit` is V8's own ceiling, which is what the process dies at \u2014 not the container's allocation.",
|
|
75
|
+
unit: "By"
|
|
76
|
+
}).addCallback((observer) => {
|
|
77
|
+
const s = heapStats();
|
|
78
|
+
observer.observe(s.heapUsed, { "heap.stat": "used" });
|
|
79
|
+
observer.observe(s.heapTotal, { "heap.stat": "total" });
|
|
80
|
+
observer.observe(s.heapLimit, { "heap.stat": "limit" });
|
|
81
|
+
observer.observe(s.rss, { "heap.stat": "rss" });
|
|
82
|
+
});
|
|
46
83
|
const shutdown = () => {
|
|
47
84
|
Promise.all([
|
|
48
85
|
tracerProviderInstance?.shutdown().catch(() => {
|
|
@@ -67,6 +104,6 @@ async function shutdownObservabilityNode() {
|
|
|
67
104
|
meterProviderInstance = void 0;
|
|
68
105
|
}
|
|
69
106
|
|
|
70
|
-
export { initObservabilityNode, shutdownObservabilityNode };
|
|
107
|
+
export { initObservabilityNode, metricsExporterKind, shutdownObservabilityNode };
|
|
71
108
|
//# sourceMappingURL=node.js.map
|
|
72
109
|
//# sourceMappingURL=node.js.map
|
package/dist/node.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/node.ts"],"names":[],"mappings":";;;;;;;;;;;AAuDA,IAAI,sBAAA;AACJ,IAAI,qBAAA;AAMJ,IAAM,iCAAA,GAAoC,GAAA;AAWnC,SAAS,sBAAsB,MAAA,EAA0C;AAC9E,EAAA,IAAI,wBAAwB,OAAO,KAAA;AACnC,EAAA,IAAI,OAAA,CAAQ,GAAA,CAAI,mBAAmB,CAAA,KAAM,QAAQ,OAAO,KAAA;AAExD,EAAA,MAAM,QAAA,GAAW,OAAA,CAAQ,GAAA,CAAI,6BAA6B,CAAA;AAC1D,EAAA,MAAM,UAAA,GAAa,OAAA,CAAQ,GAAA,CAAI,uBAAuB,CAAA,KAAM,MAAA;AAM5D,EAAA,IAAI,CAAC,QAAA,IAAY,CAAC,UAAA,EAAY,OAAO,KAAA;AAErC,EAAA,MAAM,WAAW,sBAAA,CAAuB;AAAA,IACtC,CAAC,iBAAiB,GAAG,QAAQ,GAAA,CAAI,mBAAmB,KAAK,MAAA,CAAO,WAAA;AAAA,IAChE,CAAC,oBAAoB,GAAG,MAAA,CAAO,cAAA,IAAkB;AAAA,GAClD,CAAA;AAGD,EAAA,MAAM,gBAAgB,QAAA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AE,OAAA,CAAQ,MAAM;AACf,MAAA,sBAAA,GAAyB,MAAA;AACzB,MAAA,qBAAA,GAAwB,MAAA;AAAA,IAC1B,CAAC,CAAA;AAAA,EACH,CAAA;AACA,EAAA,OAAA,CAAQ,IAAA,CAAK,WAAW,QAAQ,CAAA;AAChC,EAAA,OAAA,CAAQ,IAAA,CAAK,UAAU,QAAQ,CAAA;AAE/B,EAAA,OAAO,IAAA;AACT;AAGA,eAAsB,yBAAA,GAA2C;AAC/D,EAAA,MAAM,QAAQ,GAAA,CAAI;AAAA,IAChB,wBAAwB,QAAA,EAAS;AAAA,IACjC,uBAAuB,QAAA;AAAS,GACjC,CAAA;AACD,EAAA,sBAAA,GAAyB,MAAA;AACzB,EAAA,qBAAA,GAAwB,MAAA;AAC1B","file":"node.js","sourcesContent":["/**\n * Node SDK initialization. Call once at the process entry point\n * (gateway `index.ts`, `worker-main.ts`, `smelter-main.ts`).\n *\n * Configuration is via standard `OTEL_*` env vars:\n * - `OTEL_SERVICE_NAME` — service identity (e.g. `semiont-gateway`)\n * - `OTEL_EXPORTER_OTLP_ENDPOINT` — collector endpoint (HTTP)\n * - `OTEL_TRACES_SAMPLER` — sampler (default: `parentbased_always_on`)\n * - `OTEL_TRACES_SAMPLER_ARG` — sampler ratio (default: `1.0`)\n * - `OTEL_CONSOLE_EXPORTER=true` — dev-only: emit spans + metrics to stderr\n * - `OTEL_SDK_DISABLED=true` — skip initialization entirely\n *\n * **Off-by-default invariant**: with neither `OTEL_EXPORTER_OTLP_ENDPOINT`\n * nor `OTEL_CONSOLE_EXPORTER=true` set, this function is a no-op and the\n * `@opentelemetry/api` no-op tracer takes over. This avoids accidentally\n * flooding production stderr (and CloudWatch) when an operator deploys\n * without configuring an exporter.\n *\n * Implementation note: this module wires `BasicTracerProvider` and\n * `MeterProvider` (both from the stable `@opentelemetry/sdk-trace-base`\n * 2.x line) directly, plus `AsyncLocalStorageContextManager` for Node\n * async-context propagation. We deliberately avoid `@opentelemetry/sdk-node`\n * because its `0.x` experimental versions cross-depend on older 2.0.x\n * SDKs, forcing npm to nest duplicate copies of the stable packages and\n * blowing up bundles for every consumer.\n */\n\nimport { context, metrics, propagation, trace } from '@opentelemetry/api';\nimport { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks';\nimport { W3CTraceContextPropagator } from '@opentelemetry/core';\nimport { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-http';\nimport { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';\nimport { resourceFromAttributes } from '@opentelemetry/resources';\nimport {\n ConsoleMetricExporter,\n MeterProvider,\n PeriodicExportingMetricReader,\n} from '@opentelemetry/sdk-metrics';\nimport {\n BasicTracerProvider,\n BatchSpanProcessor,\n ConsoleSpanExporter,\n} from '@opentelemetry/sdk-trace-base';\nimport {\n ATTR_SERVICE_NAME,\n ATTR_SERVICE_VERSION,\n} from '@opentelemetry/semantic-conventions';\n\nexport interface NodeObservabilityConfig {\n /** Service identity (e.g. `semiont-gateway`). Overridden by `OTEL_SERVICE_NAME`. */\n serviceName: string;\n /** Service version. Defaults to `0.0.0` if omitted. */\n serviceVersion?: string;\n}\n\nlet tracerProviderInstance: BasicTracerProvider | undefined;\nlet meterProviderInstance: MeterProvider | undefined;\n\n/**\n * Default metric export interval. 30s mirrors the SDK default and gives\n * operators enough granularity without flooding the collector.\n */\nconst DEFAULT_METRIC_EXPORT_INTERVAL_MS = 30_000;\n\n/**\n * Initialize OTel for the current process. Wires up both tracing and\n * metrics. Idempotent — calling twice is a no-op. Returns `true` if the\n * SDK started, `false` if disabled, no exporter is configured, or\n * already initialized.\n *\n * Metrics export at `OTEL_METRIC_EXPORT_INTERVAL` ms (default 30s) to\n * the same `OTEL_EXPORTER_OTLP_ENDPOINT` as traces.\n */\nexport function initObservabilityNode(config: NodeObservabilityConfig): boolean {\n if (tracerProviderInstance) return false;\n if (process.env['OTEL_SDK_DISABLED'] === 'true') return false;\n\n const endpoint = process.env['OTEL_EXPORTER_OTLP_ENDPOINT'];\n const useConsole = process.env['OTEL_CONSOLE_EXPORTER'] === 'true';\n\n // No exporter configured = no SDK init. The `@opentelemetry/api`\n // no-op tracer takes over; `withSpan` still runs `fn` but emits\n // nothing, `getActiveSpan()` returns a sentinel. Avoids flooding\n // production stderr when no collector endpoint is set.\n if (!endpoint && !useConsole) return false;\n\n const resource = resourceFromAttributes({\n [ATTR_SERVICE_NAME]: process.env['OTEL_SERVICE_NAME'] ?? config.serviceName,\n [ATTR_SERVICE_VERSION]: config.serviceVersion ?? '0.0.0',\n });\n\n // Trace SDK\n const traceExporter = endpoint ? new OTLPTraceExporter() : new ConsoleSpanExporter();\n tracerProviderInstance = new BasicTracerProvider({\n resource,\n spanProcessors: [new BatchSpanProcessor(traceExporter)],\n });\n\n // Async-context propagation across `await` boundaries — equivalent\n // to what NodeSDK installs internally, but pinned to the stable 2.x\n // cohort.\n context.setGlobalContextManager(new AsyncLocalStorageContextManager().enable());\n trace.setGlobalTracerProvider(tracerProviderInstance);\n\n // W3C trace-context propagator. Without this, `propagation.inject`\n // and `propagation.extract` walk an empty propagator chain and\n // silently do nothing — which means traceparent never makes it onto\n // outgoing HTTP headers or SSE `_trace` payloads, and cross-service\n // traces stay disconnected. NodeSDK registers this for you; bare\n // BasicTracerProvider does not.\n propagation.setGlobalPropagator(new W3CTraceContextPropagator());\n\n // Metric SDK — same exporter selection as traces.\n const metricExporter = endpoint ? new OTLPMetricExporter() : new ConsoleMetricExporter();\n const intervalRaw = process.env['OTEL_METRIC_EXPORT_INTERVAL'];\n const exportIntervalMillis = intervalRaw\n ? Number.parseInt(intervalRaw, 10)\n : DEFAULT_METRIC_EXPORT_INTERVAL_MS;\n\n meterProviderInstance = new MeterProvider({\n resource,\n readers: [\n new PeriodicExportingMetricReader({\n exporter: metricExporter,\n exportIntervalMillis: Number.isFinite(exportIntervalMillis) && exportIntervalMillis > 0\n ? exportIntervalMillis\n : DEFAULT_METRIC_EXPORT_INTERVAL_MS,\n }),\n ],\n });\n metrics.setGlobalMeterProvider(meterProviderInstance);\n\n // Flush traces + metrics on shutdown so nothing is lost on SIGTERM/SIGINT.\n const shutdown = () => {\n Promise.all([\n tracerProviderInstance?.shutdown().catch(() => {}),\n meterProviderInstance?.shutdown().catch(() => {}),\n ]).finally(() => {\n tracerProviderInstance = undefined;\n meterProviderInstance = undefined;\n });\n };\n process.once('SIGTERM', shutdown);\n process.once('SIGINT', shutdown);\n\n return true;\n}\n\n/** Force-flush + shutdown both SDKs. Test cleanup, not production. */\nexport async function shutdownObservabilityNode(): Promise<void> {\n await Promise.all([\n tracerProviderInstance?.shutdown(),\n meterProviderInstance?.shutdown(),\n ]);\n tracerProviderInstance = undefined;\n meterProviderInstance = undefined;\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/runtime-stats.ts","../src/node.ts"],"names":[],"mappings":";;;;;;;;;;;;;AA8BO,SAAS,SAAA,GAAuB;AACrC,EAAA,MAAM,GAAA,GAAM,QAAQ,WAAA,EAAY;AAChC,EAAA,OAAO;AAAA,IACL,UAAU,GAAA,CAAI,QAAA;AAAA,IACd,WAAW,GAAA,CAAI,SAAA;AAAA,IACf,SAAA,EAAW,mBAAkB,CAAE,eAAA;AAAA,IAC/B,KAAK,GAAA,CAAI;AAAA,GACX;AACF;ACmBA,IAAI,sBAAA;AACJ,IAAI,qBAAA;AAMJ,IAAM,iCAAA,GAAoC,GAAA;AASnC,SAAS,mBAAA,CACd,UACA,SAAA,EACoB;AACpB,EAAA,IAAI,SAAA,KAAc,WAAW,OAAO,SAAA;AACpC,EAAA,OAAO,WAAW,MAAA,GAAS,SAAA;AAC7B;AAWO,SAAS,sBAAsB,MAAA,EAA0C;AAC9E,EAAA,IAAI,wBAAwB,OAAO,KAAA;AACnC,EAAA,IAAI,OAAA,CAAQ,GAAA,CAAI,mBAAmB,CAAA,KAAM,QAAQ,OAAO,KAAA;AAExD,EAAA,MAAM,QAAA,GAAW,OAAA,CAAQ,GAAA,CAAI,6BAA6B,CAAA;AAC1D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EAAE,WAAA,EAAa,SAAS,CAAA;AACtD,IAAA,QAAA,CAAS,QAAQ,CAAA,CAAE,SAAA,EAAW,EAAE,WAAA,EAAa,SAAS,CAAA;AACtD,IAAA,QAAA,CAAS,QAAQ,CAAA,CAAE,GAAA,EAAK,EAAE,WAAA,EAAa,OAAO,CAAA;AAAA,EAChD,CAAC,CAAA;AAGH,EAAA,MAAM,WAAW,MAAM;AACrB,IAAA,OAAA,CAAQ,GAAA,CAAI;AAAA,MACV,sBAAA,EAAwB,QAAA,EAAS,CAAE,KAAA,CAAM,MAAM;AAAA,MAAC,CAAC,CAAA;AAAA,MACjD,qBAAA,EAAuB,QAAA,EAAS,CAAE,KAAA,CAAM,MAAM;AAAA,MAAC,CAAC;AAAA,KACjD,CAAA,CAAE,OAAA,CAAQ,MAAM;AACf,MAAA,sBAAA,GAAyB,MAAA;AACzB,MAAA,qBAAA,GAAwB,MAAA;AAAA,IAC1B,CAAC,CAAA;AAAA,EACH,CAAA;AACA,EAAA,OAAA,CAAQ,IAAA,CAAK,WAAW,QAAQ,CAAA;AAChC,EAAA,OAAA,CAAQ,IAAA,CAAK,UAAU,QAAQ,CAAA;AAE/B,EAAA,OAAO,IAAA;AACT;AAGA,eAAsB,yBAAA,GAA2C;AAC/D,EAAA,MAAM,QAAQ,GAAA,CAAI;AAAA,IAChB,wBAAwB,QAAA,EAAS;AAAA,IACjC,uBAAuB,QAAA;AAAS,GACjC,CAAA;AACD,EAAA,sBAAA,GAAyB,MAAA;AACzB,EAAA,qBAAA,GAAwB,MAAA;AAC1B","file":"node.js","sourcesContent":["/**\n * Process-runtime readings (ARCHIVIST-STAYS-UP P4).\n *\n * Node-only, and deliberately a plain function rather than a gauge callback:\n * the numbers are the thing worth testing, and a callback registered inside\n * an SDK is awkward to assert against.\n *\n * **Why `heapLimit` is the field that matters.** The Archivist died at\n * ~1016 MB inside a 2048 MB container (`bugs/absent-archivist-wedges-browse.md`)\n * — not because it exhausted the container, but because it hit V8's OWN\n * default old-space ceiling, which is derived from visible memory and lands\n * well under it. `heapUsed` alone cannot express \"how close to death is\n * this\"; only the pair can. It is also how a configured\n * `--max-old-space-size` is verified to have taken effect, rather than\n * assumed from the fact that someone set an env var.\n */\n\nimport { getHeapStatistics } from 'node:v8';\n\nexport interface HeapStats {\n /** Live heap in use. */\n heapUsed: number;\n /** Heap V8 has currently reserved. */\n heapTotal: number;\n /** The ceiling V8 will die at — NOT the container's limit. */\n heapLimit: number;\n /** Resident set: everything, including buffers outside the JS heap. */\n rss: number;\n}\n\nexport function heapStats(): HeapStats {\n const mem = process.memoryUsage();\n return {\n heapUsed: mem.heapUsed,\n heapTotal: mem.heapTotal,\n heapLimit: getHeapStatistics().heap_size_limit,\n rss: mem.rss,\n };\n}\n","/**\n * Node SDK initialization. Call once at the process entry point\n * (gateway `index.ts`, `worker-main.ts`, `smelter-main.ts`).\n *\n * Configuration is via standard `OTEL_*` env vars:\n * - `OTEL_SERVICE_NAME` — service identity (e.g. `semiont-gateway`)\n * - `OTEL_EXPORTER_OTLP_ENDPOINT` — collector endpoint (HTTP)\n * - `OTEL_TRACES_SAMPLER` — sampler (default: `parentbased_always_on`)\n * - `OTEL_TRACES_SAMPLER_ARG` — sampler ratio (default: `1.0`)\n * - `OTEL_CONSOLE_EXPORTER=true` — dev-only: emit spans + metrics to stderr\n * - `OTEL_SDK_DISABLED=true` — skip initialization entirely\n *\n * **Off-by-default invariant**: with neither `OTEL_EXPORTER_OTLP_ENDPOINT`\n * nor `OTEL_CONSOLE_EXPORTER=true` set, this function is a no-op and the\n * `@opentelemetry/api` no-op tracer takes over. This avoids accidentally\n * flooding production stderr (and CloudWatch) when an operator deploys\n * without configuring an exporter.\n *\n * Implementation note: this module wires `BasicTracerProvider` and\n * `MeterProvider` (both from the stable `@opentelemetry/sdk-trace-base`\n * 2.x line) directly, plus `AsyncLocalStorageContextManager` for Node\n * async-context propagation. We deliberately avoid `@opentelemetry/sdk-node`\n * because its `0.x` experimental versions cross-depend on older 2.0.x\n * SDKs, forcing npm to nest duplicate copies of the stable packages and\n * blowing up bundles for every consumer.\n */\n\nimport { monitorEventLoopDelay } from 'node:perf_hooks';\nimport { heapStats } from './runtime-stats';\nimport { context, metrics, propagation, trace } from '@opentelemetry/api';\nimport { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks';\nimport { W3CTraceContextPropagator } from '@opentelemetry/core';\nimport { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-http';\nimport { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';\nimport { resourceFromAttributes } from '@opentelemetry/resources';\nimport {\n ConsoleMetricExporter,\n MeterProvider,\n PeriodicExportingMetricReader,\n} from '@opentelemetry/sdk-metrics';\nimport {\n BasicTracerProvider,\n BatchSpanProcessor,\n ConsoleSpanExporter,\n} from '@opentelemetry/sdk-trace-base';\nimport {\n ATTR_SERVICE_NAME,\n ATTR_SERVICE_VERSION,\n} from '@opentelemetry/semantic-conventions';\n\nexport interface NodeObservabilityConfig {\n /** Service identity (e.g. `semiont-gateway`). Overridden by `OTEL_SERVICE_NAME`. */\n serviceName: string;\n /** Service version. Defaults to `0.0.0` if omitted. */\n serviceVersion?: string;\n}\n\nlet tracerProviderInstance: BasicTracerProvider | undefined;\nlet meterProviderInstance: MeterProvider | undefined;\n\n/**\n * Default metric export interval. 30s mirrors the SDK default and gives\n * operators enough granularity without flooding the collector.\n */\nconst DEFAULT_METRIC_EXPORT_INTERVAL_MS = 30_000;\n\n/**\n * Which exporter metrics use. `console` wins even with an OTLP endpoint set —\n * the readout for a bare process or CI with no collector; traces unaffected.\n * Any other value (incl. unrecognised) leaves the endpoint to decide, so an\n * unknown value cannot silently disable metrics. Exported and pure so tests\n * assert the choice without mocking the exporter modules.\n */\nexport function metricsExporterKind(\n endpoint: string | undefined,\n requested: string | undefined,\n): 'otlp' | 'console' {\n if (requested === 'console') return 'console';\n return endpoint ? 'otlp' : 'console';\n}\n\n/**\n * Initialize OTel for the current process. Wires up both tracing and\n * metrics. Idempotent — calling twice is a no-op. Returns `true` if the\n * SDK started, `false` if disabled, no exporter is configured, or\n * already initialized.\n *\n * Metrics export at `OTEL_METRIC_EXPORT_INTERVAL` ms (default 30s) to\n * the same `OTEL_EXPORTER_OTLP_ENDPOINT` as traces.\n */\nexport function initObservabilityNode(config: NodeObservabilityConfig): boolean {\n if (tracerProviderInstance) return false;\n if (process.env['OTEL_SDK_DISABLED'] === 'true') return false;\n\n const endpoint = process.env['OTEL_EXPORTER_OTLP_ENDPOINT'];\n const useConsole = process.env['OTEL_CONSOLE_EXPORTER'] === 'true';\n\n // No exporter configured = no SDK init. The `@opentelemetry/api`\n // no-op tracer takes over; `withSpan` still runs `fn` but emits\n // nothing, `getActiveSpan()` returns a sentinel. Avoids flooding\n // production stderr when no collector endpoint is set.\n if (!endpoint && !useConsole) return false;\n\n const resource = resourceFromAttributes({\n [ATTR_SERVICE_NAME]: process.env['OTEL_SERVICE_NAME'] ?? config.serviceName,\n [ATTR_SERVICE_VERSION]: config.serviceVersion ?? '0.0.0',\n });\n\n // Trace SDK\n const traceExporter = endpoint ? new OTLPTraceExporter() : new ConsoleSpanExporter();\n tracerProviderInstance = new BasicTracerProvider({\n resource,\n spanProcessors: [new BatchSpanProcessor(traceExporter)],\n });\n\n // Async-context propagation across `await` boundaries — equivalent\n // to what NodeSDK installs internally, but pinned to the stable 2.x\n // cohort.\n context.setGlobalContextManager(new AsyncLocalStorageContextManager().enable());\n trace.setGlobalTracerProvider(tracerProviderInstance);\n\n // W3C trace-context propagator. Without this, `propagation.inject`\n // and `propagation.extract` walk an empty propagator chain and\n // silently do nothing — which means traceparent never makes it onto\n // outgoing HTTP headers or SSE `_trace` payloads, and cross-service\n // traces stay disconnected. NodeSDK registers this for you; bare\n // BasicTracerProvider does not.\n propagation.setGlobalPropagator(new W3CTraceContextPropagator());\n\n // Traces follow the endpoint; metrics additionally honour OTEL_METRICS_EXPORTER.\n const metricExporter =\n metricsExporterKind(endpoint, process.env['OTEL_METRICS_EXPORTER']) === 'console'\n ? new ConsoleMetricExporter()\n : new OTLPMetricExporter();\n const intervalRaw = process.env['OTEL_METRIC_EXPORT_INTERVAL'];\n const exportIntervalMillis = intervalRaw\n ? Number.parseInt(intervalRaw, 10)\n : DEFAULT_METRIC_EXPORT_INTERVAL_MS;\n\n meterProviderInstance = new MeterProvider({\n resource,\n readers: [\n new PeriodicExportingMetricReader({\n exporter: metricExporter,\n exportIntervalMillis: Number.isFinite(exportIntervalMillis) && exportIntervalMillis > 0\n ? exportIntervalMillis\n : DEFAULT_METRIC_EXPORT_INTERVAL_MS,\n }),\n ],\n });\n metrics.setGlobalMeterProvider(meterProviderInstance);\n\n // The cause-AGNOSTIC detector for a blocked process: it rises whether the\n // cause is a large JSON parse, GC, or a sync subprocess. Cheap — libuv keeps\n // the histogram; we read percentiles and reset once per export interval.\n const loopDelay = monitorEventLoopDelay({ resolution: 10 });\n loopDelay.enable();\n const runtimeMeter = meterProviderInstance.getMeter('semiont-runtime');\n runtimeMeter\n .createObservableGauge('semiont.runtime.event_loop.lag', {\n description: 'Event-loop delay percentiles over the last export interval. Time the process could not serve anything.',\n unit: 'ms',\n })\n .addCallback((observer) => {\n observer.observe(loopDelay.mean / 1e6, { 'lag.stat': 'mean' });\n observer.observe(loopDelay.percentile(99) / 1e6, { 'lag.stat': 'p99' });\n observer.observe(loopDelay.max / 1e6, { 'lag.stat': 'max' });\n loopDelay.reset();\n });\n\n // Heap (ARCHIVIST-STAYS-UP P4), on the SAME registration as lag rather than\n // a second mechanism — they are read together when diagnosing a process\n // that stopped answering, and splitting them would mean two things to wire.\n //\n // `limit` is the field that repays the effort: the Archivist died at\n // ~1016 MB inside a 2048 MB container because V8's own default ceiling sits\n // well under the container's. `used` alone cannot say how close to death a\n // process is, and `limit` is what a configured --max-old-space-size changes\n // — so this is also how that setting is VERIFIED rather than assumed.\n runtimeMeter\n .createObservableGauge('semiont.runtime.heap', {\n description: \"Process memory by kind. `limit` is V8's own ceiling, which is what the process dies at — not the container's allocation.\",\n unit: 'By',\n })\n .addCallback((observer) => {\n const s = heapStats();\n observer.observe(s.heapUsed, { 'heap.stat': 'used' });\n observer.observe(s.heapTotal, { 'heap.stat': 'total' });\n observer.observe(s.heapLimit, { 'heap.stat': 'limit' });\n observer.observe(s.rss, { 'heap.stat': 'rss' });\n });\n\n // Flush traces + metrics on shutdown so nothing is lost on SIGTERM/SIGINT.\n const shutdown = () => {\n Promise.all([\n tracerProviderInstance?.shutdown().catch(() => {}),\n meterProviderInstance?.shutdown().catch(() => {}),\n ]).finally(() => {\n tracerProviderInstance = undefined;\n meterProviderInstance = undefined;\n });\n };\n process.once('SIGTERM', shutdown);\n process.once('SIGINT', shutdown);\n\n return true;\n}\n\n/** Force-flush + shutdown both SDKs. Test cleanup, not production. */\nexport async function shutdownObservabilityNode(): Promise<void> {\n await Promise.all([\n tracerProviderInstance?.shutdown(),\n meterProviderInstance?.shutdown(),\n ]);\n tracerProviderInstance = undefined;\n meterProviderInstance = undefined;\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/process-logger.ts"],"names":["trace"],"mappings":";;;;;AAqDA,wBAAA,CAAyB,MAAM;AAC7B,EAAA,MAAM,IAAA,GAAO,MAAM,aAAA,EAAc;AACjC,EAAA,IAAI,CAAC,MAAM,OAAO,MAAA;AAClB,EAAA,MAAM,GAAA,GAAM,KAAK,WAAA,EAAY;AAC7B,EAAA,IAAI,CAAC,kBAAA,CAAmB,GAAG,CAAA,EAAG,OAAO,MAAA;AACrC,EAAA,OAAO,GAAA,CAAI,OAAA;AACb,CAAC,CAAA;AA+JM,SAAS,kBAAA,GAAwE;AACtF,EAAA,MAAM,IAAA,GAAO,MAAM,aAAA,EAAc;AACjC,EAAA,IAAI,CAAC,MAAM,OAAO,MAAA;AAClB,EAAA,MAAM,GAAA,GAAM,KAAK,WAAA,EAAY;AAC7B,EAAA,IAAI,CAAC,kBAAA,CAAmB,GAAG,CAAA,EAAG,OA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G,OAAO,CAAA,CAAA;AAAA,IAChF,CAAC;AAAA,GACH,GACA,QAAQ,MAAA,CAAO,OAAA;AAAA,IACb,OAAA,CAAQ,OAAO,SAAA,EAAU;AAAA,IACzB,QAAQ,MAAA,CAAO,MAAA,CAAO,EAAE,KAAA,EAAO,MAAM,CAAA;AAAA,IACrC,kBAAA;AAAA,IACA,OAAA,CAAQ,OAAO,IAAA;AAAK,GACtB;AAEJ,EAAA,MAAM,MAAA,GAAS,QAAQ,YAAA,CAAa;AAAA,IAClC,KAAA;AAAA,IACA,WAAA,EAAa,EAAE,SAAA,EAAU;AAAA,IACzB,MAAA;AAAA,IACA,YAAY,CAAC,IAAI,OAAA,CAAQ,UAAA,CAAW,SAAS;AAAA,GAC9C,CAAA;AAED,EAAA,OAAO,MAAA;AACT","file":"process-logger.js","sourcesContent":["/**\n * @semiont/observability — public API.\n *\n * Universal surface (works in Node + browser). For SDK *initialization*,\n * import from `@semiont/observability/node` or `/web` at the process entry\n * point. Everything else uses this module.\n *\n * Tier 2 of `.plans/OBSERVABILITY.md`. The public surface:\n *\n * - `withSpan(name, fn, options?)` — wrap an async block in a span;\n * `options` carries `kind` and `attrs`.\n * - `withActorSpan(actor, channel, fn, extraAttrs?)` — consumer-span\n * wrapper for bus-event handlers, with handler-duration recording.\n * - `injectTraceparent(payload)` / `extractTraceparent(payload)` — W3C\n * trace-context propagation across the SSE channel (the bus payload\n * gets a `_trace?: { traceparent }` sibling to `correlationId`).\n * - `withTraceparent(carrier, fn)` — run `fn` with the incoming\n * traceparent as the parent context.\n * - `getActiveTraceparent()` — read the active span's traceparent for\n * manual propagation (e.g. attaching to a fetch header or SSE field).\n * - `getLogTraceContext()` — active `trace_id` / `span_id` for log-line\n * correlation.\n * - Metric recorders (`recordBusEmit`, `recordHandlerDuration`,\n * `recordJobOutcome`, `recordSubscriberConnect` / `Disconnect`,\n * `recordInferenceUsage`) and gauge providers\n * (`registerJobQueueProvider`, `registerVectorIndexSizeProvider`).\n *\n * No-op when no exporter is configured: `@opentelemetry/api`'s default\n * tracer is a no-op, so `withSpan` is essentially free until\n * `initObservability*()` runs.\n */\n\nimport {\n context,\n isSpanContextValid,\n metrics,\n propagation,\n SpanKind,\n SpanStatusCode,\n trace,\n type Attributes,\n type Counter,\n type Histogram,\n type ObservableGauge,\n type Span,\n type UpDownCounter,\n} from '@opentelemetry/api';\nimport { setBusLogTraceIdProvider } from '@semiont/core';\n\n// Wire `busLog`'s trace-id provider once at module load. When an OTel\n// SDK is initialized (and a span is active when `busLog` fires), the\n// emitted line gets a `trace=<8hex>` suffix that correlates the\n// grep-timeline with the trace UI. No-op when no SDK is active.\nsetBusLogTraceIdProvider(() => {\n const span = trace.getActiveSpan();\n if (!span) return undefined;\n const ctx = span.spanContext();\n if (!isSpanContextValid(ctx)) return undefined;\n return ctx.traceId;\n});\n\nconst TRACER_NAME = 'semiont';\n\nconst tracer = () => trace.getTracer(TRACER_NAME);\n\n// ── withSpan ───────────────────────────────────────────────────────────\n\n/**\n * Wrap an async block in a span. The span is started before `fn` runs and\n * ended after it resolves or rejects; exceptions are recorded and the span\n * status is set to ERROR. `kind` defaults to INTERNAL.\n */\nexport async function withSpan<T>(\n name: string,\n fn: (span: Span) => Promise<T> | T,\n options?: { kind?: SpanKind; attrs?: Attributes },\n): Promise<T> {\n const span = tracer().startSpan(name, {\n kind: options?.kind ?? SpanKind.INTERNAL,\n ...(options?.attrs ? { attributes: options.attrs } : {}),\n });\n try {\n return await context.with(trace.setSpan(context.active(), span), () => fn(span));\n } catch (err) {\n span.recordException(err as Error);\n span.setStatus({\n code: SpanStatusCode.ERROR,\n message: err instanceof Error ? err.message : String(err),\n });\n throw err;\n } finally {\n span.end();\n }\n}\n\n// ── Traceparent on bus payloads ────────────────────────────────────────\n\nconst TRACE_FIELD = '_trace';\n\n/**\n * Sibling of `correlationId` on bus payloads. Lives on the SSE event body\n * because SSE has no header trailer; the SDK strips it before delivering\n * the payload to subscribers. Additive — payloads without `_trace` parse\n * unchanged.\n */\nexport interface TraceCarrier {\n /** W3C `traceparent` header value (`00-<traceId>-<spanId>-<flags>`). */\n traceparent: string;\n /** W3C `tracestate` header value (vendor-specific extensions). */\n tracestate?: string;\n}\n\n/**\n * Read the active span's W3C traceparent (and tracestate). Returns\n * `undefined` if no span is active.\n */\nexport function getActiveTraceparent(): TraceCarrier | undefined {\n const carrier: Record<string, string> = {};\n propagation.inject(context.active(), carrier);\n const traceparent = carrier['traceparent'];\n if (!traceparent) return undefined;\n return carrier['tracestate']\n ? { traceparent, tracestate: carrier['tracestate'] }\n : { traceparent };\n}\n\n/**\n * Attach the active span's trace-context to a payload object as\n * `_trace`. No-op when no span is active. Returns the same object\n * reference for chaining.\n */\nexport function injectTraceparent<T extends Record<string, unknown>>(payload: T): T {\n const carrier = getActiveTraceparent();\n if (carrier) {\n (payload as Record<string, unknown>)[TRACE_FIELD] = carrier;\n }\n return payload;\n}\n\n/**\n * Strip and return the `_trace` field from a payload. Mutates `payload`.\n * The field is internal plumbing and should not be visible to subscribers.\n */\nexport function extractTraceparent<T extends Record<string, unknown>>(\n payload: T,\n): TraceCarrier | undefined {\n const carrier = (payload as Record<string, unknown>)[TRACE_FIELD] as\n | TraceCarrier\n | undefined;\n if (carrier !== undefined) {\n delete (payload as Record<string, unknown>)[TRACE_FIELD];\n }\n if (!carrier || typeof carrier.traceparent !== 'string') return undefined;\n return carrier;\n}\n\n/**\n * Run `fn` with the given W3C traceparent set as the parent context.\n * Any spans started inside `fn` will be children of the incoming trace.\n * No-op if `carrier` is undefined.\n */\nexport function withTraceparent<T>(\n carrier: TraceCarrier | undefined,\n fn: () => T,\n): T {\n if (!carrier) return fn();\n const carrierObj: Record<string, string> = { traceparent: carrier.traceparent };\n if (carrier.tracestate) carrierObj['tracestate'] = carrier.tracestate;\n const ctx = propagation.extract(context.active(), carrierObj);\n return context.with(ctx, fn);\n}\n\n// ── Actor handler convenience ──────────────────────────────────────────\n\n/**\n * Wrap a bus-event handler in an `actor.<name>:<channel>` consumer span.\n * Used at every `eventBus.get(channel).subscribe(handler)` site inside\n * an actor (Stower, Gatherer, Matcher, Browser, Smelter), to attribute\n * each in-process subscriber's work to a span without scattering manual\n * `withSpan` calls across handler bodies.\n *\n * The span's parent is the active context at the time the handler\n * fires — which is the `bus.dispatch:<channel>` span on the gateway\n * (Subject.next runs synchronously inside the dispatch span), or the\n * `bus.emit:<channel>` span when an actor emits to itself.\n */\nexport async function withActorSpan<T>(\n actor: string,\n channel: string,\n fn: (span: Span) => Promise<T> | T,\n extraAttrs?: Attributes,\n): Promise<T> {\n const start = performance.now();\n try {\n return await withSpan(`actor.${actor}:${channel}`, fn, {\n kind: SpanKind.CONSUMER,\n attrs: {\n actor,\n 'bus.channel': channel,\n ...(extraAttrs ?? {}),\n },\n });\n } finally {\n recordHandlerDuration(actor, channel, performance.now() - start);\n }\n}\n\n// ── Log correlation ────────────────────────────────────────────────────\n\n/**\n * Read the active span's `trace_id` / `span_id` for log-line correlation.\n * Tier 3 of `.plans/OBSERVABILITY.md`. Each structured log line gets\n * tagged with these so a log query in CloudWatch / Loki / Datadog can\n * jump to the trace in Tempo / Jaeger / X-Ray.\n *\n * Returns `undefined` if no span is active, or if the active span's\n * context is invalid (uninitialized SDK, no-op tracer).\n */\nexport function getLogTraceContext(): { trace_id: string; span_id: string } | undefined {\n const span = trace.getActiveSpan();\n if (!span) return undefined;\n const ctx = span.spanContext();\n if (!isSpanContextValid(ctx)) return undefined;\n return { trace_id: ctx.traceId, span_id: ctx.spanId };\n}\n\n// ── Metrics — Tier 3 ───────────────────────────────────────────────────\n\nconst METER_NAME = 'semiont';\n\nconst meter = () => metrics.getMeter(METER_NAME);\n\nlet _busEmitCounter: Counter | undefined;\nlet _handlerDurationHistogram: Histogram | undefined;\nlet _jobOutcomeCounter: Counter | undefined;\nlet _jobDurationHistogram: Histogram | undefined;\nlet _gatherDegradeCounter: Counter | undefined;\nlet _inferenceCallsCounter: Counter | undefined;\nlet _inferenceTokensCounter: Counter | undefined;\nlet _inferenceDurationHistogram: Histogram | undefined;\nlet _sseSubscribers: UpDownCounter | undefined;\nlet _jobQueueGauge: ObservableGauge | undefined;\nlet _jobQueueProvider: (() => Promise<JobQueueSnapshot> | JobQueueSnapshot) | undefined;\nlet _vectorIndexSizeGauge: ObservableGauge | undefined;\nlet _vectorIndexSizeProvider: (() => Promise<number> | number) | undefined;\n\n/** Snapshot of job-queue contents by status. Match `JobQueue.getStats()`. */\nexport interface JobQueueSnapshot {\n pending: number;\n running: number;\n complete: number;\n failed: number;\n cancelled: number;\n}\n\nfunction busEmitCounter(): Counter {\n if (!_busEmitCounter) {\n _busEmitCounter = meter().createCounter('semiont.bus.emit', {\n description: 'Bus emits by channel and scope',\n });\n }\n return _busEmitCounter;\n}\n\nfunction handlerDurationHistogram(): Histogram {\n if (!_handlerDurationHistogram) {\n _handlerDurationHistogram = meter().createHistogram('semiont.handler.duration', {\n description: 'In-process actor handler duration',\n unit: 'ms',\n });\n }\n return _handlerDurationHistogram;\n}\n\nfunction jobOutcomeCounter(): Counter {\n if (!_jobOutcomeCounter) {\n _jobOutcomeCounter = meter().createCounter('semiont.job.outcome', {\n description: 'Worker job completions by type and outcome',\n });\n }\n return _jobOutcomeCounter;\n}\n\nfunction jobDurationHistogram(): Histogram {\n if (!_jobDurationHistogram) {\n _jobDurationHistogram = meter().createHistogram('semiont.job.duration', {\n description: 'Worker job duration by type',\n unit: 'ms',\n });\n }\n return _jobDurationHistogram;\n}\n\nfunction inferenceCallsCounter(): Counter {\n if (!_inferenceCallsCounter) {\n _inferenceCallsCounter = meter().createCounter('semiont.inference.calls', {\n description: 'Inference API calls by provider, model, and outcome',\n });\n }\n return _inferenceCallsCounter;\n}\n\nfunction inferenceTokensCounter(): Counter {\n if (!_inferenceTokensCounter) {\n _inferenceTokensCounter = meter().createCounter('semiont.inference.tokens', {\n description: 'Inference token usage by provider, model, and direction',\n });\n }\n return _inferenceTokensCounter;\n}\n\nfunction inferenceDurationHistogram(): Histogram {\n if (!_inferenceDurationHistogram) {\n _inferenceDurationHistogram = meter().createHistogram('semiont.inference.duration', {\n description: 'Inference call duration by provider, model, and outcome',\n unit: 'ms',\n });\n }\n return _inferenceDurationHistogram;\n}\n\nfunction sseSubscribersCounter(): UpDownCounter {\n if (!_sseSubscribers) {\n _sseSubscribers = meter().createUpDownCounter('semiont.sse.subscribers', {\n description: 'Active SSE subscribers',\n });\n }\n return _sseSubscribers;\n}\n\n/** Increment the bus-emit counter. Called at every transport `emit` site. */\nexport function recordBusEmit(channel: string, scope?: string): void {\n busEmitCounter().add(1, {\n 'bus.channel': channel,\n ...(scope ? { 'bus.scope': scope } : {}),\n });\n}\n\n/** Record an in-process actor handler's duration. */\nexport function recordHandlerDuration(actor: string, channel: string, durationMs: number): void {\n handlerDurationHistogram().record(durationMs, {\n actor,\n 'bus.channel': channel,\n });\n}\n\n/** Record a worker job's outcome and duration. */\nexport function recordJobOutcome(jobType: string, outcome: 'completed' | 'failed', durationMs: number): void {\n jobOutcomeCounter().add(1, { 'job.type': jobType, 'job.outcome': outcome });\n jobDurationHistogram().record(durationMs, { 'job.type': jobType, 'job.outcome': outcome });\n}\n\nfunction gatherDegradeCounter(): Counter {\n if (!_gatherDegradeCounter) {\n _gatherDegradeCounter = meter().createCounter('semiont.gather.degraded', {\n description: 'Gathers that degraded because an eventually-consistent projection did not catch up within its read barrier (vectors: absent semanticContext; graph: projection-lag failure). Labeled by projection.',\n });\n }\n return _gatherDegradeCounter;\n}\n\n/**\n * Record a gather degraded by a projection read barrier: `'vectors'` — the\n * Smelter settle barrier timed out (semanticContext shipped absent);\n * `'graph'` — the Weaver applied barrier + poll floor exhausted (projection\n * lag surfaced as a distinct failure). Fleet-alertable counterpart of the\n * `[gather DEGRADED]` L4 breadcrumbs — a rising rate on either label means\n * that pipeline is not keeping up.\n */\nexport function recordGatherDegrade(projection: 'graph' | 'vectors'): void {\n gatherDegradeCounter().add(1, { projection });\n}\n\n/** Increment the SSE subscriber gauge — call on `/bus/subscribe` open. */\nexport function recordSubscriberConnect(): void {\n sseSubscribersCounter().add(1);\n}\n\n/** Decrement on disconnect. Pair with `recordSubscriberConnect`. */\nexport function recordSubscriberDisconnect(): void {\n sseSubscribersCounter().add(-1);\n}\n\n/**\n * Register a callback that returns the current job-queue snapshot.\n * Polled at the SDK's metric-collection interval. The single gauge\n * emits one observation per status (`pending`, `running`, …) tagged\n * with the `job.status` attribute. Idempotent — last registered\n * provider wins.\n */\nexport function registerJobQueueProvider(\n provider: () => Promise<JobQueueSnapshot> | JobQueueSnapshot,\n): void {\n _jobQueueProvider = provider;\n if (!_jobQueueGauge) {\n _jobQueueGauge = meter().createObservableGauge('semiont.job.queue.size', {\n description: 'Job queue size by status',\n });\n _jobQueueGauge.addCallback(async (observer) => {\n if (!_jobQueueProvider) return;\n const snap = await _jobQueueProvider();\n observer.observe(snap.pending, { 'job.status': 'pending' });\n observer.observe(snap.running, { 'job.status': 'running' });\n observer.observe(snap.complete, { 'job.status': 'complete' });\n observer.observe(snap.failed, { 'job.status': 'failed' });\n observer.observe(snap.cancelled, { 'job.status': 'cancelled' });\n });\n }\n}\n\n/**\n * Register a callback that returns the current vector-index size\n * (point count). Async to allow remote queries (Qdrant). Polled at\n * the metric-collection interval.\n */\nexport function registerVectorIndexSizeProvider(\n provider: () => Promise<number> | number,\n): void {\n _vectorIndexSizeProvider = provider;\n if (!_vectorIndexSizeGauge) {\n _vectorIndexSizeGauge = meter().createObservableGauge('semiont.vector.index.size', {\n description: 'Vector store point count',\n });\n _vectorIndexSizeGauge.addCallback(async (observer) => {\n if (_vectorIndexSizeProvider) {\n const value = await _vectorIndexSizeProvider();\n observer.observe(value);\n }\n });\n }\n}\n\n/**\n * Record an inference call. Token counts are optional — providers that\n * don't expose them (or fail before generating) record only call count\n * and duration.\n */\nexport function recordInferenceUsage(opts: {\n provider: string;\n model: string;\n durationMs: number;\n outcome: 'success' | 'error';\n inputTokens?: number;\n outputTokens?: number;\n}): void {\n const baseAttrs = {\n 'inference.provider': opts.provider,\n 'inference.model': opts.model,\n 'inference.outcome': opts.outcome,\n };\n inferenceCallsCounter().add(1, baseAttrs);\n inferenceDurationHistogram().record(opts.durationMs, baseAttrs);\n if (opts.inputTokens != null && opts.inputTokens > 0) {\n inferenceTokensCounter().add(opts.inputTokens, {\n 'inference.provider': opts.provider,\n 'inference.model': opts.model,\n 'inference.direction': 'input',\n });\n }\n if (opts.outputTokens != null && opts.outputTokens > 0) {\n inferenceTokensCounter().add(opts.outputTokens, {\n 'inference.provider': opts.provider,\n 'inference.model': opts.model,\n 'inference.direction': 'output',\n });\n }\n}\n\n// ── Re-exports from @opentelemetry/api ─────────────────────────────────\n\nexport { SpanKind, SpanStatusCode, type Attributes, type Span } from '@opentelemetry/api';\n","/**\n * Process-level structured logger for Node entry points.\n *\n * Used by long-lived Node processes (gateway, workers, smelter) that\n * want JSON-structured stdout with active-span trace correlation. The\n * `trace_id` / `span_id` fields are populated from the current OTel\n * span context via `getLogTraceContext` — this is the same Tier 3\n * correlation that lets a grep through stdout line up with the trace\n * UI without manual stitching.\n *\n * Reads `LOG_LEVEL` (default `info`) and `LOG_FORMAT` (`json` default,\n * `simple` for human-friendly dev output).\n *\n * Co-located with `getLogTraceContext` deliberately: this is the\n * only reasonably-shaped consumer of that helper, and putting them in\n * the same package keeps the trace-id wiring in one place.\n */\n\nimport winston from 'winston';\nimport type { Logger } from '@semiont/core';\nimport { getLogTraceContext } from './index.js';\n\nconst traceContextFormat = winston.format((info) => {\n const trace = getLogTraceContext();\n if (trace) {\n info.trace_id = trace.trace_id;\n info.span_id = trace.span_id;\n }\n return info;\n})();\n\nexport function createProcessLogger(component: string): Logger {\n const level = process.env.LOG_LEVEL ?? 'info';\n const format = process.env.LOG_FORMAT === 'simple'\n ? winston.format.combine(\n winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),\n winston.format.errors({ stack: true }),\n traceContextFormat,\n winston.format.printf(({ level: lvl, message, timestamp, ...meta }) => {\n const metaStr = Object.keys(meta).length > 0 ? ` ${JSON.stringify(meta)}` : '';\n return `${timestamp} [${lvl.toUpperCase()}] [${component}] ${message}${metaStr}`;\n }),\n )\n : winston.format.combine(\n winston.format.timestamp(),\n winston.format.errors({ stack: true }),\n traceContextFormat,\n winston.format.json(),\n );\n\n const logger = winston.createLogger({\n level,\n defaultMeta: { component },\n format,\n transports: [new winston.transports.Console()],\n });\n\n return logger;\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/process-logger.ts"],"names":["trace"],"mappings":";;;;;AAqDA,wBAAA,CAAyB,MAAM;AAC7B,EAAA,MAAM,IAAA,GAAO,MAAM,aAAA,EAAc;AACjC,EAAA,IAAI,CAAC,MAAM,OAAO,MAAA;AAClB,EAAA,MAAM,GAAA,GAAM,KAAK,WAAA,EAAY;AAC7B,EAAA,IAAI,CAAC,kBAAA,CAAmB,GAAG,CAAA,EAAG,OAAO,MAAA;AACrC,EAAA,OAAO,GAAA,CAAI,OAAA;AACb,CAAC,CAAA;AA+JM,SAAS,kBAAA,GAAwE;AACtF,EAAA,MAAM,IAAA,GAAO,MAAM,aAAA,EAAc;AACjC,EAAA,IAAI,CAAC,MAAM,OAAO,MAAA;AAClB,EAAA,MAAM,GAAA,GAAM,KAAK,WAAA,EAAY;AAC7B,EAAA,IAAI,CAAC,kBAAA,CAAmB,GAAG,CAAA,EAAG,OAAO,MAAA;AACrC,EAAA,OAAO,EAAE,QAAA,EAAU,GAAA,CAAI,OAAA,EAAS,OAAA,EAAS,IAAI,MAAA,EAAO;AACtD;;;AC1MA,IAAM,kBAAA,GAAqB,OAAA,CAAQ,MAAA,CAAO,CAAC,IAAA,KAAS;AAClD,EAAA,MAAMA,SAAQ,kBAAA,EAAmB;AACjC,EAAA,IAAIA,MAAAA,EAAO;AACT,IAAA,IAAA,CAAK,WAAWA,MAAAA,CAAM,QAAA;AACtB,IAAA,IAAA,CAAK,UAAUA,MAAAA,CAAM,OAAA;AAAA,EACvB;AACA,EAAA,OAAO,IAAA;AACT,C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A,IACA,YAAY,CAAC,IAAI,OAAA,CAAQ,UAAA,CAAW,SAAS;AAAA,GAC9C,CAAA;AAED,EAAA,OAAO,MAAA;AACT","file":"process-logger.js","sourcesContent":["/**\n * @semiont/observability — public API.\n *\n * Universal surface (works in Node + browser). For SDK *initialization*,\n * import from `@semiont/observability/node` or `/web` at the process entry\n * point. Everything else uses this module.\n *\n * Tier 2 of `.plans/OBSERVABILITY.md`. The public surface:\n *\n * - `withSpan(name, fn, options?)` — wrap an async block in a span;\n * `options` carries `kind` and `attrs`.\n * - `withActorSpan(actor, channel, fn, extraAttrs?)` — consumer-span\n * wrapper for bus-event handlers, with handler-duration recording.\n * - `injectTraceparent(payload)` / `extractTraceparent(payload)` — W3C\n * trace-context propagation across the SSE channel (the bus payload\n * gets a `_trace?: { traceparent }` sibling to `correlationId`).\n * - `withTraceparent(carrier, fn)` — run `fn` with the incoming\n * traceparent as the parent context.\n * - `getActiveTraceparent()` — read the active span's traceparent for\n * manual propagation (e.g. attaching to a fetch header or SSE field).\n * - `getLogTraceContext()` — active `trace_id` / `span_id` for log-line\n * correlation.\n * - Metric recorders (`recordBusEmit`, `recordHandlerDuration`,\n * `recordJobOutcome`, `recordSubscriberConnect` / `Disconnect`,\n * `recordInferenceUsage`) and gauge providers\n * (`registerJobQueueProvider`, `registerVectorIndexSizeProvider`).\n *\n * No-op when no exporter is configured: `@opentelemetry/api`'s default\n * tracer is a no-op, so `withSpan` is essentially free until\n * `initObservability*()` runs.\n */\n\nimport {\n context,\n isSpanContextValid,\n metrics,\n propagation,\n SpanKind,\n SpanStatusCode,\n trace,\n type Attributes,\n type Counter,\n type Histogram,\n type ObservableGauge,\n type Span,\n type UpDownCounter,\n} from '@opentelemetry/api';\nimport { setBusLogTraceIdProvider } from '@semiont/core';\n\n// Wire `busLog`'s trace-id provider once at module load. When an OTel\n// SDK is initialized (and a span is active when `busLog` fires), the\n// emitted line gets a `trace=<8hex>` suffix that correlates the\n// grep-timeline with the trace UI. No-op when no SDK is active.\nsetBusLogTraceIdProvider(() => {\n const span = trace.getActiveSpan();\n if (!span) return undefined;\n const ctx = span.spanContext();\n if (!isSpanContextValid(ctx)) return undefined;\n return ctx.traceId;\n});\n\nconst TRACER_NAME = 'semiont';\n\nconst tracer = () => trace.getTracer(TRACER_NAME);\n\n// ── withSpan ───────────────────────────────────────────────────────────\n\n/**\n * Wrap an async block in a span. The span is started before `fn` runs and\n * ended after it resolves or rejects; exceptions are recorded and the span\n * status is set to ERROR. `kind` defaults to INTERNAL.\n */\nexport async function withSpan<T>(\n name: string,\n fn: (span: Span) => Promise<T> | T,\n options?: { kind?: SpanKind; attrs?: Attributes },\n): Promise<T> {\n const span = tracer().startSpan(name, {\n kind: options?.kind ?? SpanKind.INTERNAL,\n ...(options?.attrs ? { attributes: options.attrs } : {}),\n });\n try {\n return await context.with(trace.setSpan(context.active(), span), () => fn(span));\n } catch (err) {\n span.recordException(err as Error);\n span.setStatus({\n code: SpanStatusCode.ERROR,\n message: err instanceof Error ? err.message : String(err),\n });\n throw err;\n } finally {\n span.end();\n }\n}\n\n// ── Traceparent on bus payloads ────────────────────────────────────────\n\nconst TRACE_FIELD = '_trace';\n\n/**\n * Sibling of `correlationId` on bus payloads. Lives on the SSE event body\n * because SSE has no header trailer; the SDK strips it before delivering\n * the payload to subscribers. Additive — payloads without `_trace` parse\n * unchanged.\n */\nexport interface TraceCarrier {\n /** W3C `traceparent` header value (`00-<traceId>-<spanId>-<flags>`). */\n traceparent: string;\n /** W3C `tracestate` header value (vendor-specific extensions). */\n tracestate?: string;\n}\n\n/**\n * Read the active span's W3C traceparent (and tracestate). Returns\n * `undefined` if no span is active.\n */\nexport function getActiveTraceparent(): TraceCarrier | undefined {\n const carrier: Record<string, string> = {};\n propagation.inject(context.active(), carrier);\n const traceparent = carrier['traceparent'];\n if (!traceparent) return undefined;\n return carrier['tracestate']\n ? { traceparent, tracestate: carrier['tracestate'] }\n : { traceparent };\n}\n\n/**\n * Attach the active span's trace-context to a payload object as\n * `_trace`. No-op when no span is active. Returns the same object\n * reference for chaining.\n */\nexport function injectTraceparent<T extends Record<string, unknown>>(payload: T): T {\n const carrier = getActiveTraceparent();\n if (carrier) {\n (payload as Record<string, unknown>)[TRACE_FIELD] = carrier;\n }\n return payload;\n}\n\n/**\n * Strip and return the `_trace` field from a payload. Mutates `payload`.\n * The field is internal plumbing and should not be visible to subscribers.\n */\nexport function extractTraceparent<T extends Record<string, unknown>>(\n payload: T,\n): TraceCarrier | undefined {\n const carrier = (payload as Record<string, unknown>)[TRACE_FIELD] as\n | TraceCarrier\n | undefined;\n if (carrier !== undefined) {\n delete (payload as Record<string, unknown>)[TRACE_FIELD];\n }\n if (!carrier || typeof carrier.traceparent !== 'string') return undefined;\n return carrier;\n}\n\n/**\n * Run `fn` with the given W3C traceparent set as the parent context.\n * Any spans started inside `fn` will be children of the incoming trace.\n * No-op if `carrier` is undefined.\n */\nexport function withTraceparent<T>(\n carrier: TraceCarrier | undefined,\n fn: () => T,\n): T {\n if (!carrier) return fn();\n const carrierObj: Record<string, string> = { traceparent: carrier.traceparent };\n if (carrier.tracestate) carrierObj['tracestate'] = carrier.tracestate;\n const ctx = propagation.extract(context.active(), carrierObj);\n return context.with(ctx, fn);\n}\n\n// ── Actor handler convenience ──────────────────────────────────────────\n\n/**\n * Wrap a bus-event handler in an `actor.<name>:<channel>` consumer span.\n * Used at every `eventBus.get(channel).subscribe(handler)` site inside\n * an actor (Stower, Gatherer, Matcher, Browser, Smelter), to attribute\n * each in-process subscriber's work to a span without scattering manual\n * `withSpan` calls across handler bodies.\n *\n * The span's parent is the active context at the time the handler\n * fires — which is the `bus.dispatch:<channel>` span on the gateway\n * (Subject.next runs synchronously inside the dispatch span), or the\n * `bus.emit:<channel>` span when an actor emits to itself.\n */\nexport async function withActorSpan<T>(\n actor: string,\n channel: string,\n fn: (span: Span) => Promise<T> | T,\n extraAttrs?: Attributes,\n): Promise<T> {\n const start = performance.now();\n try {\n return await withSpan(`actor.${actor}:${channel}`, fn, {\n kind: SpanKind.CONSUMER,\n attrs: {\n actor,\n 'bus.channel': channel,\n ...(extraAttrs ?? {}),\n },\n });\n } finally {\n recordHandlerDuration(actor, channel, performance.now() - start);\n }\n}\n\n// ── Log correlation ────────────────────────────────────────────────────\n\n/**\n * Read the active span's `trace_id` / `span_id` for log-line correlation.\n * Tier 3 of `.plans/OBSERVABILITY.md`. Each structured log line gets\n * tagged with these so a log query in CloudWatch / Loki / Datadog can\n * jump to the trace in Tempo / Jaeger / X-Ray.\n *\n * Returns `undefined` if no span is active, or if the active span's\n * context is invalid (uninitialized SDK, no-op tracer).\n */\nexport function getLogTraceContext(): { trace_id: string; span_id: string } | undefined {\n const span = trace.getActiveSpan();\n if (!span) return undefined;\n const ctx = span.spanContext();\n if (!isSpanContextValid(ctx)) return undefined;\n return { trace_id: ctx.traceId, span_id: ctx.spanId };\n}\n\n// ── Metrics — Tier 3 ───────────────────────────────────────────────────\n\nconst METER_NAME = 'semiont';\n\nconst meter = () => metrics.getMeter(METER_NAME);\n\nlet _busEmitCounter: Counter | undefined;\nlet _replySuppressedCounter: Counter | undefined;\nlet _resumeGapCounter: Counter | undefined;\nlet _unanswerableCounter: Counter | undefined;\nlet _correlationRegistryGauge: ObservableGauge | undefined;\nlet _correlationRegistryProvider: (() => CorrelationRegistrySnapshot) | undefined;\nlet _handlerDurationHistogram: Histogram | undefined;\nlet _jobOutcomeCounter: Counter | undefined;\nlet _jobDurationHistogram: Histogram | undefined;\nlet _gatherDegradeCounter: Counter | undefined;\nlet _inferenceCallsCounter: Counter | undefined;\nlet _inferenceTokensCounter: Counter | undefined;\nlet _inferenceDurationHistogram: Histogram | undefined;\nlet _sseSubscribers: UpDownCounter | undefined;\nlet _jobQueueGauge: ObservableGauge | undefined;\nlet _jobQueueProvider: (() => Promise<JobQueueSnapshot> | JobQueueSnapshot) | undefined;\nlet _vectorIndexSizeGauge: ObservableGauge | undefined;\nlet _factPumpDepthGauge: ObservableGauge | undefined;\nlet _factPumpDepthProvider: (() => number) | undefined;\nlet _vectorIndexSizeProvider: (() => Promise<number> | number) | undefined;\n\n/** Snapshot of job-queue contents by status. Match `JobQueue.getStats()`. */\nexport interface JobQueueSnapshot {\n pending: number;\n running: number;\n complete: number;\n failed: number;\n cancelled: number;\n}\n\nfunction busEmitCounter(): Counter {\n if (!_busEmitCounter) {\n _busEmitCounter = meter().createCounter('semiont.bus.emit', {\n description: 'Bus emits by channel and scope',\n });\n }\n return _busEmitCounter;\n}\n\nfunction handlerDurationHistogram(): Histogram {\n if (!_handlerDurationHistogram) {\n _handlerDurationHistogram = meter().createHistogram('semiont.handler.duration', {\n description: 'In-process actor handler duration',\n unit: 'ms',\n });\n }\n return _handlerDurationHistogram;\n}\n\nfunction jobOutcomeCounter(): Counter {\n if (!_jobOutcomeCounter) {\n _jobOutcomeCounter = meter().createCounter('semiont.job.outcome', {\n description: 'Worker job completions by type and outcome',\n });\n }\n return _jobOutcomeCounter;\n}\n\nfunction jobDurationHistogram(): Histogram {\n if (!_jobDurationHistogram) {\n _jobDurationHistogram = meter().createHistogram('semiont.job.duration', {\n description: 'Worker job duration by type',\n unit: 'ms',\n });\n }\n return _jobDurationHistogram;\n}\n\nfunction inferenceCallsCounter(): Counter {\n if (!_inferenceCallsCounter) {\n _inferenceCallsCounter = meter().createCounter('semiont.inference.calls', {\n description: 'Inference API calls by provider, model, and outcome',\n });\n }\n return _inferenceCallsCounter;\n}\n\nfunction inferenceTokensCounter(): Counter {\n if (!_inferenceTokensCounter) {\n _inferenceTokensCounter = meter().createCounter('semiont.inference.tokens', {\n description: 'Inference token usage by provider, model, and direction',\n });\n }\n return _inferenceTokensCounter;\n}\n\nfunction inferenceDurationHistogram(): Histogram {\n if (!_inferenceDurationHistogram) {\n _inferenceDurationHistogram = meter().createHistogram('semiont.inference.duration', {\n description: 'Inference call duration by provider, model, and outcome',\n unit: 'ms',\n });\n }\n return _inferenceDurationHistogram;\n}\n\nfunction sseSubscribersCounter(): UpDownCounter {\n if (!_sseSubscribers) {\n _sseSubscribers = meter().createUpDownCounter('semiont.sse.subscribers', {\n description: 'Active SSE subscribers',\n });\n }\n return _sseSubscribers;\n}\n\nfunction replySuppressedCounter(): Counter {\n if (!_replySuppressedCounter) {\n _replySuppressedCounter = meter().createCounter('semiont.bus.reply.suppressed', {\n description: 'Correlated replies withheld from a non-owning subscriber',\n });\n }\n return _replySuppressedCounter;\n}\n\n/**\n * A correlated reply was withheld from a subscriber that does not own its\n * correlationId (CORRELATED-REPLY-ROUTING P5).\n *\n * Counts ONLY that case. A frame with no correlationId is a shape violation\n * (warned, not counted), and a cid nobody claimed is the structural in-process\n * case that fires constantly — counting either would drown the signal this\n * metric exists to show: the fan-out amplification the delivery filter removes.\n */\nexport function recordReplySuppressed(channel: string): void {\n replySuppressedCounter().add(1, { 'bus.channel': channel });\n}\n\nfunction resumeGapCounter(): Counter {\n if (!_resumeGapCounter) {\n _resumeGapCounter = meter().createCounter('semiont.bus.resume_gap', {\n description: 'SSE resumes that degraded to a gap because replay was unavailable',\n });\n }\n return _resumeGapCounter;\n}\n\n/**\n * An SSE resume could not be served and the client was told to fall back to\n * cache. This degradation is CORRECT by design and therefore silent — which is\n * exactly why it needs a number. A rising rate means clients are losing\n * history, and nothing else in the stack says so.\n */\nexport function recordResumeGap(reason: string): void {\n resumeGapCounter().add(1, { 'bus.resume_gap.reason': reason });\n}\n\nfunction unanswerableCounter(): Counter {\n if (!_unanswerableCounter) {\n _unanswerableCounter = meter().createCounter('semiont.bus.unanswerable', {\n description: 'Request emits that reached zero subscribers and were failed at the gateway',\n });\n }\n return _unanswerableCounter;\n}\n\n/**\n * A request-shaped emit reached no subscriber, so the gateway synthesized its\n * mapped failure (ARCHIVIST-STAYS-UP P3). By channel, this is the absence rate\n * of the service that answers it — the difference between \"it went down once\"\n * and \"it is flapping.\"\n */\nexport function recordUnanswerableRequest(channel: string): void {\n unanswerableCounter().add(1, { 'bus.channel': channel });\n}\n\n/** Claims held and reply payloads retained by a gateway's correlation registry. */\nexport interface CorrelationRegistrySnapshot {\n claims: number;\n retainedReplies: number;\n}\n\n/**\n * Register a callback returning the gateway's correlation-registry occupancy.\n *\n * COUNTS, not bytes: retention is count-budgeted today (byte-budgeting is a\n * known limit in CORRELATED-REPLY-ROUTING), so `retainedReplies` is a proxy for\n * heap, not a measure of it. It is still the closest observable to the question\n * two OOM investigations keep asking — a browse result can be 1-2 MB, and up to\n * REPLY_RETENTION_MAX of them are held at once.\n */\nexport function registerCorrelationRegistryProvider(\n provider: () => CorrelationRegistrySnapshot,\n): void {\n _correlationRegistryProvider = provider;\n if (!_correlationRegistryGauge) {\n _correlationRegistryGauge = meter().createObservableGauge('semiont.bus.correlation.size', {\n description: 'Correlation registry occupancy: live claims and retained reply payloads',\n });\n _correlationRegistryGauge.addCallback((observer) => {\n if (!_correlationRegistryProvider) return;\n const snap = _correlationRegistryProvider();\n observer.observe(snap.claims, { 'correlation.kind': 'claims' });\n observer.observe(snap.retainedReplies, { 'correlation.kind': 'retained_replies' });\n });\n }\n}\n\n/** Increment the bus-emit counter. Called at every transport `emit` site. */\nexport function recordBusEmit(channel: string, scope?: string): void {\n busEmitCounter().add(1, {\n 'bus.channel': channel,\n ...(scope ? { 'bus.scope': scope } : {}),\n });\n}\n\n/** Record an in-process actor handler's duration. */\nexport function recordHandlerDuration(actor: string, channel: string, durationMs: number): void {\n handlerDurationHistogram().record(durationMs, {\n actor,\n 'bus.channel': channel,\n });\n}\n\n/** Record a worker job's outcome and duration. */\nexport function recordJobOutcome(jobType: string, outcome: 'completed' | 'failed', durationMs: number): void {\n jobOutcomeCounter().add(1, { 'job.type': jobType, 'job.outcome': outcome });\n jobDurationHistogram().record(durationMs, { 'job.type': jobType, 'job.outcome': outcome });\n}\n\nlet _appendStageHistogram: Histogram | undefined;\nfunction appendStageHistogram(): Histogram {\n if (!_appendStageHistogram) {\n _appendStageHistogram = meter().createHistogram('semiont.record.append.duration', {\n description: 'Time spent in one stage of appending an event to the record, labeled by stage: persist (JSONL write + git), materialize (view rebuild), enrich, publish. The Archivist\\'s core write path.',\n unit: 'ms',\n });\n }\n return _appendStageHistogram;\n}\n\n/**\n * Record one stage of `EventStore.appendEvent` (ARCHIVIST-STAYS-UP P7).\n *\n * The append path is the one operation only the Archivist can perform, and it\n * was entirely dark: reads had `recordHandlerDuration` and the bus had its own\n * counters, while writes had nothing. Stage-labeled because the useful\n * question is never \"was the append slow\" but WHICH PART — and `materialize`\n * in particular does work proportional to a resource's annotation count, so it\n * degrades with history rather than with load.\n */\nexport function recordAppendStage(\n stage: 'persist' | 'materialize' | 'enrich' | 'publish',\n durationMs: number,\n): void {\n appendStageHistogram().record(durationMs, { 'record.stage': stage });\n}\n\nlet _gitCommandHistogram: Histogram | undefined;\nfunction gitCommandHistogram(): Histogram {\n if (!_gitCommandHistogram) {\n _gitCommandHistogram = meter().createHistogram('semiont.git.duration', {\n description: 'Wall time of a git subprocess. Async — this is latency, not event-loop blockage. Staging is deduped, so the `add` count is far below the number of appended events.',\n unit: 'ms',\n });\n }\n return _gitCommandHistogram;\n}\n\n/**\n * Record a git invocation. Read the `add` count against events appended: one\n * per event means deferred staging has stopped deduping.\n */\nexport function recordGitCommand(command: string, durationMs: number): void {\n gitCommandHistogram().record(durationMs, { 'git.command': command });\n}\n\nfunction gatherDegradeCounter(): Counter {\n if (!_gatherDegradeCounter) {\n _gatherDegradeCounter = meter().createCounter('semiont.gather.degraded', {\n description: 'Gathers that degraded because an eventually-consistent projection did not catch up within its read barrier (vectors: absent semanticContext; graph: projection-lag failure). Labeled by projection.',\n });\n }\n return _gatherDegradeCounter;\n}\n\n/**\n * Record a gather degraded by a projection read barrier: `'vectors'` — the\n * Smelter settle barrier timed out (semanticContext shipped absent);\n * `'graph'` — the Weaver applied barrier + poll floor exhausted (projection\n * lag surfaced as a distinct failure). Fleet-alertable counterpart of the\n * `[gather DEGRADED]` L4 breadcrumbs — a rising rate on either label means\n * that pipeline is not keeping up.\n */\nexport function recordGatherDegrade(projection: 'graph' | 'vectors'): void {\n gatherDegradeCounter().add(1, { projection });\n}\n\n/** Increment the SSE subscriber gauge — call on `/bus/subscribe` open. */\nexport function recordSubscriberConnect(): void {\n sseSubscribersCounter().add(1);\n}\n\n/** Decrement on disconnect. Pair with `recordSubscriberConnect`. */\nexport function recordSubscriberDisconnect(): void {\n sseSubscribersCounter().add(-1);\n}\n\n/**\n * Register a callback that returns the current job-queue snapshot.\n * Polled at the SDK's metric-collection interval. The single gauge\n * emits one observation per status (`pending`, `running`, …) tagged\n * with the `job.status` attribute. Idempotent — last registered\n * provider wins.\n */\nexport function registerJobQueueProvider(\n provider: () => Promise<JobQueueSnapshot> | JobQueueSnapshot,\n): void {\n _jobQueueProvider = provider;\n if (!_jobQueueGauge) {\n _jobQueueGauge = meter().createObservableGauge('semiont.job.queue.size', {\n description: 'Job queue size by status',\n });\n _jobQueueGauge.addCallback(async (observer) => {\n if (!_jobQueueProvider) return;\n const snap = await _jobQueueProvider();\n observer.observe(snap.pending, { 'job.status': 'pending' });\n observer.observe(snap.running, { 'job.status': 'running' });\n observer.observe(snap.complete, { 'job.status': 'complete' });\n observer.observe(snap.failed, { 'job.status': 'failed' });\n observer.observe(snap.cancelled, { 'job.status': 'cancelled' });\n });\n }\n}\n\n/**\n * Register a callback that returns the current vector-index size\n * (point count). Async to allow remote queries (Qdrant). Polled at\n * the metric-collection interval.\n */\n/**\n * Register the Archivist's fact-pump backlog — facts appended to the record\n * but not yet republished onto the bus.\n *\n * At rest this is zero. A value that climbs and does not come back means the\n * pump is outrunning its transport, which is the leading hypothesis for the\n * load-correlated heap growth in `bugs/absent-archivist-wedges-browse.md`\n * (ARCHIVIST-STAYS-UP P5). The backlog is deliberately unbounded today, so\n * this number is the only thing standing between \"the pump is behind\" and an\n * OOM whose cause is inferred from RSS after the fact.\n */\nexport function registerFactPumpDepthProvider(provider: () => number): void {\n _factPumpDepthProvider = provider;\n if (!_factPumpDepthGauge) {\n _factPumpDepthGauge = meter().createObservableGauge('semiont.archivist.fact_pump.depth', {\n description: 'Facts appended to the record but not yet published to the bus. Zero at rest; a rising floor means the pump is behind its transport.',\n });\n _factPumpDepthGauge.addCallback((observer) => {\n if (_factPumpDepthProvider) observer.observe(_factPumpDepthProvider());\n });\n }\n}\n\nexport function registerVectorIndexSizeProvider(\n provider: () => Promise<number> | number,\n): void {\n _vectorIndexSizeProvider = provider;\n if (!_vectorIndexSizeGauge) {\n _vectorIndexSizeGauge = meter().createObservableGauge('semiont.vector.index.size', {\n description: 'Vector store point count',\n });\n _vectorIndexSizeGauge.addCallback(async (observer) => {\n if (_vectorIndexSizeProvider) {\n const value = await _vectorIndexSizeProvider();\n observer.observe(value);\n }\n });\n }\n}\n\n/**\n * Record an inference call. Token counts are optional — providers that\n * don't expose them (or fail before generating) record only call count\n * and duration.\n */\nexport function recordInferenceUsage(opts: {\n provider: string;\n model: string;\n durationMs: number;\n outcome: 'success' | 'error';\n inputTokens?: number;\n outputTokens?: number;\n}): void {\n const baseAttrs = {\n 'inference.provider': opts.provider,\n 'inference.model': opts.model,\n 'inference.outcome': opts.outcome,\n };\n inferenceCallsCounter().add(1, baseAttrs);\n inferenceDurationHistogram().record(opts.durationMs, baseAttrs);\n if (opts.inputTokens != null && opts.inputTokens > 0) {\n inferenceTokensCounter().add(opts.inputTokens, {\n 'inference.provider': opts.provider,\n 'inference.model': opts.model,\n 'inference.direction': 'input',\n });\n }\n if (opts.outputTokens != null && opts.outputTokens > 0) {\n inferenceTokensCounter().add(opts.outputTokens, {\n 'inference.provider': opts.provider,\n 'inference.model': opts.model,\n 'inference.direction': 'output',\n });\n }\n}\n\nlet _detectionCallCounter: Counter | undefined;\nlet _detectionDurationHistogram: Histogram | undefined;\nlet _detectionItemsHistogram: Histogram | undefined;\nlet _detectionTokensHistogram: Histogram | undefined;\n\nfunction detectionCallCounter(): Counter {\n if (!_detectionCallCounter) {\n _detectionCallCounter = meter().createCounter('semiont.detection.calls', {\n description: 'Detection model calls, labeled by motivation, outcome, subdivision depth and whether this was the floor re-roll.',\n });\n }\n return _detectionCallCounter;\n}\n\nfunction detectionDurationHistogram(): Histogram {\n if (!_detectionDurationHistogram) {\n _detectionDurationHistogram = meter().createHistogram('semiont.detection.call.duration', {\n description: 'Wall time of one detection model call, including the attempts that failed and were retried smaller.',\n unit: 'ms',\n });\n }\n return _detectionDurationHistogram;\n}\n\nfunction detectionItemsHistogram(): Histogram {\n if (!_detectionItemsHistogram) {\n _detectionItemsHistogram = meter().createHistogram('semiont.detection.call.items', {\n description: 'Annotations returned by one detection call. Against the input size on the same record, this is yield.',\n });\n }\n return _detectionItemsHistogram;\n}\n\nfunction detectionTokensHistogram(): Histogram {\n if (!_detectionTokensHistogram) {\n _detectionTokensHistogram = meter().createHistogram('semiont.detection.call.tokens', {\n description: \"Provider-reported tokens for one detection call, by direction. Deliberately separate from semiont.inference.tokens: that series is the authoritative total but carries no subdivision depth, and 'what does a depth-2 call cost' is the question every sizing decision asks.\",\n });\n }\n return _detectionTokensHistogram;\n}\n\n/**\n * Record one detection model call (DETECTION-QUALITY-THROUGHPUT P1).\n *\n * The adapters already record provider/model/duration/tokens for every\n * inference call. What they cannot know is the detection shape around it:\n * which motivation asked, how big the piece was, how many annotations came\n * back, how deep subdivision had descended, and whether this was the floor\n * re-roll. Those are the facts that distinguish a healthy call from a\n * expensive descent, and without them a slow detection run is one\n * undifferentiated number.\n *\n * FAILED attempts are recorded too, and that is the point: the calls paid for\n * and thrown away during a descent are exactly the cost later phases exist to\n * avoid, so a record only of successes would hide the thing being optimized.\n *\n * Tokens are the PROVIDER's counts, passed through — never estimated. Absent\n * means the provider did not report them.\n */\nexport function recordDetectionCall(opts: {\n label: string;\n pieceChars: number;\n durationMs: number;\n items: number;\n depth: number;\n reroll: boolean;\n outcome: 'success' | 'truncated' | 'timeout' | 'collapsed' | 'error';\n inputTokens?: number;\n outputTokens?: number;\n}): void {\n const attrs = {\n 'detection.label': opts.label,\n 'detection.outcome': opts.outcome,\n 'detection.depth': opts.depth,\n 'detection.reroll': opts.reroll,\n };\n detectionCallCounter().add(1, attrs);\n detectionDurationHistogram().record(opts.durationMs, attrs);\n detectionItemsHistogram().record(opts.items, attrs);\n if (opts.inputTokens !== undefined) {\n detectionTokensHistogram().record(opts.inputTokens, { ...attrs, 'detection.direction': 'input' });\n }\n if (opts.outputTokens !== undefined) {\n detectionTokensHistogram().record(opts.outputTokens, { ...attrs, 'detection.direction': 'output' });\n }\n}\n\nlet _anchorOutcomeCounter: Counter | undefined;\nfunction anchorOutcomeCounter(): Counter {\n if (!_anchorOutcomeCounter) {\n _anchorOutcomeCounter = meter().createCounter('semiont.detection.anchors', {\n description: 'Every annotation anchoring, labeled by the method that resolved it. EVERY outcome is counted, not just the risky ones, because a bare count of degraded anchors has no denominator — the rate is the precision signal.',\n });\n }\n return _anchorOutcomeCounter;\n}\n\n/**\n * Record how one annotation got anchored (DETECTION-QUALITY-THROUGHPUT P5).\n *\n * The selector-vs-source check is already a WRITE-TIME INVARIANT — both\n * `buildTextAnnotation` and `buildPdfAnnotation` throw on a selector that does\n * not match its source — so mechanical correctness is guaranteed rather than\n * sampled, and auditing it would measure a constant.\n *\n * What is genuinely uncertain is which anchoring METHOD got there. An `exact`\n * the model quoted verbatim and that appears once is certain; one resolved by\n * `first-of-many` (several occurrences, no usable context) or `fuzzy-match`\n * picked a plausible occurrence and may have picked wrong. Those were visible\n * only as log warnings — countable by a human reading worker output, which is\n * how 47 of them went unreviewed. As a rate they are the precision number that\n * sits beside the yield numbers.\n */\nexport function recordAnchorOutcome(label: string, method: string): void {\n anchorOutcomeCounter().add(1, { 'detection.label': label, 'anchor.method': method });\n}\n\n// ── Re-exports from @opentelemetry/api ─────────────────────────────────\n\nexport { SpanKind, SpanStatusCode, type Attributes, type Span } from '@opentelemetry/api';\n","/**\n * Process-level structured logger for Node entry points.\n *\n * Used by long-lived Node processes (gateway, workers, smelter) that\n * want JSON-structured stdout with active-span trace correlation. The\n * `trace_id` / `span_id` fields are populated from the current OTel\n * span context via `getLogTraceContext` — this is the same Tier 3\n * correlation that lets a grep through stdout line up with the trace\n * UI without manual stitching.\n *\n * Reads `LOG_LEVEL` (default `info`) and `LOG_FORMAT` (`json` default,\n * `simple` for human-friendly dev output).\n *\n * Co-located with `getLogTraceContext` deliberately: this is the\n * only reasonably-shaped consumer of that helper, and putting them in\n * the same package keeps the trace-id wiring in one place.\n */\n\nimport winston from 'winston';\nimport type { Logger } from '@semiont/core';\nimport { getLogTraceContext } from './index.js';\n\nconst traceContextFormat = winston.format((info) => {\n const trace = getLogTraceContext();\n if (trace) {\n info.trace_id = trace.trace_id;\n info.span_id = trace.span_id;\n }\n return info;\n})();\n\nexport function createProcessLogger(component: string): Logger {\n const level = process.env.LOG_LEVEL ?? 'info';\n const format = process.env.LOG_FORMAT === 'simple'\n ? winston.format.combine(\n winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),\n winston.format.errors({ stack: true }),\n traceContextFormat,\n winston.format.printf(({ level: lvl, message, timestamp, ...meta }) => {\n const metaStr = Object.keys(meta).length > 0 ? ` ${JSON.stringify(meta)}` : '';\n return `${timestamp} [${lvl.toUpperCase()}] [${component}] ${message}${metaStr}`;\n }),\n )\n : winston.format.combine(\n winston.format.timestamp(),\n winston.format.errors({ stack: true }),\n traceContextFormat,\n winston.format.json(),\n );\n\n const logger = winston.createLogger({\n level,\n defaultMeta: { component },\n format,\n transports: [new winston.transports.Console()],\n });\n\n return logger;\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@semiont/observability",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.31",
|
|
4
4
|
"description": "OpenTelemetry-based tracing for Semiont — Tier 2 of OBSERVABILITY.md. Process-init helpers (Node + Web), withSpan helper, W3C traceparent inject/extract for bus payloads. No-op when no exporter is configured.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -63,16 +63,16 @@
|
|
|
63
63
|
},
|
|
64
64
|
"dependencies": {
|
|
65
65
|
"@opentelemetry/api": "^1.9.1",
|
|
66
|
-
"@opentelemetry/context-async-hooks": "^2.
|
|
67
|
-
"@opentelemetry/core": "^2.
|
|
68
|
-
"@opentelemetry/exporter-metrics-otlp-http": "^0.
|
|
69
|
-
"@opentelemetry/exporter-trace-otlp-http": "^0.
|
|
70
|
-
"@opentelemetry/resources": "^2.
|
|
66
|
+
"@opentelemetry/context-async-hooks": "^2.11.0",
|
|
67
|
+
"@opentelemetry/core": "^2.11.0",
|
|
68
|
+
"@opentelemetry/exporter-metrics-otlp-http": "^0.222.0",
|
|
69
|
+
"@opentelemetry/exporter-trace-otlp-http": "^0.222.0",
|
|
70
|
+
"@opentelemetry/resources": "^2.11.0",
|
|
71
71
|
"@opentelemetry/sdk-metrics": "^2.10.0",
|
|
72
|
-
"@opentelemetry/sdk-trace-base": "^2.
|
|
73
|
-
"@opentelemetry/sdk-trace-web": "^2.
|
|
72
|
+
"@opentelemetry/sdk-trace-base": "^2.11.0",
|
|
73
|
+
"@opentelemetry/sdk-trace-web": "^2.11.0",
|
|
74
74
|
"@opentelemetry/semantic-conventions": "^1.43.0",
|
|
75
|
-
"@semiont/core": "0.5.
|
|
75
|
+
"@semiont/core": "0.5.31",
|
|
76
76
|
"winston": "^3.17.0"
|
|
77
77
|
}
|
|
78
78
|
}
|