Skip to content

Commit

Permalink
feat(example-metrics-prometheus): add an example for prometheus metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed Nov 1, 2019
1 parent 7e485f6 commit 2c11c6d
Show file tree
Hide file tree
Showing 24 changed files with 1,545 additions and 0 deletions.
2 changes: 2 additions & 0 deletions examples/metrics-prometheus/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Exclude *.tsbuildinfo - cache for tsc incremental builds
*.tsbuildinfo
1 change: 1 addition & 0 deletions examples/metrics-prometheus/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=true
2 changes: 2 additions & 0 deletions examples/metrics-prometheus/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist
*.json
6 changes: 6 additions & 0 deletions examples/metrics-prometheus/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"bracketSpacing": false,
"singleQuote": true,
"printWidth": 80,
"trailingComma": "all"
}
20 changes: 20 additions & 0 deletions examples/metrics-prometheus/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"editor.rulers": [80],
"editor.tabCompletion": "on",
"editor.tabSize": 2,
"editor.trimAutoWhitespace": true,
"editor.formatOnSave": true,

"files.exclude": {
"**/.DS_Store": true,
"**/.git": true,
"**/.hg": true,
"**/.svn": true,
"**/CVS": true,
"dist": true,
},
"files.insertFinalNewline": true,
"files.trimTrailingWhitespace": true,

"typescript.tsdk": "./node_modules/typescript/lib"
}
29 changes: 29 additions & 0 deletions examples/metrics-prometheus/.vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Watch and Compile Project",
"type": "shell",
"command": "npm",
"args": ["--silent", "run", "build:watch"],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": "$tsc-watch"
},
{
"label": "Build, Test and Lint",
"type": "shell",
"command": "npm",
"args": ["--silent", "run", "test:dev"],
"group": {
"kind": "test",
"isDefault": true
},
"problemMatcher": ["$tsc", "$tslint5"]
}
]
}
25 changes: 25 additions & 0 deletions examples/metrics-prometheus/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Copyright (c) IBM Corp. 2019. All Rights Reserved.
Node module: @loopback/example-metrics-prometheus
This project is licensed under the MIT License, full text below.

--------

MIT license

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
45 changes: 45 additions & 0 deletions examples/metrics-prometheus/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# @loopback/example-metrics-prometheus

This project contains an example to illustrate metrics provided by
[`@loopback/extension-metrics`](https://github.com/strongloop/loopback-next/blob/master/extensions/metrics)
and [Prometheus](https://prometheus.io/).

## Use

```sh
npm start
```

To run full demo:

```sh
npm run demo
```

Now let's generate some traffic:

http://127.0.0.1:3000/greet/world?count=50

The metrics is reported at http://127.0.0.1:3000/metrics.

Open http://localhost:9090 for the Prometheus Web UI.

![Prometheus Demo](prometheus-demo.png)

## Contributions

- [Guidelines](https://github.com/strongloop/loopback-next/blob/master/docs/CONTRIBUTING.md)
- [Join the team](https://github.com/strongloop/loopback-next/issues/110)

## Tests

Run `npm test` from the root folder.

## Contributors

See
[all contributors](https://github.com/strongloop/loopback-next/graphs/contributors).

## License

MIT
20 changes: 20 additions & 0 deletions examples/metrics-prometheus/bin/demo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

# This script runs prometheus to scape and visulize metrics collected for the
# example LoopBack 4 application.

DIR=`dirname $0`
CWD=`pwd`
CONFIG=$CWD/$DIR/prometheus.yml

PROM_CONTAINER_NAME="prometheus_lb4_demo"
docker rm -f $PROM_CONTAINER_NAME
docker pull prom/prometheus:latest
docker run --name $PROM_CONTAINER_NAME -p 9090:9090 -v $CONFIG:/etc/prometheus/prometheus.yml -d prom/prometheus

echo Prometheus is running at http://localhost:9090.

pushd $DIR/.. >/dev/null
npm run build
node .
popd
29 changes: 29 additions & 0 deletions examples/metrics-prometheus/bin/prometheus.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'prometheus'

# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.

static_configs:
- targets: ['host.docker.internal:3000']
6 changes: 6 additions & 0 deletions examples/metrics-prometheus/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright IBM Corp. 2018. All Rights Reserved.
// Node module: @loopback/example-metrics-prometheus
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

export * from './dist';
31 changes: 31 additions & 0 deletions examples/metrics-prometheus/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright IBM Corp. 2018. All Rights Reserved.
// Node module: @loopback/example-metrics-prometheus
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

module.exports = require('./dist');

async function main() {
const config = {
rest: {
port: +(process.env.PORT || 3000),
host: process.env.HOST || '127.0.0.1',
openApiSpec: {
// useful when used with OpenAPI-to-GraphQL to locate your application
setServersFromRequest: true,
},
},
};
const app = new module.exports.GreetingApplication(config);
await app.main();
const url = app.restServer.url;
console.log(`Greeting service is running at ${url}/greet/world?count=5.`);
console.log(`Metrics is running at ${url}/metrics`);
}

if (require.main === module) {
main().catch(err => {
console.error('Cannot start the application.', err);
process.exit(1);
});
}
8 changes: 8 additions & 0 deletions examples/metrics-prometheus/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright IBM Corp. 2018. All Rights Reserved.
// Node module: @loopback/example-metrics-prometheus
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

// DO NOT EDIT THIS FILE
// Add any additional (re)exports to src/index.ts instead.
export * from './src';
Loading

0 comments on commit 2c11c6d

Please sign in to comment.