-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.js
179 lines (165 loc) · 6.47 KB
/
index.js
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
'use strict';
const AWS = require('aws-sdk');
const http = require('http');
const zlib = require('zlib');
const request = require('request');
const baseUrl = process.env['SCALYR_BASE_URL'] || 'https://www.scalyr.com';
const addEventsUrl = baseUrl + '/addEvents';
const uploadLogsUrl = baseUrl + '/api/uploadLogs';
let logGroupOptions = {};
let userLogFile = null;
let userParserName = null;
let userServerHost = null;
if (process.env['LOG_GROUP_OPTIONS']) {
logGroupOptions = JSON.parse(process.env['LOG_GROUP_OPTIONS']);
let functionLogGroupName = process.env['AWS_LAMBDA_LOG_GROUP_NAME'].substr(1);
const logGroupName = logGroupOptions[functionLogGroupName];
if (logGroupName) {
userLogFile = logGroupName['logfile'];
userServerHost = logGroupName['serverHost'];
userParserName = logGroupName['parser'];
}
}
let defaultParserName = (process.env['PARSER_NAME'] || userParserName);
if (!defaultParserName || !defaultParserName.length) defaultParserName = 'cloudWatchLogs';
let defaultServerHost = (process.env['SERVER_HOST'] || userServerHost);
const useAddEventsApi = (process.env['USE_ADD_EVENTS_API'] == 'true');
const encryptedScalyrApiKey = process.env['SCALYR_WRITE_LOGS_KEY'];
let decryptedScalyrApiKey;
/**
* Translates a CloudWatch message into a format appropriate for submitting to the Scalyr addEvents API endpoint.
*
* @param cloudWatchMessage Incoming CloudWatch message.
* @returns {Object} Outgoing Scalyr message.
*/
function transformToAddEventsMessage(cloudWatchMessage) {
let sessionOpts = logGroupOptions[cloudWatchMessage.logGroup] || {};
let defaultSession = {
'serverHost': defaultServerHost || `cloudwatch-${cloudWatchMessage.owner}`,
'logfile': cloudWatchMessage.logGroup,
'parser': defaultParserName
};
return {
'token': decryptedScalyrApiKey,
'session': cloudWatchMessage.logStream,
'sessionInfo': Object.assign(defaultSession, sessionOpts),
'events': cloudWatchMessage.logEvents.map((cloudWatchEvent) => {
return {
'ts': `${cloudWatchEvent.timestamp}000000`,
'type': 0,
'sev': 3,
'attrs': {
// TODO make changes here if you want to parse in AWS Lambda before sending to Scalyr
'cwStream': cloudWatchMessage.logStream,
'cwId': cloudWatchEvent.id,
'message': cloudWatchEvent.message
}
};
})
};
}
/**
* Translates a CloudWatch message into a format appropriate for submitting to the Scalyr uploadLogs API endpoint.
* Note that the calling function will unpack some of these values into URL params.
*
* @param {Object} cloudWatchMessage Incoming CloudWatch message.
* @returns {Object} Outgoing Scalyr message.
*/
function transformToUploadLogsMessage(cloudWatchMessage) {
const serverHost = (defaultServerHost || `cloudwatch-${cloudWatchMessage.owner}`);
const logfile = (userLogFile || cloudWatchMessage.logGroup);
return {
'token': encodeURIComponent(decryptedScalyrApiKey),
'host': encodeURIComponent(serverHost),
'logfile': encodeURIComponent(logfile),
'logStream': encodeURIComponent(cloudWatchMessage.logStream),
'body': cloudWatchMessage.logEvents.map((cloudWatchEvent) => {
if (cloudWatchEvent.message.endsWith('\n')) {
return cloudWatchEvent.message.substr(0, cloudWatchEvent.message.length - 1);
}
return cloudWatchEvent.message;
}).join('\n')
};
}
/**
* Transforms a CloudWatch message into a format appropriate for submitting to a Scalyr API endpoint (either uploadLogs
* or addEvents). The endpoint used depends on the value of the USE_ADD_EVENTS_API environment variable, which should
* be true or false. If this environment variable isn't specified, then the uploadLogs API will be used.
*
* @param {Object} cloudWatchMessage Incoming CloudWatch message.
* @returns {Object} Outgoing Scalyr message.
*/
function transformToPost(cloudWatchMessage) {
if (useAddEventsApi) {
return {
headers: {'content-type': 'application/json'},
url: addEventsUrl,
body: JSON.stringify(transformToAddEventsMessage(cloudWatchMessage))
};
} else {
const message = transformToUploadLogsMessage(cloudWatchMessage);
return {
headers: {'content-type': 'text/plain'},
url: `${uploadLogsUrl}?token=${message.token}&host=${message.host}&logfile=${message.logfile}&server-logStream=${message.logStream}&parser=${defaultParserName}`,
body: message.body
};
}
}
/**
* Actual logic for processing a CloudWatch event. Unzips the event payload, translates it to a format appropriate
* for Scalyr, then POSTs that to the Scalyr addEvents API.
*
* @param event
* @param context
* @param callback
*/
function processEvent(event, context, callback) {
const payload = new Buffer(event.awslogs.data, 'base64');
zlib.gunzip(payload, (err, res) => {
if (err) return callback(err);
const cloudWatchMessage = JSON.parse(res.toString('utf8'));
if (cloudWatchMessage.logEvents && cloudWatchMessage.logEvents.length) {
request.post(transformToPost(cloudWatchMessage), (error, response, body) => {
console.log('Response from Scalyr:', body);
if (response && response.statusCode) {
if (response.statusCode === 200) {
const msg = `Successfully submitted ${cloudWatchMessage.logEvents.length} log events to Scalyr.`;
console.log(msg);
callback(null, msg);
} else {
let msg = `Received status code ${response.statusCode}`;
if (error) msg += ` and error '${error}'`;
msg += ' from Scalyr';
console.log(msg);
callback(null, msg);
}
}
});
}
});
}
/**
* Entry point for AWS Lambda. Handles decryption of the Scalyr "Write Logs" API key (if necessary), then
* delegates to processEvent.
*
* @param event
* @param context
* @param callback
*/
exports.handler = (event, context, callback) => {
if (decryptedScalyrApiKey) {
processEvent(event, context, callback);
} else {
// decryption code runs once and variables are stored outside of the function handler so that these
// are decrypted once per container
const kms = new AWS.KMS();
kms.decrypt({CiphertextBlob: new Buffer(encryptedScalyrApiKey, 'base64')}, (err, data) => {
if (err) {
console.log('Decryption error:', err);
return callback(err);
}
decryptedScalyrApiKey = data.Plaintext.toString('ascii');
processEvent(event, context, callback);
});
}
};