Skip to content

Commit 135dfac

Browse files
author
Adnan Rahić
authored
Merge pull request #269 from sematext/sc-10353-ecs-improvements
2 parents c101541 + 89bfc86 commit 135dfac

File tree

4 files changed

+74
-23
lines changed

4 files changed

+74
-23
lines changed

config/examples/aws-ecs-input-es-output.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ input:
55
module: input-aws-ecs
66
port: 6666
77
useIndexFromUrlPath: true
8-
workers: 4
8+
# workers: 4
99

1010
outputFilter:
1111
aws-ecs-format:
@@ -16,3 +16,4 @@ output:
1616
sematext-cloud:
1717
module: elasticsearch
1818
url: https://logsene-receiver.sematext.com
19+
# debug: true
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
options:
2+
includeOriginalLine: false
3+
4+
input:
5+
files:
6+
- '/var/log'
7+
8+
# this parses the message when it is a structured JSON
9+
parser:
10+
json:
11+
enabled: true
12+
13+
output:
14+
sematext:
15+
module: elasticsearch
16+
url: https://logsene-receiver.sematext.com
17+
index: <add your logs token>

lib/plugins/input/aws-ecs.js

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const throng = require('throng')
44
const extractTokenRegEx = /([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})/
55
const tokenFormatRegEx = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/
66
const TokenBlacklist = require('../../util/token-blacklist.js')
7+
const zlib = require('zlib')
78

89
class AwsEcs {
910
constructor (config, eventEmitter) {
@@ -97,7 +98,6 @@ class AwsEcs {
9798
if (body.length === 0) {
9899
return
99100
}
100-
101101
const self = this
102102
const docs = JSON.parse(body)
103103
if (docs && docs.length > 0) {
@@ -127,6 +127,8 @@ class AwsEcs {
127127
const path = req.url.split('/')
128128
let token = null
129129
let bodyIn = ''
130+
const buffer = []
131+
130132
if (self.config.useIndexFromUrlPath === true && path.length > 1) {
131133
if (path[1] && path[1].length > 31 && tokenFormatRegEx.test(path[1])) {
132134
const match = path[1].match(extractTokenRegEx)
@@ -147,25 +149,56 @@ class AwsEcs {
147149
res.end(`invalid logs token in url ${req.url}`)
148150
return
149151
}
150-
req.on('data', function (data) {
151-
bodyIn += String(data)
152-
})
153-
req.on('end', function endHandler () {
154-
try {
155-
self.parseRes(req.headers, bodyIn, token)
156-
} catch (err) {
157-
if (self.config.debug) {
158-
consoleLogger.error('Error in ECS HttpHandler: ' + err)
152+
153+
const isGzip = req.headers['content-encoding'] === 'gzip'
154+
// if the JSON content is gzipped this will unzip it and parse it into a stringified JSON string
155+
if (isGzip) {
156+
const gunzip = zlib.createGunzip()
157+
req.pipe(gunzip)
158+
159+
gunzip.on('data', function onGzipHandler (data) {
160+
buffer.push(data.toString())
161+
})
162+
gunzip.on('end', function endGzipHandler () {
163+
try {
164+
const body = buffer.join('')
165+
self.parseRes(req.headers, body, token)
166+
} catch (err) {
167+
if (self.config.debug) {
168+
consoleLogger.error('Error in ECS HttpHandler: ' + err)
169+
}
170+
171+
res.writeHead(500, { 'Content-Type': 'text/plain' })
172+
res.end(`Invalid gzip input: ${err}\n`)
173+
return
159174
}
175+
// send response to client
176+
res.writeHead(200, { 'Content-Type': 'text/plain' })
177+
res.end('OK\n')
178+
})
160179

161-
res.writeHead(500, { 'Content-Type': 'text/plain' })
162-
res.end(`Invalid json input: ${err}\n`)
163-
return
164-
}
165-
// send response to client
166-
res.writeHead(200, { 'Content-Type': 'text/plain' })
167-
res.end('OK\n')
168-
})
180+
return
181+
} else {
182+
req.on('data', function onHandler (data) {
183+
bodyIn += String(data)
184+
})
185+
req.on('end', function endHandler () {
186+
try {
187+
self.parseRes(req.headers, bodyIn, token)
188+
} catch (err) {
189+
if (self.config.debug) {
190+
consoleLogger.error('Error in ECS HttpHandler: ' + err)
191+
}
192+
193+
res.writeHead(500, { 'Content-Type': 'text/plain' })
194+
res.end(`Invalid json input: ${err}\n`)
195+
return
196+
}
197+
// send response to client
198+
res.writeHead(200, { 'Content-Type': 'text/plain' })
199+
res.end('OK\n')
200+
})
201+
}
169202
} catch (err) {
170203
res.statusCode = 500
171204
res.end()

lib/plugins/output-filter/aws-ecs-format.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ function formatAwsEcs (context, config, eventEmitter, log, callback) {
6565
}
6666

6767
if (data && data._type) {
68-
const type = `${data._type}`
6968
delete data['@timestamp']
7069
delete data.logSource
7170
delete data._type
72-
log.type = type
73-
log[type] = {}
74-
Object.assign(log[type], data)
71+
72+
log.type = 'ecs'
73+
log[log.source] = {}
74+
Object.assign(log[log.source], data)
7575
}
7676
return callback(null, log)
7777
})

0 commit comments

Comments
 (0)