Skip to content

Commit

Permalink
Add prom-client and export Node.js metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
zubron committed May 21, 2018
1 parent b5179e8 commit 0f4125f
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 2 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,13 @@ Then, follow the [`prometheus` README](./prometheus/README.md) to verify that yo
provided configuration.

Having problems? Please open an issue in this project :)

### S1

In this branch, we add support for exporting Prometheus metrics from the
API in `todo-app`.

See the [`todo-app` README](./todo-app/README.md) for more details.

These metrics can be queried within the Prometheus UI as our configuration
already has an [entry for the ToDo API](./prometheus/prometheus.yml)
24 changes: 24 additions & 0 deletions todo-app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,27 @@ To print only the URL, use the following command:
```
minikube service demo-prom-demo --url
```

## S1: Exporting Prometheus metrics

To export Prometheus metrics from our API, we can use a [Prometheus client for Node.js](https://github.com/siimon/prom-client).

We add this client library as a dependency of our project (including it in [`package.json`](./app/package.json)).
To use the library, we instantiate an instance of it in the code for handling the [API routes](./app/api/routes.js), and use the client to collect default metrics about the Node.js process.
We then add an additional endpoint to our API (`/metrics`) which, when requested, sends all the metrics our Prometheus client has gathered.

To deploy this code, [build the latest image](#building-the-images).
The Helm chart contains changes in this branch to use this latest image.
To update your `helm` deployment use the following command:

```
helm upgrade demo chart
```

You can check the status of the deployment using the [previous instructions](#deploying-the-application).

Once the new version has been deployed, you can see the metrics that are being exported by visting:

```
$(minikube service demo-prom-demo --url)/metrics
```
9 changes: 9 additions & 0 deletions todo-app/app/api/routes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const paths = require('../config/paths');
const Todo = require('./models/todo');
const Prometheus = require('prom-client');

Prometheus.collectDefaultMetrics();

const getTodos = (res) => {
Todo.find((err, todos) => {
Expand Down Expand Up @@ -85,6 +88,12 @@ module.exports = (app) => {
});
});

// Export Prometheus metrics
app.get('/metrics', (req, res) => {
res.set('Content-Type', Prometheus.register.contentType);
res.end(Prometheus.register.metrics());
});

app.get('/', (req, res) => {
res.sendFile(paths.staticFiles);
});
Expand Down
3 changes: 2 additions & 1 deletion todo-app/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"body-parser": "^1.18.2",
"express": "^4.16.3",
"mongoose": "^5.0.17",
"morgan": "^1.9.0"
"morgan": "^1.9.0",
"prom-client": "^11.0.0"
}
}
16 changes: 16 additions & 0 deletions todo-app/app/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ basic-auth@~2.0.0:
dependencies:
safe-buffer "5.1.1"

bintrees@1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/bintrees/-/bintrees-1.0.1.tgz#0e655c9b9c2435eaab68bf4027226d2b55a34524"

bluebird@3.5.0:
version "3.5.0"
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.5.0.tgz#791420d7f551eea2897453a8a77653f96606d67c"
Expand Down Expand Up @@ -328,6 +332,12 @@ path-to-regexp@0.1.7:
version "0.1.7"
resolved "https://registry.yarnpkg.com/path-to-regexp/-/path-to-regexp-0.1.7.tgz#df604178005f522f15eb4490e7247a1bfaa67f8c"

prom-client@^11.0.0:
version "11.0.0"
resolved "https://registry.yarnpkg.com/prom-client/-/prom-client-11.0.0.tgz#d7e5b58778e04c774de2aa7e4d400552b48713ab"
dependencies:
tdigest "^0.1.1"

proxy-addr@~2.0.3:
version "2.0.3"
resolved "https://registry.yarnpkg.com/proxy-addr/-/proxy-addr-2.0.3.tgz#355f262505a621646b3130a728eb647e22055341"
Expand Down Expand Up @@ -443,6 +453,12 @@ statuses@~1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/statuses/-/statuses-1.4.0.tgz#bb73d446da2796106efcc1b601a253d6c46bd087"

tdigest@^0.1.1:
version "0.1.1"
resolved "https://registry.yarnpkg.com/tdigest/-/tdigest-0.1.1.tgz#2e3cb2c39ea449e55d1e6cd91117accca4588021"
dependencies:
bintrees "1.0.1"

type-is@~1.6.15, type-is@~1.6.16:
version "1.6.16"
resolved "https://registry.yarnpkg.com/type-is/-/type-is-1.6.16.tgz#f89ce341541c672b25ee7ae3c73dee3b2be50194"
Expand Down
2 changes: 1 addition & 1 deletion todo-app/chart/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
replicaCount: 1
image:
repository: todo-demo
tag: s0
tag: s1
pullPolicy: IfNotPresent
service:
name: todo-app
Expand Down

0 comments on commit 0f4125f

Please sign in to comment.