Principle:Truera Trulens Application Recording
| Knowledge Sources | |
|---|---|
| Domains | Observability, Tracing |
| Last Updated | 2026-02-14 08:00 GMT |
Overview
A context-manager-based recording pattern that defines trace boundaries for instrumented application invocations and collects execution spans.
Description
Application Recording uses Python's context manager protocol to establish and manage recording sessions. When an instrumented application wrapper (TruChain, TruApp, TruGraph) is used as a context manager, it:
- Enters: Sets up OpenTelemetry baggage/context for span collection
- During: All instrumented method calls within the block produce OTEL spans
- Exits: Finalizes the recording, persists traces, and triggers feedback evaluation
This pattern provides explicit boundaries for what constitutes a single "record" — one user interaction with the application. Without explicit recording boundaries, it would be impossible to group related spans together or know when to trigger evaluation.
Usage
Use this principle whenever invoking an instrumented application. The context manager pattern is the standard way to record application traces in TruLens. Each with block produces one Recording object containing one or more Records, each with a unique record ID.
Theoretical Basis
Application Recording implements the Scope Pattern from distributed tracing. The context manager defines a logical scope within which all operations are attributed to a single trace/record.
Pseudo-code Logic:
# Abstract recording pattern
with instrumented_app as recording:
# All calls here are traced
set_otel_baggage("__trulens_recording__", True)
result = app.invoke(user_input)
# Spans collected automatically
# After exit:
# 1. Traces persisted to database
# 2. Feedback evaluation triggered (sync or deferred)
# 3. Recording object contains record IDs
records = recording.records # Access individual records
The RAII (Resource Acquisition Is Initialization) principle ensures recording resources are properly cleaned up even if exceptions occur within the block.