jeremydaly / lambda-api

Lightweight web framework for your serverless applications
https://serverless-api.com
MIT License
1.41k stars 125 forks source link

1st entry in log sampling rules configuration is not used. #214

Closed rtaylor-logitech closed 1 year ago

rtaylor-logitech commented 1 year ago

There's a bug when configuring sampling rules.

Using the following configuration:

const api = Api(
    logger: {
        level: 'error',
        sampling: {
            target: 1,
            rate: 0.2,
            period: 30,
            rules: [
                { route: '/report', target: 0, rate: 0, period: 1 },
            ]
        },
    }
);

requests for the /report route would still end up using the default sampling configuration, instead of the route specific one.

The bug lies in the lines https://github.com/jeremydaly/lambda-api/blob/5ca68708194a26a66c65b010c00ad5ff3082dbe6/lib/logger.js#L222-L230

The code attempts to check whether there is a specific sampling rule specified for the request's route, by looking it up in the rulesMap. The rulesMap is an object, in the example above is { __ANY: 0 }. 0 is the index into the rules array.

However, the code in the lines above evaluates to -1 because 0 is falsy.

ref is then -1, which is treated as no specific rule matching, so you get the default configuration.

As a work around, one can simply specify a dummy rule before any import rules in the sample configuration, which will then happily be ignored.