Implementation:Truera Trulens App Context Manager
| Knowledge Sources | |
|---|---|
| Domains | Observability, Tracing |
| Last Updated | 2026-02-14 08:00 GMT |
Overview
Concrete tool for establishing recording boundaries around instrumented application invocations using Python context managers, provided by the trulens-core library.
Description
The App.__enter__ and App.__exit__ methods implement Python's context manager protocol for TruLens application wrappers (TruChain, TruApp, TruGraph). When used with a with statement, they establish a recording session that collects OTEL spans from all instrumented method calls within the block.
Under OTEL tracing mode, __enter__ creates an OtelRecordingContext that sets OpenTelemetry baggage to enable span collection. __exit__ finalizes the recording, persists traces, and returns a Recording object containing the collected records.
This is a Pattern Doc -- it documents the context manager pattern users apply across all TruLens app wrapper types.
Usage
Use the context manager pattern whenever invoking an instrumented application. This is the standard recording mechanism in TruLens. Each with block produces one Recording with one or more Record objects.
Code Reference
Source Location
- Repository: trulens
- File: src/core/trulens/core/app.py
- Lines: L1146-1197 (__enter__ L1146-1175, __exit__ L1178-1197)
Signature
class App:
def __enter__(self) -> Recording:
"""Enter recording context.
Under OTEL tracing: Creates OtelRecordingContext with app metadata
and sets baggage for span collection.
Returns:
Recording (or OtelRecordingContext) object for accessing records.
"""
def __exit__(self, exc_type, exc_value, exc_tb) -> None:
"""Exit recording context.
Finalizes the recording, resets context, and triggers
feedback evaluation if configured.
"""
Import
# No separate import needed - used via app wrappers:
from trulens.apps.langchain import TruChain
from trulens.apps.app import TruApp
from trulens.apps.langgraph import TruGraph
# Usage pattern:
with tru_app as recording:
result = app.invoke(query)
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| (implicit) | App wrapper | Yes | The TruChain/TruApp/TruGraph instance used as context manager |
Outputs
| Name | Type | Description |
|---|---|---|
| recording | Recording | Object containing records collected during the with block |
| recording.records | List[Record] | Individual records with record_id, main_input, main_output |
Usage Examples
Standard Recording Pattern
from trulens.apps.langchain import TruChain
tru_recorder = TruChain(chain, app_name="MyApp", feedbacks=[f1, f2])
# Record a single invocation
with tru_recorder as recording:
result = chain.invoke("What is TruLens?")
# Access the record
record = recording.get()
print(f"Input: {record.main_input}")
print(f"Output: {record.main_output}")
print(f"Record ID: {record.record_id}")
Multiple Invocations in One Recording
with tru_recorder as recording:
result1 = chain.invoke("Question 1")
result2 = chain.invoke("Question 2")
# Access individual records
print(f"Records collected: {len(recording)}")
record1 = recording[0]
record2 = recording[1]