-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathindex.js
246 lines (214 loc) · 7.97 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/**
* Copyright (c) 2017, Neap Pty Ltd.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
const axios = require('axios')
const aws4 = require('aws4')
const getRequestParams = (method, region, payload, keys={}) => {
if (!region)
throw new Error('Missing required argument: \'region\' is required.')
if (!method)
throw new Error('Missing required argument: \'method\' is required.')
let opts = {
service: 'logs',
region: region,
path: `/?Action=${method}`,
headers: {
'X-Amz-Target': `Logs_20140328.${method}`,
'Accept': 'application/json',
'Content-Type': 'application/x-amz-json-1.1'
}
}
if (payload)
opts.body = JSON.stringify(payload)
if (keys.accessKeyId && keys.secretAccessKey)
aws4.sign(opts, { accessKeyId: keys.accessKeyId, secretAccessKey: keys.secretAccessKey })
else
aws4.sign(opts)
return {
uri: `https://${opts.hostname}${opts.path}`,
headers: opts.headers
}
}
const createLogStream = (logStreamName, { logGroupName, region, accessKeyId, secretAccessKey, local }) => {
if (local && local != 'false')
return Promise.resolve({ message: 'No need to create stream. Local mode is on.' })
if (!logGroupName)
throw new Error('Missing required argument: \'logGroupName\' is required.')
if (!logStreamName)
throw new Error('Missing required argument: \'logStreamName\' is required.')
if (!secretAccessKey)
throw new Error('Missing required argument: \'secretAccessKey\' is required.')
if (!accessKeyId)
throw new Error('Missing required argument: \'accessKeyId\' is required.')
if (!region)
throw new Error('Missing required argument: \'region\' is required.')
const service = 'CreateLogStream'
const payload = {
logGroupName: logGroupName,
logStreamName: logStreamName
}
const { uri, headers } = getRequestParams(service, region, payload, { accessKeyId, secretAccessKey })
const request = axios.create({
baseURL: uri,
headers: headers
})
return request.post('', payload)
.then(results => results.data)
.catch(err => {
throw new Error(err.response.data.message)
})
}
const _sequenceTokens = new Map()
/**
* Adds logs to a AWS CloudWatch log stream
* @param {String|Object|Array} entry If type is Object it must be structured as follow: { message: ..., timestamp: ... }
* where 'message' must be a string and 'timestamp' must be an UTC date number
* (e.g. Date.now())
* If type is Array, then each item must either be a string or an object { message: ..., timestamp: ... }
* following the same rules as above.
* @param {String} logGroupName [description]
* @param {String} logStreamName [description]
* @param {String} region [description]
* @param {Object} keys { accessKeyId: ..., secretAccessKey: ... }
* @param {String} sequenceToken [description]
* @param {Number} retryCount [description]
* @return {Promise} [description]
*/
const addLogsToStream = (entry, logGroupName, logStreamName, region, keys={}, sequenceToken='', retryCount=0) => {
if (!logGroupName)
throw new Error('Missing required argument: \'logGroupName\' is required.')
if (!logStreamName)
throw new Error('Missing required argument: \'logStreamName\' is required.')
if (!entry)
throw new Error('Missing required argument: \'entry\' is required.')
const entryType = typeof(entry)
const now = Date.now()
const events =
// entry is a message
entryType == 'string' ? [{ message: entry, timestamp: now }] :
// entry is a well-formatted log event
entryType == 'object' && entry.timestamp ? [{ message: entry.message, timestamp: entry.timestamp }] :
// entry is an array of items
entry.length > 0 ? entry.map(e =>
typeof(e) == 'string' ? { message: e, timestamp: now } :
e.timestamp ? { message: e.message, timestamp: e.timestamp } : null).filter(x => x)
: null
const nothingToLog = events == null
const service = 'PutLogEvents'
let payload = {
logEvents: events,
logGroupName: logGroupName,
logStreamName: logStreamName
}
const tokenKey = logGroupName + '__' + logStreamName
sequenceToken = sequenceToken || _sequenceTokens.get(tokenKey)
if (sequenceToken)
payload.sequenceToken = sequenceToken
const { uri, headers } = getRequestParams(service, region, payload, keys)
const request = axios.create({
baseURL: uri,
headers: headers
})
return retryCount > 3 || nothingToLog ? Promise.resolve(null) : request.post('', payload)
.then(results => {
//console.log('Yes')
const token = results.data.nextSequenceToken
_sequenceTokens.set(tokenKey, token)
})
.catch(err => {
//console.log('Oops')
const token = err.response && err.response.data && err.response.data.expectedSequenceToken
if (token) {
_sequenceTokens.set(tokenKey, token)
retryCount += 1
return addLogsToStream(entry, logGroupName, logStreamName, region, keys, token, retryCount)
}
else
if (err.response && err.response.data) {
err = err.response.data
}
console.error(err)
})
}
const delayFn = (fn,time) => makeQuerablePromise((new Promise((onSuccess) => setTimeout(() => onSuccess(), time))).then(() => fn()))
const makeQuerablePromise = promise => {
// Don't modify any promise that has been already modified.
if (promise.isResolved) return promise
// Set initial state
let isPending = true
let isRejected = false
let isFulfilled = false
// Observe the promise, saving the fulfillment in a closure scope.
let result = promise.then(
v => {
isFulfilled = true
isPending = false
return v
},
e => {
isRejected = true
isPending = false
throw e
}
)
result.isFulfilled = () => isFulfilled
result.isPending = () => isPending
result.isRejected = () => isRejected
return result
}
const _logbuffer = new WeakMap()
const Logger = class {
constructor({ logGroupName, logStreamName, region, accessKeyId, secretAccessKey, uploadFreq, local }) {
if (!logGroupName)
throw new Error('Missing required argument: \'logGroupName\' is required.')
if (!logStreamName)
throw new Error('Missing required argument: \'logStreamName\' is required.')
if (!region)
throw new Error('Missing required argument: \'region\' is required.')
if (!secretAccessKey)
throw new Error('Missing required argument: \'secretAccessKey\' is required.')
if (!accessKeyId)
throw new Error('Missing required argument: \'accessKeyId\' is required.')
const keys = accessKeyId && secretAccessKey ? { accessKeyId, secretAccessKey } : {}
let log
if (!uploadFreq || uploadFreq < 0) {
log = (...args) => {
const logs = (args || []).map(x => JSON.stringify(x))
// console.log('Logging now...')
// console.log(logs)
addLogsToStream(logs, logGroupName, logStreamName, region, keys)
}
}
else {
log = (...args) => {
// 1. Accumulate all logs
const now = Date.now()
const logs = (args || []).map(x => ({ message: JSON.stringify(x), timestamp: now }))
let latestBuffer = (_logbuffer.get(this) || { latest: now, data: [], job: null })
latestBuffer.data = latestBuffer.data.concat(logs)
// 2. If no job has ever been started, start it, or if the job is ready to process more
if (!latestBuffer.job || !latestBuffer.job.isPending()) {
latestBuffer.job = makeQuerablePromise(delayFn(() => {
const { latest, data, job } = (_logbuffer.get(this) || { latest: now, data: [], job: null })
// console.log('Finally logging now...')
// console.log(data)
_logbuffer.set(this, { latest, data:[], job })
addLogsToStream(data, logGroupName, logStreamName, region, keys)
}, uploadFreq))
}
//console.log('Buffering logs now...')
// 3. In any case, memoize
_logbuffer.set(this, latestBuffer)
}
}
this.log = local && local != 'false' ? console.log : log
}
}
module.exports = {
createLogStream,
Logger
}