Skip to content

Commit

Permalink
Fix printMuzzleReferences gradle task (#3808)
Browse files Browse the repository at this point in the history
* Fix printMuzzleReferences gradle task

* suppress SystemOut warnings

* bump gradle-plugins version
  • Loading branch information
Mateusz Rzeszutek authored Aug 10, 2021
1 parent beab394 commit bebb877
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 40 deletions.
2 changes: 1 addition & 1 deletion gradle-plugins/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
}

group = "io.opentelemetry.instrumentation"
version = "0.3.0"
version = "0.4.0"

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,45 +108,13 @@ public static void assertInstrumentationMuzzled(
*
* <p>Called by the {@code printMuzzleReferences} gradle task.
*/
public static void printMuzzleReferences(ClassLoader instrumentationClassLoader) {
for (InstrumentationModule instrumentationModule :
ServiceLoader.load(InstrumentationModule.class, instrumentationClassLoader)) {
try {
System.out.println(instrumentationModule.getClass().getName());
for (ClassRef ref : instrumentationModule.getMuzzleReferences().values()) {
System.out.print(prettyPrint(ref));
}
} catch (RuntimeException e) {
String message =
"Unexpected exception printing references for "
+ instrumentationModule.getClass().getName();
System.out.println(message);
throw new IllegalStateException(message, e);
}
}
}

private static String prettyPrint(ClassRef ref) {
StringBuilder builder = new StringBuilder(INDENT).append(ref).append(lineSeparator());
if (!ref.getSources().isEmpty()) {
builder.append(INDENT).append(INDENT).append("Sources:").append(lineSeparator());
for (Source source : ref.getSources()) {
builder
.append(INDENT)
.append(INDENT)
.append(INDENT)
.append("at: ")
.append(source)
.append(lineSeparator());
}
}
for (FieldRef field : ref.getFields()) {
builder.append(INDENT).append(INDENT).append(field).append(lineSeparator());
}
for (MethodRef method : ref.getMethods()) {
builder.append(INDENT).append(INDENT).append(method).append(lineSeparator());
}
return builder.toString();
public static void printMuzzleReferences(ClassLoader instrumentationClassLoader)
throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException,
IllegalAccessException {
Class<?> matcherClass =
instrumentationClassLoader.loadClass(
"io.opentelemetry.javaagent.tooling.muzzle.ReferencesPrinter");
matcherClass.getMethod("printMuzzleReferences").invoke(null);
}

private MuzzleGradlePluginUtil() {}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.tooling.muzzle;

import static java.lang.System.lineSeparator;

import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
import io.opentelemetry.javaagent.extension.muzzle.ClassRef;
import io.opentelemetry.javaagent.extension.muzzle.FieldRef;
import io.opentelemetry.javaagent.extension.muzzle.MethodRef;
import io.opentelemetry.javaagent.extension.muzzle.Source;
import java.util.ServiceLoader;

@SuppressWarnings("SystemOut")
public final class ReferencesPrinter {

private static final String INDENT = " ";

/**
* For all {@link InstrumentationModule}s found in the current thread's context classloader this
* method prints references returned by the {@link InstrumentationModule#getMuzzleReferences()}
* method to the standard output.
*/
public static void printMuzzleReferences() {
for (InstrumentationModule instrumentationModule :
ServiceLoader.load(InstrumentationModule.class)) {
try {
System.out.println(instrumentationModule.getClass().getName());
for (ClassRef ref : instrumentationModule.getMuzzleReferences().values()) {
System.out.print(prettyPrint(ref));
}
} catch (RuntimeException e) {
String message =
"Unexpected exception printing references for "
+ instrumentationModule.getClass().getName();
System.out.println(message);
throw new IllegalStateException(message, e);
}
}
}

private static String prettyPrint(ClassRef ref) {
StringBuilder builder = new StringBuilder(INDENT).append(ref).append(lineSeparator());
if (!ref.getSources().isEmpty()) {
builder.append(INDENT).append(INDENT).append("Sources:").append(lineSeparator());
for (Source source : ref.getSources()) {
builder
.append(INDENT)
.append(INDENT)
.append(INDENT)
.append("at: ")
.append(source)
.append(lineSeparator());
}
}
for (FieldRef field : ref.getFields()) {
builder.append(INDENT).append(INDENT).append(field).append(lineSeparator());
}
for (MethodRef method : ref.getMethods()) {
builder.append(INDENT).append(INDENT).append(method).append(lineSeparator());
}
return builder.toString();
}

private ReferencesPrinter() {}
}

0 comments on commit bebb877

Please sign in to comment.