[data-reveal]{opacity:1!important;transform:none!important}
DocumentationDiagnostics

Diagnostics

Reference for the module lifecycle tracing channels that the AntelopeJS core publishes on node:diagnostics_channel for tracing, metrics, and profiling tools.

Overview

The core publishes each module lifecycle operation listed below on node:diagnostics_channel as a TracingChannel. An observability tool subscribes to the channel names and receives an event for each operation the core runs. Subscribing needs no configuration, and the channels cost nothing while nobody listens.

A module reload is not a span of its own. A hot reload destroys the running module and builds a replacement, so it publishes the old module's destroy, then the replacement's load, construct, and start, all under the same moduleId.

These channels are for diagnostics, not logging. Application logs still go through Logging. The two are independent, and neither feeds the other.

Channels

The core publishes five operations, each as a tracing channel:

ChannelOperation
antelopejs.module.loadLoading a module's entry point and getting its callbacks.
antelopejs.module.constructThe module's construct transition.
antelopejs.module.startThe module's start transition.
antelopejs.module.stopThe module's stop transition.
antelopejs.module.destroyThe module's destroy transition.

A tracing channel is not one channel on the wire. Each of the five names expands into five channels, one per event:

text
tracing:antelopejs.module.load:start
tracing:antelopejs.module.load:end
tracing:antelopejs.module.load:asyncStart
tracing:antelopejs.module.load:asyncEnd
tracing:antelopejs.module.load:error

Use diagnostics_channel.tracingChannel(name) and subscribe() instead of binding the expanded names yourself.

Payload

Every event of one span carries the same object:

FieldTypeMeaning
moduleIdstringThe module name from its manifest.
moduleVersionstring | undefinedThe module version when the span opened.
resultunknownAdded by Node on asyncStart and asyncEnd when the operation resolved.
errorunknownAdded by Node on error, and present on asyncStart and asyncEnd when the operation rejected.

moduleVersion is the version that the module's package.json declares, and the core does not validate that file. A module that declares no version publishes undefined on every event of every span. Guard before you call string methods on it. The note on subscriber errors below says what an unguarded throw costs.

result is the operation's return value. On antelopejs.module.load, that value is the module's whole export namespace, as the loader resolved it. Node assigns it inside tracePromise, and the core cannot suppress it. A module's exports often include a configured client that holds the credentials it was configured with. Treat the result of a load span as sensitive, and do not serialize it whole into a trace backend. The four transition spans resolve to nothing, so their result is always undefined.

No channel publishes the module configuration passed to construct. It can hold credentials, and a diagnostics subscriber is not an authorized reader of them.

Subscribing

typescript
import diagnostics_channel from "node:diagnostics_channel";

interface ModuleContext {
  moduleId: string;
  moduleVersion: string | undefined;
}

const construct = diagnostics_channel.tracingChannel<
  ModuleContext,
  ModuleContext
>("antelopejs.module.construct");

construct.subscribe({
  start(message) {
    console.log("constructing", message.moduleId, message.moduleVersion);
  },
  end() {},
  asyncStart() {},
  asyncEnd(message) {
    console.log("constructed", message.moduleId);
  },
  error(message) {
    console.log("construct failed", message.moduleId, message.error);
  },
});

Each span publishes start and end around the synchronous part of the operation, then asyncStart and asyncEnd around its asynchronous completion. A failed operation also publishes error, between end and asyncStart.

Behaviour a subscriber must expect

Lifecycle transitions run one at a time, and the wait is outside the span. The core queues transitions per module, so a stop requested while a start runs waits for the start to finish. That wait belongs to no span, because a span opens only when the operation begins. Span durations measure work, not time in the queue.

A transition that changes nothing publishes nothing. Starting a module that is already active, stopping a module that is not active, or constructing a module that is already constructed does nothing, and the core publishes no span for it. Do not expect one span per call.

Destroying an active module nests a stop span inside the destroy span. The core stops the module before it destroys it, and publishes that stop as an ordinary antelopejs.module.stop span inside the enclosing antelopejs.module.destroy span. The stop span sits inside the destroy span as a whole, but their events interleave. The destroy span's end lands between the stop span's end and its asyncStart, so the events arrive in this order: destroy:start, stop:start, stop:end, destroy:end, stop:asyncStart, stop:asyncEnd, destroy:asyncStart, destroy:asyncEnd. Build a span tree from start and asyncEnd, not from end.

A destroy span can report an error for a module that the core did destroy. When the nested stop fails but the destroy callback succeeds, the module reaches the loaded state, and the destroy span still reports the stop error. Read error on a destroy span as "part of the teardown failed", not as "the module is still alive".

A failed span does not always leave the state unchanged. construct marks the module constructed before it calls the callback. start and stop update the state after their callback returns. A construct span that reports an error leaves a module that counts as constructed and still needs a destroy. A start span that reports an error leaves a module that never became active.

A start span can follow a failed destroy of the same module. When a reload stops an active module but its destroy callback fails, the core keeps that module and starts it again to recover. A subscriber sees destroy report an error, then an ordinary, successful start span for the same moduleId. Nothing in either payload marks the second span as a recovery.

A reload that fails to activate leaves nothing running under that module ID. The core destroys the replacement, and it has already destroyed the module it was replacing. What a subscriber sees depends on where the replacement failed:

  • If its construct failed, that moduleId publishes load, construct with the error, then destroy.
  • If its start failed, a successful construct comes between load and destroy.
  • If its load failed, that moduleId publishes the failed load span and nothing else. The replacement never left the loaded state, so the destroy that follows does nothing and, by the rule above, publishes nothing.

In every case, nothing runs under that ID until something reloads it.

A failed load also emits the older ModuleDestroyed event. When the loader throws, the antelopejs.module.load span reports the error. The core also emits its Events.ModuleDestroyed event for that module, for consumers written before these channels existed.

Subscribers run synchronously, in the lifecycle path. Node calls subscribers on the call stack that publishes the event, so slow work in a subscriber slows module startup. A subscriber that throws does not change the transition. Node catches the throw and raises it again on a later tick as an uncaught exception, which by default ends the process and does not name the span it came from. The core does not guard against this. Keep subscribers short, and catch your own errors.

Project lifecycle events

Two events of @antelopejs/interface-core/modules tell a module when the project as a whole can serve requests. They are EventProxy events without arguments, not diagnostics channels. The HTTP module uses them to answer 503 until every module has started.

EventEmitted
Events.ModulesStartedOnce every module of a start has resolved its start. This is the whole project at launch, or a module that was reloaded or started later.
Events.ModulesStoppingOnce per start, before the project's modules stop: at shutdown, or when a failed start tears the project down. It is emitted even while a start is still running, so work that start awaits can stop. Stopping, reloading, or removing one module does not emit it. The core calls each started module's drain right after it.

The core emits no ModulesStarted when a module fails to start, or when the project began stopping before every start resolved, so a gate that waits for ModulesStarted stays closed. A module that fails to start at launch leaves the project answering 503. A module that fails to start later, after a reload for example, leaves the gate open.

typescript
import { Events } from "@antelopejs/interface-core/modules";

declare function open(): void;
declare function close(): void;

Events.ModulesStarted.register(() => open());
Events.ModulesStopping.register(() => close());

The core unregisters a module's handlers along with the module, as for any EventProxy registration.

The core does not wait for these handlers. To make the shutdown wait for the work in flight, a module exports drain.

start means "ready to serve". In start, a module awaits only what it needs to answer requests, and launches background work without awaiting it. The core logs a warning that names a module whose start has not settled after 10 s, and fails that module after 120 s. The antelopejs.module.start span measures every start.

Mapping spans to a tracing SDK

The core has no OpenTelemetry integration. It publishes plain diagnostics channel events and depends on no tracing SDK. The consumer maps these spans onto an OpenTelemetry tracer, which means creating spans, propagating context, and naming attributes. That mapping is a small adapter over the events on this page.