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 memory logs #1999

Open
wants to merge 1 commit into
base: feature/mini-runtime-release
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Expand Up @@ -17,6 +17,7 @@
import com.akto.log.LoggerMaker;
import com.akto.log.LoggerMaker.LogDb;
import com.akto.metrics.AllMetrics;
import com.akto.monitor.MemoryMonitor;
import com.akto.sql.SampleDataAltDb;
import com.akto.hybrid_parsers.HttpCallParser;
import com.akto.data_actor.DataActor;
Expand Down Expand Up @@ -150,6 +151,9 @@ public static String getTopicName(){
return topicName;
}

private static final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();


// REFERENCE: https://www.oreilly.com/library/view/kafka-the-definitive/9781491936153/ch04.html (But how do we Exit?)
public static void main(String[] args) {
//String mongoURI = System.getenv("AKTO_MONGO_CONN");;
Expand Down Expand Up @@ -303,7 +307,7 @@ public void run() {

long lastSyncOffset = 0;

Map<Integer, Integer> logSentMap = new HashMap<>();
executorService.scheduleAtFixedRate(new MemoryMonitor.MemoryMonitorTask(), 0, 10, TimeUnit.SECONDS);

try {
main.consumer.subscribe(Arrays.asList(topicName, "har_"+topicName));
Expand Down
36 changes: 36 additions & 0 deletions libs/utils/src/main/java/com/akto/monitor/MemoryMonitor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.akto.monitor;

public class MemoryMonitor {

// Runnable task for monitoring memory
public static class MemoryMonitorTask implements Runnable {
@Override
public void run() {
Runtime runtime = Runtime.getRuntime();

// Loop to print memory usage every 1 second
while (true) {
// Calculate memory statistics
long totalMemory = runtime.totalMemory();
long freeMemory = runtime.freeMemory();
long usedMemory = totalMemory - freeMemory;

// Print memory statistics
System.out.print("Used Memory: " + (usedMemory / 1024 / 1024) + " MB ");
System.out.print("Free Memory: " + (freeMemory / 1024 / 1024) + " MB ");
System.out.print("Total Memory: " + (totalMemory / 1024 / 1024) + " MB ");
System.out.print("Available Memory: " + ((runtime.maxMemory() - usedMemory) / 1024 / 1024) + " MB ");
System.out.println("-------------------------");

// Pause for 1 second
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.err.println("Memory monitor thread interrupted: " + e.getMessage());
break; // Exit the loop if thread is interrupted
}
}
}
}

}
Loading