Skip to content
This repository has been archived by the owner on May 5, 2022. It is now read-only.

Commit

Permalink
Merge pull request #1 from jamesemwallis/master
Browse files Browse the repository at this point in the history
Committing appmetrics-prometheus.
  • Loading branch information
tobespc authored Sep 5, 2017
2 parents 3a48fa0 + 1a60743 commit b78a0b4
Show file tree
Hide file tree
Showing 39 changed files with 1,008 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
test/apps/lb-3
24 changes: 24 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"parserOptions": {
"ecmaVersion": 6
},
"extends": "strongloop",
"env": {
"node": true,
"browser": true,
},
"rules": {
"eqeqeq": "off",
"max-len": "off",
"comma-dangle": "off",
"no-unused-vars": ["error", { "varsIgnorePattern": "clearProfilingData",
"argsIgnorePattern": "^_" }],
"spaced-comment": ["error", "always", { "exceptions": ["*"] }]
},
"globals": {
"TreeNode": false,
"clearFlameGraph": false,
"refreshFlameGraph": false,
"socket": false,
}
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
node-report*
18 changes: 18 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>appmetrics-prometheus</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name></name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.nodeclipse.ui.NodeNature</nature>
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
</natures>
</projectDescription>
14 changes: 14 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
language: node_js
node_js:
- "node"
- "6"
- "4"
env:
- CXX=g++-4.8
addons:
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- g++-4.8
script: npm run-script travis
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

43 changes: 43 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Contributing to appmetrics-prometheus

We welcome contributions, but request you follow these guidelines.

- [Raising issues](#raising-issues)
- [Feature requests](#feature-requests)
- [Pull-Requests](#pull-requests)
- [Contributor License Agreement](#contributor-license-agreement)

## Raising issues

Please raise any bug reports on the relevant project's issue tracker. Be sure to
search the list to see if your issue has already been raised.

A good bug report is one that make it easy for us to understand what you were
trying to do and what went wrong.

Provide as much context as possible so we can try to recreate the issue.

## Feature requests

For feature requests, please raise a Github [issue](https://github.com/RuntimeTools/appmetrics-prometheus/issues).

## Pull-Requests

If you want to raise a pull-request with a new feature, or a refactoring
of existing code, it may well get rejected if you haven't discussed it in
a Github [issue](https://github.com/RuntimeTools/appmetrics-prometheus/issues).

### Contributor License Agreement

In order for us to accept pull-requests from a new contributor, the contributor must indicate in their first Pull Request that they accept and agree to be bound by the terms of the IBM Contributor License Agreement that can be found here:
- https://github.com/RuntimeTools/appmetrics-prometheus/raw/master/IBM-RuntimesTools-V1.0.1-CLA.odt

If you are an IBMer, please contact us directly as the contribution process is
slightly different.

### Coding standards

Please ensure you follow the coding standards used through-out the existing
code base. Some basic rules include:

- all files must have the Apache license in the header.
Binary file added IBM-RuntimesTools-V1.0.1-CLA.odt
Binary file not shown.
17 changes: 17 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Copyright (c) IBM Corp. 2015,2017. All Rights Reserved.
Node module: appmetrics-prometheus

--------
Copyright 2015,2017 IBM Corp.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
173 changes: 172 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,172 @@
"# appmetrics-prometheus"
# appmetrics-prometheus

<!-- [![Build Status](https://travis-ci.org/RuntimeTools/appmetrics-dash.svg?branch=master)](https://travis-ci.org/RuntimeTools/appmetrics-dash)
[![codebeat badge](https://codebeat.co/badges/52b7334d-70b0-4659-9acb-b080d6413906)](https://codebeat.co/projects/github-com-runtimetools-appmetrics-dash-master)
[![codecov.io](https://codecov.io/github/RuntimeTools/appmetrics-dash/coverage.svg?branch=master)](https://codecov.io/github/RuntimeTools/appmetrics-dash?branch=master)
![Apache 2](https://img.shields.io/badge/license-Apache2-blue.svg?style=flat)
[![Homepage](https://img.shields.io/badge/homepage-Node%20Application%20Metrics-blue.svg)](https://developer.ibm.com/node/monitoring-post-mortem/application-metrics-node-js/) -->

appmetrics-prometheus provides a /metrics endpoint which is necessary for [Prometheus monitoring](https://prometheus.io/).

The data available on the /metrics endpoint is as follows:
* CPU
* Memory

appmetrics-prometheus uses [Node Application Metrics][1] to monitor the application.

Tested using Node versions 4.8.4, 6.11.2, 8.3.0 and 8.4.0.

## Configuring Prometheus

[Prometheus Documentation](https://prometheus.io/docs/introduction/overview/)

### Local Installation

Download Prometheus from: [Prometheus Downloads](https://prometheus.io/download/).

Follow the instructions on the [Prometheus getting started](https://prometheus.io/docs/introduction/getting_started/) page.

Or follow the simple example below.

Install Prometheus using:

```
tar xvfz prometheus-*.tar.gz
cd prometheus-*
```
Next you need to modify the configuration file that Prometheus uses.
In the prometheus folder there is a file named `prometheus.yml`.
In this file you can alter which IP addresses and port numbers are scraped by Prometheus and also how often the scraping occurs.

```
global:
scrape_interval: 15s # By default, scrape targets every 15 seconds.
# Attach these labels to any time series or alerts when communicating with
# external systems (federation, remote storage, Alertmanager).
external_labels:
monitor: 'codelab-monitor'
# A scrape configuration:
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'YOUR JOB NAME'
# Override the global default and scrape targets from this job every 5 seconds.
scrape_interval: 5s
static_configs:
- targets: ['IPADDRESS:PORT', 'IPADDRESS:PORT']
```

Set the targets field to your IP address and port number. You can monitor many applications by adding a comma between each IP address and port number.

Start Prometheus by using the command:

```
./prometheus -config.file=prometheus.yml
```
Prometheus can be found at `localhost:9090`.

<!-- ### Kubernetes
To use Prometheus with Kubernetes you can install it using [Helm](https://github.com/kubernetes/helm).
[Prometheus Chart](https://github.com/kubernetes/charts/tree/master/stable/prometheus)
`$ helm install stable/prometheus` -->

## Installation

```console
npm install appmetrics-prometheus
```

## Usage

Place the following code at the top of your applications server file.
```
require('appmetrics-prometheus').attach()
```

## prometheus = require('appmetrics-prometheus').attach()

This will launch the prometheus endpoint and start monitoring your application.
The prometheus metrics page is located at /metrics.

Simple example using the express framework.

```js
// This application uses express as its web server
// for more info, see: http://expressjs.com
var express = require('express');

var prometheus = require('appmetrics-prometheus').attach();

// cfenv provides access to your Cloud Foundry environment
// for more info, see: https://www.npmjs.com/package/cfenv
var cfenv = require('cfenv');

// create a new express server
var app = express();

// serve the files out of ./public as our main files
app.use(express.static(__dirname + '/public'));

// get the app environment from Cloud Foundry
var appEnv = cfenv.getAppEnv();

// start server on the specified port and binding host
var server = app.listen(appEnv.port, '0.0.0.0', function() {
// print a message when the server starts listening
console.log("server starting on " + appEnv.url);
});
```

## prometheus.attach(options)

* options.appmetrics {Object} An instance of `require('appmetrics')` can be
injected if the application wants to use appmetrics, since it is a singleton
module and only one can be present in an application. Optional, defaults to
the appmetrics dependency of this module.

Auto-attach to all `http` servers created after this call, calling `prometheus.monitor(options)` for every server.

Simple example using attach.
```js
require('appmetrics-prometheus').attach();

var http = require('http');

const port = 3000;

const requestHandler = (request, response) => {
response.end('Hello')
}

const server = http.createServer(requestHandler);

server.listen(port, (err) => {
if (err) {
return console.log('An error occurred', err)
}
console.log(`Server is listening on ${port}`)
});
```

## Performance overhead

Our testing has shown that the performance overhead in terms of processing is minimal, adding less than 0.5 % to the CPU usage of your application.

We gathered this information by monitoring the sample application [Acme Air][3]. We used MongoDB as our datastore and used JMeter to drive load though the program. We have performed this testing with Node.js version 6.10.3.

## Contributing

We welcome contributions. Please see [CONTRIBUTING.md](CONTRIBUTING.md) for details about the contributor licence agreement and other information. If you want to do anything more involved than a bug fix or a minor enhancement then we would recommend discussing it in an issue first before doing the work to make sure that it's likely to be accepted. We're also keen to improve test coverage and may not accept new code unless there are accompanying tests.

### License
The Node Application Metrics Prometheus is licensed using an Apache v2.0 License.


[1]:https://developer.ibm.com/open/node-application-metrics/
[2]:https://www.npmjs.com/package/node-report/
[3]:https://github.com/acmeair/acmeair-nodejs/
17 changes: 17 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*******************************************************************************
* Copyright 2015 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
'use strict';
module.exports = require('./lib/appmetrics-prometheus.js');
Loading

0 comments on commit b78a0b4

Please sign in to comment.