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

health check endpoint - jvm stats #136

Merged
merged 1 commit into from
Jun 29, 2023
Merged
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
@@ -0,0 +1,57 @@
package org.assimbly.dil.validation.stats;

import org.codehaus.jackson.annotate.JsonIgnore;

import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.lang.management.ThreadMXBean;
import java.util.HashMap;
import java.util.Map;

public class BackendResponse {

public enum StatusTag {
OK, TROUBLING, FAILING
}

@JsonIgnore
private final OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
private final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();

private Map<String, Object> jvm = new HashMap<>();
private Map<String, Long> memory = new HashMap<>();
private Map<String, Integer> threads = new HashMap<>();


public Map<String, Long> getMemory() {
return memory;
}
public void setMemory(Map<String, Long> memory) {
this.memory = memory;
}
public void addMemory(String key, Long value) {
this.memory.put(key, value);
}

public Map<String, Integer> getThreads() {
return threads;
}
public void setThreads(Map<String, Integer> threads) {
this.threads = threads;
}
public void addThread(String key, Integer value) {
this.threads.put(key, value);
}

public Map<String, Object> getJvm() {
return jvm;
}
public void setJvm(Map<String, Object> jvm) {
this.jvm = jvm;
}
public void addJvm(String key, Object value) {
this.jvm.put(key, value);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -995,4 +995,8 @@ public interface Integration {
*/
public List<ValidationErrorMessage> validateXslt(String url, String xsltBody);

public long convertSizeToKb(double size);

public Object invokeMethod(String methodName);

}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
import java.io.File;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.lang.reflect.Method;
import java.net.URL;
import java.nio.charset.Charset;
Expand Down Expand Up @@ -111,6 +112,8 @@ public class CamelIntegration extends BaseIntegration {
private String loadReport;
private FlowLoaderReport flowLoaderReport;

private final OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();

public CamelIntegration() throws Exception {
super();
context = new DefaultCamelContext(registry);
Expand Down Expand Up @@ -2917,4 +2920,20 @@ public void addServiceByName(String className){

}

public long convertSizeToKb(double size) {
return (long) (size / 1024);
}

public Object invokeMethod(String methodName) {
try {
Class<?> unixOS = Class.forName("com.sun.management.UnixOperatingSystemMXBean");

if (unixOS.isInstance(operatingSystemMXBean))
return unixOS.getMethod(methodName).invoke(operatingSystemMXBean);

} catch (Throwable ignored) { }

return "Unknown";
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.assimbly.integrationrest;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.v3.oas.annotations.Parameter;
import org.assimbly.dil.validation.stats.BackendResponse;
import org.assimbly.integration.Integration;
import org.assimbly.util.rest.ResponseUtil;
import org.slf4j.Logger;
Expand All @@ -10,6 +12,14 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.ByteArrayOutputStream;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryUsage;
import java.lang.management.ThreadMXBean;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;


/**
* Resource to return information about the currently running Spring profiles.
Expand Down Expand Up @@ -272,4 +282,41 @@ public ResponseEntity<String> getHistoryMetrics(@Parameter(hidden = true) @Reque
}
}

@GetMapping(
path = "/integration/{integrationId}/jvm",
produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE, MediaType.TEXT_PLAIN_VALUE}
)
public ResponseEntity<String> getJvmStats(@Parameter(hidden = true) @RequestHeader("Accept") String mediaType, @PathVariable Long integrationId) throws Exception {

plainResponse = true;
integration = integrationRuntime.getIntegration();

try {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final ObjectMapper mapper = new ObjectMapper();
final BackendResponse backendResponse = new BackendResponse();
final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();

MemoryUsage mem = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();

backendResponse.addMemory("current", integration.convertSizeToKb(mem.getUsed()));
backendResponse.addMemory("max", integration.convertSizeToKb(mem.getMax()));
backendResponse.addMemory("committed", integration.convertSizeToKb(mem.getCommitted()));
backendResponse.addMemory("cached", integration.convertSizeToKb(mem.getCommitted() - mem.getUsed()));
backendResponse.addMemory("currentUsedPercentage", (mem.getUsed() * 100 / mem.getMax()));

backendResponse.addThread("threadCount", threadMXBean.getThreadCount());
backendResponse.addThread("peakThreadCount", threadMXBean.getPeakThreadCount());

backendResponse.addJvm("openFileDescriptors", integration.invokeMethod("getOpenFileDescriptorCount"));
backendResponse.addJvm("maxFileDescriptors", integration.invokeMethod("getMaxFileDescriptorCount"));

mapper.writeValue(out, backendResponse);
return ResponseUtil.createSuccessResponse(integrationId, mediaType, "/validation/{integrationId}/jvm", out.toString(StandardCharsets.UTF_8), plainResponse);
} catch (Exception e) {
log.error("Get jvm failed",e);
return ResponseUtil.createFailureResponse(integrationId, mediaType,"/integration/{integrationId}/jvm",e.getMessage());
}
}

}