mjdi

Search documentation

Search the mjdi v0.10.0 documentation

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)

EventFires when
value-set: Tset_value configures a slot
construct: Ta service construction starts (depth-first entry order)
post-construct: Ta lifecycle hook finished (dependency-completion order)
cache-miss: Ta cache lookup found the slot empty
cache-insert: Tthe canonical instance was stored
cache-hit: Ta copy of a cached canonical instance was handed out
factory-run: Ta factory provider executed
scope-createcreate_scope opened a scope
pre-destruct: Ta cached canonical instance is being released
scope-close / container-closethe scope/container released its caches
linear-destroy: Ta 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.