mjdi docsv0.10.0latest
Lifecycle tracing (P1-15)
Container[registry, traced=True] (and the scope it creates —
Source: mjdi/docs/lifecycle-tracing.md
Container[registry, traced=True] (and the scope it creates —
create_scope propagates the configuration) records a bounded,
ordered event log of everything mjdi does while resolving,
caching, and releasing instances. Tracing is disabled by default;
the recording calls compile away entirely in the default
specialization, so the untraced container has zero tracing cost.
var app = Container[type_of(reg), True]()
app.set_value[Cfg](Cfg("db"))
_ = app.resolve[UserService]()
_ = app.resolve[UserService]()
print(app.trace_report())
print(app.trace_count()) # number of events
print(app.trace_event(0)) # one formatted event
print(app.trace_overflowed()) # were events dropped?
The event vocabulary (deterministic order)
| Event | Fires when |
|---|---|
value-set: T | set_value configures a slot |
construct: T | a service construction starts (depth-first entry order) |
post-construct: T | a lifecycle hook finished (dependency-completion order) |
cache-miss: T | a cache lookup found the slot empty |
cache-insert: T | the canonical instance was stored |
cache-hit: T | a copy of a cached canonical instance was handed out |
factory-run: T | a factory provider executed |
scope-create | create_scope opened a scope |
pre-destruct: T | a cached canonical instance is being released |
scope-close / container-close | the scope/container released its caches |
linear-destroy: T | a linear service was destroyed (after its single use, or on the error path) |
Events are recorded in the order the operations happen, so the log is
deterministic for a deterministic program — the construction-ordering
contract (see Construction ordering) is
defined in terms of this sequence and pinned by
tests/test_construction_order.mojo.
The log is bounded
TraceLog keeps at most 256 events (raise the capacity through the
TraceLog[N] parameter if you trace longer programs); once full it
stops recording and sets the overflow flag — trace_overflowed()
reports the truncation.
Reading events programmatically
TraceEvent (kind / service / detail) is a plain value type; the log
supports record, count, event(i), format(), reset(). Pull
model: mjdi fills the log, application or test code reads it. A
push-model callback/observer interface is not provided — the
supported compiler cannot store capturing closures or stateful function
values inside comptime-parameterized containers safely; this is a
documented limitation (see Known limitations).
No logging dependency is introduced: the log is plain mjdi data.
Scope traces
Scopes record their own event streams (resolution inside the scope,
scope closure); create_scope records on the container's log.
Related
- Cache statistics — the counter-based sibling.
- Construction ordering — the ordering contract the trace proves.
- Testing —
assert_trace_count/assert_trace_last.