Research and describe How to host production node.js app on Azure
.
I and my customer have node.js app on Azure, and sometimes we can't find following infromation.. To solve this situation, I started this repo.
- Logging (How to collect log from node.js App on Web Apps)
- CI/CD (How to automate node.js deployment using dev/staging/production slots)
If you have other challenges for hosting node.js app on Azure, let's discuss and collaborate through issue and pull request.
- You have Azure subscription (If you don't have, please create it here free)
- You can undersntad node.js development
- I expect you uses Web Apps for hosting node.js app
- How to store application log
- CI/CD (coming soon)
My favorite way to store and analyze log is using Application Insights. It automatically collect request, dependency and logs. Then these information is visualized such as
- How may request and how many errors
- How long to fetch data from db and API
- What kind of error occured with external API (401, 403 and sometimes.. 5xx)
I really love this because it's easy to use and can start from free. At least, it is easier than grep command to analyze log XD.
Microsoft published good starter document. Pleae follow this at first.
You need to get instrumention Key for using Application Insights. You may not want to do hardcoding in your source. If so, please use following technig.
- Code
process.env.APPINSIGHTS_INSTRUMENTATIONKEY
in your node for getting environmental variable. - Set environmental variable in your dev machine.
- Bash: export APPINSIGHTS_INSTRUMENTATIONKEY="Your key"
- Windows (cmd): set APPINSIGHTS_INSTRUMENTATIONKEY="Your key"
- Type
node app.js
for local debug - For production environment, you may use Azure Web Apps. Web Apps can store environmental variable in [Application settings]-[App settings]. This is key-value store. The left side is for key and right side is for value, so please input
APPINSIGHTS_INSTRUMENTATIONKEY
in left side and input your application insight instrument key in right side like the image.
Developers tend to use console.log for logging in local environment and may want to do logging without any code changes. Application Insights doesn't collect it default as described this document.
by default setAutoCollectConsole is configured to exclude calls to console.log (and other console methods). By default, only calls to supported third-party loggers (e.g. winston, bunyan) will be collected. You can change this behavior to include calls to console methods by using setAutoCollectConsole(true, true).
If you want to see console.log() logg in Application Insights, please write the code like below.
appInsights.setup(process.env.APPINSIGHTS_INSTRUMENTATIONKEY)
.setAutoCollectConsole(true,true)//It enables console.log() logging
.start();
Please deploy it Azure. OK, let's see our logs.
- Go to Application Insights in Azure portal
- Click [Analytics] in [Overview] blade
- Click [+] button and create new tab
- Type
traces
and click [Go]button
Conguratulations! You can see your logs. You can also write sql-like command like
traces | where message contains "test2"
to filter logs. It may be easier than grep command.
- Local Git Repository
- CI/CD
- How to backup application Insights log
need to click [Essentials]tab in [Overview]blade to see Instrumentation Key
in Azure portal