diff --git a/bin/logagent.js b/bin/logagent.js index d057f092..aa7a9201 100755 --- a/bin/logagent.js +++ b/bin/logagent.js @@ -70,8 +70,6 @@ var moduleAlias = { // input filters 'input-filter-k8s-containerd': '../lib/plugins/input-filter/kubernetesContainerd.js', - 'input-filter-k8s-containerd-multiline': - '../lib/plugins/input-filter/kubernetesContainerdMultiline.js', grep: '../lib/plugins/input-filter/grep.js', grok: 'logagent-input-filter-grok', // output filters diff --git a/lib/plugins/input-filter/kubernetesContainerd.js b/lib/plugins/input-filter/kubernetesContainerd.js index 05532536..941a393c 100644 --- a/lib/plugins/input-filter/kubernetesContainerd.js +++ b/lib/plugins/input-filter/kubernetesContainerd.js @@ -1,26 +1,37 @@ -var containerdSplitRegexp = /^(.+[stdout|stderr] [F|P]) / +const containerdSplitRegexp = /^(.+[stdout|stderr] [F|P]) / + +// Dictionary to store sources and log lines +/** + * Key: sourceName + * Value (Object): { streamFlag, previousStreamFlag, logLines } + */ const sources = {} + /** * sourceName - origin of the log, e.g. file name * config - properties from the config section for this plugin * data - the log message as string * callback - callback function (err, data). */ - -// fileName: app-77b4d5595b-hmjxs_default_app-80209fd578c6be842b5b8a2d6389227ccab0196b7b658bd245eed4767c9b843c.log -// meta: { -// 0: app-77b4d5595b-hmjxs, // pod -// 1: default // namespace -// 2: app-80209fd578c6be842b5b8a2d6389227ccab0196b7b658bd245eed4767c9b843c.log // container with .log suffix -// } - function parseK8sFileName (sourceName) { + /** + * SAMPLE sourceName * + * ***************** * + * sourceName: /var/log/containers/app-77b4d5595b-hmjxs_default_app-80209fd578c6be842b5b8a2d6389227ccab0196b7b658bd245eed4767c9b843c.log + * fileName: app-77b4d5595b-hmjxs_default_app-80209fd578c6be842b5b8a2d6389227ccab0196b7b658bd245eed4767c9b843c.log + * meta: { + * 0: app-77b4d5595b-hmjxs, // pod + * 1: default // namespace + * 2: app-80209fd578c6be842b5b8a2d6389227ccab0196b7b658bd245eed4767c9b843c.log // container with .log suffix + * } + */ + // cut path from /var/log/containers/__-.log // Reference: https://github.com/kubernetes/community/blob/master/contributors/design-proposals/node/kubelet-cri-logging.md - var index = sourceName.lastIndexOf('/') - var fileName = sourceName.substr(index + 1, sourceName.length) - var meta = fileName.split('_') - var info = {} + const index = sourceName.lastIndexOf('/') + const fileName = sourceName.substr(index + 1, sourceName.length) + const meta = fileName.split('_') + const info = {} if (meta.length === 3) { const { 0: name, 1: namespace, 2: containerWithLogSuffix } = meta @@ -69,12 +80,6 @@ module.exports = function (context, config, data, callback) { } sources[sourceName].streamFlag = k8sInfo.streamFlag - console.log('\n\n\n\n\n\n\n\n\n\n\n\n') - console.log('BEFORE') - console.log(sourceName) - console.log(sources[sourceName]) - console.log('\n\n\n\n\n\n\n\n\n\n\n\n') - if (sources[sourceName].streamFlag === 'P') { if (sources[sourceName].logLines === undefined) { sources[sourceName].logLines = [] @@ -83,12 +88,6 @@ module.exports = function (context, config, data, callback) { sources[sourceName].logLines.push(logLine) sources[sourceName].previousStreamFlag = 'P' - console.log('\n\n\n\n\n\n\n\n\n\n\n\n') - console.log('PARTIAL') - console.log(sourceName) - console.log(sources[sourceName]) - console.log('\n\n\n\n\n\n\n\n\n\n\n\n') - return callback(null, null) } @@ -98,10 +97,6 @@ module.exports = function (context, config, data, callback) { ) { sources[sourceName].logLines.push(logLine) const joinedLogLine = sources[sourceName].logLines.join(' ') - console.log('\n\n\n\n\n\n\n\n\n\n\n\n') - console.log('FULL') - console.log(sources[sourceName]) - console.log('\n\n\n\n\n\n\n\n\n\n\n\n') delete sources[sourceName] // a special property in context object to propagate fields to @@ -111,9 +106,6 @@ module.exports = function (context, config, data, callback) { } } - console.log('\n\n\n\n\n\n\n\n\n\n\n\n\n') - console.log(logLine) - console.log('\n\n\n\n\n\n\n\n\n\n\n\n\n') return callback(null, logLine) } return callback(null, data) diff --git a/lib/plugins/input-filter/kubernetesContainerdMultiline.js b/lib/plugins/input-filter/kubernetesContainerdMultiline.js deleted file mode 100644 index e4ca30a9..00000000 --- a/lib/plugins/input-filter/kubernetesContainerdMultiline.js +++ /dev/null @@ -1,79 +0,0 @@ -const containerdSplitRegexp = /^(.+[stdout|stderr] [F|P]) / -const sources = {} -/** - * sourceName - origin of the log, e.g. file name - * config - properties from the config section for this plugin - * data - the log message as string - * callback - callback function (err, data). - */ -module.exports = function (context, config, data, callback) { - try { - const sections = data.split(containerdSplitRegexp) - if (sections && sections.length === 3) { - const k8sInfo = {} - const meta = sections[1].split(' ') - const logLine = sections[2] - - if (meta.length === 3 && meta[0]) { - k8sInfo['@timestamp'] = new Date(meta[0]) - k8sInfo.streamName = meta[1] - k8sInfo.streamFlag = meta[2] - - const sourceName = context.sourceName - if (sources[sourceName] === undefined) { - sources[sourceName] = {} - } - sources[sourceName].streamFlag = k8sInfo.streamFlag - - console.log('\n\n\n\n\n\n\n\n\n\n\n\n') - console.log('BEFORE') - console.log(sourceName) - console.log(sources[sourceName]) - console.log('\n\n\n\n\n\n\n\n\n\n\n\n') - - if (sources[sourceName].streamFlag === 'P') { - if (sources[sourceName].logLines === undefined) { - sources[sourceName].logLines = [] - } - - sources[sourceName].logLines.push(logLine) - sources[sourceName].previousStreamFlag = 'P' - - console.log('\n\n\n\n\n\n\n\n\n\n\n\n') - console.log('PARTIAL') - console.log(sourceName) - console.log(sources[sourceName]) - console.log('\n\n\n\n\n\n\n\n\n\n\n\n') - - return callback(null, null) - } - - if ( - sources[sourceName].streamFlag === 'F' && - sources[sourceName].previousStreamFlag === 'P' - ) { - sources[sourceName].logLines.push(logLine) - const joinedLogLine = sources[sourceName].logLines.join(' ') - - console.log('\n\n\n\n\n\n\n\n\n\n\n\n') - console.log('FULL') - console.log(sources[sourceName]) - console.log('\n\n\n\n\n\n\n\n\n\n\n\n') - - delete sources[sourceName] - return callback(null, joinedLogLine) - } - } - - console.log('\n\n\n\n\n\n\n\n\n\n\n\n') - console.log(logLine) - console.log('\n\n\n\n\n\n\n\n\n\n\n\n') - return callback(null, logLine) - } - - return callback(null, data) - } catch (err) { - console.error(err) - return callback(null, data) - } -}