Public documentation
Documentation indexRead as MarkdownJava
Languages · Shared configuration · Verification
This example uses JDK 17 or later, Maven, and OpenTelemetry Java 1.66.0. The upstream Java SDK lists traces, metrics, and logs as Stable. The sample configures tracing only and uses SDK autoconfiguration to read exporter environment settings; SDK autoconfiguration does not itself instrument application libraries.
Install and configure
Create pom.xml in a new directory:
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>example</groupId><artifactId>corridora-java</artifactId><version>1</version>
<dependencies>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk-extension-autoconfigure</artifactId>
<version>1.66.0</version>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-exporter-otlp</artifactId>
<version>1.66.0</version>
</dependency>
</dependencies>
</project>
Retrieve the dependencies and configure the local Collector:
mvn dependency:copy-dependencies -DoutputDirectory=lib
export OTEL_SERVICE_NAME='corridora-java'
export OTEL_RESOURCE_ATTRIBUTES='service.version=example-1,deployment.environment.name=development'
export OTEL_TRACES_EXPORTER='otlp'
export OTEL_METRICS_EXPORTER='none'
export OTEL_LOGS_EXPORTER='none'
export OTEL_EXPORTER_OTLP_PROTOCOL='http/protobuf'
export OTEL_EXPORTER_OTLP_ENDPOINT='http://127.0.0.1:4318'
unset OTEL_EXPORTER_OTLP_TRACES_ENDPOINT
unset OTEL_EXPORTER_OTLP_HEADERS OTEL_EXPORTER_OTLP_TRACES_HEADERS
Initialize and emit
Save as Example.java:
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.Scope;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
public class Example {
public static void main(String[] args) throws InterruptedException {
try (OpenTelemetrySdk sdk = AutoConfiguredOpenTelemetrySdk.initialize()
.getOpenTelemetrySdk()) {
Tracer tracer = sdk.getTracer("corridora.examples.java");
Span span = tracer.spanBuilder("example.calculate").startSpan();
try (Scope scope = span.makeCurrent()) {
span.setAttribute("example.language", "java");
Thread.sleep(50);
System.out.println("trace_id=" + span.getSpanContext().getTraceId());
} finally {
span.end();
}
}
}
}
Compile and run on a Unix-like shell:
javac -cp 'lib/*' Example.java
java -cp '.:lib/*' Example
Windows uses ; instead of : between classpath entries. Closing the SDK shuts down its providers and gives queued spans an opportunity to export. A real service should retain its SDK for the process lifetime and close it after active requests finish.
Automatic instrumentation and correlation
The OpenTelemetry Java agent instruments supported HTTP frameworks, JDBC clients, and other libraries without adding their instrumentation code to your application. Supply a reviewed agent JAR in your deployment and add -javaagent:/absolute/path/opentelemetry-javaagent.jar to the JVM startup. Configure its OTLP protocol and endpoint explicitly with the same environment settings above. Agent and SDK version numbers are separate release series.
When the agent owns the SDK, manual application spans should obtain a tracer from GlobalOpenTelemetry; do not also run the initialization in this sample. The agent's supported logging integrations can associate logs with active spans, subject to the specific logging library and enabled export configuration.
makeCurrent() establishes scope on the current thread. Close the scope even on failure and end the span separately. For custom executors or messaging, propagate OpenTelemetry Context and W3C headers explicitly; thread-local state does not automatically cover every asynchronous boundary. System.out.println is not an exported log.
Add signals, export directly, and verify
To add metrics with autoconfiguration, enable OTEL_METRICS_EXPORTER=otlp, obtain a meter from the same SDK, and record instruments. To add logs, enable OTEL_LOGS_EXPORTER=otlp and connect a supported Logback/Log4j appender or other bridge. Enabling a logs exporter alone does not redirect stdout or an arbitrary logging framework.
For direct export, replace the base endpoint with Corridora's HTTPS ingest URL and set OTEL_EXPORTER_OTLP_HEADERS="X-CORRIDORA-KEY=${CORRIDORA_INGEST_KEY}" after following shared configuration. These variables are supported because the sample explicitly uses the autoconfiguration module; a manually built exporter needs corresponding builder settings.
In Corridora, find service corridora-java, the printed trace ID, and example.calculate. Check stderr for export failures, and distinguish a missing agent-generated span from a missing manual sample. Follow verification for the Collector and UI checks. Version and API references were checked against upstream Java 1.66.0 on 2026-09-28.