@semiont/observability 0.4.21
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 +143 -0
- package/dist/index.js +245 -0
- package/dist/index.js.map +1 -0
- package/dist/node.d.ts +46 -0
- package/dist/node.js +72 -0
- package/dist/node.js.map +1 -0
- package/dist/web.d.ts +37 -0
- package/dist/web.js +30 -0
- package/dist/web.js.map +1 -0
- package/package.json +70 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { Span, Attributes, SpanKind } from '@opentelemetry/api';
|
|
2
|
+
export { Attributes, Span, SpanKind, SpanStatusCode } from '@opentelemetry/api';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @semiont/observability — public API.
|
|
6
|
+
*
|
|
7
|
+
* Universal surface (works in Node + browser). For SDK *initialization*,
|
|
8
|
+
* import from `@semiont/observability/node` or `/web` at the process entry
|
|
9
|
+
* point. Everything else uses this module.
|
|
10
|
+
*
|
|
11
|
+
* Tier 2 of `.plans/OBSERVABILITY.md`. The public surface is intentionally
|
|
12
|
+
* thin:
|
|
13
|
+
*
|
|
14
|
+
* - `withSpan(name, fn, attrs?)` — wrap an async block in a span.
|
|
15
|
+
* - `injectTraceparent(payload)` / `extractTraceparent(value)` — W3C
|
|
16
|
+
* trace-context propagation across the SSE channel (the bus payload
|
|
17
|
+
* gets a `_trace?: { traceparent }` sibling to `correlationId`).
|
|
18
|
+
* - `setSpanContextFromTraceparent(traceparent, fn)` — set incoming
|
|
19
|
+
* traceparent as the parent context for a synchronous block.
|
|
20
|
+
* - `getActiveTraceparent()` — read the active span's traceparent for
|
|
21
|
+
* manual propagation (e.g. attaching to a fetch header or SSE field).
|
|
22
|
+
*
|
|
23
|
+
* No-op when no exporter is configured: `@opentelemetry/api`'s default
|
|
24
|
+
* tracer is a no-op, so `withSpan` is essentially free until
|
|
25
|
+
* `initObservability*()` runs.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Wrap an async block in a span. The span is started before `fn` runs and
|
|
30
|
+
* ended after it resolves or rejects; exceptions are recorded and the span
|
|
31
|
+
* status is set to ERROR. `kind` defaults to INTERNAL.
|
|
32
|
+
*/
|
|
33
|
+
declare function withSpan<T>(name: string, fn: (span: Span) => Promise<T> | T, options?: {
|
|
34
|
+
kind?: SpanKind;
|
|
35
|
+
attrs?: Attributes;
|
|
36
|
+
}): Promise<T>;
|
|
37
|
+
/**
|
|
38
|
+
* Sibling of `correlationId` on bus payloads. Lives on the SSE event body
|
|
39
|
+
* because SSE has no header trailer; the SDK strips it before delivering
|
|
40
|
+
* the payload to subscribers. Additive — payloads without `_trace` parse
|
|
41
|
+
* unchanged.
|
|
42
|
+
*/
|
|
43
|
+
interface TraceCarrier {
|
|
44
|
+
/** W3C `traceparent` header value (`00-<traceId>-<spanId>-<flags>`). */
|
|
45
|
+
traceparent: string;
|
|
46
|
+
/** W3C `tracestate` header value (vendor-specific extensions). */
|
|
47
|
+
tracestate?: string;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Read the active span's W3C traceparent (and tracestate). Returns
|
|
51
|
+
* `undefined` if no span is active.
|
|
52
|
+
*/
|
|
53
|
+
declare function getActiveTraceparent(): TraceCarrier | undefined;
|
|
54
|
+
/**
|
|
55
|
+
* Attach the active span's trace-context to a payload object as
|
|
56
|
+
* `_trace`. No-op when no span is active. Returns the same object
|
|
57
|
+
* reference for chaining.
|
|
58
|
+
*/
|
|
59
|
+
declare function injectTraceparent<T extends Record<string, unknown>>(payload: T): T;
|
|
60
|
+
/**
|
|
61
|
+
* Strip and return the `_trace` field from a payload. Mutates `payload`.
|
|
62
|
+
* The field is internal plumbing and should not be visible to subscribers.
|
|
63
|
+
*/
|
|
64
|
+
declare function extractTraceparent<T extends Record<string, unknown>>(payload: T): TraceCarrier | undefined;
|
|
65
|
+
/**
|
|
66
|
+
* Run `fn` with the given W3C traceparent set as the parent context.
|
|
67
|
+
* Any spans started inside `fn` will be children of the incoming trace.
|
|
68
|
+
* No-op if `carrier` is undefined.
|
|
69
|
+
*/
|
|
70
|
+
declare function withTraceparent<T>(carrier: TraceCarrier | undefined, fn: () => T): T;
|
|
71
|
+
/**
|
|
72
|
+
* Wrap a bus-event handler in an `actor.<name>:<channel>` consumer span.
|
|
73
|
+
* Used at every `eventBus.get(channel).subscribe(handler)` site inside
|
|
74
|
+
* an actor (Stower, Gatherer, Matcher, Browser, Smelter), to attribute
|
|
75
|
+
* each in-process subscriber's work to a span without scattering manual
|
|
76
|
+
* `withSpan` calls across handler bodies.
|
|
77
|
+
*
|
|
78
|
+
* The span's parent is the active context at the time the handler
|
|
79
|
+
* fires — which is the `bus.dispatch:<channel>` span on the backend
|
|
80
|
+
* (Subject.next runs synchronously inside the dispatch span), or the
|
|
81
|
+
* `bus.emit:<channel>` span when an actor emits to itself.
|
|
82
|
+
*/
|
|
83
|
+
declare function withActorSpan<T>(actor: string, channel: string, fn: (span: Span) => Promise<T> | T, extraAttrs?: Attributes): Promise<T>;
|
|
84
|
+
/**
|
|
85
|
+
* Read the active span's `trace_id` / `span_id` for log-line correlation.
|
|
86
|
+
* Tier 3 of `.plans/OBSERVABILITY.md`. Each structured log line gets
|
|
87
|
+
* tagged with these so a log query in CloudWatch / Loki / Datadog can
|
|
88
|
+
* jump to the trace in Tempo / Jaeger / X-Ray.
|
|
89
|
+
*
|
|
90
|
+
* Returns `undefined` if no span is active, or if the active span's
|
|
91
|
+
* context is invalid (uninitialized SDK, no-op tracer).
|
|
92
|
+
*/
|
|
93
|
+
declare function getLogTraceContext(): {
|
|
94
|
+
trace_id: string;
|
|
95
|
+
span_id: string;
|
|
96
|
+
} | undefined;
|
|
97
|
+
/** Snapshot of job-queue contents by status. Match `JobQueue.getStats()`. */
|
|
98
|
+
interface JobQueueSnapshot {
|
|
99
|
+
pending: number;
|
|
100
|
+
running: number;
|
|
101
|
+
complete: number;
|
|
102
|
+
failed: number;
|
|
103
|
+
cancelled: number;
|
|
104
|
+
}
|
|
105
|
+
/** Increment the bus-emit counter. Called at every transport `emit` site. */
|
|
106
|
+
declare function recordBusEmit(channel: string, scope?: string): void;
|
|
107
|
+
/** Record an in-process actor handler's duration. */
|
|
108
|
+
declare function recordHandlerDuration(actor: string, channel: string, durationMs: number): void;
|
|
109
|
+
/** Record a worker job's outcome and duration. */
|
|
110
|
+
declare function recordJobOutcome(jobType: string, outcome: 'completed' | 'failed', durationMs: number): void;
|
|
111
|
+
/** Increment the SSE subscriber gauge — call on `/bus/subscribe` open. */
|
|
112
|
+
declare function recordSubscriberConnect(): void;
|
|
113
|
+
/** Decrement on disconnect. Pair with `recordSubscriberConnect`. */
|
|
114
|
+
declare function recordSubscriberDisconnect(): void;
|
|
115
|
+
/**
|
|
116
|
+
* Register a callback that returns the current job-queue snapshot.
|
|
117
|
+
* Polled at the SDK's metric-collection interval. The single gauge
|
|
118
|
+
* emits one observation per status (`pending`, `running`, …) tagged
|
|
119
|
+
* with the `job.status` attribute. Idempotent — last registered
|
|
120
|
+
* provider wins.
|
|
121
|
+
*/
|
|
122
|
+
declare function registerJobQueueProvider(provider: () => Promise<JobQueueSnapshot> | JobQueueSnapshot): void;
|
|
123
|
+
/**
|
|
124
|
+
* Register a callback that returns the current vector-index size
|
|
125
|
+
* (point count). Async to allow remote queries (Qdrant). Polled at
|
|
126
|
+
* the metric-collection interval.
|
|
127
|
+
*/
|
|
128
|
+
declare function registerVectorIndexSizeProvider(provider: () => Promise<number> | number): void;
|
|
129
|
+
/**
|
|
130
|
+
* Record an inference call. Token counts are optional — providers that
|
|
131
|
+
* don't expose them (or fail before generating) record only call count
|
|
132
|
+
* and duration.
|
|
133
|
+
*/
|
|
134
|
+
declare function recordInferenceUsage(opts: {
|
|
135
|
+
provider: string;
|
|
136
|
+
model: string;
|
|
137
|
+
durationMs: number;
|
|
138
|
+
outcome: 'success' | 'error';
|
|
139
|
+
inputTokens?: number;
|
|
140
|
+
outputTokens?: number;
|
|
141
|
+
}): void;
|
|
142
|
+
|
|
143
|
+
export { type JobQueueSnapshot, type TraceCarrier, extractTraceparent, getActiveTraceparent, getLogTraceContext, injectTraceparent, recordBusEmit, recordHandlerDuration, recordInferenceUsage, recordJobOutcome, recordSubscriberConnect, recordSubscriberDisconnect, registerJobQueueProvider, registerVectorIndexSizeProvider, withActorSpan, withSpan, withTraceparent };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
import { trace, isSpanContextValid, SpanKind, context, SpanStatusCode, propagation, metrics } from '@opentelemetry/api';
|
|
2
|
+
export { SpanKind, SpanStatusCode } from '@opentelemetry/api';
|
|
3
|
+
import { setBusLogTraceIdProvider } from '@semiont/core';
|
|
4
|
+
|
|
5
|
+
// src/index.ts
|
|
6
|
+
setBusLogTraceIdProvider(() => {
|
|
7
|
+
const span = trace.getActiveSpan();
|
|
8
|
+
if (!span) return void 0;
|
|
9
|
+
const ctx = span.spanContext();
|
|
10
|
+
if (!isSpanContextValid(ctx)) return void 0;
|
|
11
|
+
return ctx.traceId;
|
|
12
|
+
});
|
|
13
|
+
var TRACER_NAME = "semiont";
|
|
14
|
+
var tracer = () => trace.getTracer(TRACER_NAME);
|
|
15
|
+
async function withSpan(name, fn, options) {
|
|
16
|
+
const span = tracer().startSpan(name, {
|
|
17
|
+
kind: options?.kind ?? SpanKind.INTERNAL,
|
|
18
|
+
...options?.attrs ? { attributes: options.attrs } : {}
|
|
19
|
+
});
|
|
20
|
+
try {
|
|
21
|
+
return await context.with(trace.setSpan(context.active(), span), () => fn(span));
|
|
22
|
+
} catch (err) {
|
|
23
|
+
span.recordException(err);
|
|
24
|
+
span.setStatus({
|
|
25
|
+
code: SpanStatusCode.ERROR,
|
|
26
|
+
message: err instanceof Error ? err.message : String(err)
|
|
27
|
+
});
|
|
28
|
+
throw err;
|
|
29
|
+
} finally {
|
|
30
|
+
span.end();
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
var TRACE_FIELD = "_trace";
|
|
34
|
+
function getActiveTraceparent() {
|
|
35
|
+
const carrier = {};
|
|
36
|
+
propagation.inject(context.active(), carrier);
|
|
37
|
+
const traceparent = carrier["traceparent"];
|
|
38
|
+
if (!traceparent) return void 0;
|
|
39
|
+
return carrier["tracestate"] ? { traceparent, tracestate: carrier["tracestate"] } : { traceparent };
|
|
40
|
+
}
|
|
41
|
+
function injectTraceparent(payload) {
|
|
42
|
+
const carrier = getActiveTraceparent();
|
|
43
|
+
if (carrier) {
|
|
44
|
+
payload[TRACE_FIELD] = carrier;
|
|
45
|
+
}
|
|
46
|
+
return payload;
|
|
47
|
+
}
|
|
48
|
+
function extractTraceparent(payload) {
|
|
49
|
+
const carrier = payload[TRACE_FIELD];
|
|
50
|
+
if (carrier !== void 0) {
|
|
51
|
+
delete payload[TRACE_FIELD];
|
|
52
|
+
}
|
|
53
|
+
if (!carrier || typeof carrier.traceparent !== "string") return void 0;
|
|
54
|
+
return carrier;
|
|
55
|
+
}
|
|
56
|
+
function withTraceparent(carrier, fn) {
|
|
57
|
+
if (!carrier) return fn();
|
|
58
|
+
const carrierObj = { traceparent: carrier.traceparent };
|
|
59
|
+
if (carrier.tracestate) carrierObj["tracestate"] = carrier.tracestate;
|
|
60
|
+
const ctx = propagation.extract(context.active(), carrierObj);
|
|
61
|
+
return context.with(ctx, fn);
|
|
62
|
+
}
|
|
63
|
+
async function withActorSpan(actor, channel, fn, extraAttrs) {
|
|
64
|
+
const start = performance.now();
|
|
65
|
+
try {
|
|
66
|
+
return await withSpan(`actor.${actor}:${channel}`, fn, {
|
|
67
|
+
kind: SpanKind.CONSUMER,
|
|
68
|
+
attrs: {
|
|
69
|
+
actor,
|
|
70
|
+
"bus.channel": channel,
|
|
71
|
+
...extraAttrs ?? {}
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
} finally {
|
|
75
|
+
recordHandlerDuration(actor, channel, performance.now() - start);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
function getLogTraceContext() {
|
|
79
|
+
const span = trace.getActiveSpan();
|
|
80
|
+
if (!span) return void 0;
|
|
81
|
+
const ctx = span.spanContext();
|
|
82
|
+
if (!isSpanContextValid(ctx)) return void 0;
|
|
83
|
+
return { trace_id: ctx.traceId, span_id: ctx.spanId };
|
|
84
|
+
}
|
|
85
|
+
var METER_NAME = "semiont";
|
|
86
|
+
var meter = () => metrics.getMeter(METER_NAME);
|
|
87
|
+
var _busEmitCounter;
|
|
88
|
+
var _handlerDurationHistogram;
|
|
89
|
+
var _jobOutcomeCounter;
|
|
90
|
+
var _jobDurationHistogram;
|
|
91
|
+
var _inferenceCallsCounter;
|
|
92
|
+
var _inferenceTokensCounter;
|
|
93
|
+
var _inferenceDurationHistogram;
|
|
94
|
+
var _sseSubscribers;
|
|
95
|
+
var _jobQueueGauge;
|
|
96
|
+
var _jobQueueProvider;
|
|
97
|
+
var _vectorIndexSizeGauge;
|
|
98
|
+
var _vectorIndexSizeProvider;
|
|
99
|
+
function busEmitCounter() {
|
|
100
|
+
if (!_busEmitCounter) {
|
|
101
|
+
_busEmitCounter = meter().createCounter("semiont.bus.emit", {
|
|
102
|
+
description: "Bus emits by channel and scope"
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
return _busEmitCounter;
|
|
106
|
+
}
|
|
107
|
+
function handlerDurationHistogram() {
|
|
108
|
+
if (!_handlerDurationHistogram) {
|
|
109
|
+
_handlerDurationHistogram = meter().createHistogram("semiont.handler.duration", {
|
|
110
|
+
description: "In-process actor handler duration",
|
|
111
|
+
unit: "ms"
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
return _handlerDurationHistogram;
|
|
115
|
+
}
|
|
116
|
+
function jobOutcomeCounter() {
|
|
117
|
+
if (!_jobOutcomeCounter) {
|
|
118
|
+
_jobOutcomeCounter = meter().createCounter("semiont.job.outcome", {
|
|
119
|
+
description: "Worker job completions by type and outcome"
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
return _jobOutcomeCounter;
|
|
123
|
+
}
|
|
124
|
+
function jobDurationHistogram() {
|
|
125
|
+
if (!_jobDurationHistogram) {
|
|
126
|
+
_jobDurationHistogram = meter().createHistogram("semiont.job.duration", {
|
|
127
|
+
description: "Worker job duration by type",
|
|
128
|
+
unit: "ms"
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
return _jobDurationHistogram;
|
|
132
|
+
}
|
|
133
|
+
function inferenceCallsCounter() {
|
|
134
|
+
if (!_inferenceCallsCounter) {
|
|
135
|
+
_inferenceCallsCounter = meter().createCounter("semiont.inference.calls", {
|
|
136
|
+
description: "Inference API calls by provider, model, and outcome"
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
return _inferenceCallsCounter;
|
|
140
|
+
}
|
|
141
|
+
function inferenceTokensCounter() {
|
|
142
|
+
if (!_inferenceTokensCounter) {
|
|
143
|
+
_inferenceTokensCounter = meter().createCounter("semiont.inference.tokens", {
|
|
144
|
+
description: "Inference token usage by provider, model, and direction"
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
return _inferenceTokensCounter;
|
|
148
|
+
}
|
|
149
|
+
function inferenceDurationHistogram() {
|
|
150
|
+
if (!_inferenceDurationHistogram) {
|
|
151
|
+
_inferenceDurationHistogram = meter().createHistogram("semiont.inference.duration", {
|
|
152
|
+
description: "Inference call duration by provider, model, and outcome",
|
|
153
|
+
unit: "ms"
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
return _inferenceDurationHistogram;
|
|
157
|
+
}
|
|
158
|
+
function sseSubscribersCounter() {
|
|
159
|
+
if (!_sseSubscribers) {
|
|
160
|
+
_sseSubscribers = meter().createUpDownCounter("semiont.sse.subscribers", {
|
|
161
|
+
description: "Active SSE subscribers"
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
return _sseSubscribers;
|
|
165
|
+
}
|
|
166
|
+
function recordBusEmit(channel, scope) {
|
|
167
|
+
busEmitCounter().add(1, {
|
|
168
|
+
"bus.channel": channel,
|
|
169
|
+
...scope ? { "bus.scope": scope } : {}
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
function recordHandlerDuration(actor, channel, durationMs) {
|
|
173
|
+
handlerDurationHistogram().record(durationMs, {
|
|
174
|
+
actor,
|
|
175
|
+
"bus.channel": channel
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
function recordJobOutcome(jobType, outcome, durationMs) {
|
|
179
|
+
jobOutcomeCounter().add(1, { "job.type": jobType, "job.outcome": outcome });
|
|
180
|
+
jobDurationHistogram().record(durationMs, { "job.type": jobType, "job.outcome": outcome });
|
|
181
|
+
}
|
|
182
|
+
function recordSubscriberConnect() {
|
|
183
|
+
sseSubscribersCounter().add(1);
|
|
184
|
+
}
|
|
185
|
+
function recordSubscriberDisconnect() {
|
|
186
|
+
sseSubscribersCounter().add(-1);
|
|
187
|
+
}
|
|
188
|
+
function registerJobQueueProvider(provider) {
|
|
189
|
+
_jobQueueProvider = provider;
|
|
190
|
+
if (!_jobQueueGauge) {
|
|
191
|
+
_jobQueueGauge = meter().createObservableGauge("semiont.job.queue.size", {
|
|
192
|
+
description: "Job queue size by status"
|
|
193
|
+
});
|
|
194
|
+
_jobQueueGauge.addCallback(async (observer) => {
|
|
195
|
+
if (!_jobQueueProvider) return;
|
|
196
|
+
const snap = await _jobQueueProvider();
|
|
197
|
+
observer.observe(snap.pending, { "job.status": "pending" });
|
|
198
|
+
observer.observe(snap.running, { "job.status": "running" });
|
|
199
|
+
observer.observe(snap.complete, { "job.status": "complete" });
|
|
200
|
+
observer.observe(snap.failed, { "job.status": "failed" });
|
|
201
|
+
observer.observe(snap.cancelled, { "job.status": "cancelled" });
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
function registerVectorIndexSizeProvider(provider) {
|
|
206
|
+
_vectorIndexSizeProvider = provider;
|
|
207
|
+
if (!_vectorIndexSizeGauge) {
|
|
208
|
+
_vectorIndexSizeGauge = meter().createObservableGauge("semiont.vector.index.size", {
|
|
209
|
+
description: "Vector store point count"
|
|
210
|
+
});
|
|
211
|
+
_vectorIndexSizeGauge.addCallback(async (observer) => {
|
|
212
|
+
if (_vectorIndexSizeProvider) {
|
|
213
|
+
const value = await _vectorIndexSizeProvider();
|
|
214
|
+
observer.observe(value);
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
function recordInferenceUsage(opts) {
|
|
220
|
+
const baseAttrs = {
|
|
221
|
+
"inference.provider": opts.provider,
|
|
222
|
+
"inference.model": opts.model,
|
|
223
|
+
"inference.outcome": opts.outcome
|
|
224
|
+
};
|
|
225
|
+
inferenceCallsCounter().add(1, baseAttrs);
|
|
226
|
+
inferenceDurationHistogram().record(opts.durationMs, baseAttrs);
|
|
227
|
+
if (opts.inputTokens != null && opts.inputTokens > 0) {
|
|
228
|
+
inferenceTokensCounter().add(opts.inputTokens, {
|
|
229
|
+
"inference.provider": opts.provider,
|
|
230
|
+
"inference.model": opts.model,
|
|
231
|
+
"inference.direction": "input"
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
if (opts.outputTokens != null && opts.outputTokens > 0) {
|
|
235
|
+
inferenceTokensCounter().add(opts.outputTokens, {
|
|
236
|
+
"inference.provider": opts.provider,
|
|
237
|
+
"inference.model": opts.model,
|
|
238
|
+
"inference.direction": "output"
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
export { extractTraceparent, getActiveTraceparent, getLogTraceContext, injectTraceparent, recordBusEmit, recordHandlerDuration, recordInferenceUsage, recordJobOutcome, recordSubscriberConnect, recordSubscriberDisconnect, registerJobQueueProvider, registerVectorIndexSizeProvider, withActorSpan, withSpan, withTraceparent };
|
|
244
|
+
//# sourceMappingURL=index.js.map
|
|
245
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";;;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AA,CAAY,OAAO,QAAA,KAAa;AACpD,MAAA,IAAI,wBAAA,EAA0B;AAC5B,QAAA,MAAM,KAAA,GAAQ,MAAM,wBAAA,EAAyB;AAC7C,QAAA,QAAA,CAAS,QAAQ,KAAK,CAAA;AAAA,MACxB;AAAA,IACF,CAAC,CAAA;AAAA,EACH;AACF;AAOO,SAAS,qBAAqB,IAAA,EAO5B;AACP,EAAA,MAAM,SAAA,GAAY;AAAA,IAChB,sBAAsB,IAAA,CAAK,QAAA;AAAA,IAC3B,mBAAmB,IAAA,CAAK,KAAA;AAAA,IACxB,qBAAqB,IAAA,CAAK;AAAA,GAC5B;AACA,EAAA,qBAAA,EAAsB,CAAE,GAAA,CAAI,CAAA,EAAG,SAAS,CAAA;AACxC,EAAA,0BAAA,EAA2B,CAAE,MAAA,CAAO,IAAA,CAAK,UAAA,EAAY,SAAS,CAAA;AAC9D,EAAA,IAAI,IAAA,CAAK,WAAA,IAAe,IAAA,IAAQ,IAAA,CAAK,cAAc,CAAA,EAAG;AACpD,IAAA,sBAAA,EAAuB,CAAE,GAAA,CAAI,IAAA,CAAK,WAAA,EAAa;AAAA,MAC7C,sBAAsB,IAAA,CAAK,QAAA;AAAA,MAC3B,mBAAmB,IAAA,CAAK,KAAA;AAAA,MACxB,qBAAA,EAAuB;AAAA,KACxB,CAAA;AAAA,EACH;AACA,EAAA,IAAI,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 is intentionally\n * thin:\n *\n * - `withSpan(name, fn, attrs?)` — wrap an async block in a span.\n * - `injectTraceparent(payload)` / `extractTraceparent(value)` — W3C\n * trace-context propagation across the SSE channel (the bus payload\n * gets a `_trace?: { traceparent }` sibling to `correlationId`).\n * - `setSpanContextFromTraceparent(traceparent, fn)` — set incoming\n * traceparent as the parent context for a synchronous block.\n * - `getActiveTraceparent()` — read the active span's traceparent for\n * manual propagation (e.g. attaching to a fetch header or SSE field).\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 backend\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 _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\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"]}
|
package/dist/node.d.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Node SDK initialization. Call once at the process entry point
|
|
3
|
+
* (backend `index.ts`, `worker-main.ts`, `smelter-main.ts`).
|
|
4
|
+
*
|
|
5
|
+
* Configuration is via standard `OTEL_*` env vars:
|
|
6
|
+
* - `OTEL_SERVICE_NAME` — service identity (e.g. `semiont-backend`)
|
|
7
|
+
* - `OTEL_EXPORTER_OTLP_ENDPOINT` — collector endpoint (HTTP)
|
|
8
|
+
* - `OTEL_TRACES_SAMPLER` — sampler (default: `parentbased_always_on`)
|
|
9
|
+
* - `OTEL_TRACES_SAMPLER_ARG` — sampler ratio (default: `1.0`)
|
|
10
|
+
* - `OTEL_CONSOLE_EXPORTER=true` — dev-only: emit spans + metrics to stderr
|
|
11
|
+
* - `OTEL_SDK_DISABLED=true` — skip initialization entirely
|
|
12
|
+
*
|
|
13
|
+
* **Off-by-default invariant**: with neither `OTEL_EXPORTER_OTLP_ENDPOINT`
|
|
14
|
+
* nor `OTEL_CONSOLE_EXPORTER=true` set, this function is a no-op and the
|
|
15
|
+
* `@opentelemetry/api` no-op tracer takes over. This avoids accidentally
|
|
16
|
+
* flooding production stderr (and CloudWatch) when an operator deploys
|
|
17
|
+
* without configuring an exporter.
|
|
18
|
+
*
|
|
19
|
+
* Implementation note: this module wires `BasicTracerProvider` and
|
|
20
|
+
* `MeterProvider` (both from the stable `@opentelemetry/sdk-trace-base`
|
|
21
|
+
* 2.x line) directly, plus `AsyncLocalStorageContextManager` for Node
|
|
22
|
+
* async-context propagation. We deliberately avoid `@opentelemetry/sdk-node`
|
|
23
|
+
* because its `0.x` experimental versions cross-depend on older 2.0.x
|
|
24
|
+
* SDKs, forcing npm to nest duplicate copies of the stable packages and
|
|
25
|
+
* blowing up bundles for every consumer.
|
|
26
|
+
*/
|
|
27
|
+
interface NodeObservabilityConfig {
|
|
28
|
+
/** Service identity (e.g. `semiont-backend`). Overridden by `OTEL_SERVICE_NAME`. */
|
|
29
|
+
serviceName: string;
|
|
30
|
+
/** Service version. Defaults to `0.0.0` if omitted. */
|
|
31
|
+
serviceVersion?: string;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Initialize OTel for the current process. Wires up both tracing and
|
|
35
|
+
* metrics. Idempotent — calling twice is a no-op. Returns `true` if the
|
|
36
|
+
* SDK started, `false` if disabled, no exporter is configured, or
|
|
37
|
+
* already initialized.
|
|
38
|
+
*
|
|
39
|
+
* Metrics export at `OTEL_METRIC_EXPORT_INTERVAL` ms (default 30s) to
|
|
40
|
+
* the same `OTEL_EXPORTER_OTLP_ENDPOINT` as traces.
|
|
41
|
+
*/
|
|
42
|
+
declare function initObservabilityNode(config: NodeObservabilityConfig): boolean;
|
|
43
|
+
/** Force-flush + shutdown both SDKs. Test cleanup, not production. */
|
|
44
|
+
declare function shutdownObservabilityNode(): Promise<void>;
|
|
45
|
+
|
|
46
|
+
export { type NodeObservabilityConfig, initObservabilityNode, shutdownObservabilityNode };
|
package/dist/node.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { context, trace, propagation, metrics } from '@opentelemetry/api';
|
|
2
|
+
import { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks';
|
|
3
|
+
import { W3CTraceContextPropagator } from '@opentelemetry/core';
|
|
4
|
+
import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-http';
|
|
5
|
+
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';
|
|
6
|
+
import { resourceFromAttributes } from '@opentelemetry/resources';
|
|
7
|
+
import { ConsoleMetricExporter, MeterProvider, PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics';
|
|
8
|
+
import { ConsoleSpanExporter, BasicTracerProvider, BatchSpanProcessor } from '@opentelemetry/sdk-trace-base';
|
|
9
|
+
import { ATTR_SERVICE_VERSION, ATTR_SERVICE_NAME } from '@opentelemetry/semantic-conventions';
|
|
10
|
+
|
|
11
|
+
// src/node.ts
|
|
12
|
+
var tracerProviderInstance;
|
|
13
|
+
var meterProviderInstance;
|
|
14
|
+
var DEFAULT_METRIC_EXPORT_INTERVAL_MS = 3e4;
|
|
15
|
+
function initObservabilityNode(config) {
|
|
16
|
+
if (tracerProviderInstance) return false;
|
|
17
|
+
if (process.env["OTEL_SDK_DISABLED"] === "true") return false;
|
|
18
|
+
const endpoint = process.env["OTEL_EXPORTER_OTLP_ENDPOINT"];
|
|
19
|
+
const useConsole = process.env["OTEL_CONSOLE_EXPORTER"] === "true";
|
|
20
|
+
if (!endpoint && !useConsole) return false;
|
|
21
|
+
const resource = resourceFromAttributes({
|
|
22
|
+
[ATTR_SERVICE_NAME]: process.env["OTEL_SERVICE_NAME"] ?? config.serviceName,
|
|
23
|
+
[ATTR_SERVICE_VERSION]: config.serviceVersion ?? "0.0.0"
|
|
24
|
+
});
|
|
25
|
+
const traceExporter = endpoint ? new OTLPTraceExporter() : new ConsoleSpanExporter();
|
|
26
|
+
tracerProviderInstance = new BasicTracerProvider({
|
|
27
|
+
resource,
|
|
28
|
+
spanProcessors: [new BatchSpanProcessor(traceExporter)]
|
|
29
|
+
});
|
|
30
|
+
context.setGlobalContextManager(new AsyncLocalStorageContextManager().enable());
|
|
31
|
+
trace.setGlobalTracerProvider(tracerProviderInstance);
|
|
32
|
+
propagation.setGlobalPropagator(new W3CTraceContextPropagator());
|
|
33
|
+
const metricExporter = endpoint ? new OTLPMetricExporter() : new ConsoleMetricExporter();
|
|
34
|
+
const intervalRaw = process.env["OTEL_METRIC_EXPORT_INTERVAL"];
|
|
35
|
+
const exportIntervalMillis = intervalRaw ? Number.parseInt(intervalRaw, 10) : DEFAULT_METRIC_EXPORT_INTERVAL_MS;
|
|
36
|
+
meterProviderInstance = new MeterProvider({
|
|
37
|
+
resource,
|
|
38
|
+
readers: [
|
|
39
|
+
new PeriodicExportingMetricReader({
|
|
40
|
+
exporter: metricExporter,
|
|
41
|
+
exportIntervalMillis: Number.isFinite(exportIntervalMillis) && exportIntervalMillis > 0 ? exportIntervalMillis : DEFAULT_METRIC_EXPORT_INTERVAL_MS
|
|
42
|
+
})
|
|
43
|
+
]
|
|
44
|
+
});
|
|
45
|
+
metrics.setGlobalMeterProvider(meterProviderInstance);
|
|
46
|
+
const shutdown = () => {
|
|
47
|
+
Promise.all([
|
|
48
|
+
tracerProviderInstance?.shutdown().catch(() => {
|
|
49
|
+
}),
|
|
50
|
+
meterProviderInstance?.shutdown().catch(() => {
|
|
51
|
+
})
|
|
52
|
+
]).finally(() => {
|
|
53
|
+
tracerProviderInstance = void 0;
|
|
54
|
+
meterProviderInstance = void 0;
|
|
55
|
+
});
|
|
56
|
+
};
|
|
57
|
+
process.once("SIGTERM", shutdown);
|
|
58
|
+
process.once("SIGINT", shutdown);
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
async function shutdownObservabilityNode() {
|
|
62
|
+
await Promise.all([
|
|
63
|
+
tracerProviderInstance?.shutdown(),
|
|
64
|
+
meterProviderInstance?.shutdown()
|
|
65
|
+
]);
|
|
66
|
+
tracerProviderInstance = void 0;
|
|
67
|
+
meterProviderInstance = void 0;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export { initObservabilityNode, shutdownObservabilityNode };
|
|
71
|
+
//# sourceMappingURL=node.js.map
|
|
72
|
+
//# sourceMappingURL=node.js.map
|
package/dist/node.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/node.ts"],"names":[],"m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(backend `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-backend`)\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-backend`). 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"]}
|
package/dist/web.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Web SDK initialization. Call once at the SPA bootstrap (e.g.
|
|
3
|
+
* `apps/frontend/src/main.tsx`).
|
|
4
|
+
*
|
|
5
|
+
* Configuration for the browser doesn't read env vars; the SPA passes
|
|
6
|
+
* config explicitly. CORS-allowed OTLP endpoints only — operators
|
|
7
|
+
* typically run a collector that exposes a CORS-enabled `/v1/traces`
|
|
8
|
+
* endpoint.
|
|
9
|
+
*
|
|
10
|
+
* Without an `otlpEndpoint`, falls back to a console exporter (visible
|
|
11
|
+
* in DevTools).
|
|
12
|
+
*/
|
|
13
|
+
interface WebObservabilityConfig {
|
|
14
|
+
/** Service identity (e.g. `semiont-frontend`). */
|
|
15
|
+
serviceName: string;
|
|
16
|
+
/** Service version. */
|
|
17
|
+
serviceVersion?: string;
|
|
18
|
+
/**
|
|
19
|
+
* OTLP HTTP endpoint (e.g. `https://collector.example.com/v1/traces`).
|
|
20
|
+
* If omitted, the SDK uses a console exporter.
|
|
21
|
+
*/
|
|
22
|
+
otlpEndpoint?: string;
|
|
23
|
+
/** Optional headers (e.g. SaaS APM auth). */
|
|
24
|
+
otlpHeaders?: Record<string, string>;
|
|
25
|
+
/**
|
|
26
|
+
* Force on (`true`) or force off (`false`). When omitted, the SDK
|
|
27
|
+
* initializes if `otlpEndpoint` is present, otherwise off.
|
|
28
|
+
*/
|
|
29
|
+
enabled?: boolean;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Initialize OTel for the SPA. Idempotent. Returns `true` if the SDK
|
|
33
|
+
* started, `false` if disabled or already initialized.
|
|
34
|
+
*/
|
|
35
|
+
declare function initObservabilityWeb(config: WebObservabilityConfig): boolean;
|
|
36
|
+
|
|
37
|
+
export { type WebObservabilityConfig, initObservabilityWeb };
|
package/dist/web.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';
|
|
2
|
+
import { resourceFromAttributes } from '@opentelemetry/resources';
|
|
3
|
+
import { ConsoleSpanExporter, WebTracerProvider, BatchSpanProcessor } from '@opentelemetry/sdk-trace-web';
|
|
4
|
+
import { ATTR_SERVICE_VERSION, ATTR_SERVICE_NAME } from '@opentelemetry/semantic-conventions';
|
|
5
|
+
|
|
6
|
+
// src/web.ts
|
|
7
|
+
var providerInstance;
|
|
8
|
+
function initObservabilityWeb(config) {
|
|
9
|
+
if (providerInstance) return false;
|
|
10
|
+
const enabled = config.enabled ?? Boolean(config.otlpEndpoint);
|
|
11
|
+
if (!enabled) return false;
|
|
12
|
+
const exporter = config.otlpEndpoint ? new OTLPTraceExporter({
|
|
13
|
+
url: config.otlpEndpoint,
|
|
14
|
+
...config.otlpHeaders ? { headers: config.otlpHeaders } : {}
|
|
15
|
+
}) : new ConsoleSpanExporter();
|
|
16
|
+
const resource = resourceFromAttributes({
|
|
17
|
+
[ATTR_SERVICE_NAME]: config.serviceName,
|
|
18
|
+
[ATTR_SERVICE_VERSION]: config.serviceVersion ?? "0.0.0"
|
|
19
|
+
});
|
|
20
|
+
providerInstance = new WebTracerProvider({
|
|
21
|
+
resource,
|
|
22
|
+
spanProcessors: [new BatchSpanProcessor(exporter)]
|
|
23
|
+
});
|
|
24
|
+
providerInstance.register();
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export { initObservabilityWeb };
|
|
29
|
+
//# sourceMappingURL=web.js.map
|
|
30
|
+
//# sourceMappingURL=web.js.map
|
package/dist/web.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/web.ts"],"names":[],"mappings":";;;;;;AA4CA,IAAI,gBAAA;AAMG,SAAS,qBAAqB,MAAA,EAAyC;AAC5E,EAAA,IAAI,kBAAkB,OAAO,KAAA;AAC7B,EAAA,MAAM,OAAA,GAAU,MAAA,CAAO,OAAA,IAAW,OAAA,CAAQ,OAAO,YAAY,CAAA;AAC7D,EAAA,IAAI,CAAC,SAAS,OAAO,KAAA;AAErB,EAAA,MAAM,QAAA,GAAW,MAAA,CAAO,YAAA,GACpB,IAAI,iBAAA,CAAkB;AAAA,IACpB,KAAK,MAAA,CAAO,YAAA;AAAA,IACZ,GAAI,OAAO,WAAA,GAAc,EAAE,SAAS,MAAA,CAAO,WAAA,KAAgB;AAAC,GAC7D,CAAA,GACD,IAAI,mBAAA,EAAoB;AAE5B,EAAA,MAAM,WAAW,sBAAA,CAAuB;AAAA,IACtC,CAAC,iBAAiB,GAAG,MAAA,CAAO,WAAA;AAAA,IAC5B,CAAC,oBAAoB,GAAG,MAAA,CAAO,cAAA,IAAkB;AAAA,GAClD,CAAA;AAED,EAAA,gBAAA,GAAmB,IAAI,iBAAA,CAAkB;AAAA,IACvC,QAAA;AAAA,IACA,cAAA,EAAgB,CAAC,IAAI,kBAAA,CAAmB,QAAQ,CAAC;AAAA,GAClD,CAAA;AAED,EAAA,gBAAA,CAAiB,QAAA,EAAS;AAC1B,EAAA,OAAO,IAAA;AACT","file":"web.js","sourcesContent":["/**\n * Web SDK initialization. Call once at the SPA bootstrap (e.g.\n * `apps/frontend/src/main.tsx`).\n *\n * Configuration for the browser doesn't read env vars; the SPA passes\n * config explicitly. CORS-allowed OTLP endpoints only — operators\n * typically run a collector that exposes a CORS-enabled `/v1/traces`\n * endpoint.\n *\n * Without an `otlpEndpoint`, falls back to a console exporter (visible\n * in DevTools).\n */\n\nimport { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';\nimport { resourceFromAttributes } from '@opentelemetry/resources';\nimport {\n BatchSpanProcessor,\n ConsoleSpanExporter,\n WebTracerProvider,\n} from '@opentelemetry/sdk-trace-web';\nimport {\n ATTR_SERVICE_NAME,\n ATTR_SERVICE_VERSION,\n} from '@opentelemetry/semantic-conventions';\n\nexport interface WebObservabilityConfig {\n /** Service identity (e.g. `semiont-frontend`). */\n serviceName: string;\n /** Service version. */\n serviceVersion?: string;\n /**\n * OTLP HTTP endpoint (e.g. `https://collector.example.com/v1/traces`).\n * If omitted, the SDK uses a console exporter.\n */\n otlpEndpoint?: string;\n /** Optional headers (e.g. SaaS APM auth). */\n otlpHeaders?: Record<string, string>;\n /**\n * Force on (`true`) or force off (`false`). When omitted, the SDK\n * initializes if `otlpEndpoint` is present, otherwise off.\n */\n enabled?: boolean;\n}\n\nlet providerInstance: WebTracerProvider | undefined;\n\n/**\n * Initialize OTel for the SPA. Idempotent. Returns `true` if the SDK\n * started, `false` if disabled or already initialized.\n */\nexport function initObservabilityWeb(config: WebObservabilityConfig): boolean {\n if (providerInstance) return false;\n const enabled = config.enabled ?? Boolean(config.otlpEndpoint);\n if (!enabled) return false;\n\n const exporter = config.otlpEndpoint\n ? new OTLPTraceExporter({\n url: config.otlpEndpoint,\n ...(config.otlpHeaders ? { headers: config.otlpHeaders } : {}),\n })\n : new ConsoleSpanExporter();\n\n const resource = resourceFromAttributes({\n [ATTR_SERVICE_NAME]: config.serviceName,\n [ATTR_SERVICE_VERSION]: config.serviceVersion ?? '0.0.0',\n });\n\n providerInstance = new WebTracerProvider({\n resource,\n spanProcessors: [new BatchSpanProcessor(exporter)],\n });\n\n providerInstance.register();\n return true;\n}\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@semiont/observability",
|
|
3
|
+
"version": "0.4.21",
|
|
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
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
},
|
|
14
|
+
"./node": {
|
|
15
|
+
"types": "./dist/node.d.ts",
|
|
16
|
+
"import": "./dist/node.js",
|
|
17
|
+
"default": "./dist/node.js"
|
|
18
|
+
},
|
|
19
|
+
"./web": {
|
|
20
|
+
"types": "./dist/web.d.ts",
|
|
21
|
+
"import": "./dist/web.js",
|
|
22
|
+
"default": "./dist/web.js"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"engines": {
|
|
26
|
+
"node": ">=20.18.1"
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"dist",
|
|
30
|
+
"README.md",
|
|
31
|
+
"LICENSE"
|
|
32
|
+
],
|
|
33
|
+
"publishConfig": {
|
|
34
|
+
"access": "public"
|
|
35
|
+
},
|
|
36
|
+
"repository": {
|
|
37
|
+
"type": "git",
|
|
38
|
+
"url": "git+https://github.com/The-AI-Alliance/semiont.git",
|
|
39
|
+
"directory": "packages/observability"
|
|
40
|
+
},
|
|
41
|
+
"scripts": {
|
|
42
|
+
"pretypecheck": "npm run build --workspace=@semiont/core --if-present",
|
|
43
|
+
"typecheck": "tsc --noEmit",
|
|
44
|
+
"build": "npm run typecheck && tsup",
|
|
45
|
+
"watch": "tsup --watch",
|
|
46
|
+
"clean": "rm -rf dist *.tsbuildinfo",
|
|
47
|
+
"test": "vitest run",
|
|
48
|
+
"test:watch": "vitest"
|
|
49
|
+
},
|
|
50
|
+
"author": "Semiont Team",
|
|
51
|
+
"license": "Apache-2.0",
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"tsup": "^8.5.1",
|
|
54
|
+
"typescript": "^5.6.3",
|
|
55
|
+
"vitest": "^4.1.0"
|
|
56
|
+
},
|
|
57
|
+
"dependencies": {
|
|
58
|
+
"@opentelemetry/api": "^1.9.0",
|
|
59
|
+
"@semiont/core": "0.4.21",
|
|
60
|
+
"@opentelemetry/context-async-hooks": "^2.7.0",
|
|
61
|
+
"@opentelemetry/core": "^2.7.0",
|
|
62
|
+
"@opentelemetry/exporter-metrics-otlp-http": "^0.215.0",
|
|
63
|
+
"@opentelemetry/exporter-trace-otlp-http": "^0.215.0",
|
|
64
|
+
"@opentelemetry/resources": "^2.7.0",
|
|
65
|
+
"@opentelemetry/sdk-metrics": "^2.7.0",
|
|
66
|
+
"@opentelemetry/sdk-trace-base": "^2.7.0",
|
|
67
|
+
"@opentelemetry/sdk-trace-web": "^2.7.0",
|
|
68
|
+
"@opentelemetry/semantic-conventions": "^1.40.0"
|
|
69
|
+
}
|
|
70
|
+
}
|