diff --git a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/init/FirstEntryPoint.java b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/init/FirstEntryPoint.java index b006b4896a5..17aa337df28 100644 --- a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/init/FirstEntryPoint.java +++ b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/init/FirstEntryPoint.java @@ -79,9 +79,11 @@ public void init(EarlyInitAgentConfig earlyConfig) { if (DEBUG_SIGNED_JAR_ACCESS) { JarVerifierClassFileTransformer transformer = new JarVerifierClassFileTransformer(); Instrumentation instrumentation = InstrumentationHolder.getInstrumentation(); - instrumentation.addTransformer(transformer, true); - instrumentation.retransformClasses(Class.forName("java.util.jar.JarVerifier")); - instrumentation.removeTransformer(transformer); + if (instrumentation != null) { + instrumentation.addTransformer(transformer, true); + instrumentation.retransformClasses(Class.forName("java.util.jar.JarVerifier")); + instrumentation.removeTransformer(transformer); + } } Path agentPath = javaagentFile.toPath(); // need to initialize version before initializing DiagnosticsHelper @@ -109,27 +111,26 @@ public void init(EarlyInitAgentConfig earlyConfig) { } checkTlsConnectionsToVirtualServersEnabled(); - if (startupLogger.isDebugEnabled()) { startupLogger.debug( - "Input arguments: " + ManagementFactory.getRuntimeMXBean().getInputArguments()); - startupLogger.debug("_JAVA_OPTIONS: " + System.getenv("_JAVA_OPTIONS")); - startupLogger.debug("JAVA_TOOL_OPTIONS: " + System.getenv("JAVA_TOOL_OPTIONS")); + "Input arguments: {} ", ManagementFactory.getRuntimeMXBean().getInputArguments()); + startupLogger.debug("_JAVA_OPTIONS: {}", System.getenv("_JAVA_OPTIONS")); + startupLogger.debug("JAVA_TOOL_OPTIONS: {}", System.getenv("JAVA_TOOL_OPTIONS")); } if (startupLogger.isTraceEnabled()) { - startupLogger.trace("OS: " + System.getProperty("os.name")); - startupLogger.trace("Classpath: " + System.getProperty("java.class.path")); - startupLogger.trace("Netty versions: " + NettyVersions.extract()); - startupLogger.trace("Env: " + System.getenv()); - startupLogger.trace("System properties: " + findSystemProperties()); + startupLogger.trace("OS: {}", System.getProperty("os.name")); + startupLogger.trace("Classpath: {}", System.getProperty("java.class.path")); + startupLogger.trace("Netty versions: {} ", NettyVersions.extract()); + startupLogger.trace("Env: {}", System.getenv()); + startupLogger.trace("System properties: {} ", findSystemProperties()); } if (startupLogger.isTraceEnabled()) { AppInsightsCertificate appInsightsCertificate = new AppInsightsCertificate(startupLogger); startupLogger.trace( - "Application Insights root certificate in the Java keystore: " - + appInsightsCertificate.isInJavaKeystore()); + "Application Insights root certificate in the Java keystore: {}", + appInsightsCertificate.isInJavaKeystore()); } } catch (Exception e) { diff --git a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/logbackpatch/FixedWindowRollingPolicy.java b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/logbackpatch/FixedWindowRollingPolicy.java index 0beb330269f..4d36d3aafe7 100644 --- a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/logbackpatch/FixedWindowRollingPolicy.java +++ b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/logbackpatch/FixedWindowRollingPolicy.java @@ -29,6 +29,8 @@ import ch.qos.logback.core.rolling.helper.RenameUtil; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import java.io.File; +import java.io.IOException; +import java.nio.file.Files; import java.util.Date; // copied from ch.qos.logback.core.rolling.FixedWindowRollingPolicy @@ -145,7 +147,11 @@ public void rollover() throws RolloverFailure { File file = new File(fileNamePattern.convertInt(maxIndex)); if (file.exists()) { - file.delete(); + try { + Files.delete(file.toPath()); + } catch (IOException e) { + addError("File deletaion failed.", e); + } } // Map {(maxIndex - 1), ..., minIndex} to {maxIndex, ..., minIndex+1} diff --git a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/profiler/Profiler.java b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/profiler/Profiler.java index de0f8b543cc..803e00dd642 100644 --- a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/profiler/Profiler.java +++ b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/profiler/Profiler.java @@ -21,6 +21,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.lang.management.ManagementFactory; +import java.nio.file.Files; import java.time.Duration; import java.time.Instant; import java.time.temporal.ChronoUnit; @@ -96,7 +97,7 @@ public Profiler(Configuration.ProfilerConfiguration config, File tempDir) { */ public void initialize( UploadService uploadService, ScheduledExecutorService scheduledExecutorService) - throws Exception { + throws IOException, InstanceNotFoundException, JfrStreamingException { this.uploadService = uploadService; this.scheduledExecutorService = scheduledExecutorService; @@ -267,9 +268,12 @@ private static void closeRecording(Recording recording, File recordingFile) { private static void writeFileFromStream(Recording recording, File recordingFile) throws IOException, JfrStreamingException { if (recordingFile.exists()) { - recordingFile.delete(); + Files.delete(recordingFile.toPath()); + } + + if (!recordingFile.createNewFile()) { + logger.error("File creation failed"); } - recordingFile.createNewFile(); try (BufferedInputStream stream = new BufferedInputStream(recording.getStream(null, null)); FileOutputStream fos = new FileOutputStream(recordingFile)) { @@ -288,7 +292,7 @@ private void clearActiveRecording() { // delete uploaded profile if (activeRecordingFile != null && activeRecordingFile.exists()) { if (!activeRecordingFile.delete()) { - logger.error("Failed to remove file " + activeRecordingFile.getAbsolutePath()); + logger.error("Failed to remove file {}", activeRecordingFile.getAbsolutePath()); } } activeRecordingFile = null;