Skip to main content
This guide demonstrates how to instrument your Java applications with OpenTelemetry and send traces to the TrueFoundry backend. We’ll cover adding the required dependencies, initializing the tracer, automatic HTTP instrumentation, adding custom attributes and spans, and configuring sampling and debugging options. By the end, you’ll have a clear blueprint for integrating OpenTelemetry tracing into your Java services using Spring Boot.

Installation

To start, add the OpenTelemetry SDK and necessary instrumentation dependencies to your project. If you’re using Maven, add these to your pom.xml:
If you’re using Gradle, add these to your build.gradle:

Initializing OpenTelemetry

Next, initialize the OpenTelemetry SDK in your Spring Boot application. Create a configuration class to set up the Tracer Provider and OTLP exporter to send traces to TrueFoundry Backend.

Automatic HTTP Instrumentation

Now that the OpenTelemetry SDK is set up, let’s instrument the HTTP server to automatically trace incoming requests. Spring Boot with OpenTelemetry automatically instruments HTTP requests when properly configured. Add this to your application.properties or application.yml:
At this point, all incoming HTTP requests are being traced automatically.

Context propagation for outgoing requests

To fully benefit from distributed tracing, you should also propagate trace context in your outgoing HTTP requests. This helps downstream services recognize that their requests are part of a larger distributed trace. Configure your HTTP client to automatically inject trace context into outgoing requests using OpenTelemetry’s instrumentation:
This functionality is not incorporated into the Complete Application example to reduce complexity

Adding Attributes to Spans

Automatic instrumentation captures basic request information, but you can add custom data to your traces using attributes. Attributes are key-value pairs that provide additional context about your operations. For example, in order service, you might add order.id to make traces more useful.

Creating Custom Spans

Automatic instrumentation captures HTTP requests and external calls, but it doesn’t track your application’s internal logic. For important operations, you can manually create spans to trace specific parts of your code. A span represents a unit of work, and creating sub-spans helps you see detailed timing and context for key processes. For example, if a request triggers a complex function or external call that isn’t automatically captured, you can create a span to trace that specific operation. Manual instrumentation fills these gaps by letting you track what happens inside your application, not just at the edges.

Complete Application Example

Below is a comprehensive example that demonstrates all the OpenTelemetry concepts we’ve covered. This application creates a Spring Boot order service that sets up OpenTelemetry tracing with proper configuration, automatically instruments HTTP requests, creates custom spans for database operations, and adds custom attributes to provide order-specific context.

Advanced Configuration

Sampling

Tracing sampling is a crucial technique for managing the volume of trace data in production environments. By default, OpenTelemetry Java traces every request, which works well for debugging or development but can become expensive and noisy in high-traffic production systems. Sampling helps in several ways: it reduces noise in traces, helping you focus on important traces while maintaining visibility into your system. It also helps with cost management in terms of storage, processing, and network bandwidth, making it essential for production deployments.

Sampling Strategies

OpenTelemetry supports several built-in samplers, but in practice, two cover most use cases: 1. TraceIdRatioBased Sampler Samples a fixed percentage of root traces. This sampler makes sampling decisions independently for each trace.
Pros: Simple to configure, predictable sampling rate, deterministic behavior Cons: May create partial traces if child spans are sampled differently, especially when spans are spread across multiple microservices or services with different sampling configurations. 2. ParentBased Sampler (Recommended) Samples a fixed percentage of root traces and ensures that child spans follow the parent’s sampling decision, maintaining complete trace integrity.
Pros: Maintains trace integrity, prevents partial traces, ensures complete trace visibility when sampled Cons: Slightly more complex configuration, but worth the additional setup for production environments

Troubleshooting

Partial Traces If you see partial traces (missing spans in the middle of a trace), ensure you’re using ParentBased sampler:
Too Much Data If you’re still collecting too much data or experiencing high costs, reduce the sampling rate:
Too Little Data If you need more visibility for debugging or monitoring, increase the sampling rate:

Local Debugging

For local development and debugging, you can use the console exporter to see traces in your terminal:

Using Instrumentation Libraries

OpenTelemetry provides instrumentation libraries for many popular frameworks and libraries in the Java ecosystem. These are drop-in packages that automatically generate spans and metrics for operations in those libraries, so you don’t have to instrument everything manually. When you are using third-party libraries or frameworks, you should take advantage of these to save time and ensure consistency. For Spring Boot applications, the io.micrometer:micrometer-tracing-bridge-otel dependency provides automatic instrumentation for HTTP requests, database operations, and other common Spring Boot components. This bridge connects Micrometer’s tracing capabilities with OpenTelemetry, enabling seamless integration with the OpenTelemetry ecosystem. OpenTelemetry’s Java Instrumentation repository contains many such instrumentation packages for popular Java libraries and frameworks.