What Gets Instrumented?
Typical examples of automatic instrumentation include:- Inbound requests (e.g., HTTP servers)
- Outbound calls (e.g., HTTP clients, gRPC)
- Database access (e.g., JDBC, R2DBC)
- Message queues and other integrations
otel4s Java Agent
The otel4s-opentelemetry-java distribution is a variant of the official OpenTelemetry Java agent. It provides all the same automatic instrumentation as the upstream agent, but with two important additions:- Cats Effect instrumentation - Fiber-based applications gain proper tracing and context propagation
- otel4s integration - Keeps agent and otel4s context in sync
Do I Need an Agent?
The agent is best suited for:- Getting started quickly - Enable observability without writing code
- Standard service edges - HTTP, databases, gRPC, messaging are automatically instrumented
- You need fine-grained control over spans, attributes, and metrics
- You want to instrument domain-specific operations (e.g., business logic)
- You’re running in complex or performance-sensitive environments where bytecode manipulation could cause issues
In practice, many teams combine both approaches:
- Use the agent for broad, zero-effort coverage
- Add otel4s manual instrumentation where business-level observability is needed
Installation
The agent can be configured via the sbt-javaagent plugin:- Enable JavaAgent sbt plugin
- Register
otel4s-opentelemetry-javaagentas a Java agent - Make sure the VM will be forked when running
- Enable Cats Effect fiber context tracking
- Add all necessary dependencies
Configuration
The agent supports the full set of official configuration options.Environment Variables
Disabling Instrumentation
You can disable or suppress specific instrumentation if certain libraries or frameworks should not be traced. See Disabling Instrumentation for details. This is especially useful when:- A library is already manually instrumented with otel4s
- Automatic instrumentation causes performance or compatibility issues
- You want to reduce telemetry volume from noisy dependencies
Application Setup
The application must be configured to use the agent-provided context:IOLocalContextStorage.localProvider- Automatically picks up agent’s context when availableOtelJava.global[IO]- You must use the global instance because the agent will autoconfigure it
You must use
OtelJava.global[IO] instead of OtelJava.autoConfigured[IO]() when using the agent.Verification
If everything is configured correctly, you’ll see these log messages on startup:How It Works
Cats Effect has its context propagation mechanism known as IOLocal. The 3.6.0 release provides a way to represent IOLocal as a ThreadLocal, which creates an opportunity to manipulate the context from the outside. The agent’s instrumentation works as follows:-
IORuntime Instrumentation - Agent instruments the constructor of
IORuntimeand stores aThreadLocalrepresentation of theIOLocal[Context]in the bootstrap classloader, so the agent and application both access the same instance -
Custom ContextStorage - Instrumentation installs a custom
ContextStoragewrapper that usesFiberLocalContextHelperto retrieve the fiber’s current context (if available) -
IOFiber Instrumentation - Agent instruments
IOFiber’s constructor and starts the fiber with the currently available context
- Automatic context propagation across fiber boundaries
- Synchronization between Java SDK ThreadLocal and Cats Effect IOLocal
- Compatibility with existing Java instrumentation libraries
Complete Example
Limitations
Single Application Per JVM
The agent’s context sharing mechanism assumes a single application per JVM. If you deploy multiple WAR files into the same Tomcat instance, both applications will attempt to configure the agent’s shared bootstrap context, leading to conflicts and unpredictable behavior. See opentelemetry-java-instrumentation#13576 for more information.Experimental Status
The Cats Effect and otel4s instrumentation uses advanced bytecode manipulation techniques that may not work in all environments. Thoroughly test in your specific deployment scenario before using in production.Performance Overhead
Bytecode instrumentation adds runtime overhead. While generally minimal, the impact depends on:- Number of instrumented libraries
- Volume of instrumented operations
- Sampling configuration
Troubleshooting
Enable Debug Logging
Context Not Propagating
Ensure fiber context tracking is enabled:Agent Not Loading
Check that:JavaAgentplugin is enabledrun / fork := trueis set- The agent JAR is in your classpath
-verbose:class to see loaded classes:
Next Steps
Context Propagation
Learn how context propagation works
Configuration
Configure the agent and SDK