christiangalsterer / pg-promise-prometheus-exporter

A prometheus exporter for pg-promise
MIT License
0 stars 0 forks source link

bug: Failed in exporter with pg-promise 11.9.0 #21

Closed apletnev closed 1 month ago

apletnev commented 1 month ago

Environment:

The code fails on the onReceive method when trying to get event.ctx.ctx.success.

    onReceive(event) {
        try {
            if (event.result !== undefined) {
                this.commands.observe((0, utils_1.mergeLabelsWithStandardLabels)({ host: event.ctx.client.host + ':' + event.ctx.client.port, database: event.ctx.client.database, command: event.result.command, status: this.getStatus(event.ctx.ctx.success) }, this.options.defaultLabels), event.result.duration / 1000);
            }
        }
        catch (error) {
            console.error('An error occurred in the receive event handling', error);
        }
    }

Stacktrace:

An error occurred in the receive event handling TypeError: Cannot read properties of undefined (reading 'success')
    at PgPromisePrometheusExporter.onReceive (/Users/alex/Projects/my_api/node_modules/@christiangalsterer/pg-promise-prometheus-exporter/dist/pgPromisePrometheusExporter.js:98:248)
    at Events.receive (/Users/alex/Projects/my_api/node_modules/pg-promise/lib/events.js:215:25)
    at Query.<anonymous> (/Users/alex/Projects/my_api/node_modules/pg-promise/lib/query.js:181:40)
    at Query.handleReadyForQuery (/Users/alex/Projects/my_api/node_modules/pg/lib/query.js:142:14)
    at Client._handleReadyForQuery (/Users/alex/Projects/my_api/node_modules/pg/lib/client.js:312:19)
    at Connection.emit (node:events:514:28)
    at /Users/alex/Projects/my_api/node_modules/pg/lib/connection.js:117:12
    at Parser.parse (/Users/alex/Projects/my_api/node_modules/pg-protocol/dist/parser.js:36:17)
    at Socket.<anonymous> (/Users/alex/Projects/my_api/node_modules/pg-protocol/dist/index.js:11:42)
christiangalsterer commented 1 month ago

Thanks for reporting the issue. I will look into.

christiangalsterer commented 1 month ago

Can you please provide some more details on how to did execute the query? Was it with query on a client object, or a pool object or as part of a tasks or transaction?

christiangalsterer commented 1 month ago

I think I found the issue. The context is only set if a query is executed inside a task/transaction. Unfortunately this is not reflected in the code of pg-promise but only mentioned in the documentation. I will make an update of the exporter to reflect this condition.

christiangalsterer commented 1 month ago

Can you by change add an example where the problem occurs as I somehow cannot reproduce it until now. I also checked the pg-promise code and this situation should actually never occur. Hence it would be great if you add more context, how you execute the query, which parameters are used etc. Thanks in advance

apletnev commented 1 month ago

Sure, here is my code


const METRICS_ID = process.env.METRICS_ID || 'node-api';
const METRICS_TYPE = process.env.METRICS_TYPE || 'api';

const initOptions = {
    capSQL: true,
    query(e) {
        logger.debug(e.query);
    }
};
const CUSTOM_Labels = {
    id: METRICS_ID,
    type: METRICS_TYPE
};

const registry = new prometheus.Registry();

const api_eventpipe_clients = new prometheus.Gauge({
    name: 'api_eventpipe_clients',
    help: 'Eventpipe clients count',
    registers: [registry],
    labelNames: [...Object.keys(CUSTOM_Labels)]
});

const pgp = require('pg-promise')(initOptions);

const pgdb = pgp({
            host: process.env.DB_HOST,
            port: process.env.DB_PORT,
            database: process.env.DB_NAME,
            user: process.env.DB_USER,
            password: process.env.DB_PASS,
            allowExitOnIdle: true
        });
pgPromiseExporter.monitorPgPromise(pgdb, initOptions, registry, {defaultLabels: CUSTOM_Labels});

// it fails here
const datasources = await store.pgdb.any('select * from s_get_drivertplls(${params})', { params: Object.assign({}, { sysuser: { id: 0 } }) }); 
christiangalsterer commented 1 month ago

Thanks for providing the example. It really help to narrow down the condition when this happens. I have partially fixed it already plan to do some more cleanup/test coverage. Will release a new version soon.

christiangalsterer commented 1 month ago

A fix is available with version 2.1.1. @apletnev: Thanks again for your support.