diff --git a/__tests__/sampling.unit.js b/__tests__/sampling.unit.js index 933975f..806b3d7 100644 --- a/__tests__/sampling.unit.js +++ b/__tests__/sampling.unit.js @@ -76,7 +76,7 @@ api_rules.get('/', (req,res) => { api_rules.get('/testNone', (req,res) => { req.log.trace('request #'+request++) - res.send('done') + res.send({ method: req.method, rule: req._sampleRule }) }) api_rules.get('/testTarget', (req,res) => { @@ -278,6 +278,18 @@ describe('Sampling Tests:', function() { expect(data.rule.period).toBe(60) expect(data.rule.level).toBe('debug') }) + + it('Matches first rule in rules array', async () => { + let _event = Object.assign({},event,{ path: '/testNone', queryStringParameters: { test: true } }) + let result = await new Promise(r => api_rules.run(_event,context,(e,res) => { r(res) })) + let data = JSON.parse(result.body) + + expect(data.method).toBe('GET') + expect(data.rule.target).toBe(0) + expect(data.rule.rate).toBe(0) + expect(data.rule.period).toBe(1) + expect(data.rule.level).toBe('trace') + }) }) diff --git a/lib/logger.js b/lib/logger.js index ea1bf05..fb0a2ee 100644 --- a/lib/logger.js +++ b/lib/logger.js @@ -219,13 +219,13 @@ exports.sampler = (app, req) => { }); // end for loop // Set rule reference based on route - let ref = map['__' + req.method] + let ref = typeof map['__' + req.method] === 'number' ? map['__' + req.method] - : map['__ANY'] + : typeof map['__ANY'] === 'number' ? map['__ANY'] - : wildcard['__' + req.method] + : typeof wildcard['__' + req.method] === 'number' ? wildcard['__' + req.method] - : wildcard['__ANY'] + : typeof wildcard['__ANY'] === 'number' ? wildcard['__ANY'] : -1;