Skip to content

Commit 77fdd7c

Browse files
committed
Move InstrumentationModuleInstaller creation from field initializer to extend()
InstrumentationLoader is instantiated by ServiceLoader (no-arg constructor), at which point InstrumentationHolder.getInstrumentation() is still null. Moving the requireNonNull + construction into extend() defers it to when Instrumentation is guaranteed to be available, fixing the NPE. Signed-off-by: Gregor Zeitlinger <gregor.zeitlinger@grafana.com>
1 parent 6a84aef commit 77fdd7c

1 file changed

Lines changed: 6 additions & 5 deletions

File tree

javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/instrumentation/InstrumentationLoader.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,21 @@
1616
import io.opentelemetry.javaagent.tooling.AgentExtension;
1717
import io.opentelemetry.javaagent.tooling.Utils;
1818
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
19+
import java.lang.instrument.Instrumentation;
1920
import java.util.logging.Logger;
2021
import net.bytebuddy.agent.builder.AgentBuilder;
2122

2223
@AutoService(AgentExtension.class)
2324
public class InstrumentationLoader implements AgentExtension {
2425
private static final Logger logger = Logger.getLogger(InstrumentationLoader.class.getName());
2526

26-
private final InstrumentationModuleInstaller instrumentationModuleInstaller =
27-
new InstrumentationModuleInstaller(
28-
requireNonNull(
29-
InstrumentationHolder.getInstrumentation(), "Instrumentation must not be null"));
30-
3127
@Override
3228
public AgentBuilder extend(AgentBuilder agentBuilder, ConfigProperties config) {
29+
Instrumentation instrumentation =
30+
requireNonNull(
31+
InstrumentationHolder.getInstrumentation(), "Instrumentation must not be null");
32+
InstrumentationModuleInstaller instrumentationModuleInstaller =
33+
new InstrumentationModuleInstaller(instrumentation);
3334
int numberOfLoadedModules = 0;
3435
ClassLoader extensionsClassLoader =
3536
requireNonNull(

0 commit comments

Comments
 (0)