elastic / elastic/apm-agent-nodejs
Questions re ESM support for MikroOrm / knex, Node v20, & OIDC
- Dominant language
- JavaScript
- Stars
- 594
- Forks
- 244
- Avg merge
- 1d 8h
- Merged PRs (30d)
- 16
Description
**Is your feature request related to a problem? Please describe.**
This is more of a question thread, but relates to problems auto-instrumenting ECMAScript / ESM modules.
**Describe the solution you'd like**
Auto-instrumentation of MikroOrm module, and OIDC flows if possible
**Describe alternatives you've considered**
Considered manual instrumentation but unsure how to proceed there.
**Additional context**
Stacktrace provided below.
Hello Team!
My company is an Elastic Cloud customer happily running a search application in production.
I'm an SRE with one of the software teams, and we are also trying to get APM set up for observability.
We've been trying to get ECMAScript / ESM auto-instrumentation set up for a service for some time with limited success. We are running it via package.json using:
```bash
node -r dotenv/config \
-r elastic-apm-node/start.js \
--experimental-loader=elastic-apm-node/loader.mjs \
--env-file .env \
dist/server.js"
```
It works in that some transactions / metrics are sent to our APM server, however it doesn't know about dependencies or databases.
Our service uses [MikroOrm](https://github.com/mikro-orm/mikro-orm) for database calls, and out of the box APM doesn't capture spans. When I enable the `ELASTIC_APM_SPAN_STACK_TRACE_MIN_DURATION` env var, it seems to prevent the DB client from initialising:
```
[discovery] - entity discovery finished, found 5 entities, took 9 ms
TypeError: Cannot read properties of undefined (reading 'Client')
at MariaDbConnection.createKnexClient (/Users/myuser/myproject/node_modules/@mikro-orm/knex/AbstractSqlConnection.js:132:66)
at MariaDbConnection.createKnex (/Users/myuser/myproject/node_modules/@mikro-orm/mariadb/MariaDbConnection.js:7:28)
at MariaDbConnection.connect (/Users/myuser/myproject/node_modules/@mikro-orm/knex/AbstractSqlConnection.js:21:14)
at MariaDbDriver.connect (/Users/myuser/myproject/node_modules/@mikro-orm/core/drivers/DatabaseDriver.js:70:31)
at MikroORM.connect (/Users/myuser/myproject/node_modules/@mikro-orm/core/MikroORM.js:101:46)
at MikroORM.init (/Users/myuser/myproject/node_modules/@mikro-orm/core/MikroORM.js:44:23)
at async file:///Users/myuser/myproject/dist/server.js:38:23
Retrying connection in 5 seconds...
```
I initially thought this was supported as MikroOrm uses knex under the hood, but now I see in the [docs](https://www.elastic.co/guide/en/apm/agent/nodejs/current/esm.html#esm-compat-modules) that the supported versions of knex are:
| | | |
| ---- | ---- | ---- |
| `knex`
| >=0.20.0 <3 | Also, only with pg@8. |
Could someone advise what pg@8 refers to here? Not PostGres v8 surely?
Knex standalone is currently at 3.1.0, and I'm not exactly sure how MikroOrm invokes it. They apparently also use some 'monkey-patching' ([their words](https://github.com/mikro-orm/mikro-orm/discussions/5230)) to apply some compatibility fixes to knex.
Also, our service has just been upgraded to node v20. I can see in [Supported Node Versions](https://www.elastic.co/guide/en/apm/agent/nodejs/current/esm.html#esm-compat-node) that `>=20.2.0` is listed, however there is an draft PR open for [enable ESM support for Node v20](https://github.com/elastic/apm-agent-nodejs/pull/3469). Could someone provide some clarity on what is / isn't supported in v20+?
Assuming I can't find a way to make auto instrumentation work with our project, do you think manual instrumentation might be possible in this kind of scenario?
For reference, here are the main components of our stack:
- Node.js v20.9.0
- TypeScript
- MicroOrm v6.1.0
- knex v3.1.0
- MariaDB 10.6
Our service is also using `oidc-provider` for authentication. As a side note, it would be amazing if APM could (now or at some point in the future) somehow instrument OIDC flows, but for the moment some observability on DB interactions are what we need the most.
So, to summarise my questions:
Auto-instrumentation:
- Is ESM auto-instrumentation supported on Node >v20?
- Can you clarify what pg@8 means in relation to knex?
- Does that strictly mean 'standalone' knex, or is it possible to auto-instrument a package invoking knex such as MikroOrm?
Manual instrumentation:
- Failing that, any suggestions as to how might we manually instrument MikroOrm transactions?
Bonus: OIDC
- Any plans to support instrumenting OIDC flows?
Contributor guide
Assessment
This issue has not been assessed yet.