prom-client
Advanced tools
Comparing version 1.0.2 to 2.0.1
@@ -8,9 +8,9 @@ 'use strict'; | ||
var Histogram = require('../lib/histogram'); | ||
var h = new Histogram({ name: 'test_histogram', help: 'Example of a histogram' }); | ||
var h = new Histogram('test_histogram', 'Example of a histogram'); | ||
var Counter = require('../lib/counter'); | ||
var c = new Counter({ name: 'test_counter', help: 'Example of a counter', labels: { 'code': 200 }}); | ||
var c = new Counter('test_counter', 'Example of a counter', { labels: { 'code': 200 }}); | ||
var Gauge = require('../lib/gauge'); | ||
var g = new Gauge({ name: 'test_gauge', help: 'Example of a gauge'}); | ||
var g = new Gauge('test_gauge', 'Example of a gauge'); | ||
@@ -17,0 +17,0 @@ setInterval(function() { |
@@ -0,1 +1,6 @@ | ||
/** | ||
* Prometheus client | ||
* @module Prometheus client | ||
*/ | ||
'use strict'; | ||
@@ -2,0 +7,0 @@ |
@@ -0,1 +1,4 @@ | ||
/** | ||
* Counter metric | ||
*/ | ||
'use strict'; | ||
@@ -6,10 +9,18 @@ var register = require('./register'); | ||
function Counter(obj) { | ||
if(!obj.help) { | ||
/** | ||
* Counter | ||
* @param {string} name - Name of the metric | ||
* @param {string} help - Help description for the metric | ||
* @param {object} obj - Configuration | ||
* @constructor | ||
*/ | ||
function Counter(name, help, obj) { | ||
if(!help) { | ||
throw new Error('Missing mandatory help parameter'); | ||
} | ||
if(!obj.name) { | ||
if(!name) { | ||
throw new Error('Missing mandatory name parameter'); | ||
} | ||
this.name = obj.name; | ||
obj = obj || {}; | ||
this.name = name; | ||
this.values = [{ | ||
@@ -19,14 +30,19 @@ value: 0, | ||
}]; | ||
this.help = obj.help; | ||
this.help = help; | ||
register.registerMetric(this); | ||
} | ||
Counter.prototype.inc = function(incValue) { | ||
if(incValue && !isNumber(incValue)) { | ||
throw new Error('Value is not a valid number', incValue); | ||
/** | ||
* Increment counter | ||
* @param {float} value - Value to increment, if omitted increment with 1 | ||
* @returns {void} | ||
*/ | ||
Counter.prototype.inc = function(value) { | ||
if(value && !isNumber(value)) { | ||
throw new Error('Value is not a valid number', value); | ||
} | ||
if(incValue < 0) { | ||
if(value < 0) { | ||
throw new Error('It is not possible to decrease a counter'); | ||
} | ||
this.values[0].value = this.values[0].value += incValue || 1; | ||
this.values[0].value = this.values[0].value += value || 1; | ||
}; | ||
@@ -33,0 +49,0 @@ |
@@ -0,1 +1,4 @@ | ||
/** | ||
* Gauge metric | ||
*/ | ||
'use strict'; | ||
@@ -7,10 +10,19 @@ | ||
var isNumber = require('./util').isNumber; | ||
function Gauge(obj) { | ||
if(!obj.help) { | ||
/** | ||
* Gauge constructor | ||
* @param {string} name - Name of the metric | ||
* @param {string} help - Help for the metric | ||
* @param {object} obj - Configuration | ||
* @constructor | ||
*/ | ||
function Gauge(name, help, obj) { | ||
if(!help) { | ||
throw new Error('Missing mandatory help parameter'); | ||
} | ||
if(!obj.name) { | ||
if(!name) { | ||
throw new Error('Missing mandatory name parameter'); | ||
} | ||
this.name = obj.name; | ||
obj = obj || {}; | ||
this.name = name; | ||
this.values = [{ | ||
@@ -20,6 +32,11 @@ value: 0, | ||
}]; | ||
this.help = obj.help; | ||
this.help = help; | ||
register.registerMetric(this); | ||
} | ||
/** | ||
* Set a gauge to a value | ||
* @param {float} value - Value to set the gauge to, must be positive | ||
* @returns {void} | ||
*/ | ||
Gauge.prototype.set = function(value) { | ||
@@ -32,10 +49,24 @@ if(!isNumber(value)) { | ||
Gauge.prototype.inc = function(val) { | ||
this.set(this._getValue() + (val || 1)); | ||
/** | ||
* Increment a gauge value | ||
* @param {float} value - Value to increment - if omitted, increment with 1 | ||
* @returns {void} | ||
*/ | ||
Gauge.prototype.inc = function(value) { | ||
this.set(this._getValue() + (value || 1)); | ||
}; | ||
Gauge.prototype.dec = function(val) { | ||
this.set(this._getValue() - (val || 1)); | ||
/** | ||
* Decrement a gauge value | ||
* @param {float} value - Value to decrement - if omitted, decrement with 1 | ||
* @returns {void} | ||
*/ | ||
Gauge.prototype.dec = function(value) { | ||
this.set(this._getValue() - (value || 1)); | ||
}; | ||
/** | ||
* Set the gauge to current unix epoch | ||
* @returns {void} | ||
*/ | ||
Gauge.prototype.setToCurrentTime = function() { | ||
@@ -45,2 +76,11 @@ this.set(new Date().getTime()); | ||
/** | ||
* Start a timer | ||
* @returns {function} - Invoke this function to set the duration in seconds since you started the timer. | ||
* @example | ||
* var done = gauge.startTimer(); | ||
* makeXHRRequest(function(err, response) { | ||
* done(); //Duration of the request will be saved | ||
* }); | ||
*/ | ||
Gauge.prototype.startTimer = function() { | ||
@@ -64,2 +104,6 @@ var start = new Date(); | ||
/** | ||
* Reset the value of the gauge to 0 | ||
* @returns {void} | ||
*/ | ||
Gauge.prototype.reset = function() { | ||
@@ -66,0 +110,0 @@ this.values[0].value = 0; |
@@ -0,1 +1,4 @@ | ||
/** | ||
* Histogram | ||
*/ | ||
'use strict'; | ||
@@ -7,9 +10,19 @@ | ||
var extend = require('util-extend'); | ||
function Histogram(obj) { | ||
if(!obj.help) { | ||
/** | ||
* Histogram | ||
* @param {string} name - Name of the metric | ||
* @param {string} help - Help for the metric | ||
* @param {object} obj - Configuration object | ||
* @constructor | ||
*/ | ||
function Histogram(name, help, obj) { | ||
if(!help) { | ||
throw new Error('Missing mandatory help parameter'); | ||
} | ||
if(!obj.name) { | ||
if(!name) { | ||
throw new Error('Missing mandatory name parameter'); | ||
} | ||
obj = obj || {}; | ||
if(obj.labels && obj.labels.le) { | ||
@@ -22,4 +35,4 @@ throw new Error('Le is a reserved keyword and can not be used as a custom label'); | ||
}); | ||
this.name = obj.name; | ||
this.help = obj.help; | ||
this.name = name; | ||
this.help = help; | ||
this.upperBounds = defaultUpperbounds; | ||
@@ -37,2 +50,7 @@ this.bucketValues = this.upperBounds.reduce(function(acc, upperBound) { | ||
/** | ||
* Observe a value in histogram | ||
* @param {float} val - Value to observe in the histogram | ||
* @returns {void} | ||
*/ | ||
Histogram.prototype.observe = function(val) { | ||
@@ -42,6 +60,9 @@ if(!isNumber(val)) { | ||
} | ||
var b = findBound(this.upperBounds, val); | ||
if(!this.bucketValues.hasOwnProperty(b)) { | ||
return; | ||
} | ||
this.bucketValues[b] += 1; | ||
this.sum += val; | ||
this.count += 1; | ||
var b = findBound(this.upperBounds, val); | ||
this.bucketValues[b] += 1; | ||
}; | ||
@@ -66,2 +87,11 @@ | ||
/** | ||
* Start a timer that could be used to logging durations | ||
* @returns {function} - Function to invoke when you want to stop the timer and observe the duration in seconds | ||
* @example | ||
* var end = histogram.startTimer(); | ||
* makeExpensiveXHRRequest(function(err, res) { | ||
* end(); //Observe the duration of expensiveXHRRequest | ||
* }); | ||
*/ | ||
Histogram.prototype.startTimer = function() { | ||
@@ -68,0 +98,0 @@ var start = new Date(); |
'use strict'; | ||
exports.isNumber = function isNumber(obj) { return !isNaN(parseFloat(obj)) } | ||
exports.isNumber = function isNumber(obj) { return !isNaN(parseFloat(obj)); }; |
{ | ||
"name": "prom-client", | ||
"version": "1.0.2", | ||
"version": "2.0.1", | ||
"description": "Client for prometheus", | ||
@@ -10,7 +10,7 @@ "main": "index.js", | ||
"scripts": { | ||
"test": "mocha test/" | ||
"test": "eslint --ignore-pattern doc/ --ignore-path .gitignore . && mocha test/" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git@github.com:siimon/prom-client.git" | ||
"url": "git+ssh://git@bitbucket.org/simewn/prometheus-client.git" | ||
}, | ||
@@ -24,6 +24,8 @@ "keywords": [ | ||
"license": "MIT", | ||
"homepage": "https://github.com/siimon/prom-client", | ||
"homepage": "https://bitbucket.org/simewn/prometheus-client#readme", | ||
"devDependencies": { | ||
"chai": "^3.4.1", | ||
"eslint": "^1.10.1", | ||
"express": "^4.13.3", | ||
"minami": "^1.1.1", | ||
"mocha": "^2.3.4", | ||
@@ -33,4 +35,5 @@ "sinon": "^1.17.2" | ||
"dependencies": { | ||
"util-extend": "^1.0.1" | ||
"util-extend": "^1.0.1", | ||
"express": "^4.13.3" | ||
} | ||
} |
@@ -1,4 +0,90 @@ | ||
#Prometheus client for node.js | ||
# Prometheus client for node.js | ||
A prometheus client for node.js that supports histogram, gauges and counters. | ||
### Usage | ||
See example folder for a sample usage. The library does not bundle any web framework, to expose the metrics just return the metrics() function in the registry. | ||
### API | ||
#### Configuration | ||
All metric types has 2 mandatory parameters, name and help. Other than that, it always takes a configuration object as 3rd parameter. For all types, you can these properties: | ||
**labels** | ||
``` | ||
new Client.counter('name', 'help', | ||
{ | ||
labels: { | ||
type: 'request' | ||
} | ||
}); | ||
``` | ||
#### Counter | ||
Counters go up, and reset when the process restarts. | ||
``` | ||
var Client = require('prom-client'); | ||
var counter = new Client.counter('metric_name', 'metric_help'); | ||
counter.inc(); // Inc with 1 | ||
counter.inc(10); // Inc with 10 | ||
``` | ||
#### Gauge | ||
Gauges are similar to Counters but Gauges value can be decreased. | ||
``` | ||
var Client = require('prom-client'); | ||
var gauge = new Client.gauge('metric_name', 'metric_help'); | ||
gauge.set(10); // Set to 10 | ||
gauge.inc(); // Inc with 1 | ||
gauge.inc(10); // Inc with 10 | ||
gauge.dec(); // Dec with 1 | ||
gauge.dec(10); // Dec with 10 | ||
``` | ||
There are some utilities for common use cases: | ||
``` | ||
gauge.setToCurrentTime(); // Sets value to current time | ||
var end = gauge.startTimer(); | ||
xhrRequest(function(err, res) { | ||
end(); // Sets value to xhrRequests duration in seconds | ||
}); | ||
``` | ||
#### Histogram | ||
Histograms track sizes and frequency of events. | ||
**Configuration** | ||
The defaults buckets are intended to cover usual web/rpc requests, this can however be overriden. | ||
``` | ||
var Client = require('prom-client'); | ||
new Client.histogram('metric_name', 'metric_help', { | ||
buckets: [ 0.10, 5, 15, 50, 100, 500 ] | ||
}); | ||
``` | ||
Examples | ||
``` | ||
var Client = require('prom-client'); | ||
var histogram = new Client.histogram('metric_name', 'metric_help'); | ||
histogram.observe(10); // Observe value in histogram | ||
``` | ||
Utility to observe request durations | ||
``` | ||
var end = histogram.startTimer(); | ||
xhrRequest(function(err, res) { | ||
end(); // Observes the value to xhrRequests duration in seconds | ||
}); | ||
``` | ||
@@ -9,3 +9,3 @@ 'use strict'; | ||
beforeEach(function() { | ||
instance = new Counter({ name: 'gauge_test', help: 'test'}); | ||
instance = new Counter('gauge_test', 'test'); | ||
}); | ||
@@ -12,0 +12,0 @@ |
@@ -9,3 +9,3 @@ 'use strict'; | ||
beforeEach(function() { | ||
instance = new Gauge({ name: 'gauge_test', help: 'help'}); | ||
instance = new Gauge('gauge_test', 'help'); | ||
instance.set(10); | ||
@@ -12,0 +12,0 @@ }); |
@@ -9,3 +9,3 @@ 'use strict'; | ||
beforeEach(function() { | ||
instance = new Histogram({ help: 'test', name: 'test_histogram'}); | ||
instance = new Histogram('test_histogram', 'test'); | ||
}); | ||
@@ -35,4 +35,4 @@ afterEach(function() { | ||
instance.observe(5); | ||
var firstValuePair = getValueByLabel(0.05, instance.get().values); | ||
var secondValuePair = getValueByLabel(5, instance.get().values); | ||
var firstValuePair = getValueByLabel(0.05, instance.get().values); | ||
var secondValuePair = getValueByLabel(5, instance.get().values); | ||
expect(firstValuePair.value).to.equal(1); | ||
@@ -50,3 +50,3 @@ expect(secondValuePair.value).to.equal(1); | ||
it('should add buckets in increasing numerical order', function() { | ||
var histogram = new Histogram({ help: 'help', buckets: [1, 5], name: 'histogram'}); | ||
var histogram = new Histogram('test_histogram', 'test', { buckets: [1, 5] }); | ||
histogram.observe(1.5); | ||
@@ -77,3 +77,3 @@ var values = histogram.get().values; | ||
it('should allow custom labels', function() { | ||
var i = new Histogram({ help: 'help', name: 'histo', labels: { code: 'test' }}); | ||
var i = new Histogram('histo', 'help', { labels: { code: 'test' }}); | ||
i.observe(1); | ||
@@ -86,3 +86,3 @@ var pair = getValueByLabel('test', instance.get().values); | ||
var fn = function() { | ||
new Histogram({ help: 'help', name: 'histo', labels: { le: 'test' }}); | ||
new Histogram('name', 'help', { labels: { le: 'test' }}); | ||
}; | ||
@@ -92,2 +92,12 @@ expect(fn).to.throw(Error); | ||
it('should not observe value if outside most upper bound', function() { | ||
instance.observe(100000); | ||
var values = instance.get().values; | ||
var valuesAboveZero = values.reduce(function(acc, v) { | ||
acc += v.value; | ||
return acc; | ||
}, 0); | ||
expect(valuesAboveZero).to.equal(0); | ||
}); | ||
function getValueByName(name, values) { | ||
@@ -94,0 +104,0 @@ return values.reduce(function(acc, val) { |
@@ -40,3 +40,3 @@ 'use strict'; | ||
get: function() { | ||
return { | ||
return { | ||
name: 'test_metric', | ||
@@ -68,3 +68,3 @@ type: 'counter', | ||
} | ||
}] | ||
}] | ||
}; | ||
@@ -71,0 +71,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20223
17
581
91
2
6
1
+ Addedexpress@^4.13.3
+ Addedaccepts@1.3.8(transitive)
+ Addedarray-flatten@1.1.1(transitive)
+ Addedbody-parser@1.20.3(transitive)
+ Addedbytes@3.1.2(transitive)
+ Addedcall-bind@1.0.7(transitive)
+ Addedcontent-disposition@0.5.4(transitive)
+ Addedcontent-type@1.0.5(transitive)
+ Addedcookie@0.6.0(transitive)
+ Addedcookie-signature@1.0.6(transitive)
+ Addeddebug@2.6.9(transitive)
+ Addeddefine-data-property@1.1.4(transitive)
+ Addeddepd@2.0.0(transitive)
+ Addeddestroy@1.2.0(transitive)
+ Addedee-first@1.1.1(transitive)
+ Addedencodeurl@1.0.22.0.0(transitive)
+ Addedes-define-property@1.0.0(transitive)
+ Addedes-errors@1.3.0(transitive)
+ Addedescape-html@1.0.3(transitive)
+ Addedetag@1.8.1(transitive)
+ Addedexpress@4.21.0(transitive)
+ Addedfinalhandler@1.3.1(transitive)
+ Addedforwarded@0.2.0(transitive)
+ Addedfresh@0.5.2(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedget-intrinsic@1.2.4(transitive)
+ Addedgopd@1.0.1(transitive)
+ Addedhas-property-descriptors@1.0.2(transitive)
+ Addedhas-proto@1.0.3(transitive)
+ Addedhas-symbols@1.0.3(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedhttp-errors@2.0.0(transitive)
+ Addediconv-lite@0.4.24(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedipaddr.js@1.9.1(transitive)
+ Addedmedia-typer@0.3.0(transitive)
+ Addedmerge-descriptors@1.0.3(transitive)
+ Addedmethods@1.1.2(transitive)
+ Addedmime@1.6.0(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)
+ Addedms@2.0.02.1.3(transitive)
+ Addednegotiator@0.6.3(transitive)
+ Addedobject-inspect@1.13.2(transitive)
+ Addedon-finished@2.4.1(transitive)
+ Addedparseurl@1.3.3(transitive)
+ Addedpath-to-regexp@0.1.10(transitive)
+ Addedproxy-addr@2.0.7(transitive)
+ Addedqs@6.13.0(transitive)
+ Addedrange-parser@1.2.1(transitive)
+ Addedraw-body@2.5.2(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedsend@0.19.0(transitive)
+ Addedserve-static@1.16.2(transitive)
+ Addedset-function-length@1.2.2(transitive)
+ Addedsetprototypeof@1.2.0(transitive)
+ Addedside-channel@1.0.6(transitive)
+ Addedstatuses@2.0.1(transitive)
+ Addedtoidentifier@1.0.1(transitive)
+ Addedtype-is@1.6.18(transitive)
+ Addedunpipe@1.0.0(transitive)
+ Addedutils-merge@1.0.1(transitive)
+ Addedvary@1.1.2(transitive)