33import datadog .trace .agent .tooling .InstrumenterModule ;
44import datadog .trace .agent .tooling .InstrumenterState ;
55import datadog .trace .agent .tooling .Utils ;
6+ import java .util .ArrayList ;
7+ import java .util .Collections ;
68import java .util .List ;
79import net .bytebuddy .matcher .ElementMatcher ;
810import org .slf4j .Logger ;
1113public class MuzzleCheck implements ElementMatcher <ClassLoader > {
1214 private static final Logger log = LoggerFactory .getLogger (MuzzleCheck .class );
1315
16+ private static final List <String > ERRORS = Collections .synchronizedList (new ArrayList <>());
17+ private static volatile boolean recordErrors = false ;
18+
1419 private final int instrumentationId ;
1520 private final String instrumentationClass ;
1621 private final ReferenceProvider runtimeMuzzleReferences ;
@@ -33,9 +38,13 @@ public boolean matches(ClassLoader classLoader) {
3338 InstrumenterState .applyInstrumentation (classLoader , instrumentationId );
3439 } else {
3540 InstrumenterState .blockInstrumentation (classLoader , instrumentationId );
36- if (log .isDebugEnabled ()) {
41+ if (recordErrors || log .isDebugEnabled ()) {
3742 final List <Reference .Mismatch > mismatches =
3843 muzzle .getMismatchedReferenceSources (classLoader );
44+ if (recordErrors ) {
45+ recordMuzzleMismatch (
46+ InstrumenterState .describe (instrumentationId ), classLoader , mismatches );
47+ }
3948 log .debug (
4049 "Muzzled - {} instrumentation.target.classloader={}" ,
4150 InstrumenterState .describe (instrumentationId ),
@@ -61,4 +70,38 @@ private ReferenceMatcher muzzle() {
6170 }
6271 return muzzle ;
6372 }
73+
74+ /**
75+ * Record a muzzle mismatch error for test visibility.
76+ *
77+ * @param instrumentationDescription the description of the instrumentation that was blocked
78+ * @param classLoader the classloader where the instrumentation was blocked
79+ * @param mismatches the list of mismatch details
80+ */
81+ private static void recordMuzzleMismatch (
82+ String instrumentationDescription ,
83+ ClassLoader classLoader ,
84+ List <Reference .Mismatch > mismatches ) {
85+ StringBuilder sb = new StringBuilder ();
86+ sb .append ("Muzzled - " )
87+ .append (instrumentationDescription )
88+ .append (" instrumentation.target.classloader=" )
89+ .append (classLoader )
90+ .append ("\n " );
91+ for (Reference .Mismatch mismatch : mismatches ) {
92+ sb .append (" Mismatch: " ).append (mismatch ).append ("\n " );
93+ }
94+ ERRORS .add (sb .toString ());
95+ }
96+
97+ // Visible for testing
98+ public static void enableRecordingAndReset () {
99+ recordErrors = true ;
100+ ERRORS .clear ();
101+ }
102+
103+ // Visible for testing
104+ public static List <String > getErrors () {
105+ return Collections .unmodifiableList (ERRORS );
106+ }
64107}
0 commit comments