Skip to content

Commit 4784045

Browse files
authored
Add UserExcludedClassloadersConfigurer (#10134)
1 parent 3dd0925 commit 4784045

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

docs/advanced-configuration-options.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ which could have unknown side-effects.
1818
| ------------------------------ | ------------------------------ | -------------------------------------------------------------------------------------------------- |
1919
| otel.javaagent.exclude-classes | OTEL_JAVAAGENT_EXCLUDE_CLASSES | Suppresses all instrumentation for specific classes, format is "my.package.MyClass,my.package2.\*" |
2020

21+
## Excluding specific classes loaders
22+
23+
This option can be used to exclude classes loaded by given class loaders from being instrumented.
24+
25+
| System property | Environment variable | Purpose |
26+
|--------------------------------------|--------------------------------------|---------------------------------------------------------------------------------|
27+
| otel.javaagent.exclude-class-loaders | OTEL_JAVAAGENT_EXCLUDE_CLASS_LOADERS | Ignore the specified class loaders, format is "my.package.MyClass,my.package2." |
28+
2129
## Running application with security manager
2230

2331
This option can be used to let agent run with all privileges without being affected by security policy restricting some operations.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.javaagent.tooling.ignore;
7+
8+
import static java.util.Collections.emptyList;
9+
10+
import com.google.auto.service.AutoService;
11+
import io.opentelemetry.javaagent.extension.ignore.IgnoredTypesBuilder;
12+
import io.opentelemetry.javaagent.extension.ignore.IgnoredTypesConfigurer;
13+
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
14+
import java.util.List;
15+
16+
@AutoService(IgnoredTypesConfigurer.class)
17+
public class UserExcludedClassLoadersConfigurer implements IgnoredTypesConfigurer {
18+
19+
// visible for tests
20+
static final String EXCLUDED_CLASS_LOADERS_CONFIG = "otel.javaagent.exclude-class-loaders";
21+
22+
@Override
23+
public void configure(IgnoredTypesBuilder builder, ConfigProperties config) {
24+
List<String> excludedClassLoaders = config.getList(EXCLUDED_CLASS_LOADERS_CONFIG, emptyList());
25+
for (String excludedClassLoader : excludedClassLoaders) {
26+
excludedClassLoader = excludedClassLoader.trim();
27+
// remove the trailing *
28+
if (excludedClassLoader.endsWith("*")) {
29+
excludedClassLoader = excludedClassLoader.substring(0, excludedClassLoader.length() - 1);
30+
}
31+
builder.ignoreClassLoader(excludedClassLoader);
32+
}
33+
}
34+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.javaagent.tooling.ignore;
7+
8+
import static io.opentelemetry.javaagent.tooling.ignore.UserExcludedClassLoadersConfigurer.EXCLUDED_CLASS_LOADERS_CONFIG;
9+
import static java.util.Arrays.asList;
10+
import static java.util.Collections.emptyList;
11+
import static org.mockito.Mockito.verify;
12+
import static org.mockito.Mockito.verifyNoInteractions;
13+
import static org.mockito.Mockito.when;
14+
15+
import io.opentelemetry.javaagent.extension.ignore.IgnoredTypesBuilder;
16+
import io.opentelemetry.javaagent.extension.ignore.IgnoredTypesConfigurer;
17+
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
18+
import org.junit.jupiter.api.Test;
19+
import org.junit.jupiter.api.extension.ExtendWith;
20+
import org.mockito.Mock;
21+
import org.mockito.junit.jupiter.MockitoExtension;
22+
23+
@ExtendWith(MockitoExtension.class)
24+
class UserExcludedClassLoadersConfigurerTest {
25+
26+
@Mock ConfigProperties config;
27+
@Mock IgnoredTypesBuilder builder;
28+
29+
IgnoredTypesConfigurer underTest = new UserExcludedClassLoadersConfigurer();
30+
31+
@Test
32+
void shouldAddNothingToBuilderWhenPropertyIsEmpty() {
33+
// when
34+
underTest.configure(builder, config);
35+
36+
// then
37+
verifyNoInteractions(builder);
38+
}
39+
40+
@Test
41+
void shouldIgnoreClassesAndPackages() {
42+
// given
43+
when(config.getList(EXCLUDED_CLASS_LOADERS_CONFIG, emptyList()))
44+
.thenReturn(
45+
asList("com.example.IgnoredClass", "com.example.ignored.*", "com.another_ignore"));
46+
47+
// when
48+
underTest.configure(builder, config);
49+
50+
// then
51+
verify(builder).ignoreClassLoader("com.example.IgnoredClass");
52+
verify(builder).ignoreClassLoader("com.example.ignored.");
53+
verify(builder).ignoreClassLoader("com.another_ignore");
54+
}
55+
}

0 commit comments

Comments
 (0)