5. Install Azure SDK client library to monitor web app
In this step, add the Azure SDK client library to the code on the virtual machine to begin collecting app logs in the Azure cloud.
Edit index.js for logging with Azure Monitor Application Insights
Still in the SSH terminal, use the Nano text editor provided in the virtual machine to open the
index.js.sudo nano index.jsEdit the
index.jsfile to add the client library and logging code, highlighted below. Many bash shells allow you to copy and paste directly into Nano.const express = require('express') const app = express() const os = require('os'); console.log(JSON.stringify(process.env)); const AppInsights = require('applicationinsights'); AppInsights.setup(process.env.APPINSIGHTS_INSTRUMENTATIONKEY) .setAutoDependencyCorrelation(true) .setAutoCollectRequests(true) .setAutoCollectPerformance(true, true) .setAutoCollectExceptions(true) .setAutoCollectDependencies(true) .setAutoCollectConsole(true) .setUseDiskRetryCaching(true) .setSendLiveMetrics(false) .setDistributedTracingMode(AppInsights.DistributedTracingModes.AI) .start(); const AppInsightsClient = AppInsights.defaultClient; app.get('/trace', (req, res) => { const clientIP = req.headers['x-forwarded-for']; const msg = `trace route ${os.hostname()} ${clientIP} ${new Date()}`; console.log(msg) AppInsightsClient.trackPageView(); AppInsightsClient.trackTrace({ message: msg}) AppInsightsClient.flush(); res.send(`${msg}`) }) app.get('/', function (req, res) { const clientIP = req.headers['x-forwarded-for']; const msg = `root route ${os.hostname()} ${clientIP} ${new Date()}` console.log(msg) res.send(msg) }) app.listen(3000, function () { console.log(`Hello world app listening on port 3000! ${os.hostname()}`) })Still in the SSH terminal, save the file in the Nano editor with control + X. Enter Y to save, when prompted. Accept the file name when prompted.
Changes to the web app are watched by PM2; this change caused a restart of the app, without having to restart the VM.
To have PM2 load the environment variable and have it available in the index.js, restart PM2 with the following command:
sudo npm run-script restartIn a web browser, test the app with the new
traceroute:http://YOUR-VM-PUBLIC-IP-ADDRESS/traceThe browser displays the response,
trace route demo-vm YOUR-CLIENT-IP VM-DATE-TIMEwith your IP address.
Viewing the log for NGINX
The virtual machine (VM) collects logs for NGINX, which are available to view.
| Service | Log location |
|---|---|
| NGINX | /var/log/nginx/access.log |
Still in the SSH terminal, view VM log for the NGINX proxy service with the following command to view the log:
cat /var/log/nginx/access.log
The log includes the call from your local computer.
"GET /trace HTTP/1.1" 200 10 "-"
Viewing the log for PM2
The virtual machine collects logs for PM2, which are available to view.
| Service | Log location |
|---|---|
| PM2 | /var/log/pm2.log |
View VM log for the PM2 service, which is your Express.js Node web app. In the same bash shell, use the following command to view the log:
cat /var/log/pm2.logThe log includes the call from your local computer.
grep "Hello world app listening on port 3000!" /var/log/pm2.logThe log also includes your environment variables, including your ApplicationInsights key, passed in the npm start script. use the following grep command to verify your key is in the environment variables.
grep APPINSIGHTS_INSTRUMENTATIONKEY /var/log/pm2.logThis displays your PM2 log with
APPINSIGHTS_INSTRUMENTATIONKEYhighlighted in a different color.
VM logging and cloud logging
In this application, using console.log writes the messages into the PM2 logs found on the VM only. If you delete the logs or the VM, you lose that information.
If you want to retain the logs beyond the lifespan of your virtual machine, use Application Insights.
Troubleshooting
If you have issues, use the following table to understand how to resolve your issue:
| Problem | Resolution |
|---|---|
| 502 Gateway error | This could indicate your index.js or package.js file has an error. View your PM2 logs at /var/log/pm2.log for more information. The most recent error is at the bottom of the file. If you are sure those files are correct, stop and start the PM2 using the npm scripts in package.json. |
Next step
Povratne informacije
Pošalјite i prikažite povratne informacije za