APM with Elastic Stack for NodeJS Application

Posted By : Jatin Gupta | 30-Oct-2018

Download and unpack APM server

curl -L -O https://artifacts.elastic.co/downloads/apm-server/apm-server-6.2.0-amd64.deb
sudo dpkg -i apm-server-6.2.0-amd64.deb
 

Import Dashboards

./apm-server setup


Edit the configuration

If you are using xpack config, you need to specify the elasticsearch credentials in apm-server.yml

output.elasticsearch:
hosts: ["<es_url>"]
  username: <username>
  password: <password>

Start APM Server

./apm-server -e

 

Install the APM Agent 

Install the APM agent as a dependency for your node js application

npm install elastic-apm-node --save

Configure the Agent

These agents will run with your application. APM services are created programmatically based on service name we will mention in the code. This agent supports Express, Koa, hapi and custom Node.js

// Add this to the VERY top of the first file loaded in your app
var apm = require('elastic-apm-node').start({
// Set required service name (allowed characters: a-z, A-Z, 0-9, -, _, and space)
  serviceName: '',

  // Use if APM Server requires a token
  secretToken: '',

  // Set custom APM Server URL (default: http://localhost:8200)
  serverUrl: ''
})

 

Add this code in your main javascript file you use to run the code which is at most times app.js or server,js or run.js.
This code should be placed at the top as it is important that this code should run before you require any module in your app.

 

What does it do?

Performance Monitoring

 

Elastic APM agent consequently measures the execution of your NodeJS Express application. It records time ranges for database queries, outer HTTP requests and other moderate activities that occur during requests to your Express application. 

The agent will instrument the most widely recognized modules. To instrument different occasions, you can utilize custom ranges. 

Time Ranges are grouped in transactions - as a matter, of course, one for every approaching HTTP request. Be that as it may, it's conceivable to make custom exchanges not related to an HTTP request. 

 

 

 

 

 

About Author

Author Image
Jatin Gupta

Jatin is a DevOps trainee. He ha deep interest in python and cloud technologies. He likes to read about science/history and fiction, listening to music and explore new places.

Request for Proposal

Name is required

Comment is required

Sending message..