Skip to content

Commit 4924bd4

Browse files
committed
fix: Add missing exclude_regex logic
A previous refactor accidentally removed the code to ignore lines that match a specific pattern. Semver: patch Ref: LOG-7472
1 parent 06bf28e commit 4924bd4

File tree

4 files changed

+85
-16
lines changed

4 files changed

+85
-16
lines changed

.taprc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ coverage-report:
1515
files:
1616
- test/**/*.js
1717
statements: 71
18-
branches: 58
18+
branches: 57
1919
functions: 77
2020
lines: 71

index.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,6 @@ function loadConfig(program) {
113113
parsedConfig.exclude = process.env.LOGDNA_EXCLUDE
114114
}
115115

116-
// allow exclude regex to be passed via env
117-
if (process.env.LOGDNA_EXCLUDE_REGEX) {
118-
parsedConfig.exclude_regex = process.env.LOGDNA_EXCLUDE_REGEX
119-
}
120-
121116
if (process.env.USEJOURNALD) {
122117
parsedConfig.usejournald = process.env.USEJOURNALD
123118
}
@@ -190,12 +185,12 @@ function loadConfig(program) {
190185
saveMessages.push(`Exclusions: ${processed.diff} been saved to config.`)
191186
}
192187

193-
if (program.excludeRegex) {
194-
parsedConfig.exclude_regex = program.excludeRegex
195-
// strip leading and trailing / if exists
196-
if (parsedConfig.exclude_regex.substring(0, 1) === '/' && parsedConfig.exclude_regex.substring(parsedConfig.exclude_regex.length - 1) === '/') {
197-
parsedConfig.exclude_regex = parsedConfig.exclude_regex.substring(1, parsedConfig.exclude_regex.length - 1)
198-
}
188+
if (program.excludeRegex || process.env.LOGDNA_EXCLUDE_REGEX) {
189+
const re = program.excludeRegex || process.env.LOGDNA_EXCLUDE_REGEX
190+
// FIXME(darinspivey) - Error prone. Doesn't support modifiers or ending with
191+
// a slash. The user should be forced to provide open and closing slashes,
192+
// otherwise it's too hard to know what's part of the pattern text.
193+
parsedConfig.exclude_regex = re.replace(/^\//, '').replace(/\/$/, '')
199194
saveMessages.push(`Exclude pattern: /${parsedConfig.exclude_regex}/ been saved to config.`)
200195
}
201196

@@ -230,6 +225,10 @@ function loadConfig(program) {
230225
, rescanTimer: null
231226
}
232227

228+
if (config.exclude_regex) {
229+
config.exclude_regex = new RegExp(config.exclude_regex)
230+
}
231+
233232
return getos(cb)
234233
}
235234
, (distro, cb) => {

lib/file-utilities.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,17 @@ function getFiles(config, dir, callback) {
102102

103103
function streamFiles(config, logfiles, callback) {
104104
logfiles.forEach((file) => {
105-
let tail, i
105+
let tail
106106

107107
if (os.platform() !== 'win32' && config.TAIL_MODE === 'unix') {
108108
debug('tailing: ' + file)
109109
tail = spawn('tail', ['-n0', '-F', file])
110110
tail.stdout.on('data', (data) => {
111111
const lines = data.toString().trim().split('\n')
112-
for (i = 0; i < lines.length; i++) {
112+
for (const line of lines) {
113+
if (config.exclude_regex && config.exclude_regex.test(line)) continue
113114
client.log.agentLog({
114-
line: lines[i]
115+
line
115116
, f: file
116117
})
117118
}
@@ -128,6 +129,7 @@ function streamFiles(config, logfiles, callback) {
128129
tail = TailReadStream.tail(file, config)
129130
tail.pipe(new Splitter())
130131
.on('data', (line) => {
132+
if (config.exclude_regex && config.exclude_regex.test(line)) return
131133
client.log.agentLog({
132134
line
133135
, f: file

test/integration/start.js

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const fileUtilities = require('../../lib/file-utilities.js')
1010

1111
nock.disableNetConnect()
1212

13-
test('start() calls healthcheck and tails a log', (t) => {
13+
test('start() creates a client logger and tails a log', (t) => {
1414
t.plan(2)
1515
t.on('end', () => {
1616
nock.cleanAll()
@@ -69,3 +69,71 @@ test('start() calls healthcheck and tails a log', (t) => {
6969
await fs.promises.appendFile(logPath, 'Here is my line1\nHere is my line 2\n', 'utf8')
7070
}, 500)
7171
})
72+
73+
test('exclude_regex successfully ignores lines', (t) => {
74+
t.plan(2)
75+
t.on('end', () => {
76+
nock.cleanAll()
77+
fileUtilities.gracefulShutdown()
78+
})
79+
80+
const logname = 'regex-testing.log'
81+
const tempDir = t.testdir({
82+
[logname]: ''
83+
})
84+
const logPath = path.join(tempDir, logname)
85+
86+
const config = {
87+
...constants
88+
, hostname: 'MyHostMachine'
89+
, UserAgent: 'logdna-agent/1.6.5 (darwin)'
90+
, mac: '3c:22:fb:27:72:f5'
91+
, ip: '192.168.1.9'
92+
, COMPRESS: false
93+
, logdir: [tempDir]
94+
, key: '<MY KEY HERE>'
95+
, exclude_regex: /\bNOPE\b/
96+
}
97+
98+
nock(config.LOGDNA_URL)
99+
.post(/.*/, (body) => {
100+
const expected = {
101+
e: 'ls'
102+
, ls: [
103+
{
104+
line: 'This line is good'
105+
, f: logPath
106+
}
107+
, {
108+
line: 'Good also'
109+
, f: logPath
110+
}
111+
, {
112+
line: 'Good because of boundary failure with xNOPEx'
113+
, f: logPath
114+
}
115+
]
116+
}
117+
t.match(body, expected, 'Ingester POST body is correct')
118+
return true
119+
})
120+
.query((qs) => {
121+
t.match(qs, {
122+
hostname: config.hostname
123+
, ip: config.ip
124+
, mac: config.mac
125+
, tags: ''
126+
}, 'Ingester POST QUERY_STRING is correct')
127+
return true
128+
})
129+
.reply(200, 'Ingested')
130+
131+
start(config)
132+
133+
setTimeout(async () => {
134+
const lines = 'This line is good\nThis line is NOPE\nGood also\n'
135+
+ 'Good because of boundary failure with xNOPEx\nAnd NOPE way this is good'
136+
137+
await fs.promises.appendFile(logPath, lines, 'utf8')
138+
}, 500)
139+
})

0 commit comments

Comments
 (0)