1
1
package org .ernest .applications .trampoline .collectors ;
2
2
3
3
import org .apache .commons .collections4 .queue .CircularFifoQueue ;
4
+ import org .ernest .applications .trampoline .entities .Instance ;
4
5
import org .ernest .applications .trampoline .entities .Metrics ;
5
6
import org .ernest .applications .trampoline .exceptions .CreatingSettingsFolderException ;
6
7
import org .ernest .applications .trampoline .exceptions .ReadingEcosystemException ;
@@ -33,8 +34,12 @@ public void collectMetrics() throws JSONException, InterruptedException, Creatin
33
34
34
35
ecosystemManager .getEcosystem ().getInstances ().forEach (instance ->{
35
36
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
+ }
38
43
39
44
if (metricsMap .containsKey (instance .getId ())) {
40
45
metricsMap .get (instance .getId ()).add (metrics );
@@ -70,7 +75,9 @@ private void removeNotActiveInstances() {
70
75
idsToBeDeleted .stream ().forEach (id -> metricsMap .remove (id ));
71
76
}
72
77
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
+
74
81
Metrics metrics = new Metrics ();
75
82
metrics .setTotalMemoryKB (Long .valueOf (metricsJson .get ("mem" ).toString ()));
76
83
metrics .setFreeMemoryKB (Long .valueOf (metricsJson .get ("mem.free" ).toString ()));
@@ -81,4 +88,23 @@ private Metrics buildMetricsFromJsonResponse(JSONObject metricsJson) throws JSON
81
88
82
89
return metrics ;
83
90
}
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
+ }
84
108
}
109
+
110
+
0 commit comments