Installation
Gradle
Add the following to yourbuild.gradle:
Maven
Add the following to yourpom.xml:
Requirements
- Java 11 or higher
- OpenTelemetry Java 1.49.0 or higher
- LangChain4j 1.0.0 or higher
Quick Start
Automatic Instrumentation
Instrument your LangChain4j application with a single line of code:Manual Instrumentation with Model Listener
For more control, register a model listener directly:OpenTelemetry Setup
Basic Setup with Phoenix
With Authentication (Phoenix Cloud)
Configuration
Custom Trace Configuration
Control what information is captured in traces:With Custom Tracer Provider
Complete Example
Here’s a complete example with tool calling:Captured Trace Data
The instrumentation automatically captures:- LLM Model Information: Model name, provider (OpenAI, etc.)
- Input Messages: User prompts, system messages, conversation history
- Output Messages: Model responses, assistant messages
- Invocation Parameters: Temperature, max tokens, top_p, etc.
- Token Usage: Prompt tokens, completion tokens, total tokens
- Tool Calls: Function names, arguments, and results
- Timing Information: Request latency and duration
- Error Information: Exceptions and error messages
Viewing Traces
Using Phoenix
-
Start Phoenix locally:
- Run your instrumented application
- View traces at http://localhost:6006
Using Other Backends
OpenInference instrumentation works with any OpenTelemetry-compatible backend:- Jaeger: Change the OTLP endpoint to your Jaeger instance
- Zipkin: Use the Zipkin exporter instead of OTLP
- Cloud Providers: AWS X-Ray, Google Cloud Trace, Azure Monitor
Best Practices
- Initialize Once: Call
LangChain4jInstrumentor.instrument()once at application startup - Set Service Name: Always set a meaningful
service.namein your OpenTelemetry resource - Use Batch Processing: Use
BatchSpanProcessorfor better performance in production - Handle Secrets: Never log API keys or sensitive data in traces
- Flush on Shutdown: Call
tracerProvider.forceFlush()before application exit to ensure all spans are sent
Troubleshooting
No traces appearing
- Verify OpenTelemetry is initialized before calling
instrument() - Check that your OTLP endpoint is accessible
- Ensure
forceFlush()is called before application exit - Enable debug logging:
System.setProperty("otel.logs.exporter", "console")
Duplicate instrumentation error
LangChain4jInstrumentor.instrument() once per application lifecycle.