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

Add a startup profiler for debugging #2085

Merged
merged 21 commits into from
Feb 7, 2022
Merged
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 @@ -40,7 +40,11 @@ public static void premain(String agentArgs, Instrumentation inst) {
if (alreadyLoaded) {
return;
}
StartupProfiler.start();

// this is used to support -Dapplicationinsights.debug.startupProfiling=true
heyams marked this conversation as resolved.
Show resolved Hide resolved
if (Boolean.parseBoolean(System.getProperty("applicationinsights.debug.startupProfiling"))) {
heyams marked this conversation as resolved.
Show resolved Hide resolved
StartupProfiler.start();
}

OpenTelemetryAgent.premain(agentArgs, inst);
alreadyLoaded = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,6 @@ final class StartupProfiler {

@SuppressWarnings("SystemOut")
public static void start() {
// this is used to support -Dapplicationinsights.debug.startupProfiling=true
if (!Boolean.parseBoolean(System.getProperty("applicationinsights.debug.startupProfiling"))) {
return;
}

if (isReadonly()) {
System.out.println("It's a readonly file system. StartupProfiler does nothing in this case.");
return;
}

String tempDirectory = System.getProperty("java.io.tmpdir");
File folder = new File(tempDirectory, "applicationinsights");
if (!folder.exists()) {
Expand All @@ -62,30 +52,22 @@ public static void start() {
try (PrintWriter out =
new PrintWriter(Files.newBufferedWriter(dumpFile.toPath(), Charset.defaultCharset()))) {
printWriter = new PrintWriter(out);
start(printWriter);
} catch (IOException e) {
System.out.println(
"Error occurred when writing dump to "
+ dumpFile.getPath()
+ " \ncause: "
+ e.getCause());
} finally {
if (printWriter != null) {
printWriter.close();
}
System.out.println("Error occurred when writing dump to " + dumpFile.getPath());
e.printStackTrace();
}

start(printWriter);
}

private static void start(PrintWriter out) {
Executors.newSingleThreadScheduledExecutor()
.scheduleAtFixedRate(new ThreadDump(out), 50, 50, TimeUnit.MILLISECONDS);
}

private static boolean isReadonly() {
File tempDir = new File(System.getProperty("java.io.tmpdir"));
return tempDir.canRead() && !tempDir.canWrite();
}

private StartupProfiler() {}

private static class ThreadDump implements Runnable {
Expand Down