From a5592250dbc609d9e017fb91f7fa19421eb887d7 Mon Sep 17 00:00:00 2001 From: Jack Berg Date: Tue, 5 Sep 2023 14:47:55 -0500 Subject: [PATCH] ThreadLocal MessageDigest, JarFile for accessing manifest, template log messages --- .../runtimemetrics/java8/JarAnalyzer.java | 8 +++--- .../runtimemetrics/java8/JarAnalyzerUtil.java | 27 ++++++++++++------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/instrumentation/runtime-telemetry/runtime-telemetry-java8/javaagent/src/main/java/io/opentelemetry/instrumentation/javaagent/runtimemetrics/java8/JarAnalyzer.java b/instrumentation/runtime-telemetry/runtime-telemetry-java8/javaagent/src/main/java/io/opentelemetry/instrumentation/javaagent/runtimemetrics/java8/JarAnalyzer.java index ed829c92ec36..6209cc002b9f 100644 --- a/instrumentation/runtime-telemetry/runtime-telemetry-java8/javaagent/src/main/java/io/opentelemetry/instrumentation/javaagent/runtimemetrics/java8/JarAnalyzer.java +++ b/instrumentation/runtime-telemetry/runtime-telemetry-java8/javaagent/src/main/java/io/opentelemetry/instrumentation/javaagent/runtimemetrics/java8/JarAnalyzer.java @@ -141,16 +141,16 @@ private void handleInternal(ProtectionDomain protectionDomain) { return; } if ("jrt".equals(archiveUrl.getProtocol())) { - logger.log(Level.FINEST, "Skipping processing for java runtime module: " + archiveUrl); + logger.log(Level.FINEST, "Skipping processing for java runtime module: {0}", archiveUrl); return; } String file = archiveUrl.getFile(); if (file.endsWith("/")) { - logger.log(Level.FINEST, "Skipping processing non-archive code location: " + archiveUrl); + logger.log(Level.FINEST, "Skipping processing non-archive code location: {0}", archiveUrl); return; } if (!file.endsWith(JAR_EXTENSION) && !file.endsWith(WAR_EXTENSION)) { - logger.log(Level.INFO, "Skipping processing unrecognized code location: " + archiveUrl); + logger.log(Level.INFO, "Skipping processing unrecognized code location: {0}", archiveUrl); return; } @@ -209,7 +209,7 @@ static void processUrl(EventEmitter eventEmitter, URL archiveUrl) { try { addPackageType(builder, archiveUrl); } catch (Exception e) { - logger.log(Level.WARNING, "Error adding package type for archive URL: " + archiveUrl, e); + logger.log(Level.WARNING, "Error adding package type for archive URL: {0}" + archiveUrl, e); } try { diff --git a/instrumentation/runtime-telemetry/runtime-telemetry-java8/javaagent/src/main/java/io/opentelemetry/instrumentation/javaagent/runtimemetrics/java8/JarAnalyzerUtil.java b/instrumentation/runtime-telemetry/runtime-telemetry-java8/javaagent/src/main/java/io/opentelemetry/instrumentation/javaagent/runtimemetrics/java8/JarAnalyzerUtil.java index 23ba2d0708a1..5c9eecbc1c90 100644 --- a/instrumentation/runtime-telemetry/runtime-telemetry-java8/javaagent/src/main/java/io/opentelemetry/instrumentation/javaagent/runtimemetrics/java8/JarAnalyzerUtil.java +++ b/instrumentation/runtime-telemetry/runtime-telemetry-java8/javaagent/src/main/java/io/opentelemetry/instrumentation/javaagent/runtimemetrics/java8/JarAnalyzerUtil.java @@ -16,6 +16,7 @@ import java.security.NoSuchAlgorithmException; import java.util.Properties; import java.util.jar.JarEntry; +import java.util.jar.JarFile; import java.util.jar.JarInputStream; import java.util.jar.Manifest; @@ -29,6 +30,18 @@ final class JarAnalyzerUtil { static final AttributeKey PACKAGE_CHECKSUM = AttributeKey.stringKey("package.checksum"); static final AttributeKey PACKAGE_PATH = AttributeKey.stringKey("package.path"); + private static final ThreadLocal MESSAGE_DIGEST_THREAD_LOCAL = + ThreadLocal.withInitial(JarAnalyzerUtil::createSha1MessageDigest); + + private static MessageDigest createSha1MessageDigest() { + try { + return MessageDigest.getInstance("SHA1"); + } catch (NoSuchAlgorithmException e) { + throw new IllegalStateException( + "Unexpected error. Checksum algorithm SHA1 does not exist.", e); + } + } + /** * Set the attributes {@link #PACKAGE_TYPE} from the {@code archiveUrl}. * @@ -55,13 +68,8 @@ static void addPackageChecksum(AttributesBuilder builder, URL archiveUrl) throws } private static String computeSha1(URL archiveUrl) throws IOException { - MessageDigest md; - try { - md = MessageDigest.getInstance("SHA1"); - } catch (NoSuchAlgorithmException e) { - throw new IllegalStateException( - "Unexpected error. Checksum algorithm SHA1 does not exist.", e); - } + MessageDigest md = MESSAGE_DIGEST_THREAD_LOCAL.get(); + md.reset(); // Reset reused thread local message digest instead try (InputStream is = new DigestInputStream(archiveUrl.openStream(), md)) { byte[] buffer = new byte[1024 * 8]; @@ -106,9 +114,8 @@ private static String archiveFilename(URL archiveUrl) throws Exception { * {Implementation-Vendor}", e.g. {@code Jackson datatype: JSR310 by FasterXML}. */ static void addPackageDescription(AttributesBuilder builder, URL archiveUrl) throws IOException { - try (InputStream inputStream = archiveUrl.openStream(); - JarInputStream jarInputStream = new JarInputStream(inputStream)) { - Manifest manifest = jarInputStream.getManifest(); + try (JarFile jarFile = new JarFile(archiveUrl.getFile())) { + Manifest manifest = jarFile.getManifest(); if (manifest == null) { return; }