Skip to content

Commit a834976

Browse files
authored
fix: fix Current Process CPU plugin (#85)
* fix cpu plugin * fix cpu plugin * fix license * fix field
1 parent de9e674 commit a834976

File tree

6 files changed

+129
-213
lines changed

6 files changed

+129
-213
lines changed

agent-plugins/agent-cpu-plugin/src/main/java/cloud/erda/agent/plugin/cpu/CPUDefaultProvider.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright (c) 2021 Terminus, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package cloud.erda.agent.plugin.cpu;
218

319
import cloud.erda.agent.core.config.AgentConfig;
@@ -14,6 +30,8 @@ public interface CPUDefaultProvider {
1430
AgentConfig agentConfig = ConfigAccessor.Default.getConfig(AgentConfig.class);
1531
ServiceConfig serviceConfig = ConfigAccessor.Default.getConfig(ServiceConfig.class);
1632

33+
Metric get();
34+
1735
default void addDefaultTags(Metric metric) {
1836
metric.addTag("terminus_key", agentConfig.terminusKey()).
1937
addTag("instance_id", serviceConfig.getServiceInstanceId()).

agent-plugins/agent-cpu-plugin/src/main/java/cloud/erda/agent/plugin/cpu/CPUInfo.java

Lines changed: 0 additions & 130 deletions
This file was deleted.
Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
1+
/*
2+
* Copyright (c) 2021 Terminus, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package cloud.erda.agent.plugin.cpu;
218

3-
import cloud.erda.agent.core.metrics.Metric;
419
import cloud.erda.agent.core.metrics.MetricDispatcher;
520
import org.apache.skywalking.apm.agent.core.boot.ServiceManager;
621

@@ -10,14 +25,14 @@
1025
* @since 2022/2/21
1126
*/
1227
public class CPUStatCollector {
13-
private final CPUUsageProvider cpuUsageProvider;
28+
29+
private final ProcessCPUUsageProvider processCPUUsageProvider;
1430

1531
public CPUStatCollector() {
16-
this.cpuUsageProvider = new CPUUsageProvider();
32+
this.processCPUUsageProvider = new ProcessCPUUsageProvider();
1733
}
1834

1935
public void collect() {
20-
Metric metric = cpuUsageProvider.get();
21-
ServiceManager.INSTANCE.findService(MetricDispatcher.class).dispatch(metric);
36+
ServiceManager.INSTANCE.findService(MetricDispatcher.class).dispatch(processCPUUsageProvider.get());
2237
}
2338
}

agent-plugins/agent-cpu-plugin/src/main/java/cloud/erda/agent/plugin/cpu/CPUStatService.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright (c) 2021 Terminus, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package cloud.erda.agent.plugin.cpu;
218

319
import cloud.erda.agent.core.utils.PluginConstants;

agent-plugins/agent-cpu-plugin/src/main/java/cloud/erda/agent/plugin/cpu/CPUUsageProvider.java

Lines changed: 0 additions & 78 deletions
This file was deleted.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright (c) 2021 Terminus, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package cloud.erda.agent.plugin.cpu;
18+
19+
import cloud.erda.agent.core.metrics.Metric;
20+
import cloud.erda.agent.core.utils.DateTime;
21+
22+
import java.lang.management.ManagementFactory;
23+
import java.lang.management.ThreadMXBean;
24+
25+
public class ProcessCPUUsageProvider implements CPUDefaultProvider {
26+
27+
private static final String METRIC_NAME = "process_cpu_usage";
28+
private final ThreadMXBean threadBean;
29+
boolean isFirst = true;
30+
private long preNanoTime = System.nanoTime();
31+
private long preUsedNanoTime = 0;
32+
33+
public ProcessCPUUsageProvider() {
34+
threadBean = ManagementFactory.getThreadMXBean();
35+
}
36+
37+
public Metric get() {
38+
Metric metric = Metric.New(METRIC_NAME, DateTime.currentTimeNano()).
39+
addField("cpu_usage", this.calculateProcessCpuUsage());
40+
this.addDefaultTags(metric);
41+
return metric;
42+
}
43+
44+
/**
45+
* Calculate the CPU usage of all threads in the current process
46+
*
47+
* @return calculateProcessCpuUsage
48+
*/
49+
public double calculateProcessCpuUsage() {
50+
long currentNanoTime = System.nanoTime();
51+
long processUsedTotalNanoTime = 0;
52+
for (long id : threadBean.getAllThreadIds()) {
53+
processUsedTotalNanoTime += threadBean.getThreadCpuTime(id);
54+
}
55+
long usedNanoTime = processUsedTotalNanoTime - preUsedNanoTime;
56+
57+
if (isFirst) {
58+
isFirst = false;
59+
preNanoTime = currentNanoTime;
60+
preUsedNanoTime = processUsedTotalNanoTime;
61+
return 0;
62+
}
63+
64+
long totalPassedNanoTime = currentNanoTime - preNanoTime;
65+
66+
preNanoTime = currentNanoTime;
67+
preUsedNanoTime = processUsedTotalNanoTime;
68+
69+
double cpuUsage = ((double) usedNanoTime) / totalPassedNanoTime * 100;
70+
if (cpuUsage < 0) {
71+
return 0;
72+
}
73+
return (double) Math.round(cpuUsage * 100) / 100;
74+
}
75+
}

0 commit comments

Comments
 (0)