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

minor code improvements #3299

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -3,6 +3,7 @@

package com.microsoft.applicationinsights.agent.bootstrap;

import java.util.Optional;
import java.util.function.Supplier;
import javax.annotation.Nullable;

Expand All @@ -21,12 +22,14 @@ public static boolean hasConnectionString() {
}

public static void configureOnce() {
if (configure != null) {
if (!hasConnectionString()) {
configure.run();
}
configure = null;
}
Optional.ofNullable(configure)
.ifPresent(result ->{
if (!hasConnectionString())
{
result.run();
}
result=null;
});
}

private AzureFunctions() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -1294,7 +1295,7 @@ public void validate() {
+ " action. "
+ "Learn more about attribute processors here: https://go.microsoft.com/fwlink/?linkid=2151557");
}
extractAttribute.validate();
Optional.ofNullable(extractAttribute).ifPresent(ExtractAttribute::validate);
}

if (action == ProcessorActionType.MASK) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
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;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ScheduledExecutorService;
Expand Down Expand Up @@ -91,12 +93,13 @@ public Profiler(Configuration.ProfilerConfiguration config, File tempDir) {
* Call init before run.
*
* @throws IOException Trouble communicating with MBean server
* @throws JfrStreamingException
* @throws InstanceNotFoundException The JVM does not support JFR, or experimental option is not
* enabled.
*/
public void initialize(
UploadService uploadService, ScheduledExecutorService scheduledExecutorService)
throws Exception {
UploadService uploadService, ScheduledExecutorService scheduledExecutorService) throws IOException, InstanceNotFoundException, JfrStreamingException
{
this.uploadService = uploadService;
this.scheduledExecutorService = scheduledExecutorService;

Expand Down Expand Up @@ -267,10 +270,13 @@ 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());
}
recordingFile.createNewFile();

if(!recordingFile.createNewFile()){
logger.error("File creation failed");
}

try (BufferedInputStream stream = new BufferedInputStream(recording.getStream(null, null));
FileOutputStream fos = new FileOutputStream(recordingFile)) {
int read;
Expand All @@ -284,13 +290,16 @@ private static void writeFileFromStream(Recording recording, File recordingFile)
private void clearActiveRecording() {
synchronized (activeRecordingLock) {
activeRecording = null;

// delete uploaded profile
if (activeRecordingFile != null && activeRecordingFile.exists()) {
if (!activeRecordingFile.delete()) {
logger.error("Failed to remove file " + activeRecordingFile.getAbsolutePath());
Optional.of(activeRecordingFile).ifPresent(result -> {
if(result.exists()) {
try {
Files.delete(result.toPath());
} catch (IOException e) {
logger.error("Failed to remove file {}" , result.getAbsolutePath());
}
}
}
});

activeRecordingFile = null;
}
}
Expand Down