Skip to content

Commit

Permalink
feat(extension-metrics): add metrics extension for prometheus
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed Jul 17, 2019
1 parent c74b870 commit a24a8fb
Show file tree
Hide file tree
Showing 24 changed files with 674 additions and 0 deletions.
1 change: 1 addition & 0 deletions extensions/metrics/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=true
25 changes: 25 additions & 0 deletions extensions/metrics/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/extension-metrics
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.
71 changes: 71 additions & 0 deletions extensions/metrics/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# @loopback/extension-metrics

This module contains a component to report metrics for
[Prometheus](https://prometheus.io/).

## Installation

```sh
npm install --save @loopback/extension-metrics
```

## Basic use

The component should be loaded in the constructor of your custom Application
class.

Start by importing the component class:

```ts
import {MetricsComponent} from '@loopback/extension-metrics';
```

In the constructor, add the component to your application:

```ts
this.component(MetricsComponent);
```

By default, Metrics route is mounted at `/metrics`. This path can be customized
via Metrics configuration as follows:

```ts
this.bind(MetricsBindings.CONFIG).to({
endpoint: {
basePath: '/metrics',
},
defaultMetrics: {
options: {
timeout: 5000,
},
},
});
```

## Try it out

```sh
npm run demo
```

Open http://localhost:9090 to load Prometheus web UI.

http://localhost:3000/metrics returns metrics in plain text format.

## 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
6 changes: 6 additions & 0 deletions extensions/metrics/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/rest-explorer
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

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

module.exports = require('./dist');
8 changes: 8 additions & 0 deletions extensions/metrics/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright IBM Corp. 2019. All Rights Reserved.
// Node module: @loopback/extension-metrics
// 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';
35 changes: 35 additions & 0 deletions extensions/metrics/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 52 additions & 0 deletions extensions/metrics/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"name": "@loopback/extension-metrics",
"version": "1.0.0",
"description": "LoopBack Metrics",
"engines": {
"node": ">=8.9"
},
"scripts": {
"build": "lb-tsc",
"clean": "lb-clean loopback-extension-metrics*.tgz dist tsconfig.build.tsbuildinfo package",
"pretest": "npm run build",
"test": "lb-mocha \"dist/__tests__/**/*.js\"",
"verify": "npm pack && tar xf loopback-extension-metrics*.tgz && tree package && npm run clean",
"demo": "./src/__examples__/demo.sh"
},
"author": "IBM Corp.",
"copyright.owner": "IBM Corp.",
"license": "MIT",
"dependencies": {
"@loopback/context": "^1.20.2",
"@loopback/core": "^1.8.5",
"@loopback/rest": "^1.16.3",
"prom-client": "^11.5.3"
},
"devDependencies": {
"@loopback/build": "^2.0.3",
"@loopback/eslint-config": "^2.0.0",
"@loopback/testlab": "^1.6.3",
"@types/node": "^10.14.12"
},
"keywords": [
"LoopBack",
"Prometheus",
"Metrics"
],
"files": [
"README.md",
"index.js",
"index.d.ts",
"dist",
"src",
"!*/__tests__",
"templates"
],
"publishConfig": {
"access": "public"
},
"repository": {
"type": "git",
"url": "https://github.com/strongloop/loopback-next.git"
}
}
21 changes: 21 additions & 0 deletions extensions/metrics/src/__examples__/demo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/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 ./dist/__examples__/metrics
popd

19 changes: 19 additions & 0 deletions extensions/metrics/src/__examples__/metrics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright IBM Corp. 2019. All Rights Reserved.
// Node module: @loopback/rest-explorer
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {RestApplication} from '@loopback/rest';
import {MetricsComponent} from '../';

async function main() {
const app = new RestApplication({rest: {port: 3000}});
app.component(MetricsComponent);
await app.start();
console.log(app.restServer.url);
}

main().catch(err => {
console.error(err);
process.exit(1);
});
29 changes: 29 additions & 0 deletions extensions/metrics/src/__examples__/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']
105 changes: 105 additions & 0 deletions extensions/metrics/src/__tests__/acceptance/metrics.acceptance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Copyright IBM Corp. 2019. All Rights Reserved.
// Node module: @loopback/rest-explorer
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {RestApplication, RestServerConfig} from '@loopback/rest';
import {
Client,
createRestAppClient,
expect,
givenHttpServerConfig,
} from '@loopback/testlab';
import {MetricsComponent} from '../..';
import {MetricsBindings} from '../../keys';
import {MetricsOptions} from '../../types';

describe('Metrics (acceptance)', () => {
let app: RestApplication;
let request: Client;

afterEach(async () => {
if (app) await app.stop();
(app as unknown) = undefined;
});

context('with default config', () => {
beforeEach(async () => {
app = givenRestApplication();
app.component(MetricsComponent);
await app.start();
request = createRestAppClient(app);
});

it('exposes metrics at "/metrics/"', async () => {
const res = await request
.get('/metrics')
.expect(200)
.expect('content-type', /text/);
expect(res.text).to.match(/# TYPE/);
expect(res.text).to.match(/# HELP/);
});
});

context('with custom defaultMetrics', () => {
it('honors prefix', async () => {
await givenAppWithCustomConfig({
defaultMetrics: {
options: {
// `-` is not allowed
prefix: 'myapp_',
},
},
});
await request
.get('/metrics')
.expect(200)
.expect('content-type', /text/);
});
});

context('with custom endpoint basePath', () => {
it('honors prefix', async () => {
await givenAppWithCustomConfig({
endpoint: {
basePath: '/internal/metrics',
},
});
await request
.get('/internal/metrics')
.expect(200)
.expect('content-type', /text/);
await request.get('/metrics').expect(404);
});
});

context('with defaultMetrics disabled', () => {
it('does not emit default metrics', async () => {
await givenAppWithCustomConfig({
defaultMetrics: {
disabled: true,
},
});
const res = await request
.get('/metrics')
.expect(200)
.expect('content-type', /text/);
expect(res.text).to.not.match(
/# TYPE process_cpu_user_seconds_total counter/,
);
});
});

async function givenAppWithCustomConfig(config: MetricsOptions) {
app = givenRestApplication();
app.bind(MetricsBindings.CONFIG).to(config);
app.component(MetricsComponent);
await app.start();
request = createRestAppClient(app);
}

function givenRestApplication(config?: RestServerConfig) {
const rest = Object.assign({}, givenHttpServerConfig(), config);
return new RestApplication({rest});
}
});
6 changes: 6 additions & 0 deletions extensions/metrics/src/controllers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright IBM Corp. 2019. All Rights Reserved.
// Node module: @loopback/extension-metrics
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

export * from './metrics.controller';
Loading

0 comments on commit a24a8fb

Please sign in to comment.