-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSumologicEmitter.java
More file actions
99 lines (84 loc) · 3.08 KB
/
Copy pathSumologicEmitter.java
File metadata and controls
99 lines (84 loc) · 3.08 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package com.sumologic.client.implementations;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.sumologic.client.SumologicSender;
import com.sumologic.client.KinesisConnectorForSumologicConfiguration;
import com.amazonaws.services.kinesis.connectors.KinesisConnectorConfiguration;
import com.amazonaws.services.kinesis.connectors.UnmodifiableBuffer;
import com.amazonaws.services.kinesis.connectors.interfaces.IEmitter;
/**
* This class is used to store records from a stream to Sumologic log files. It requires the use of a
* SumologicTransformer, which is able to transform records into a format that can be sent to
* Sumologic.
*/
public class SumologicEmitter implements IEmitter<String> {
private static final Log LOG = LogFactory.getLog(SumologicEmitter.class);
private SumologicSender sender;
private KinesisConnectorForSumologicConfiguration config;
private static final boolean SEND_RECORDS_IN_BATCHES = true;
public SumologicEmitter(KinesisConnectorConfiguration configuration) {
this.config = (KinesisConnectorForSumologicConfiguration) configuration;
sender = new SumologicSender(this.config.SUMOLOGIC_URL);
}
public SumologicEmitter(String url) {
sender = new SumologicSender(url);
}
@Override
public List<String> emit(final UnmodifiableBuffer<String> buffer)
throws IOException {
List<String> records = buffer.getRecords();
if (SEND_RECORDS_IN_BATCHES) {
return sendBatchConcatenating(records);
} else {
return sendRecordsOneByOne(records);
}
}
public List<String> sendBatchConcatenating(List<String> records) {
boolean success = false;
String message = "";
for(String record: records) {
message += record;
message += "\n";
}
try {
LOG.info("Sending batch of: "+records.size()+" records");
success = sender.sendToSumologic(message);
} catch (IOException e) {
LOG.warn("Couldn't send record to Sumologic: "+e.getMessage());
return records;
}
if (success)
return new ArrayList<String>();
else {
return records;
}
}
public List<String> sendRecordsOneByOne (List<String> records) {
ArrayList<String> failedRecords = new ArrayList<String>();
for (String record: records) {
try {
if (!sender.sendToSumologic(record)) {
failedRecords.add(record);
}
} catch (IOException e) {
LOG.warn("Couldn't send record: "+record);
}
}
LOG.info("Sent records: "+(records.size()-failedRecords.size())+" failed: "+failedRecords.size());
return failedRecords;
}
@Override
public void fail(List<String> records) {
for (String record : records) {
LOG.error("Could not emit record: " + record);
}
}
@Override
public void shutdown() {
}
}