-
Notifications
You must be signed in to change notification settings - Fork 304
Add scope filtering for symbol extraction #8676
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
dd-java-agent/agent-debugger/src/main/java/com/datadog/debugger/symbol/AvroFilter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.datadog.debugger.symbol; | ||
|
||
public class AvroFilter implements ScopeFilter { | ||
@Override | ||
public boolean filterOut(Scope scope) { | ||
if (scope == null) { | ||
return false; | ||
} | ||
LanguageSpecifics languageSpecifics = scope.getLanguageSpecifics(); | ||
if (languageSpecifics != null) { | ||
String superClass = languageSpecifics.getSuperClass(); | ||
// Allow Avro data classes that extend SpecificRecordBase. | ||
if ("org.apache.avro.specific.SpecificRecordBase".equals(superClass)) { | ||
return false; | ||
} | ||
} | ||
// Filter out classes that appear to be just schema wrappers. | ||
if (scope.getScopeType() == ScopeType.CLASS | ||
&& scope.getSymbols() != null | ||
&& scope.getSymbols().stream() | ||
.anyMatch( | ||
it -> | ||
it.getSymbolType() == SymbolType.STATIC_FIELD | ||
&& "SCHEMA$".equals(it.getName()) | ||
&& it.getType() != null | ||
&& it.getType().contains("org.apache.avro.Schema"))) { | ||
return true; | ||
} | ||
// Otherwise, do not filter. | ||
return false; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
dd-java-agent/agent-debugger/src/main/java/com/datadog/debugger/symbol/ProtoFilter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.datadog.debugger.symbol; | ||
|
||
import java.util.List; | ||
|
||
public class ProtoFilter implements ScopeFilter { | ||
@Override | ||
public boolean filterOut(Scope scope) { | ||
if (scope == null) { | ||
return false; | ||
} | ||
LanguageSpecifics languageSpecifics = scope.getLanguageSpecifics(); | ||
if (languageSpecifics != null) { | ||
List<String> interfaces = languageSpecifics.getInterfaces(); | ||
if (interfaces != null) { | ||
if (interfaces.contains("com.google.protobuf.MessageOrBuilder")) { | ||
// MessageOrBuilder is an interface implemented by both message classes and their | ||
// builders. | ||
// Scopes implementing this interface are filtered out because they do not represent | ||
// concrete data structures but rather interfaces for accessing or building messages. | ||
return true; | ||
} | ||
} | ||
String superClass = languageSpecifics.getSuperClass(); | ||
if ("com.google.protobuf.AbstractParser".equals(superClass)) { | ||
// AbstractParser is a base class for parsing protobuf messages. Scopes with this super | ||
// class are filtered out because they are utility classes for parsing and do not contain | ||
// actual data fields. | ||
return true; | ||
} | ||
if ("com.google.protobuf.GeneratedMessageV3$Builder".equals(superClass)) { | ||
// GeneratedMessageV3$Builder is a builder class for constructing GeneratedMessageV3 | ||
// instances. These scopes are filtered out because they are used for building messages and | ||
// do not represent the final data structure. | ||
return true; | ||
} | ||
} | ||
// If none of the above matched, see if the class has a proto descriptor field. This is the case | ||
// for wrapper | ||
// classes (`OuterClass`) and `Enum` classes. They contain metadata, not data. | ||
if (hasProtoDescriptorField(scope)) { | ||
return true; | ||
} | ||
// Probably no protobuf, pass | ||
return false; | ||
} | ||
|
||
private boolean hasProtoDescriptorField(Scope scope) { | ||
return scope.getScopeType() == ScopeType.CLASS | ||
&& scope.getSymbols() != null | ||
&& scope.getSymbols().stream() | ||
.anyMatch( | ||
it -> | ||
it.getSymbolType() == SymbolType.STATIC_FIELD | ||
&& it.getType() != null | ||
&& it.getType().contains("com.google.protobuf.Descriptors")); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
dd-java-agent/agent-debugger/src/main/java/com/datadog/debugger/symbol/ScopeFilter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.datadog.debugger.symbol; | ||
|
||
public interface ScopeFilter { | ||
/** returns true if the scope should be excluded */ | ||
boolean filterOut(Scope scope); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
dd-java-agent/agent-debugger/src/main/java/com/datadog/debugger/symbol/WireFilter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.datadog.debugger.symbol; | ||
|
||
import java.util.List; | ||
|
||
public class WireFilter implements ScopeFilter { | ||
@Override | ||
public boolean filterOut(Scope scope) { | ||
// Filter out classes generated by Square Wire: https://square.github.io/wire/ | ||
if (scope == null) { | ||
return false; | ||
} | ||
LanguageSpecifics languageSpecifics = scope.getLanguageSpecifics(); | ||
if (languageSpecifics == null) { | ||
return false; | ||
} | ||
List<String> interfaces = languageSpecifics.getInterfaces(); | ||
if (interfaces != null) { | ||
if (interfaces.contains("com.squareup.wire.Message")) { | ||
// Pass-through for Message since it contains data | ||
return false; | ||
} | ||
if (interfaces.stream().anyMatch(it -> it.startsWith("com.squareup.wire"))) { | ||
return true; | ||
} | ||
} | ||
String superClass = languageSpecifics.getSuperClass(); | ||
if (superClass != null) { | ||
if (superClass.startsWith("com.squareup.wire")) { | ||
return true; | ||
} | ||
} | ||
// Probably no protobuf, pass | ||
return false; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
dd-java-agent/agent-debugger/src/test/java/com/datadog/debugger/symbol/AvroFilterTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.datadog.debugger.symbol; | ||
|
||
import static java.util.Arrays.asList; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class AvroFilterTest { | ||
@Test | ||
void filterOut() { | ||
AvroFilter avroFilter = new AvroFilter(); | ||
assertFalse(avroFilter.filterOut(null)); | ||
Scope scope = Scope.builder(ScopeType.CLASS, "", 0, 0).build(); | ||
assertFalse(avroFilter.filterOut(scope)); | ||
scope = Scope.builder(ScopeType.CLASS, "", 0, 0).name("org.apache.avro.MyClass").build(); | ||
assertFalse(avroFilter.filterOut(scope)); | ||
scope = | ||
Scope.builder(ScopeType.CLASS, "", 0, 0) | ||
.languageSpecifics( | ||
new LanguageSpecifics.Builder() | ||
.superClass("org.apache.avro.specific.SpecificRecordBase") | ||
.build()) | ||
.build(); | ||
assertFalse(avroFilter.filterOut(scope)); | ||
scope = | ||
Scope.builder(ScopeType.CLASS, "", 0, 0) | ||
.symbols( | ||
asList( | ||
new Symbol( | ||
SymbolType.STATIC_FIELD, "SCHEMA$", 0, "org.apache.avro.Schema", null))) | ||
.build(); | ||
assertTrue(avroFilter.filterOut(scope)); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
dd-java-agent/agent-debugger/src/test/java/com/datadog/debugger/symbol/ProtoFilterTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.datadog.debugger.symbol; | ||
|
||
import static java.util.Arrays.asList; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class ProtoFilterTest { | ||
@Test | ||
void filterOut() { | ||
ProtoFilter protoFilter = new ProtoFilter(); | ||
assertFalse(protoFilter.filterOut(null)); | ||
Scope scope = Scope.builder(ScopeType.CLASS, "", 0, 0).build(); | ||
assertFalse(protoFilter.filterOut(scope)); | ||
scope = Scope.builder(ScopeType.CLASS, "", 0, 0).name("com.google.protobuf.MyClass").build(); | ||
assertFalse(protoFilter.filterOut(scope)); | ||
scope = | ||
Scope.builder(ScopeType.CLASS, "", 0, 0) | ||
.languageSpecifics( | ||
new LanguageSpecifics.Builder() | ||
.addInterfaces(asList("com.google.protobuf.MessageOrBuilder")) | ||
.build()) | ||
.build(); | ||
assertTrue(protoFilter.filterOut(scope)); | ||
scope = | ||
Scope.builder(ScopeType.CLASS, "", 0, 0) | ||
.languageSpecifics( | ||
new LanguageSpecifics.Builder() | ||
.superClass("com.google.protobuf.AbstractParser") | ||
.build()) | ||
.build(); | ||
assertTrue(protoFilter.filterOut(scope)); | ||
scope = | ||
Scope.builder(ScopeType.CLASS, "", 0, 0) | ||
.languageSpecifics( | ||
new LanguageSpecifics.Builder() | ||
.superClass("com.google.protobuf.GeneratedMessageV3$Builder") | ||
.build()) | ||
.build(); | ||
assertTrue(protoFilter.filterOut(scope)); | ||
scope = | ||
Scope.builder(ScopeType.CLASS, "", 0, 0) | ||
.symbols( | ||
asList( | ||
new Symbol( | ||
SymbolType.STATIC_FIELD, | ||
"SCHEMA$", | ||
0, | ||
"com.google.protobuf.Descriptors", | ||
null))) | ||
.build(); | ||
assertTrue(protoFilter.filterOut(scope)); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to add a test that we don't filter out non-proto scopes. This is true for the other tests as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done