Skip to content

Commit d6bb486

Browse files
committed
fixing metrics endpoint sb v2.x
1 parent 71372ad commit d6bb486

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

trampoline/src/main/java/org/ernest/applications/trampoline/collectors/MetricsCollector.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.ernest.applications.trampoline.collectors;
22

33
import org.apache.commons.collections4.queue.CircularFifoQueue;
4+
import org.ernest.applications.trampoline.entities.Instance;
45
import org.ernest.applications.trampoline.entities.Metrics;
56
import org.ernest.applications.trampoline.exceptions.CreatingSettingsFolderException;
67
import org.ernest.applications.trampoline.exceptions.ReadingEcosystemException;
@@ -33,8 +34,12 @@ public void collectMetrics() throws JSONException, InterruptedException, Creatin
3334

3435
ecosystemManager.getEcosystem().getInstances().forEach(instance ->{
3536
try {
36-
JSONObject metricsJson = new JSONObject(new RestTemplate().getForObject("http://127.0.0.1:" + instance.getPort() + instance.getActuatorPrefix() + "/metrics", String.class));
37-
Metrics metrics = buildMetricsFromJsonResponse(metricsJson);
37+
Metrics metrics;
38+
try {
39+
metrics = buildMetricsFromJsonResponseV1x(instance);
40+
}catch (Exception e){
41+
metrics = buildMetricsFromJsonResponseV2x(instance);
42+
}
3843

3944
if (metricsMap.containsKey(instance.getId())) {
4045
metricsMap.get(instance.getId()).add(metrics);
@@ -70,7 +75,9 @@ private void removeNotActiveInstances() {
7075
idsToBeDeleted.stream().forEach(id-> metricsMap.remove(id));
7176
}
7277

73-
private Metrics buildMetricsFromJsonResponse(JSONObject metricsJson) throws JSONException {
78+
private Metrics buildMetricsFromJsonResponseV1x(Instance instance) throws JSONException {
79+
JSONObject metricsJson = new JSONObject(new RestTemplate().getForObject("http://127.0.0.1:" + instance.getPort() + instance.getActuatorPrefix() + "/metrics", String.class));
80+
7481
Metrics metrics = new Metrics();
7582
metrics.setTotalMemoryKB(Long.valueOf(metricsJson.get("mem").toString()));
7683
metrics.setFreeMemoryKB(Long.valueOf(metricsJson.get("mem.free").toString()));
@@ -81,4 +88,23 @@ private Metrics buildMetricsFromJsonResponse(JSONObject metricsJson) throws JSON
8188

8289
return metrics;
8390
}
91+
92+
private Metrics buildMetricsFromJsonResponseV2x(Instance instance) throws JSONException {
93+
Metrics metrics = new Metrics();
94+
metrics.setTotalMemoryKB(getValueMetric(instance, "jvm.memory.max"));
95+
metrics.setHeapKB(0L);
96+
metrics.setInitHeapKB(0L);
97+
metrics.setUsedHeapKB(getValueMetric(instance, "jvm.memory.used"));
98+
metrics.setFreeMemoryKB(metrics.getTotalMemoryKB() - metrics.getUsedHeapKB());
99+
metrics.setDate(new SimpleDateFormat("HH:mm:ss").format(new Date()));
100+
101+
return metrics;
102+
}
103+
104+
private Long getValueMetric(Instance instance, String key) throws JSONException {
105+
JSONObject metricsJson = new JSONObject(new RestTemplate().getForObject("http://127.0.0.1:" + instance.getPort() + instance.getActuatorPrefix() + "/metrics/"+key, String.class));
106+
return Long.valueOf(metricsJson.getJSONArray("measurements").getJSONObject(0).getInt("value"));
107+
}
84108
}
109+
110+

0 commit comments

Comments
 (0)