Skip to content

Use prefix trie for proxy ignores #8678

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

Merged
merged 4 commits into from
Apr 29, 2025
Merged
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
@@ -0,0 +1,86 @@
package datadog.trace.agent.tooling;

import datadog.trace.agent.tooling.bytebuddy.matcher.ProxyIgnoredClassNameTrie;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.infra.Blackhole;

/*
Benchmark (className) Mode Cnt Score Error Units
ProxyIgnoreBenchmark.useContains org.springframework.util.ConcurrentReferenceHashMap$4 thrpt 2 0.007 ops/ns
ProxyIgnoreBenchmark.useContains java.lang.invoke.LambdaForm$DMH/0x00007fe9f0388000 thrpt 2 0.008 ops/ns
ProxyIgnoreBenchmark.useContains org.springframework.core.annotation.RepeatableContainers$StandardRepeatableContainers$$Lambda$315/0x00007fe9f03adc70 thrpt 2 0.003 ops/ns
ProxyIgnoreBenchmark.useContains datadog.test.package.redis.RedisTemplateProvider$$TestCGLIB$$FastClass$$0 thrpt 2 0.025 ops/ns
ProxyIgnoreBenchmark.useTrie org.springframework.util.ConcurrentReferenceHashMap$4 thrpt 2 0.026 ops/ns
ProxyIgnoreBenchmark.useTrie java.lang.invoke.LambdaForm$DMH/0x00007fe9f0388000 thrpt 2 0.026 ops/ns
ProxyIgnoreBenchmark.useTrie org.springframework.core.annotation.RepeatableContainers$StandardRepeatableContainers$$Lambda$315/0x00007fe9f03adc70 thrpt 2 0.009 ops/ns
ProxyIgnoreBenchmark.useTrie datadog.test.package.redis.RedisTemplateProvider$$TestCGLIB$$FastClass$$0 thrpt 2 0.029 ops/ns
*/
@State(Scope.Benchmark)
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Measurement(iterations = 2, time = 5, timeUnit = TimeUnit.SECONDS)
@Warmup(iterations = 1)
@Fork(value = 1)
public class ProxyIgnoreBenchmark {
@Param({
"org.springframework.util.ConcurrentReferenceHashMap$4",
"java.lang.invoke.LambdaForm$DMH/0x00007fe9f0388000",
"org.springframework.core.annotation.RepeatableContainers$StandardRepeatableContainers$$Lambda$315/0x00007fe9f03adc70",
// worst case for trie based
"datadog.test.package.redis.RedisTemplateProvider$$TestCGLIB$$FastClass$$0"
})
public String className;

@Benchmark
public void useContains(Blackhole bh) {
if (className.indexOf('$') > -1) {
if (className.contains("$JaxbAccessor")
|| className.contains("CGLIB$$")
|| className.contains("$__sisu")
|| className.contains("$$EnhancerByGuice$$")
|| className.contains("$$EnhancerByProxool$$")
|| className.contains("$$$view")
|| className.contains("$$$endpoint") // jboss mdb proxies
|| className.contains("$$_Weld")
|| className.contains("_$$_jvst")) {
bh.consume(true);
}
}
}

@Benchmark
public void useTrie(Blackhole bh) {
int last = -1;
int idx;
while (true) {
idx = className.indexOf('$', last + 1);
if (idx < 0) {
break;
}
if (last < 0 && className.contains("CGLIB$$")) {
break;
}
if (idx == last + 1) {
// skip the trie if consecutive $$ since, to be efficient, we can match prefixes from the
// first dollar
last = idx;
continue;
}
last = idx;
if (ProxyIgnoredClassNameTrie.apply(className, idx) >= 0) {
return;
}
}
bh.consume(true);
Comment on lines +63 to +84
Copy link
Contributor

Choose a reason for hiding this comment

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

What about calling the isIgnored() method and consume this result instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wanted to have it clearly copied to that specifc bench since thing can change

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ public class ProxyClassIgnores {
private ProxyClassIgnores() {}

public static boolean isIgnored(String name) {
if (name.indexOf('$') > -1) {
if (name.contains("$JaxbAccessor")
|| name.contains("CGLIB$$")
|| name.contains("$__sisu")
|| name.contains("$$EnhancerByGuice$$")
|| name.contains("$$EnhancerByProxool$$")
|| name.contains("$$$view")
|| name.contains("$$$endpoint") // jboss mdb proxies
|| name.contains("$$_Weld")
|| name.contains("_$$_jvst")) {
for (int last = -1, idx; (idx = name.indexOf('$', last + 1)) >= 0; last = idx) {
if (last < 0 && name.contains("CGLIB$$")) {
// check this once
return true;
}
if (idx == last + 1) {
// skip the trie if consecutive $$ since, to be efficient, we can match prefixes from the
// first dollar
continue;
}
if (ProxyIgnoredClassNameTrie.apply(name, idx) == 1) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generates 'ProxyIgnoredClassNameTrie.java'

# This file lists classes that are considered as proxy hence ignored.
# The trie will be matched starting from the first '$' char included.
# Use 0 to allow transformation of packages or classes beneath ignored packages
# Use 1 to ignore.
# End lines with '*' to match any trailing char if needed.

# 0 = global allows
# 1 = system-level ignores

# --------- ALLOW SHORTCUTS -----------

# lambda factory generated classes are not proxies
0 $$Lambda$*

# -------- SYSTEM-LEVEL IGNORES --------

1 $$weld*
1 $JaxbAccessor*
1 $__sisu*
1 $$EnhancerByGuice$$*
1 $$EnhancerByProxool$$*
1 $$$view*
# jboss mdb proxies
1 $$$endpoint*
1 $$_Weld*
1 $$_jvst*
1 $$SpringCGLIB$$*
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package datadog.trace.agent.tooling.bytebuddy.matcher

import datadog.trace.test.util.DDSpecification

class ProxyClassIgnoresTest extends DDSpecification {
def 'test #clsName exclusion'() {
given:
def result = ProxyClassIgnores.isIgnored(clsName)
expect:
result == excluded
where:
clsName | excluded
'org.springframework.util.ConcurrentReferenceHashMap$4' | false
'io.github.resilience4j.springboot3.circuitbreaker.autoconfigure.CircuitBreakerAutoConfiguration$CircuitBreakerEndpointAutoConfiguration$$SpringCGLIB$$0' | true
'io.micrometer.core.instrument.config.NamingConvention$$Lambda$1415' | false
'com.package.name.Class$JaxbAccessorF_variablename' | true
'my.package.Resource$Proxy$_$$_Weld$EnterpriseProxy' | true
'com.abc.MyResourceImpl$$EnhancerByGuice$$69175a50' | true
}
}