# Nodejs Agent NestJS and Typeorm

**URL:** <https://discuss.elastic.co/t/nodejs-agent-nestjs-and-typeorm/164171>\
**Category:** APM\
**Created:** [January 14, 2019, 4:20pm UTC](https://discuss.elastic.co/t/nodejs-agent-nestjs-and-typeorm/164171 "2019-01-14T16:20:46Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Juan\_Colo](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@Juan\_Colo](https://discuss.elastic.co/u/Juan_Colo)\
**Post date:** [January 14, 2019, 4:20pm UTC](https://discuss.elastic.co/t/nodejs-agent-nestjs-and-typeorm/164171/1 "2019-01-14T16:20:46Z")

</div>

Hello everyone,

I'm currently testing the NodeJs APM agent. I did a basic integration of the agent in my NestJS API, but the automatic registration request transactions did not work. Therefore, create a custom interceptor to create transactions in each incoming request:

```javascript
   intercept(context: ExecutionContext, call$: Observable<any>): Observable<any> {
        const [IncomingMessage, ...res] = context.getArgs();
        let transaction = this.apmService.startTransaction(`${IncomingMessage.method} ${IncomingMessage.url}`, 'Request');

        return call$.pipe(
            tap(() => {
                transaction.end('HTTP 2xx');
            }),
            catchError(error => {
                transaction.end('HTTP 5xx');
                this.apmService.captureError(error);
                throw error;
            }),
        );
    }

```

With this implementation I see the requests in kibana.

Now I'm trying to use the query logs in MSSQL (azure), I'm using typeorm, and this uses the mssql ([https://www.npmjs.com/package/mssql](https://www.npmjs.com/package/mssql)) and this uses [Tedious](https://www.npmjs.com/package/tedious) driver to connect.

From what I understood, the agent of Nodejs automatically logs the queries of [Tedious](https://www.npmjs.com/package/tedious) , but I can not see the spans in kibana.

I'm starting the APM agent like this:

```javascript
Import ...
import * as APM from 'elastic-apm-node';

@Injectable()
export class ApmService {
    apm: any;

    constructor(@Inject(APM_OPTIONS) private readonly options: ApmOptions) {
        this.apm = APM.isStarted() ? APM : APM.start({
            serviceName: options.serviceName || '',
            secretToken: options.secureToken || '',
            serverUrl: options.url || '' 
        });
    }
}

```

What am I doing wrong?  
Regards,

NodeJs: 10.15.0  
NestJs: 5.5.0  
TypeORM: 0.2.9  
mssql: 4.3.0  
Tedious: 2.7.1

---

<div class="post-metadata">

**Author:** ![wa7son](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wa7son/32/21259_2.png) [@wa7son](https://discuss.elastic.co/u/wa7son)\
**Post date:** [January 14, 2019, 5:00pm UTC](https://discuss.elastic.co/t/nodejs-agent-nestjs-and-typeorm/164171/2 "2019-01-14T17:00:21Z")

</div>

Hi @Juan_Colo

Thanks for trying out the Node.js agent and Elastic APM. You're right that we don't support NestJS directly. But we should automatically pick up the incoming HTTP requests. So without your custom interceptor you should have seen a lot of `GET unknown route` transactions - did you see that?

If you saw those, it means that we correctly registered the incoming HTTP requests and automatically started and ended transactions for them.

The only thing your custom intercepter then needs to do is give them a more meaningful name than simply `GET unknown route`. You do that by calling `this.apmService.setTransactionName(<name>)` ([see its documentation here](https://www.elastic.co/guide/en/apm/agent/nodejs/current/agent-api.html#apm-set-transaction-name)). You shouldn't manually have to start and end a new transaction. In fact I would think this might mess some things up as there already should be an existing transaction.

This is all documented in the "[Get started with a custom Node.js stack](https://www.elastic.co/guide/en/apm/agent/nodejs/current/custom-stack.html)" part of our documentation which I recommend that you read 🙂

But regardless, when a transaction is active, you're right that we should automatically pick up MSSQL queries made using the `tedious` module. I see now that there's a slight typo in our [Supported Technologies](https://www.elastic.co/guide/en/apm/agent/nodejs/current/supported-technologies.html#compatibility-tracing-and-instrumentation) docs, as it says we only support version ^0.0.5 of Tedious, but I can verify that we do support the entire 2.x range as well.

There's a few reasons why we might not pick them up however. To see exactly what's going on I recommend setting the log level to `debug` and sending an HTTP request to the app that will trigger an MSSQL query and see what the agent logs. See our [Troubleshooting](https://www.elastic.co/guide/en/apm/agent/nodejs/current/troubleshooting.html#debug-mode) docs for details. If you like, I'd be happy to take a look at the resulting logs.

/thomas

---

<div class="post-metadata">

**Author:** ![wa7son](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wa7son/32/21259_2.png) [@wa7son](https://discuss.elastic.co/u/wa7son)\
**Post date:** [January 14, 2019, 5:04pm UTC](https://discuss.elastic.co/t/nodejs-agent-nestjs-and-typeorm/164171/3 "2019-01-14T17:04:17Z")

</div>

Follow up: I missed this when reading your message the first time, but I see that you're using `import` instead of `require`. And it seems like you're using TypeScript. When you compile the code to JavaScript, this will most likely mess with order of when `start()` is called on the agent. And this in turn will explain why the Tedious queries are not picked up. See the "[Babel / ES Modules support](https://www.elastic.co/guide/en/apm/agent/nodejs/current/advanced-setup.html#es-modules)" section in our docs for details on how to fix that.

---

<div class="post-metadata">

**Author:** ![Juan\_Colo](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@Juan\_Colo](https://discuss.elastic.co/u/Juan_Colo)\
**Post date:** [January 14, 2019, 6:44pm UTC](https://discuss.elastic.co/t/nodejs-agent-nestjs-and-typeorm/164171/4 "2019-01-14T18:44:35Z")

</div>

Hi Thomas, thanks for your answer.

Change the call to the start method as indicated by the documentation for BABEL and set the log Level to debug:

```javascript

import * as apm from 'elastic-apm-node/start';

"scripts": {
 "start": "ELASTIC_APM_LOG_LEVEL=debug ts-node -r tsconfig-paths/register src/main.ts",
}

```

Now, when a request is sent to my API, the console displays an error:

```auto
intercepted call to generic-pool.PriorityQueue.prototype.enqueue { id: null }
no active transaction found - cannot build new span

```

Any idea what is happening?

Regards,

---

<div class="post-metadata">

**Author:** ![Juan\_Colo](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@Juan\_Colo](https://discuss.elastic.co/u/Juan_Colo)\
**Post date:** [January 14, 2019, 10:10pm UTC](https://discuss.elastic.co/t/nodejs-agent-nestjs-and-typeorm/164171/5 "2019-01-14T22:10:10Z")

</div>

I uploaded a [repository](https://github.com/xiz0r/nestjs-elastic-apm) with an example with nestJs that is giving me the same error.  
This one does not have Typeorm but it has the same error of the transaction:

```auto
no active transaction found - cannot build new span

```

Thanks

---

<div class="post-metadata">

**Author:** ![wa7son](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wa7son/32/21259_2.png) [@wa7son](https://discuss.elastic.co/u/wa7son)\
**Post date:** [January 15, 2019, 2:05pm UTC](https://discuss.elastic.co/t/nodejs-agent-nestjs-and-typeorm/164171/6 "2019-01-15T14:05:49Z")

</div>

Thanks for sharing the code. It looks like you're not calling `start()` on the APM agent until after a lot of the imports have been called. That means that the agent will not have a chance to load before the rest of the app have loaded. And this in turn mean that it cannot add its hooks to get notified about database queries etc.

As far as I could see this project is actually based on Express, which means that we should support automatic naming of transactions. The reason why it's not working is probably the same as described above.

If you move the call to `import 'elastic-apm-node/start'` up to either be the very first thing in `src/main.ts` - or maybe even simpler start node with `-r elastic-apm-node/start` instead of importing `elastic-apm-node/start` in the source code - that should fix the problem.

---

<div class="post-metadata">

**Author:** ![Juan\_Colo](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@Juan\_Colo](https://discuss.elastic.co/u/Juan_Colo)\
**Post date:** [January 15, 2019, 9:00pm UTC](https://discuss.elastic.co/t/nodejs-agent-nestjs-and-typeorm/164171/7 "2019-01-15T21:00:34Z")

</div>

Thomas, that was the problem.  
I moved import to the start of the file and everything works fine without interceptor.

Thank you very much for your help.

---

<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:** [February 5, 2019, 5:00pm UTC](https://discuss.elastic.co/t/nodejs-agent-nestjs-and-typeorm/164171/8 "2019-02-05T17:00:43Z")

</div>

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