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 log file if size is not too big #6993

Merged
merged 5 commits into from
May 13, 2024
Merged
Changes from all 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
@@ -1,6 +1,7 @@
package datadog.trace.core.flare;

import static datadog.trace.util.AgentThreadFactory.AgentThread.TRACER_FLARE;
import static java.nio.file.Files.readAllBytes;

import datadog.communication.http.OkHttpUtils;
import datadog.trace.api.Config;
Expand Down Expand Up @@ -48,6 +49,10 @@ final class TracerFlareService {

private static final MediaType OCTET_STREAM = MediaType.get("application/octet-stream");

private static final int MAX_LOGFILE_SIZE_MB = 15;

private static final int MAX_LOGFILE_SIZE_BYTES = MAX_LOGFILE_SIZE_MB << 20;

private final AgentTaskScheduler scheduler = new AgentTaskScheduler(TRACER_FLARE);

private final Config config;
Expand Down Expand Up @@ -214,6 +219,7 @@ private byte[] buildFlareZip(long startMillis, long endMillis, boolean dumpThrea
if (dumpThreads) {
addThreadDump(zip);
}
addLogs(zip);
zip.finish();

return bytes.toByteArray();
Expand Down Expand Up @@ -274,6 +280,38 @@ private void addThreadDump(ZipOutputStream zip) throws IOException {
TracerFlare.addText(zip, "threads.txt", buf.toString());
}

private void addLogs(ZipOutputStream zip) throws IOException {
// org.slf4j.simpleLogger.logFile transformed as datadog.slf4j.simpleLogger.logFile in the final
// dd-java-agent jar
String logFile = System.getProperty("org.slf4j.simpleLogger.logFile");
mcculls marked this conversation as resolved.
Show resolved Hide resolved
if (logFile == null || logFile.isEmpty()) {
TracerFlare.addText(zip, "tracer.log", "No tracer log file specified");
return;
}
Path path = Paths.get(logFile);
if (Files.exists(path)) {
try {
long size = Files.size(path);
if (size > MAX_LOGFILE_SIZE_BYTES) {
TracerFlare.addText(
zip,
"tracer.log",
"Can't add tracer log file to the flare due to its size: "
+ size
+ "."
+ "Max Size is "
+ MAX_LOGFILE_SIZE_MB
+ " MB.");
} else {

TracerFlare.addBinary(zip, "tracer.log", readAllBytes(path));
}
} catch (Throwable e) {
TracerFlare.addText(zip, "tracer.log", "Problem collecting tracer log: " + e);
}
}
}

final class CleanupTask implements Runnable {
@Override
public void run() {
Expand Down
Loading