Skip to content

Commit

Permalink
Change CPU code for MQAppliance to be accumulated
Browse files Browse the repository at this point in the history
  • Loading branch information
liurui-1 committed Jun 14, 2024
1 parent 3ad33e4 commit 63a8e94
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
2 changes: 1 addition & 1 deletion host/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group = "com.instana.dc"
version = "0.1.4"
version = "0.1.5"

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
package com.instana.dc.host.impl.mqappliance;

import com.instana.dc.host.AbstractHostDc;
import com.instana.dc.host.HostDcUtil;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.semconv.ResourceAttributes;
Expand All @@ -33,6 +31,9 @@ public class MqApplianceDc extends AbstractHostDc {
private Process process;
protected BufferedReader bufferedReader;

private long cpuTime = 0;
private long cpuTimeIdle = 0;

public MqApplianceDc(Map<String, Object> properties, String applianceSystem) {
super(properties, applianceSystem);
applianceHost = (String) properties.get(APPLIANCE_HOST);
Expand Down Expand Up @@ -102,7 +103,19 @@ public void collectData() {
if (tokens.length >= 7) {
String[] systemMetricsTokens = tokens[0].split(":");
if (systemMetricsTokens.length == 6) {
getRawMetric(SYSTEM_CPU_TIME_NAME).setValue(MqApplianceUtil.getApplianceCpuUsageResults(Long.parseLong(systemMetricsTokens[0])));
long cpuUsage = Long.parseLong(systemMetricsTokens[0]);
if (cpuUsage >= 0) {
long cpuTime1 = cpuTime + cpuUsage;
long cpuTimeIdle1 = cpuTimeIdle + (100 - cpuUsage);
if (cpuTime1 >= 0 && cpuTimeIdle1 >= 0) {
cpuTime = cpuTime1;
cpuTimeIdle = cpuTimeIdle1;
} else { //Reset
cpuTime = 0;
cpuTimeIdle = 0;
}
getRawMetric(SYSTEM_CPU_TIME_NAME).setValue(MqApplianceUtil.getApplianceCpuUsageResults(cpuTime, cpuTimeIdle));
}
getRawMetric(SYSTEM_CPU_LOAD1_NAME).setValue(Double.parseDouble(systemMetricsTokens[1]));
getRawMetric(SYSTEM_CPU_LOAD5_NAME).setValue(Double.parseDouble(systemMetricsTokens[2]));
getRawMetric(SYSTEM_CPU_LOAD15_NAME).setValue(Double.parseDouble(systemMetricsTokens[3]));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,19 @@
public class MqApplianceUtil {
private static final Logger logger = Logger.getLogger(MqApplianceUtil.class.getName());

public static List<SimpleQueryResult> getApplianceCpuUsageResults(Long cpuUsage) {
public static List<SimpleQueryResult> getApplianceCpuUsageResults(long cpuTime, long cpuTimeIdle) {
List<SimpleQueryResult> results = new ArrayList<SimpleQueryResult>(2);
if (cpuUsage != null) {
SimpleQueryResult resultUser = new SimpleQueryResult(cpuUsage);
resultUser.setKey("user");
resultUser.setAttribute("cpu", "cpu");
resultUser.setAttribute("state", "user");
results.add(resultUser);
SimpleQueryResult resultIdle = new SimpleQueryResult(100 - cpuUsage);
resultIdle.setKey("idle");
resultIdle.setAttribute("cpu", "cpu");
resultIdle.setAttribute("state", "idle");
results.add(resultIdle);
return results;
}
return null;
SimpleQueryResult resultUser = new SimpleQueryResult(cpuTime);
resultUser.setKey("user");
resultUser.setAttribute("cpu", "cpu");
resultUser.setAttribute("state", "user");
results.add(resultUser);
SimpleQueryResult resultIdle = new SimpleQueryResult(cpuTimeIdle);
resultIdle.setKey("idle");
resultIdle.setAttribute("cpu", "cpu");
resultIdle.setAttribute("state", "idle");
results.add(resultIdle);
return results;
}

public static List<SimpleQueryResult> getApplianceMemUsageResults(Long usedMem, Long freeMem) {
Expand Down

0 comments on commit 63a8e94

Please sign in to comment.