Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,6 @@

public final class AgentConfig {

public static boolean isInstrumentationEnabled(
ConfigProperties config, Iterable<String> instrumentationNames, boolean defaultEnabled) {
for (String name : instrumentationNames) {
String propertyName = "otel.instrumentation." + name + ".enabled";
Boolean enabled = config.getBoolean(propertyName);
if (enabled != null) {
return enabled;
}
}
return defaultEnabled;
}

public static boolean isDebugModeEnabled(ConfigProperties config) {
return config.getBoolean("otel.javaagent.debug", false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.not;

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.api.incubator.config.internal.DeclarativeConfigUtil;
import io.opentelemetry.instrumentation.api.incubator.config.internal.ExtendedDeclarativeConfigProperties;
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule;
Expand All @@ -21,7 +24,6 @@
import io.opentelemetry.javaagent.tooling.TransformSafeLogger;
import io.opentelemetry.javaagent.tooling.Utils;
import io.opentelemetry.javaagent.tooling.bytebuddy.LoggingFailSafeMatcher;
import io.opentelemetry.javaagent.tooling.config.AgentConfig;
import io.opentelemetry.javaagent.tooling.field.VirtualFieldImplementationInstaller;
import io.opentelemetry.javaagent.tooling.field.VirtualFieldImplementationInstallerFactory;
import io.opentelemetry.javaagent.tooling.instrumentation.indy.ClassInjectorImpl;
Expand Down Expand Up @@ -67,8 +69,7 @@ AgentBuilder install(
InstrumentationModule instrumentationModule,
AgentBuilder parentAgentBuilder,
ConfigProperties config) {
if (!AgentConfig.isInstrumentationEnabled(
config,
if (!isInstrumentationEnabled(
instrumentationModule.instrumentationNames(),
instrumentationModule.defaultEnabled(config))) {
logger.log(
Expand Down Expand Up @@ -240,6 +241,20 @@ private AgentBuilder installInjectingModule(
return agentBuilder;
}

static boolean isInstrumentationEnabled(
Iterable<String> instrumentationNames, boolean defaultEnabled) {
ExtendedDeclarativeConfigProperties config =
DeclarativeConfigUtil.get(GlobalOpenTelemetry.get());
for (String name : instrumentationNames) {
String normalizedName = name.replace('-', '_');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

renaming usages to use _ is a later PR?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's needed here?

otherwise you'd need to use

instrumentation/development:
  java:
    apache-httpclient:
      enabled: false

instead of

instrumentation/development:
  java:
    apache_httpclient:
      enabled: false

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's right.
I meant that you could also rename the instrumentation to apache_httpclient - not sure that's really better though

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kind of like that, it would be nice for the "instrumentation name" to match what's in declarative config yaml

Boolean enabled = config.get(normalizedName).getBoolean("enabled");
if (enabled != null) {
return enabled;
}
}
return defaultEnabled;
}

private static AgentBuilder.Identified.Narrowable setTypeMatcher(
AgentBuilder agentBuilder,
InstrumentationModule instrumentationModule,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,32 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.tooling.config;
package io.opentelemetry.javaagent.tooling.instrumentation;

import static java.util.Arrays.asList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.incubator.ExtendedOpenTelemetry;
import io.opentelemetry.api.incubator.config.ConfigProvider;
import io.opentelemetry.instrumentation.config.bridge.ConfigPropertiesBackedDeclarativeConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import java.util.TreeSet;
import java.util.stream.Stream;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

class AgentConfigTest {
class InstrumentationModuleInstallerTest {

@AfterEach
void tearDown() {
GlobalOpenTelemetry.resetForTest();
}

@ParameterizedTest(name = "isInstrumentationEnabled({0}) = {4}")
@MethodSource("instrumentationEnabledParams")
Expand All @@ -32,10 +43,13 @@ void testIsInstrumentationEnabled(
when(config.getBoolean("otel.instrumentation.first.enabled")).thenReturn(firstEnabled);
when(config.getBoolean("otel.instrumentation.second.enabled")).thenReturn(secondEnabled);

assertEquals(
expected,
AgentConfig.isInstrumentationEnabled(
config, new TreeSet<>(asList("first", "second")), defaultEnabled));
OpenTelemetry openTelemetry = createOpenTelemetry(config);
GlobalOpenTelemetry.set(openTelemetry);

assertThat(
InstrumentationModuleInstaller.isInstrumentationEnabled(
new TreeSet<>(asList("first", "second")), defaultEnabled))
.isEqualTo(expected);
}

private static Stream<Arguments> instrumentationEnabledParams() {
Expand Down Expand Up @@ -77,4 +91,14 @@ private static Stream<Arguments> instrumentationEnabledParams() {
true),
Arguments.of("disabled by default", null, null, false, false));
}

private static OpenTelemetry createOpenTelemetry(ConfigProperties config) {
ExtendedOpenTelemetry otel = mock(ExtendedOpenTelemetry.class);
ConfigProvider configProvider = mock(ConfigProvider.class);
when(otel.getConfigProvider()).thenReturn(configProvider);
when(configProvider.getInstrumentationConfig())
.thenReturn(
ConfigPropertiesBackedDeclarativeConfigProperties.createInstrumentationConfig(config));
return otel;
}
}
Loading