This repository has been archived by the owner on Mar 31, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 152
/
CloudWatchLogsSubscriptionTransformer.java
117 lines (99 loc) · 4.51 KB
/
CloudWatchLogsSubscriptionTransformer.java
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
* Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Amazon Software License (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/asl/
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.amazonaws.services.logs.subscriptions;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.GZIPInputStream;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.amazonaws.services.kinesis.connectors.interfaces.ICollectionTransformer;
import com.amazonaws.services.kinesis.model.Record;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* Converts a Kinesis record to a collection of CloudWatchLogsEvent records.
*/
public abstract class CloudWatchLogsSubscriptionTransformer<T> implements
ICollectionTransformer<CloudWatchLogsEvent, T> {
private static final Log LOG = LogFactory.getLog(CloudWatchLogsSubscriptionTransformer.class);
private static final ObjectMapper JSON_OBJECT_MAPPER = new ObjectMapper();
private static final TypeReference<HashMap<String, String>> MAP_TYPE = new TypeReference<HashMap<String, String>>() {};
@Override
public Collection<CloudWatchLogsEvent> toClass(Record record) throws IOException {
List<CloudWatchLogsEvent> result = new ArrayList<>();
// uncompress the payload
byte[] uncompressedPayload;
try {
uncompressedPayload = uncompress(record.getData().array());
} catch (IOException e) {
LOG.error("Unable to uncompress the record. Skipping it.", e);
return result;
}
// get the JSON root node
String jsonPayload = new String(uncompressedPayload, Charset.forName("UTF-8"));
JsonNode rootNode;
try {
rootNode = JSON_OBJECT_MAPPER.readTree(new StringReader(jsonPayload));
} catch (JsonProcessingException e) {
LOG.error("Unable to parse the record as JSON. Skipping it.", e);
return result;
}
// only process records whose type is DATA_MESSAGE
JsonNode messageTypeNode = rootNode.get("messageType");
if (messageTypeNode == null || !messageTypeNode.asText().equals("DATA_MESSAGE")) {
LOG.warn("This record is not a data message. Skipping it.");
return result;
}
// extract the common attributes for all the log events in the batch
String owner = rootNode.get("owner").asText();
String logGroup = rootNode.get("logGroup").asText();
String logStream = rootNode.get("logStream").asText();
// construct the log events
for (JsonNode logEventNode : rootNode.get("logEvents")) {
String id = logEventNode.get("id").asText();
long timestamp = logEventNode.get("timestamp").asLong();
String message = logEventNode.get("message").asText();
Map<String, String> extractedFields = null;
if (logEventNode.get("extractedFields") != null) {
extractedFields = JSON_OBJECT_MAPPER.readValue(logEventNode.get("extractedFields").toString(), MAP_TYPE);
}
result.add(new CloudWatchLogsEvent(id, timestamp, message, extractedFields, owner, logGroup, logStream));
}
return result;
}
private static byte[] uncompress(byte[] compressedData) throws IOException {
byte[] buffer = new byte[1024];
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
try (GZIPInputStream gzis = new GZIPInputStream(new ByteArrayInputStream(compressedData))) {
int len;
while ((len = gzis.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
}
out.flush();
return out.toByteArray();
}
}
}