Even if you know how to fix a memory leak, you would have preferred not having to!
Monitoring your application is the key.
In this exercise, you will start a Prometheus, make it scrape your application, and create an alert.
Run the exercise application
npm run start-exercise-3
In another terminal, run the docker image "prom/prometheus"
docker run \
--network host \
--volume "/prometheus" \
--volume "$(pwd)/src/exercise-3/prometheus-data:/prometheus-data" \
prom/prometheus \
--storage.tsdb.path=/prometheus-data/storage \
--config.file=/prometheus-data/prometheus.yml
Open http://localhost:9090, you should see the prometheus interface
Have a look at the prometheus queries!
From there, create graphs showing the heap spaces sizes over 5 minutes.
Solution
- click on "add graph" to get 2 graphs
- switch to graph mode for both
- enter the following queries
and
nodejs_heap_space_size_used_bytes{space="new"}
nodejs_heap_space_size_used_bytes{space="old"}
- press "execute" for both
Create another graph on the same page showing the garbage collector reclaimed bytes increase per minute over 5 minutes..
Solution
- click on "add graph"
- switch to graph mode
- enter the following query
increase(nodejs_gc_reclaimed_bytes_total{}[1m])
- check "stacked"
- press "execute"
Eventually, you should reach this state.