This project requires having an elk stack up and running. For our project, we have 2 servers
For our demonstration, we use elk version 8.5.2. In our journey, we will show you how to monitor a simple nodejs app with the elk APM.
To monitor your application, you have to create a fleet server in kibana. we will follow the steps below.
1) we have to create the fleet server, so we need to go into our kibana and in the menu on the left, click on fleet.

2) On the fleet page we have to click on settings and create hosts. this will allow us to configure the fleet server. In our case, it is an ubuntu server with the IP address 192.169.208.134.

3) We have to configure the IP address for our fleet server like below

4) For the output part we have to configure our elk ip address and because we do not use our own certificate, we have to add the flag ssl.verification_mode: none. This command tells elastic to do not verify the SSL certificate that it receives. It is obvious that it is not something to do in a production environment. In the end, you have to choose a name for your policy and add the fleet server.


5) after adding the fleet server, we have to add an agent. this part will give you the command to run on your fleet server (192.168.208.134). In the deployment mode section, you have to choose quick start because, in the beginning, we didn't configure any certificate.
You have to copy and paste the commands at the end of the page to your fleet server. remember to choose the commands related to your distribution. A simple copy-paste will get your server run and you will see your fleet server appear on the kibana page like below.

Now you got your fleet and agent going. You will need to configure the integration. Go to the menu on the left and go into the observability part and click on APM and click on add integration

click on add new integration

you have to put the IP address of your fleet server and name the integration. Since you already have already created an agent policy. at the end of the page,click on the existing host and choose the policy you created, and create the agent


the agent should appear now on the agent page

Now that you already have everything configured to your elk stack, you can run your app and start to send data to your elk. you can follow the steps to install the agent nodejs here
On a server you can install nodejs, npm and the elastic apm node module.
for ubuntu it goes like this :
apt install nodejs
apt install npm
apt install elastic-apm-node --savede --savenpm install elastic-apm-node --save
https://www.elastic.co/guide/en/apm/guide/current/apm-quick-start.html
Below you can see the code of my nodejs app
// Add this to the VERY top of the first file loaded in your app
var apm = require('elastic-apm-node').start({
// Override the service name from package.json
// Allowed characters: a-z, A-Z, 0-9, -, _, and space
serviceName: 'mon test apm',
// Use if APM Server requires a secret token
secretToken: '',
// Set the custom APM Server URL (default: http://localhost:8200)
serverUrl: 'http://192.168.208.134:8200',
// Set the service environment
environment: 'production'
})
const express = require ('express')
const app = express()
const port = 3030
app.get('/',(req,res) => {
res.send(' hello world')
})
app.get('/demo',(req,res) => {
res.send(' welcome demo')
})
app.get('/error',(req,res) => {
res.send(' capture error')
const err = new Error('Trigger Error')
apm.captureError(err)
})
app.listen(port, () => {
console.log (' App lsiten on port 3030')
})
When you run the app, you will get the APM data in your elastic monitoring tool
Comments
Post a Comment