-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
55 lines (45 loc) · 1.4 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
'use strict'
const {once} = require('events')
const {object} = require('@logdna/stdlib')
const config = require('./lib/config.js')
const {handleEvent} = require('./lib/event-handler.js')
const {createLoggerClient} = require('./lib/logger.js')
const {getLogs, prepareLogs} = require('./lib/transformer.js')
const {trimTags} = require('./lib/utils/index.js')
const HOSTNAME_REGEX = /[^0-9a-zA-Z\-.]/g
module.exports = {
handler
}
async function handler(event, context) {
config.validateEnvVars()
const eventData = handleEvent(event)
if (!eventData) {
const error = new Error('Cannot Parse the S3 Event')
error.meta = {event}
throw error
}
const s3params = {
Bucket: object.get(eventData, 'meta.bucket.name')
, Key: object.get(eventData, 'meta.object.key')
}
const tags = config.get('tags')
if (tags) {
config.set('tags', trimTags(tags))
}
const hostname = config.get('hostname') || s3params.Bucket
if (hostname) {
config.set('hostname', hostname.replace(HOSTNAME_REGEX, ''))
}
const logger = createLoggerClient(config)
const lines = await getLogs(s3params)
logger.on('error', console.error)
logger.on('warn', console.warn)
const logs = prepareLogs(lines, eventData)
for (const log of logs) {
const {line, opts} = log
logger.log(line, opts)
}
// Ensure logs have been flushed to LogDNA before finishing
await once(logger, 'cleared')
return 'Done!'
}