Skip to content

Commit 32fc86d

Browse files
authored
Merge pull request #55 from ErnestOrt/42-migrate-sb2
42 migrate sb2
2 parents 677d145 + cc35291 commit 32fc86d

File tree

8 files changed

+316
-752
lines changed

8 files changed

+316
-752
lines changed

README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
# Trampoline [![Twitter](https://img.shields.io/twitter/follow/espadrine.svg?style=social&logo=twitter&label=Follow)](https://twitter.com/TrampolineSB)
22

33
[![Build Status](https://travis-ci.org/stunstunstun/awesome-spring-boot.svg?branch=master)](https://travis-ci.org/stunstunstun/awesome-spring-boot)
4-
[![Wercker](https://img.shields.io/badge/spring--boot-1.5.3.RELEASE-brightgreen.svg)]()
5-
[![Wercker](https://img.shields.io/badge/java-8-brightgreen.svg)]()
6-
[![Wercker](https://img.shields.io/badge/gradle-3.5-brightgreen.svg)]()
7-
[![Maven metadata URI](https://img.shields.io/maven-metadata/v/http/central.maven.org/maven2/com/google/code/gson/gson/maven-metadata.xml.svg)]()
84

95
*NOTE: If you want to use examples provided (microservice-example-gradle & microservice-example-maven), please, clone the repository instead of downloading release zip. They need .git folder due to git related plugins specified on pom.xml and build.gradle*
106

@@ -20,7 +16,7 @@ The aim is to **help during the course of developing an application based on the
2016

2117
Also you will be able to:
2218

23-
* Admin Spring Boot locally
19+
* Admin Spring Boot locally (fully support for v1.x and v2.x)
2420
* Use configurable Actuator Endpoints and VM arguments
2521
* Register nicroservices directly from a GIT url
2622
* Monitor memory usage for each instance, capturing their metrics every 30 seconds

microservice-example-gradle-v2x/service.log

Lines changed: 100 additions & 233 deletions
Large diffs are not rendered by default.
Binary file not shown.

microservice-example-maven-v2x/service.log

Lines changed: 149 additions & 508 deletions
Large diffs are not rendered by default.
Binary file not shown.

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+

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

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,34 @@ public List<TraceActuator> getTraces(String idInstance) throws CreatingSettingsF
2828
List<TraceActuator> traces = new ArrayList<>();
2929

3030
Instance instance = ecosystemManager.getEcosystem().getInstances().stream().filter(i -> i.getId().equals(idInstance)).findAny().get();
31-
JSONArray traceArrayJson = new JSONArray(new RestTemplate().getForObject("http://127.0.0.1:" + instance.getPort() + instance.getActuatorPrefix() + "/trace", String.class));
31+
JSONArray traceArrayJson;
3232

33+
try {
34+
traceArrayJson = new JSONArray(new RestTemplate().getForObject("http://127.0.0.1:" + instance.getPort() + instance.getActuatorPrefix() + "/trace", String.class));
35+
buildTracesV1x(traces, traceArrayJson);
36+
}catch (Exception e){
37+
traceArrayJson = new JSONObject(new RestTemplate().getForObject("http://127.0.0.1:" + instance.getPort() + instance.getActuatorPrefix() + "/httptrace", String.class)).getJSONArray("traces");
38+
buildTracesV2x(traces, traceArrayJson);
39+
}
40+
41+
return traces;
42+
}
43+
44+
private void buildTracesV2x(List<TraceActuator> traces, JSONArray traceArrayJson) throws JSONException {
45+
for (int i = 0; i < traceArrayJson.length(); i++) {
46+
JSONObject traceJson = traceArrayJson.getJSONObject(i);
47+
48+
org.ernest.applications.trampoline.entities.TraceActuator traceActuator = new org.ernest.applications.trampoline.entities.TraceActuator();
49+
traceActuator.setDate(traceJson.getString("timestamp"));
50+
traceActuator.setMethod(traceJson.getJSONObject("request").getString("method"));
51+
traceActuator.setPath(traceJson.getJSONObject("request").getString("uri"));
52+
traceActuator.setStatus(traceJson.getJSONObject("response").getString("status"));
53+
traces.add(traceActuator);
54+
}
55+
56+
}
57+
58+
private void buildTracesV1x(List<TraceActuator> traces, JSONArray traceArrayJson) throws JSONException {
3359
for (int i = 0; i < traceArrayJson.length(); i++) {
3460
JSONObject traceJson = traceArrayJson.getJSONObject(i);
3561

@@ -40,7 +66,5 @@ public List<TraceActuator> getTraces(String idInstance) throws CreatingSettingsF
4066
traceActuator.setStatus(traceJson.getJSONObject("info").getJSONObject("headers").getJSONObject("response").getString("status"));
4167
traces.add(traceActuator);
4268
}
43-
44-
return traces;
4569
}
4670
}

trampoline/src/main/java/org/ernest/applications/trampoline/entities/TraceActuator.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,14 @@ public String getStatus() {
3939
public void setStatus(String status) {
4040
this.status = status;
4141
}
42+
43+
@Override
44+
public String toString() {
45+
return "TraceActuator{" +
46+
"date='" + date + '\'' +
47+
", method='" + method + '\'' +
48+
", path='" + path + '\'' +
49+
", status='" + status + '\'' +
50+
'}';
51+
}
4252
}

0 commit comments

Comments
 (0)