Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 | 1x 96x 96x 14x 14x 96x 33x 2x 31x 31x 3x 28x 28x 34x 28x 28x 31x 32x 2x 1x 1x 1x 1x 1x 10x 11x 3x 8x 8x 8x 2x 6x 2x 4x 3x 1x 35x 35x 35x 11x 35x 32x 35x 2x 35x 35x 35x 1x | import { AppCtx } from '@tao.js/core';
import { newTraceId, newSignalId, parseTraceparent } from './ids';
/**
* One traced signal, as delivered to every sink's `signal(record)`.
*
* A record describes a network dispatch, not a handler execution — a
* channel's mirrored dispatch of the same AppCon on its private registry is
* not a second record.
*
* @typedef {Object} TraceRecord
* @property {string} traceId - 32 lowercase hex chars (W3C trace id), shared
* by every signal in the causal tree
* @property {string} signalId - 16 lowercase hex chars (W3C span id) naming
* this signal
* @property {string|null} parentId - `signalId` of the signal whose handler
* chained this one; null for a trace root
* @property {string} t - term of the signal's trigram
* @property {string} a - action of the signal's trigram
* @property {string} o - orient of the signal's trigram
* @property {string} key - the trigram's AppCtx key (`t|a|o`)
* @property {number} timestamp - ms-epoch time from the tracer's clock
* @property {'Intercept'|'Async'|'Inline'} [via] - handler phase that chained
* this signal (ENVELOPE-SPEC.md §4); entry hops carry no `via`
* @property {{ intercept: number, async: number, inline: number }} [handlers] -
* counts of handlers matching this trigram (wildcard registrations
* included) on the decorated network at dispatch time; handlers
* attached to Channels' private registries are not counted
* @property {*} [data] - the AppCon data, only with the `captureData` option
*/
/**
* Namespaced chain key under which trace context is derived per hop by the
* envelope engine (see ENVELOPE-SPEC.md). Chain keys are exclusive — a
* Network accepts one reducer per key — so constructing a second Tracer on
* the same network throws. This is also the key transports carry across
* process boundaries (§9): an inbound W3C `traceparent` maps to
* `chain: { [TRACE_CHAIN]: { traceId, signalId } }` on entry.
*/
export const TRACE_CHAIN = 'taoTrace';
/**
* Count a handler iterator without materializing it.
*
* @param {Iterable<function>} iterator
* @returns {number}
*/
function countHandlers(iterator) {
let count = 0;
for (const handler of iterator) {
handler;
count++;
}
return count;
}
/**
* Causal signal tracing as a pure Network decoration: a chain reducer
* derives `{ traceId, signalId, parentId }` per hop, and an observer emits
* one record per dispatched signal to attached sinks. No instrumentation of
* any entry surface is required — every cascade (kernel entries, channel
* entries, transponder entries, chained hops, channel mirrors) carries
* causality natively.
*
* @export
* @class Tracer
*/
/**
* Any surface a Tracer can attach to: a Kernel-/Channel-shaped wrapper
* exposing the shared network via `_network`, or a bare `Network`
* (exposing `enter` + `decorate`) decorated directly.
*
* @typedef {Object} TraceableSurface
* @property {Function} [enter] - present on a bare Network
* @property {Function} [decorate] - present on a bare Network
* @property {import('@tao.js/core').Network} [_network] - present on Kernel-/Channel-shaped wrappers
* @property {boolean} [canSetWildcard] - mirrored onto traced wildcard handling when present
*/
export default class Tracer {
/**
* Creates an instance of Tracer.
* @param {TraceableSurface} kernel - Kernel (or Channel, or raw Network) whose signals
* to trace; anything exposing the shared network via `_network`, or a
* Network itself, is decorated
* @param {Object} [opts]
* @param {Array<{signal: function(TraceRecord): void}>} [opts.sinks] - sinks
* receiving one record per signal
* @param {function(): number} [opts.clock] - returns a ms-epoch timestamp
* (defaults to Date.now)
* @param {boolean|function(*, AppCtx): *} [opts.captureData] - false (default)
* omits AppCon data; true attaches `ac.data` by reference; a function
* receives `(data, ac)` and its return value is attached (use to
* redact / clone)
* @throws when `kernel` is absent, when the core predates envelope support,
* or when the network's {@link TRACE_CHAIN} chain key is already
* reduced by another decoration (one Tracer per network)
* @memberof Tracer
*/
// Stryker disable next-line ArrayDeclaration: junk entries in the sinks default are call-guarded by the per-sink try
constructor(kernel, { sinks = [], clock, captureData = false } = {}) {
if (!kernel || (typeof kernel.enter !== 'function' && !kernel._network)) {
throw new Error(
'must provide `kernel` to attach the Tracer to a network',
);
}
// a Kernel (or Channel) exposes the shared network via `_network`; a raw
// Network is decorated directly
this._network = kernel._network || kernel;
if (
typeof this._network.enter !== 'function' ||
typeof this._network.decorate !== 'function'
) {
throw new Error(
'Tracer requires a @tao.js/core version with envelope support - upgrade @tao.js/core',
);
}
this._canSetWildcard = !!kernel.canSetWildcard;
this._sinks = new Set(sinks);
this._clock = typeof clock === 'function' ? clock : () => Date.now();
this._captureData = captureData;
this._undecorate = this._network.decorate({
// Stryker disable next-line StringLiteral: decoration name is a diagnostic label with no observable behavior
name: 'tracer',
chain: {
key: TRACE_CHAIN,
next: (prev) => ({
traceId: prev ? prev.traceId : newTraceId(),
signalId: newSignalId(),
parentId: prev ? prev.signalId : null,
}),
},
onDispatch: (ac, envelope, handler) =>
this._record(ac, envelope, handler),
});
}
/**
* Attach a sink to receive one {@link TraceRecord} per signal.
*
* @param {{ signal: function(TraceRecord): void }} sink
* @returns {this}
* @memberof Tracer
*/
addSink(sink) {
if (!sink || typeof sink.signal !== 'function') {
throw new Error('a sink must implement signal(record)');
}
this._sinks.add(sink);
return this;
}
/**
* Detach a previously attached sink (a no-op when not attached).
*
* @param {{ signal: function(TraceRecord): void }} sink
* @returns {this}
* @memberof Tracer
*/
removeSink(sink) {
this._sinks.delete(sink);
return this;
}
/**
* Set a Context on the network, optionally continuing a trace started
* elsewhere (e.g. in another process).
*
* Plain `kernel.setCtx` entries are traced identically — this entry point
* only adds the continuation capability.
*
* @param {{ t?: string, a?: string, o?: string, term?: string, action?: string, orient?: string }} trigram -
* `{ t, a, o }` or `{ term, action, orient }` (long forms win when
* both are present)
* @param {*} [data]
* @param {{ traceparent?: string, traceId?: string, parentId?: string|null }} [traceContext] -
* `{ traceparent }` (W3C header value) or `{ traceId, parentId }` to
* continue a trace started elsewhere: the entry keeps the remote
* `traceId` and is parented to `parentId`. An absent or malformed
* context starts a new root trace instead
* @memberof Tracer
*/
setCtx({ t, term, a, action, o, orient }, data, traceContext) {
this.setAppCtx(
new AppCtx(term || t, action || a, orient || o, data),
traceContext,
);
}
/**
* Set an AppCtx on the network, optionally continuing a remote trace.
*
* A continuation seeds the entry's chain with
* `{ [TRACE_CHAIN]: { traceId, signalId: parentId } }`, which the chain
* reducer treats exactly like a parent hop's stamp — the remote signal
* becomes the entry's parent.
*
* @param {AppCtx} ac - dropped silently when wildcard and the attached
* kernel disallows wildcard entry (parity with `Kernel.setAppCtx`)
* @param {{ traceparent?: string, traceId?: string, parentId?: string|null }} [traceContext] -
* see {@link Tracer#setCtx}
* @memberof Tracer
*/
setAppCtx(ac, traceContext) {
if (!this._canSetWildcard && ac.isWildcard) {
return;
}
const remote = this._remoteContext(traceContext);
this._network.enter(
ac,
remote
? {
chain: {
[TRACE_CHAIN]: {
traceId: remote.traceId,
signalId: remote.parentId,
},
},
}
: {},
);
}
/**
* Normalize a continuation context to `{ traceId, parentId }`, preferring
* a W3C `traceparent` header when given. Returns null (start a new root)
* when absent or malformed.
*
* @param {{ traceparent?: string, traceId?: string, parentId?: string|null }} [traceContext]
* @returns {{ traceId: string, parentId: string|null }|null}
*/
_remoteContext(traceContext) {
if (!traceContext) {
return null;
}
if (typeof traceContext.traceparent === 'string') {
return parseTraceparent(traceContext.traceparent);
}
if (traceContext.traceId) {
return {
traceId: traceContext.traceId,
parentId: traceContext.parentId || null,
};
}
return null;
}
/**
* The `onDispatch` observer: assemble one {@link TraceRecord} from the
* hop's {@link TRACE_CHAIN} chain stamp and fan it out to every sink.
* Handler counts are taken from the matched registry entry at dispatch
* time (the decorated main network only); a throwing sink is isolated so
* it never breaks signal dispatch.
*
* @param {AppCtx} ac
* @param {{ cascade: Object, hop: Object, chain: Object }} envelope
* @param {Object} handler - the matched AppCtxHandlers registry entry
*/
_record(ac, envelope, handler) {
const stamp = envelope.chain[TRACE_CHAIN];
/** @type {TraceRecord} */
const record = {
traceId: stamp.traceId,
signalId: stamp.signalId,
parentId: stamp.parentId || null,
t: ac.t,
a: ac.a,
o: ac.o,
key: ac.key,
timestamp: this._clock(),
};
if (envelope.hop.via) {
// the handler phase that produced this hop (ENVELOPE-SPEC.md §4)
record.via = envelope.hop.via;
}
if (handler) {
record.handlers = {
intercept: countHandlers(handler.interceptHandlers),
async: countHandlers(handler.asyncHandlers),
inline: countHandlers(handler.inlineHandlers),
};
}
if (this._captureData) {
record.data =
typeof this._captureData === 'function'
? this._captureData(ac.data, ac)
: ac.data;
}
for (const sink of this._sinks) {
try {
sink.signal(record);
} catch {
// a failing sink must never break signal dispatch
}
}
}
/**
* Detach the tracer decoration from the network.
*
* @memberof Tracer
*/
dispose() {
this._undecorate();
}
}
|