22
33import static datadog .trace .util .AgentThreadFactory .AGENT_THREAD_GROUP ;
44
5- import datadog .trace . agent . tooling . bytebuddy . ClassCodeFilter ;
5+ import datadog .instrument . utils . ClassNameFilter ;
66import datadog .trace .api .Config ;
77import datadog .trace .api .DDTraceApiInfo ;
88import datadog .trace .api .InstrumenterConfig ;
2020import org .slf4j .LoggerFactory ;
2121
2222/** Compact filter that records uninteresting types. */
23- final class NoMatchFilter extends ClassCodeFilter {
23+ final class NoMatchFilter {
2424 private static final Logger log = LoggerFactory .getLogger (NoMatchFilter .class );
2525
26- NoMatchFilter () {
27- super (InstrumenterConfig .get ().getResolverNoMatchesSize ());
28-
29- // seed filter from previously collected results?
26+ public static ClassNameFilter build () {
27+ // support persisting/restoring no-match results?
3028 Path noMatchFile = discoverNoMatchFile ();
3129 if (null != noMatchFile ) {
32- seedNoMatchFilter (noMatchFile );
30+ if (Files .exists (noMatchFile )) {
31+ // restore existing filter with previously collected results
32+ return seedNoMatchFilter (noMatchFile );
33+ } else {
34+ // populate filter from current run and persist at shutdown
35+ ClassNameFilter filter = emptyNoMatchFilter ();
36+ Runtime .getRuntime ().addShutdownHook (new ShutdownHook (noMatchFile , filter ));
37+ return filter ;
38+ }
39+ } else {
40+ return emptyNoMatchFilter ();
3341 }
3442 }
3543
36- static Path discoverNoMatchFile () {
44+ private static Path discoverNoMatchFile () {
3745 String cacheDir = InstrumenterConfig .get ().getResolverCacheDir ();
3846 if (null == cacheDir ) {
3947 return null ;
@@ -53,51 +61,49 @@ static Path discoverNoMatchFile() {
5361 return Paths .get (cacheDir , noMatchFilterName );
5462 }
5563
56- void seedNoMatchFilter (Path noMatchFile ) {
57- if (!Files .exists (noMatchFile )) {
58- Runtime .getRuntime ().addShutdownHook (new ShutdownHook (noMatchFile ));
59- } else {
60- log .debug ("Seeding NoMatchFilter from {}" , noMatchFile );
61- try (DataInputStream in =
62- new DataInputStream (new BufferedInputStream (Files .newInputStream (noMatchFile )))) {
63- while (true ) {
64- switch (in .readUTF ()) {
65- case "dd-java-agent" :
66- expectVersion (in , DDTraceApiInfo .VERSION );
67- break ;
68- case "NoMatchFilter" :
69- if (in .readInt () != slots .length ) {
70- throw new IOException ("filter size mismatch" );
71- }
72- for (int i = 0 ; i < slots .length ; i ++) {
73- slots [i ] = in .readLong ();
74- }
75- return ;
76- default :
77- throw new IOException ("unexpected content" );
78- }
79- }
80- } catch (IOException e ) {
81- if (log .isDebugEnabled ()) {
82- log .info ("Unable to seed NoMatchFilter from {}" , noMatchFile , e );
83- } else {
84- log .info ("Unable to seed NoMatchFilter from {}: {}" , noMatchFile , e .getMessage ());
64+ private static ClassNameFilter emptyNoMatchFilter () {
65+ return new ClassNameFilter (InstrumenterConfig .get ().getResolverNoMatchesSize ());
66+ }
67+
68+ private static ClassNameFilter seedNoMatchFilter (Path noMatchFile ) {
69+ log .debug ("Seeding NoMatchFilter from {}" , noMatchFile );
70+ try (DataInputStream in =
71+ new DataInputStream (new BufferedInputStream (Files .newInputStream (noMatchFile )))) {
72+ while (true ) {
73+ switch (in .readUTF ()) {
74+ case "dd-java-agent" :
75+ expectVersion (in , DDTraceApiInfo .VERSION );
76+ break ;
77+ case "NoMatchFilter" :
78+ return ClassNameFilter .readFrom (in );
79+ default :
80+ throw new IOException ("unexpected content" );
8581 }
8682 }
83+ } catch (Throwable e ) {
84+ if (log .isDebugEnabled ()) {
85+ log .info ("Unable to seed NoMatchFilter from {}" , noMatchFile , e );
86+ } else {
87+ log .info ("Unable to seed NoMatchFilter from {}: {}" , noMatchFile , e .getMessage ());
88+ }
89+ return emptyNoMatchFilter ();
8790 }
8891 }
8992
90- void persistNoMatchFilter (Path noMatchFile ) {
93+ private static void expectVersion (DataInputStream in , String version ) throws IOException {
94+ if (!version .equals (in .readUTF ())) {
95+ throw new IOException ("version mismatch" );
96+ }
97+ }
98+
99+ static void persistNoMatchFilter (Path noMatchFile , ClassNameFilter filter ) {
91100 log .debug ("Persisting NoMatchFilter to {}" , noMatchFile );
92101 try (DataOutputStream out =
93102 new DataOutputStream (new BufferedOutputStream (Files .newOutputStream (noMatchFile )))) {
94103 out .writeUTF ("dd-java-agent" );
95104 out .writeUTF (DDTraceApiInfo .VERSION );
96105 out .writeUTF ("NoMatchFilter" );
97- out .writeInt (slots .length );
98- for (long slot : slots ) {
99- out .writeLong (slot );
100- }
106+ filter .writeTo (out );
101107 } catch (IOException e ) {
102108 if (log .isDebugEnabled ()) {
103109 log .info ("Unable to persist NoMatchFilter to {}" , noMatchFile , e );
@@ -107,23 +113,19 @@ void persistNoMatchFilter(Path noMatchFile) {
107113 }
108114 }
109115
110- static void expectVersion (DataInputStream in , String version ) throws IOException {
111- if (!version .equals (in .readUTF ())) {
112- throw new IOException ("version mismatch" );
113- }
114- }
115-
116- class ShutdownHook extends Thread {
116+ private static final class ShutdownHook extends Thread {
117117 private final Path noMatchFile ;
118+ private final ClassNameFilter filter ;
118119
119- ShutdownHook (Path noMatchFile ) {
120+ ShutdownHook (Path noMatchFile , ClassNameFilter filter ) {
120121 super (AGENT_THREAD_GROUP , "dd-NoMatchFilter-persist-hook" );
121122 this .noMatchFile = noMatchFile ;
123+ this .filter = filter ;
122124 }
123125
124126 @ Override
125127 public void run () {
126- persistNoMatchFilter (noMatchFile );
128+ persistNoMatchFilter (noMatchFile , filter );
127129 }
128130 }
129131}
0 commit comments