Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
71 lines (61 loc) · 2.6 KB
/
Copy pathMain.java
File metadata and controls
71 lines (61 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package io.github.binaryfoo;
import io.github.binaryfoo.gclog.GCEvent;
import io.github.binaryfoo.gclog.JavaParser;
import org.apache.commons.io.IOUtils;
import org.elasticsearch.action.bulk.BulkProcessor;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.joda.time.DateTime;
import scala.Tuple2;
import scala.collection.JavaConversions;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.zip.GZIPInputStream;
/**
* Read a jvm garbage collection log and push each event to an elasticsearch index named gc.
*/
public class Main {
public static void main(String[] args) throws IOException, InterruptedException {
Client client = TransportClient.builder().build()
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
BulkProcessor processor = BulkProcessor.builder(client, LISTENER).build();
String gcLog = IOUtils.toString(new GZIPInputStream(new FileInputStream(args[0])));
List<GCEvent> gcEvents = JavaParser.parseLog(gcLog);
for (GCEvent event : gcEvents) {
XContentBuilder b = XContentFactory.jsonBuilder().startObject();
for (Tuple2<String, Object> attribute : JavaConversions.asJavaIterable(event.toExport())) {
String name = attribute._1();
Object value = attribute._2();
if (value instanceof DateTime) {
b.field(name, ((DateTime) value).toDate());
} else {
b.field(name, value);
}
}
processor.add(new IndexRequest("gc", "gc").source(b.endObject()));
}
processor.awaitClose(1, TimeUnit.MINUTES);
client.close();
}
private static final BulkProcessor.Listener LISTENER = new BulkProcessor.Listener() {
public void beforeBulk(long executionId, BulkRequest request) {
System.out.println("before " + executionId);
}
public void afterBulk(long executionId, BulkRequest request, BulkResponse response) {
System.out.println("after " + executionId);
}
public void afterBulk(long executionId, BulkRequest request, Throwable failure) {
System.err.println("after " + executionId);
failure.printStackTrace();
}
};
}