Skip to content
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

Fix JarAnalyzer warnings on Payara #10458

Merged
merged 5 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
Expand Up @@ -8,6 +8,9 @@
import static io.opentelemetry.instrumentation.javaagent.runtimemetrics.java8.JarDetails.EAR_EXTENSION;
import static io.opentelemetry.instrumentation.javaagent.runtimemetrics.java8.JarDetails.JAR_EXTENSION;
import static io.opentelemetry.instrumentation.javaagent.runtimemetrics.java8.JarDetails.WAR_EXTENSION;
import static java.util.logging.Level.FINE;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING;

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.common.AttributeKey;
Expand All @@ -18,6 +21,7 @@
import io.opentelemetry.instrumentation.runtimemetrics.java8.internal.JmxRuntimeMetricsUtil;
import io.opentelemetry.sdk.common.Clock;
import io.opentelemetry.sdk.internal.DaemonThreadFactory;
import java.io.File;
import java.io.IOException;
import java.lang.instrument.ClassFileTransformer;
import java.net.URI;
Expand Down Expand Up @@ -54,27 +58,29 @@ final class JarAnalyzer implements ClassFileTransformer {
AttributeKey.stringKey("package.checksum_algorithm");
static final AttributeKey<String> PACKAGE_PATH = AttributeKey.stringKey("package.path");

private final boolean debug;
private final Set<URI> seenUris = new HashSet<>();
private final BlockingQueue<URL> toProcess = new LinkedBlockingDeque<>();

private JarAnalyzer(OpenTelemetry unused, int jarsPerSecond) {
private JarAnalyzer(OpenTelemetry unused, int jarsPerSecond, boolean debug) {
this.debug = debug;
// TODO(jack-berg): Use OpenTelemetry to obtain EventEmitter when event API is stable
EventEmitter eventEmitter =
GlobalEventEmitterProvider.get()
.eventEmitterBuilder(JmxRuntimeMetricsUtil.getInstrumentationName())
.setInstrumentationVersion(JmxRuntimeMetricsUtil.getInstrumentationVersion())
.setEventDomain(EVENT_DOMAIN_PACKAGE)
.build();
Worker worker = new Worker(eventEmitter, toProcess, jarsPerSecond);
Worker worker = new Worker(eventEmitter, toProcess, jarsPerSecond, debug);
Thread workerThread =
new DaemonThreadFactory(JarAnalyzer.class.getSimpleName() + "_WorkerThread")
.newThread(worker);
workerThread.start();
}

/** Create {@link JarAnalyzer} and start the worker thread. */
public static JarAnalyzer create(OpenTelemetry unused, int jarsPerSecond) {
return new JarAnalyzer(unused, jarsPerSecond);
public static JarAnalyzer create(OpenTelemetry unused, int jarsPerSecond, boolean debug) {
return new JarAnalyzer(unused, jarsPerSecond, debug);
}

/**
Expand Down Expand Up @@ -108,7 +114,8 @@ private void handle(ProtectionDomain protectionDomain) {
try {
locationUri = archiveUrl.toURI();
} catch (URISyntaxException e) {
logger.log(Level.WARNING, "Unable to get URI for code location URL: " + archiveUrl, e);
logger.log(
debug ? WARNING : FINE, "Unable to get URI for code location URL: " + archiveUrl, e);
return;
}

Expand All @@ -127,10 +134,27 @@ private void handle(ProtectionDomain protectionDomain) {
if (!file.endsWith(JAR_EXTENSION)
&& !file.endsWith(WAR_EXTENSION)
&& !file.endsWith(EAR_EXTENSION)) {
logger.log(Level.INFO, "Skipping processing unrecognized code location: {0}", archiveUrl);
logger.log(
debug ? INFO : FINE, "Skipping processing unrecognized code location: {0}", archiveUrl);
return;
}

// Payara 5 and 6 have url with file protocol that fail on openStream with
// java.io.IOException: no entry name specified
// at
// java.base/sun.net.www.protocol.jar.JarURLConnection.getInputStream(JarURLConnection.java:160)
// To avoid this here we recreate the URL when it points to a file.
if ("file".equals(archiveUrl.getProtocol())) {
try {
File archiveFile = new File(archiveUrl.toURI().getSchemeSpecificPart());
if (archiveFile.exists() && archiveFile.isFile()) {
archiveUrl = archiveFile.toURI().toURL();
}
} catch (Exception e) {
logger.log(debug ? WARNING : FINE, "Unable to normalize location URL: " + archiveUrl, e);
}
}

// Only code locations with .jar and .war extension should make it here
toProcess.add(archiveUrl);
}
Expand All @@ -140,18 +164,21 @@ private static final class Worker implements Runnable {
private final EventEmitter eventEmitter;
private final BlockingQueue<URL> toProcess;
private final io.opentelemetry.sdk.internal.RateLimiter rateLimiter;
private final boolean debug;

private Worker(EventEmitter eventEmitter, BlockingQueue<URL> toProcess, int jarsPerSecond) {
private Worker(
EventEmitter eventEmitter, BlockingQueue<URL> toProcess, int jarsPerSecond, boolean debug) {
this.eventEmitter = eventEmitter;
this.toProcess = toProcess;
this.rateLimiter =
new io.opentelemetry.sdk.internal.RateLimiter(
jarsPerSecond, jarsPerSecond, Clock.getDefault());
this.debug = debug;
}

/**
* Continuously poll the {@link #toProcess} for archive {@link URL}s, and process each wit
* {@link #processUrl(EventEmitter, URL)}.
* {@link #processUrl(EventEmitter, URL, boolean)}.
*/
@Override
public void run() {
Expand All @@ -172,25 +199,26 @@ public void run() {
try {
// TODO(jack-berg): add ability to optionally re-process urls periodically to re-emit
// events
processUrl(eventEmitter, archiveUrl);
processUrl(eventEmitter, archiveUrl, debug);
} catch (Throwable e) {
logger.log(Level.WARNING, "Unexpected error processing archive URL: " + archiveUrl, e);
logger.log(
debug ? WARNING : FINE, "Unexpected error processing archive URL: " + archiveUrl, e);
}
}
logger.warning("JarAnalyzer stopped");
logger.log(debug ? WARNING : FINE, "JarAnalyzer stopped");
}
}

/**
* Process the {@code archiveUrl}, extracting metadata from it and emitting an event with the
* content.
*/
static void processUrl(EventEmitter eventEmitter, URL archiveUrl) {
static void processUrl(EventEmitter eventEmitter, URL archiveUrl, boolean debug) {
JarDetails jarDetails;
try {
jarDetails = JarDetails.forUrl(archiveUrl);
} catch (IOException e) {
logger.log(Level.WARNING, "Error reading package for archive URL: " + archiveUrl, e);
logger.log(debug ? WARNING : FINE, "Error reading package for archive URL: " + archiveUrl, e);
laurit marked this conversation as resolved.
Show resolved Hide resolved
return;
}
AttributesBuilder builder = Attributes.builder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.google.auto.service.AutoService;
import io.opentelemetry.javaagent.bootstrap.InstrumentationHolder;
import io.opentelemetry.javaagent.tooling.BeforeAgentListener;
import io.opentelemetry.javaagent.tooling.config.AgentConfig;
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
import io.opentelemetry.sdk.autoconfigure.internal.AutoConfigureUtil;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
Expand All @@ -32,7 +33,10 @@ public void beforeAgent(AutoConfiguredOpenTelemetrySdk autoConfiguredOpenTelemet
int jarsPerSecond =
config.getInt("otel.instrumentation.runtime-telemetry.package-emitter.jars-per-second", 10);
JarAnalyzer jarAnalyzer =
JarAnalyzer.create(autoConfiguredOpenTelemetrySdk.getOpenTelemetrySdk(), jarsPerSecond);
JarAnalyzer.create(
autoConfiguredOpenTelemetrySdk.getOpenTelemetrySdk(),
jarsPerSecond,
AgentConfig.isDebugModeEnabled(config));
inst.addTransformer(jarAnalyzer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class JarAnalyzerTest {
@MethodSource("processUrlArguments")
void processUrl_EmitsEvents(URL archiveUrl, Consumer<AttributesAssert> attributesConsumer) {
EventEmitter eventEmitter = mock(EventEmitter.class);
JarAnalyzer.processUrl(eventEmitter, archiveUrl);
JarAnalyzer.processUrl(eventEmitter, archiveUrl, false);

ArgumentCaptor<Attributes> attributesArgumentCaptor = ArgumentCaptor.forClass(Attributes.class);
verify(eventEmitter).emit(eq("info"), attributesArgumentCaptor.capture());
Expand Down
Loading