# Only showing Postgres START and COMMIT but not Query (Node/Express/Postgresql)

**URL:** <https://discuss.elastic.co/t/only-showing-postgres-start-and-commit-but-not-query-node-express-postgresql/290378>\
**Category:** APM\
**Tags:** nodejs\
**Created:** [November 28, 2021, 10:03pm UTC](https://discuss.elastic.co/t/only-showing-postgres-start-and-commit-but-not-query-node-express-postgresql/290378 "2021-11-28T22:03:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeremy\_Gillick](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jeremy_gillick/32/97631_2.png) [@Jeremy\_Gillick](https://discuss.elastic.co/u/Jeremy_Gillick)\
**Post date:** [November 28, 2021, 10:03pm UTC](https://discuss.elastic.co/t/only-showing-postgres-start-and-commit-but-not-query-node-express-postgresql/290378/1 "2021-11-28T22:03:32Z")

</div>

I'm setting up Cloud Elastic APM with a node/express/postgres server. The web requests are coming in as expected, however, only the postgresql START and COMMIT commands from the transactions are being shown but not the queries themselves.

 ![Screen Shot 2021-11-28 at 1.51.49 PM](https://us1.discourse-cdn.com/elastic/original/3X/3/4/34d332c264db6fc951648f94b34e6ee0df3bd456.png)

When I set the logs to "trace", I can see the query _is_ being intercepted from `pg` (see below), but for some reason it's not logged. The only difference between the queries that are logged (START/COMMIT) and the ones that are not, is that the ones logged have IDs and the main query does not. However, I'm not sure if that's important. I also notice that it says "no active transaction found - cannot build new span".

I have enabled the `pg_stat_statements` extension in postgres as recommended in [this post](https://discuss.elastic.co/t/apm-transaction-for-postgres-only-showing-begin-and-commit-statements-why-i-cant-i-see-the-statements-in-between/239201). However, I'm not sure that would do anything since it appears that the APM module intercepts queries directly from `pg`.

**Stack:**

- Elastic/APM Cloud: v7.x (not sure where to find the version in the web UI)
- Postgres: v14.1
- Express: v4.17.1
- NodeJS: v14.15.0
- elastic-apm-node: v3.25.0
- TypeScript: v3.9.3
- Knex: v0.21.19
- Objection: v2.2.2

**Test Express Route**

```auto
app.get('/testQueryTrace', testQueryTraceRoute);

```

```auto
export const testQueryTraceRoute = async (req, res, next) => {
    try {
        // Simple Postgres transaction wrapper
        const account = await withTransaction(({ trx }) => {
            return AccountModel.query(trx).limit(1);
        });
        return res.status(200).send({ account });
    } catch (err) {
        next(err);
    }
};

```

```auto
export const withTransaction = async <T>(cb: TransactionCB<T>): Promise<T> => {
    const trx = await startTransaction();
    const dataStore = new PostgresDataSource(trx);
    try {
        const result = await cb(dataStore);
        await trx.commit();
        return result;
    } catch (err) {
        if (!trx.isCompleted()) {
            await trx.rollback();
        }
        throw err;
    }
};

```

**Logs**  
For a single request to the test route.

```auto
{"message":"intercepted request event call to http.Server.prototype.emit for /testQueryTrace"}

{"message":"start trace {\"trans\":\"f2c4fc7a588c6350\",\"trace\":\"acd249e81967cfe7d8e8fcaac71e9a1a\",\"name\":\"unnamed\",\"type\":null,\"subtype\":null,\"action\":null}"}

{"message":"supersedeWithTransRunContext(<Trans f2c4fc7a588c6350>)"}

{"message":"start span {\"span\":\"806b9e53c0c9bf17\",\"parent\":\"f2c4fc7a588c6350\",\"trace\":\"acd249e81967cfe7d8e8fcaac71e9a1a\",\"name\":\"SQL\",\"type\":\"db\",\"subtype\":\"postgresql\",\"action\":\"query\"}"}

{"message":"supersedeWithSpanRunContext(<Span 806b9e53c0c9bf17>)"}

{"message":"intercepted call to pg.Client.prototype.query {\"id\":\"f2c4fc7a588c6350\",\"sql\":\"BEGIN;\"}"}

{"message":"intercepted end of pg.Client.prototype.query {\"id\":\"f2c4fc7a588c6350\"}"}

{"message":"ended span {\"span\":\"806b9e53c0c9bf17\",\"parent\":\"f2c4fc7a588c6350\",\"trace\":\"acd249e81967cfe7d8e8fcaac71e9a1a\",\"name\":\"BEGIN\",\"type\":\"db\",\"subtype\":\"postgresql\",\"action\":\"query\"}"}

{"message":"addEndedSpan(BEGIN)"}

{"message":"encoding span {\"span\":\"806b9e53c0c9bf17\",\"parent\":\"f2c4fc7a588c6350\",\"trace\":\"acd249e81967cfe7d8e8fcaac71e9a1a\",\"name\":\"BEGIN\",\"type\":\"db\"}"}

{"message":"sending span {\"span\":\"806b9e53c0c9bf17\",\"parent\":\"f2c4fc7a588c6350\",\"trace\":\"acd249e81967cfe7d8e8fcaac71e9a1a\",\"name\":\"BEGIN\",\"type\":\"db\"}"}

{"message":"capturing custom stack trace for knex"}

{"message":"shimming knex runner.query"}

{"message":"intercepted call to knex runner.query"}

{"message":"no active transaction found - cannot build new span"}

{"message":"intercepted call to pg.Client.prototype.query {\"id\":null,\"sql\":\"select \\\"account\\\".* from \\\"account\\\" limit $1\"}"}

{"message":"start span {\"span\":\"c44fe6262140bc8e\",\"parent\":\"f2c4fc7a588c6350\",\"trace\":\"acd249e81967cfe7d8e8fcaac71e9a1a\",\"name\":\"SQL\",\"type\":\"db\",\"subtype\":\"postgresql\",\"action\":\"query\"}"}

{"message":"supersedeWithSpanRunContext(<Span c44fe6262140bc8e>)"}

{"message":"intercepted call to pg.Client.prototype.query {\"id\":\"f2c4fc7a588c6350\",\"sql\":\"COMMIT;\"}"}

{"message":"applying custom stack trace to span {\"span\":\"c44fe6262140bc8e\",\"parent\":\"f2c4fc7a588c6350\",\"trace\":\"acd249e81967cfe7d8e8fcaac71e9a1a\"}"}

{"message":"intercepted end of pg.Client.prototype.query {\"id\":\"f2c4fc7a588c6350\"}"}

{"message":"ended span {\"span\":\"c44fe6262140bc8e\",\"parent\":\"f2c4fc7a588c6350\",\"trace\":\"acd249e81967cfe7d8e8fcaac71e9a1a\",\"name\":\"COMMIT\",\"type\":\"db\",\"subtype\":\"postgresql\",\"action\":\"query\"}"}

{"message":"addEndedSpan(COMMIT)"}

{"message":"encoding span {\"span\":\"c44fe6262140bc8e\",\"parent\":\"f2c4fc7a588c6350\",\"trace\":\"acd249e81967cfe7d8e8fcaac71e9a1a\",\"name\":\"COMMIT\",\"type\":\"db\"}"}

{"message":"sending span {\"span\":\"c44fe6262140bc8e\",\"parent\":\"f2c4fc7a588c6350\",\"trace\":\"acd249e81967cfe7d8e8fcaac71e9a1a\",\"name\":\"COMMIT\",\"type\":\"db\"}"}

{"message":"setting transaction result {\"trans\":\"f2c4fc7a588c6350\",\"trace\":\"acd249e81967cfe7d8e8fcaac71e9a1a\",\"result\":\"HTTP 2xx\"}"}

{"message":"setting default transaction name: GET /testQueryTrace {\"trans\":\"f2c4fc7a588c6350\",\"trace\":\"acd249e81967cfe7d8e8fcaac71e9a1a\"}"}

{"message":"addEndedTransaction(GET /testQueryTrace)"}

{"message":"sending transaction {\"trans\":\"f2c4fc7a588c6350\",\"trace\":\"acd249e81967cfe7d8e8fcaac71e9a1a\"}"}

{"message":"ended transaction {\"trans\":\"f2c4fc7a588c6350\",\"trace\":\"acd249e81967cfe7d8e8fcaac71e9a1a\",\"type\":\"request\",\"result\":\"HTTP 2xx\",\"name\":\"GET /testQueryTrace\"}"}

{"message":"intake request start"}

{"message":"_writeBatch"}

{"message":"intakeReq \"socket\": unref it"}

```

---

<div class="post-metadata">

**Author:** ![Jeremy\_Gillick](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jeremy_gillick/32/97631_2.png) [@Jeremy\_Gillick](https://discuss.elastic.co/u/Jeremy_Gillick)\
**Post date:** [November 28, 2021, 10:57pm UTC](https://discuss.elastic.co/t/only-showing-postgres-start-and-commit-but-not-query-node-express-postgresql/290378/2 "2021-11-28T22:57:40Z")

</div>

I found a workaround.

TL;DR: Upgrade node to 15+ or set [`asyncHooks` to `false`](https://www.elastic.co/guide/en/apm/agent/nodejs/current/configuration.html#async-hooks)

[This report](https://github.com/elastic/apm-agent-nodejs/issues/873) suggested setting `asyncHooks` to `false` for node versions below v14.6. This fixed it, but I'm on node version v14.15.0, so I shouldn't need to set this.

However, updating to Node 17.1 seemed to also resolve it cleanly for me.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [December 19, 2021, 6:58pm UTC](https://discuss.elastic.co/t/only-showing-postgres-start-and-commit-but-not-query-node-express-postgresql/290378/3 "2021-12-19T18:58:13Z")

</div>

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.
