Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java.lang.NoClassDefFoundError: datadog/opentelemetry/shim/trace/OtelSpanEvent #8017

Open
krosen040 opened this issue Nov 26, 2024 · 3 comments
Assignees
Labels
inst: opentelemetry OpenTelemetry instrumentation type: bug
Milestone

Comments

@krosen040
Copy link

krosen040 commented Nov 26, 2024

Description

When running an application with the Java agent and support for OpenTelemetry enabled (dd.trace.otel.enabled=true) and calling addEvent or recordException on an io.opentelemetry.api.trace.Span (support for this added in #7408), in certain cases it fails with the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: datadog/opentelemetry/shim/trace/OtelSpanEvent
        at datadog.opentelemetry.shim.trace.OtelSpan.addEvent(OtelSpan.java:85)
        at io.opentelemetry.api.trace.Span.addEvent(Span.java:198)
        at org.example.App.thisThrows(App.java:18)
        at org.example.App.main(App.java:11)
Caused by: java.lang.ClassNotFoundException: datadog.opentelemetry.shim.trace.OtelSpanEvent
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:525)
        ... 4 more

Tested using the following versions:

  • Java:
$ java -version
openjdk version "17.0.8" 2023-07-18 LTS
OpenJDK Runtime Environment Zulu17.44+15-CA (build 17.0.8+7-LTS)
OpenJDK 64-Bit Server VM Zulu17.44+15-CA (build 17.0.8+7-LTS, mixed mode, sharing)
  • dd-trace-java: 1.43.0

Example

A minimal example that reproduces the error can be found below.

In the example, I use @WithSpan because it's an easy way to make the Java agent create a span (so that Span.current() doesn't return a no-op span), however we're seeing the same problem in other scenarios as well.

It should be noted that if thisThrows is called after thisWorks, no exception is thrown.

App.java

package org.example;

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.Scope;
import io.opentelemetry.instrumentation.annotations.WithSpan;

public class App {
    public static void main(String[] args) {
        thisThrows();
        thisWorks();
    }

    @WithSpan
    static void thisThrows() {
        Span span = Span.current();
        span.addEvent("test");
    }

    static void thisWorks() {
        Tracer tracer = GlobalOpenTelemetry.getTracer("scope");
        Span span = tracer.spanBuilder("span").startSpan();

        try (Scope scope = span.makeCurrent()) {
            span.addEvent("test");
        } finally {
            span.end();
        }
    }
}

build.gradle

plugins {
    id 'application'
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'io.opentelemetry:opentelemetry-api:1.44.1'
    implementation 'io.opentelemetry.instrumentation:opentelemetry-instrumentation-annotations:2.10.0'
}

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(17)
    }
}

application {
    mainClass = 'org.example.App'
    applicationDefaultJvmArgs = ["-javaagent:../dd-java-agent-1.43.0.jar", "-Ddd.trace.otel.enabled=true", "-Ddd.writer.type=LoggingWriter"]
}
@PerfectSlayer PerfectSlayer added type: bug inst: opentelemetry OpenTelemetry instrumentation labels Nov 26, 2024
@mcculls mcculls self-assigned this Nov 26, 2024
@mcculls
Copy link
Contributor

mcculls commented Nov 26, 2024

Thanks for the report @krosen040 - I've confirmed the issue locally and we'll fix it for the next release

@mcculls
Copy link
Contributor

mcculls commented Nov 26, 2024

The issue is that for the @WithSpan example only OpenTelemetryContextStorageInstrumentation is getting applied - and that doesn't currently inject OtelSpanEvent, which was an oversight.

As a workaround you can add this to your main application class:

    static {
        GlobalOpenTelemetry.get();
    }

This will trigger injection of OtelSpanEvent via the main OpenTelemetryInstrumentation

@mcculls
Copy link
Contributor

mcculls commented Nov 26, 2024

This will be fixed in v1.44.0 by #8019

@mcculls mcculls added this to the 1.44.0 milestone Nov 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
inst: opentelemetry OpenTelemetry instrumentation type: bug
Projects
None yet
Development

No branches or pull requests

3 participants