Principle:Truera Trulens Custom App Wrapping
| Knowledge Sources | |
|---|---|
| Domains | Observability, LLM_Evaluation |
| Last Updated | 2026-02-14 08:00 GMT |
Overview
An instrumentation pattern that wraps arbitrary Python classes for tracing and evaluation by discovering and recording methods decorated with @instrument.
Description
Custom App Wrapping provides the most flexible instrumentation option in TruLens. Unlike framework-specific wrappers (TruChain for LangChain, TruGraph for LangGraph), the custom wrapper works with any Python class. It discovers methods decorated with @instrument and automatically instruments them for OTEL tracing.
The wrapper handles:
- Method discovery: Finds all @instrument-decorated methods on the application
- Main method detection: Identifies the entry point (decorated with RECORD_ROOT or explicitly specified)
- Recording management: Context manager protocol for trace boundary definition
- Feedback evaluation: Configured feedback functions run on collected traces
This principle enables evaluation of custom RAG pipelines, agents, or any application architecture not natively supported by TruLens framework wrappers.
Usage
Use this principle when your application is a custom Python class (not a LangChain chain or LangGraph graph). First decorate application methods with @instrument, then wrap the instance with TruApp. The wrapper requires at least one instrumented method to function.
Theoretical Basis
Custom App Wrapping combines the Adapter Pattern (wrapping arbitrary objects into a standard interface) with Reflection (discovering instrumented methods through decorator flags).
Pseudo-code Logic:
# Abstract custom app wrapping
class MyApp:
@instrument(span_type=SpanType.RETRIEVAL)
def retrieve(self, query): ...
@instrument(span_type=SpanType.GENERATION)
def generate(self, context): ...
# Wrap for recording and evaluation
wrapped = TruApp(
app=MyApp(),
main_method=my_app.generate,
feedbacks=[metric_1, metric_2]
)