Application Performance Monitoring (APM)
APM stands for Application Performance Monitoring, it allows to see execution traces and improvde overall understanding of what happened and when. It is key to be able to debug quickly at scale issues. It is configurable, expandable.
We use Elastic APM to trace requests and correlate them with logs. You can add metadata to requests and spans for richer observability.
Example: Adding User Context & Labels
typescript
import * as apm from 'elastic-apm-node';
apm.setUserContext({
username: userData.displayName,
email: userData.email,
});
const user = await this.firebaseUserService.createUser(toCreate);
apm.setUserContext({
id: user.firebaseUid,
});
apm.setLabel('phoneNumber', userData.phoneNumber || 'N/A');
apm.setLabel('companyId', userData.companyId || 'N/A');- User context and labels are attached to traces and visible in Kibana.
- Use these methods to enrich your traces with business-relevant metadata.
App setup: Elastic APM Setup
To enable tracing and observability, initialize Elastic APM at the very top of your main entry file (before any other imports):
js
const apm = require('elastic-apm-node').start({
captureBody: 'off',
captureErrorLogStackTraces: 'messages',
serviceName: `tellia-${packageName}`,
apiKey: loggingCfg.apm.apiKey,
serverUrl: loggingCfg.apm.endpoint,
environment: baseCfg.environment,
// verifyServerCert: loggingCfg.transport.elasticsearch.verifyServerCert
});- This will automatically instrument your app and send traces/metrics to Elasticsearch.
- You can then use the APM API to add user context, labels, and custom spans as shown above.