Required dependencies

Add the following dependencies to your project:

<project>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>io.opentelemetry</groupId>
                <artifactId>opentelemetry-bom</artifactId>
                <version>1.35.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>io.opentelemetry</groupId>
            <artifactId>opentelemetry-api</artifactId>
        </dependency>
    </dependencies>
</project>

Creating Spans

To create a span, use the Tracer interface. The Tracer interface is the main entry point for the OpenTelemetry API. It allows you to create spans and manage the current span. Acquire a Tracer instance using the OpenTelemetry class:

import io.opentelemetry.api.GlobalOpenTelemetry;

Tracer tracer = GlobalOpenTelemetry.getTracer("instrumentation-scope-name", "instrumentation-scope-version");

Now you can create a span using the Tracer instance:

Span span = tracer.spanBuilder("span-name").startSpan();
try (Scope scope = span.makeCurrent()) {
    // Your code goes here.
} finally {
    span.end();
}

Additional information

For more use cases, see the OpenTelemetry Java API documentation.