Skip to content

Commit 40c3d6e

Browse files
authored
Merge pull request #1 from scouter-project/dev
Dev
2 parents 5d0cc5c + 8b407db commit 40c3d6e

File tree

16 files changed

+772
-38
lines changed

16 files changed

+772
-38
lines changed

README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,70 @@
11
# scouter-pulse
22
Pulse is the platform for building lightweight agent program for many types of data you want to enrich with Scouter. Whether you’re interested in system performance metrics.
3+
4+
### Quick Start
5+
1. Start up scouter server with http enabled option
6+
> net_http_server_enabled=true
7+
8+
2. Register object type and counter definition list by json once. It should be returned 201.
9+
> http://server_ip:6180/register
10+
```json
11+
{
12+
"object" : {
13+
"type" : "type_name",
14+
"display" : "DisplayName"
15+
},
16+
"counters" : [
17+
{"name" : "counter1",
18+
"unit" : "cnt",
19+
"display" : "Counter1",
20+
},
21+
{"name" : "counter2",
22+
"unit" : "cnt",
23+
"display" : "Counter2",
24+
},
25+
{"name" : "counter3",
26+
"unit" : "cnt",
27+
"display" : "Counter3",
28+
"total" : false
29+
},
30+
]
31+
}
32+
```
33+
34+
3. Send object heartbeat and counter value by json periodically(recommended 2~5sec).
35+
> http://server_ip:6180/counters
36+
```json
37+
[
38+
{
39+
"object" : {
40+
"host" : "host1",
41+
"name" : "name1",
42+
"type" : "type_name",
43+
"address" : "10.10.10.10"
44+
},
45+
"counters" : [
46+
{"name" : "counter1", "value" : 55},
47+
{"name" : "counter2", "value" : 245},
48+
{"name" : "counter3", "value" : 4245}
49+
]
50+
},
51+
{
52+
"object" : {
53+
"host" : "host2",
54+
"name" : "name2",
55+
"type" : "type_name",
56+
"address" : "10.10.10.11"
57+
},
58+
"counters" : [
59+
{"name" : "counter1", "value" : 35},
60+
{"name" : "counter2", "value" : 65},
61+
{"name" : "counter3", "value" : 8888}
62+
]
63+
}
64+
]
65+
```
66+
67+
4. Then you can see realtime data on scouter client
68+
69+
70+

pulse.lib.http/pom.xml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>scouter.pulse</groupId>
8+
<artifactId>pulse.lib.http</artifactId>
9+
<version>0.1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13+
<maven.compiler.source>1.8</maven.compiler.source>
14+
<maven.compiler.target>1.8</maven.compiler.target>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>com.squareup.retrofit2</groupId>
20+
<artifactId>retrofit</artifactId>
21+
<version>2.1.0</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>com.squareup.retrofit2</groupId>
25+
<artifactId>converter-gson</artifactId>
26+
<version>2.1.0</version>
27+
</dependency>
28+
<dependency>
29+
<groupId>com.google.code.gson</groupId>
30+
<artifactId>gson</artifactId>
31+
<version>2.6.2</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>com.squareup.okhttp</groupId>
35+
<artifactId>okhttp</artifactId>
36+
<version>2.7.5</version>
37+
</dependency>
38+
<dependency>
39+
<groupId>commons-cli</groupId>
40+
<artifactId>commons-cli</artifactId>
41+
<version>1.2</version>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.projectlombok</groupId>
45+
<artifactId>lombok</artifactId>
46+
<version>1.16.10</version>
47+
<scope>provided</scope>
48+
</dependency>
49+
</dependencies>
50+
51+
<build>
52+
<plugins>
53+
<plugin>
54+
<groupId>org.apache.maven.plugins</groupId>
55+
<artifactId>maven-compiler-plugin</artifactId>
56+
<version>3.1</version>
57+
<configuration>
58+
<source>1.8</source>
59+
<target>1.8</target>
60+
</configuration>
61+
</plugin>
62+
</plugins>
63+
</build>
64+
65+
</project>
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package scouterx.pulse.common.http;
2+
3+
import retrofit2.Call;
4+
import retrofit2.Callback;
5+
import retrofit2.Response;
6+
import retrofit2.Retrofit;
7+
import retrofit2.converter.gson.GsonConverterFactory;
8+
import scouterx.pulse.common.protocol.counter.ObjectCounterBean;
9+
import scouterx.pulse.common.protocol.register.RegisterBean;
10+
11+
import java.util.List;
12+
import java.util.concurrent.BlockingQueue;
13+
import java.util.concurrent.LinkedBlockingQueue;
14+
15+
/**
16+
* @author Gun Lee ([email protected]) on 2016. 7. 31.
17+
*/
18+
public class HttpTrain extends Thread {
19+
static final String TYPE_REGISTER = "R";
20+
static final String TYPE_COUNTER_ARRAY = "A";
21+
static final String TYPE_COUNTER_LIST = "L";
22+
public static final String ENV_KEY_TARGETADDR = "pulse.targetaddr";
23+
public static final String DEFAULT_TARGETADDR = "http://localhost:6180/";
24+
25+
private static ScouterPulseService service;
26+
27+
class Bucket {
28+
String type;
29+
Object obj;
30+
31+
Bucket(String type, Object o) {
32+
this.type = type;
33+
this.obj = o;
34+
}
35+
}
36+
37+
static BlockingQueue<Bucket> queue = new LinkedBlockingQueue<>(100);
38+
39+
private static HttpTrain instance = null;
40+
41+
public final static synchronized HttpTrain getInstance() {
42+
if (instance == null) {
43+
instance = new HttpTrain();
44+
instance.setDaemon(true);
45+
instance.setName(instance.getClass().getName());
46+
instance.start();
47+
}
48+
return instance;
49+
}
50+
51+
private HttpTrain() {
52+
}
53+
54+
@Override
55+
public void run() {
56+
while (true) {
57+
Object o = null;
58+
59+
try {
60+
Bucket bucket = queue.take();
61+
switch(bucket.type) {
62+
case TYPE_REGISTER:
63+
getService().register((RegisterBean)bucket.obj).enqueue(callback);
64+
break;
65+
case TYPE_COUNTER_ARRAY:
66+
getService().counter((ObjectCounterBean[]) bucket.obj).enqueue(callback);
67+
break;
68+
case TYPE_COUNTER_LIST:
69+
getService().counter((List<ObjectCounterBean>) bucket.obj).enqueue(callback);
70+
break;
71+
default:
72+
break;
73+
}
74+
} catch (InterruptedException e) {
75+
e.printStackTrace();
76+
}
77+
}
78+
}
79+
80+
Callback callback = new Callback() {
81+
@Override
82+
public void onResponse(Call call, Response response) {
83+
//System.out.println("[SUCCESS]" + response.code());
84+
}
85+
86+
@Override
87+
public void onFailure(Call call, Throwable throwable) {
88+
System.out.println("[FAIL]" + throwable.getMessage());
89+
}
90+
};
91+
92+
private synchronized static ScouterPulseService getService() {
93+
if(service == null) {
94+
Retrofit client =new Retrofit.Builder()
95+
.baseUrl(System.getProperty(ENV_KEY_TARGETADDR, DEFAULT_TARGETADDR))
96+
.addConverterFactory(GsonConverterFactory.create())
97+
.build();
98+
service = client.create(ScouterPulseService.class);
99+
}
100+
return service;
101+
}
102+
103+
public boolean putRegisterBean(RegisterBean bean) {
104+
return queue.offer(new Bucket(TYPE_REGISTER, bean));
105+
}
106+
107+
public boolean putObjectCounterBeans(ObjectCounterBean[] beans) {
108+
if(beans == null || beans.length == 0) return true;
109+
return queue.offer(new Bucket(TYPE_COUNTER_ARRAY, beans));
110+
}
111+
112+
public boolean putObjectCounterBeans(List<ObjectCounterBean> beans) {
113+
if(beans == null || beans.size() == 0) return true;
114+
return queue.offer(new Bucket(TYPE_COUNTER_LIST, beans));
115+
}
116+
117+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package scouterx.pulse.common.http;
2+
3+
import retrofit2.Call;
4+
import retrofit2.http.Body;
5+
import retrofit2.http.Headers;
6+
import retrofit2.http.POST;
7+
import scouterx.pulse.common.protocol.counter.ObjectCounterBean;
8+
import scouterx.pulse.common.protocol.register.RegisterBean;
9+
10+
import java.util.List;
11+
12+
/**
13+
* @author Gun Lee ([email protected]) on 2016. 7. 31.
14+
*/
15+
public interface ScouterPulseService {
16+
@POST("/register")
17+
@Headers({"accept: application/json", "content-type: application/json"})
18+
Call<Void> register(@Body RegisterBean bean);
19+
20+
@POST("/counter")
21+
@Headers({"accept: application/json", "content-type: application/json"})
22+
Call<Void> counter(@Body ObjectCounterBean[] objs);
23+
24+
@POST("/counter")
25+
@Headers({"accept: application/json", "content-type: application/json"})
26+
Call<Void> counter(@Body List<ObjectCounterBean> objs);
27+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package scouterx.pulse.common.protocol.counter;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
7+
/**
8+
* @author Gun Lee ([email protected]) on 2016. 7. 30.
9+
*/
10+
@Data
11+
@AllArgsConstructor
12+
@NoArgsConstructor
13+
public class CounterValue {
14+
String name;
15+
Number value;
16+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package scouterx.pulse.common.protocol.counter;
2+
3+
import lombok.Data;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
/**
9+
* @author Gun Lee ([email protected]) on 2016. 7. 30.
10+
*/
11+
@Data
12+
public class ObjectCounterBean {
13+
private ObjectValue object;
14+
private List<CounterValue> counters;
15+
16+
private ObjectCounterBean(ObjectValue object, List<CounterValue> counters) {
17+
this.object = object;
18+
this.counters = counters;
19+
}
20+
21+
public static class Builder {
22+
private ObjectValue object;
23+
private List<CounterValue> counters = new ArrayList<>();
24+
25+
public Builder setObject(ObjectValue object) {
26+
this.object = object;
27+
return this;
28+
}
29+
30+
public Builder addCounterValue(CounterValue counter) {
31+
this.counters.add(counter);
32+
return this;
33+
}
34+
35+
public ObjectCounterBean build() {
36+
return new ObjectCounterBean(this.object, this.counters);
37+
}
38+
}
39+
}
40+
41+
42+
/*
43+
[
44+
{
45+
"object" : {
46+
"host" : "VM123",
47+
"name" : "my_server1",
48+
"type" : "redis",
49+
"address" : "10.10.10.10"
50+
},
51+
"counters" : [
52+
{"name" : "aof_rewrite_scheduled", "value" : 55},
53+
{"name" : "client_longest_output_list", "value" : 245},
54+
{"name" : "used_cpu_user", "value" : 4245}
55+
]
56+
},
57+
{
58+
"object" : {
59+
"host" : "VM123",
60+
"name" : "my_server2",
61+
"type" : "redis",
62+
"address" : "10.10.10.10"
63+
},
64+
"counters" : [
65+
{"name" : "aof_rewrite_scheduled", "value" : 35},
66+
{"name" : "client_longest_output_list", "value" : 65},
67+
{"name" : "used_cpu_user", "value" : 8888}
68+
]
69+
}
70+
71+
]
72+
*/
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package scouterx.pulse.common.protocol.counter;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
7+
/**
8+
* @author Gun Lee ([email protected]) on 2016. 7. 30.
9+
*/
10+
@Data
11+
@AllArgsConstructor
12+
@NoArgsConstructor
13+
public class ObjectValue {
14+
String host;
15+
String name;
16+
String type;
17+
String address;
18+
}

0 commit comments

Comments
 (0)