Elastic APM does not showing the Spans for NestJS application

56 views Asked by At
import { Microservices } from '../configurations/constants';
import elasticApmNode from 'elastic-apm-node';

export const APM_SERVICE = 'APM_SERVICE';

// start APM function
export const startAPM = (service: Microservices) => {
  return elasticApmNode.start({
    serviceName: `backend_${service}`,
    secretToken: process.env.ELASTIC_APM_SECRET_TOKEN || '',
    serverUrl: process.env.ELASTIC_APM_SERVER_URL || 'http://localhost:8200',
    environment: process.env.ENV
  });
};

main.ts

import { AppModule } from './app.module';
import { GlobalErrorFilter } from './utils/error-capture';
import { startAPM } from '@app/shared/apm/apm-provider';
import { Microservices } from '@app/shared/configurations/constants';
import { sleep } from '@app/shared/utils/file-utils';
import { Logger } from '@app/shared/utils/logs';
import { NestFactory } from '@nestjs/core';

export const APM = startAPM(Microservices.apiThreatDetect);

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
// rest of the bootstrap

controller.ts

// new endpoint just for test elastic apm
  @Post('/test-elastic-apm')
  async testElasticApm() {
    const transaction = APM.startTransaction('Test Elastic APM', 'API Request');
    const span = transaction.startSpan('Test Elastic APM');
    // wait 2 seconds
    await new Promise((resolve) => setTimeout(resolve, 2000));
    span.end();
    transaction.end();
    return 'Test Elastic APM';
  }

I'm using elasitc-apm-node version 4.4.1 and it does not recognize any spans or the transaction(cannot see any logs in the console) in Elastic APM. Only the request has identified.

enter image description here

My node version is v20.9.0. Does anyone come across this issue or know how to resolve it?. It's perfectly working for my other nestJS micro services.

0

There are 0 answers