PauloPepp / axios-prometheus-adapter

Plug-and-play prometheus adapter to monitor outgoing axios requests
3 stars 1 forks source link

NPE: when an API is down (no response). It is not aggregated in metrics #1

Open gian1200 opened 3 months ago

gian1200 commented 3 months ago

When the endpoint is not found (eg. when API is down), Metrics are not updated.

Error:

TypeError: Cannot read properties of undefined (reading 'status')
at ...\node_modules\axios-prometheus-adapter\dist\lib\AxiosPrometheusAdapter.js:35:41

AxiosPrometheusAdapter.js:

...
, error => {
        const labels = {
            status_code: error.response.status, <-- line 35. Fails because response is null
            method: error.response.request.method,
            protocol: error.response.request.protocol,
            host: error.response.request.host,
            path: error.response.request.path.split('?')[0],
        };
        error.config.metadata.endTimer(labels);
        return Promise.reject(error);
    });
...

Code: https://github.com/PauloPepp/axios-prometheus-adapter/blob/c13c24a7b8d7fb91869de9a79b5f5858ab58a291/lib/AxiosPrometheusAdapter.ts#L46

DEBUG:

error.code: 'ECONNREFUSED'
error.request._currentRequest.method: 'POST'
error.request._currentRequest.protocol: 'http:'
error.request._currentRequest.host: 'localhost'
error.request._currentRequest.path.split('?')[0]: '/expected-path'

Workaround: Maybe when error.response is undefined/null, then asume status code 500 and read values from error.request._currentRequest (if _currentRequest exists, else "UNKNOWN" can be put as value on all labels, as final fallback).

gian1200 commented 1 month ago

Implemented a PR with everything mentioned above https://github.com/PauloPepp/axios-prometheus-adapter/pull/2