prom-client
Advanced tools
Comparing version 10.1.1 to 10.2.0
@@ -11,2 +11,10 @@ # Changelog | ||
## [10.2.0] - 2017-10-16 | ||
### Changed | ||
- Don't add event listeners if cluster module is not used. | ||
- Fixed issue with counters having extra records when using empty labels | ||
### Added | ||
- Added `reset` to Counter and Gauge | ||
- Added `resetMetrics` to register to calling `reset` of all metric instances | ||
## [10.1.1] - 2017-09-26 | ||
@@ -80,4 +88,5 @@ ### Changed | ||
[Unreleased]: https://github.com/siimon/prom-client/compare/v10.1.1...HEAD | ||
[10.1.0]: https://github.com/siimon/prom-client/compare/v10.1.0...v10.1.1 | ||
[Unreleased]: https://github.com/siimon/prom-client/compare/v10.2.0...HEAD | ||
[10.2.0]: https://github.com/siimon/prom-client/compare/v10.1.1...v10.2.0 | ||
[10.1.1]: https://github.com/siimon/prom-client/compare/v10.1.0...v10.1.1 | ||
[10.1.0]: https://github.com/siimon/prom-client/compare/v10.0.4...v10.1.0 | ||
@@ -84,0 +93,0 @@ [10.0.4]: https://github.com/siimon/prom-client/compare/v10.0.3...v10.0.4 |
@@ -29,2 +29,7 @@ // Type definitions for prom-client | ||
/** | ||
* Reset all metrics in the registry | ||
*/ | ||
resetMetrics(): void; | ||
/** | ||
* Register metric to register | ||
@@ -189,2 +194,7 @@ * @param metric Metric to add to register | ||
labels(...values: string[]): Counter.Internal; | ||
/** | ||
* Reset counter values | ||
*/ | ||
reset(): void; | ||
} | ||
@@ -292,2 +302,7 @@ | ||
labels(...values: string[]): Gauge.Internal; | ||
/** | ||
* Reset gauge values | ||
*/ | ||
reset(): void; | ||
} | ||
@@ -294,0 +309,0 @@ |
@@ -21,5 +21,11 @@ 'use strict'; | ||
let requestCtr = 0; // Concurrency control | ||
let listenersAdded = false; | ||
const requests = new Map(); // Pending requests for workers' local metrics. | ||
class AggregatorRegistry extends Registry { | ||
constructor() { | ||
super(); | ||
addListeners(); | ||
} | ||
/** | ||
@@ -132,31 +138,43 @@ * Gets aggregated metrics for all workers. The optional callback and | ||
if (cluster.isMaster) { | ||
// Listen for worker responses to requests for local metrics | ||
cluster.on('message', function(worker, message) { | ||
if (arguments.length === 2) { | ||
// pre-Node.js v6.0 | ||
message = worker; | ||
worker = undefined; | ||
} | ||
/** | ||
* Adds event listeners for cluster aggregation. Idempotent (safe to call more | ||
* than once). | ||
* @return {void} | ||
*/ | ||
function addListeners() { | ||
if (listenersAdded) return; | ||
listenersAdded = true; | ||
if (message.type === GET_METRICS_RES) { | ||
const request = requests.get(message.requestId); | ||
message.metrics.forEach(registry => request.responses.push(registry)); | ||
request.pending--; | ||
if (cluster.isMaster) { | ||
// Listen for worker responses to requests for local metrics | ||
cluster.on('message', function(worker, message) { | ||
if (arguments.length === 2) { | ||
// pre-Node.js v6.0 | ||
message = worker; | ||
worker = undefined; | ||
} | ||
if (request.pending === 0) { | ||
// finalize | ||
requests.delete(message.requestId); | ||
clearTimeout(request.errorTimeout); | ||
if (message.type === GET_METRICS_RES) { | ||
const request = requests.get(message.requestId); | ||
message.metrics.forEach(registry => request.responses.push(registry)); | ||
request.pending--; | ||
if (request.failed) return; // Callback already run with Error. | ||
if (request.pending === 0) { | ||
// finalize | ||
requests.delete(message.requestId); | ||
clearTimeout(request.errorTimeout); | ||
const registry = AggregatorRegistry.aggregate(request.responses); | ||
const promString = registry.metrics(); | ||
request.callback(null, promString); | ||
request.resolve(promString); | ||
if (request.failed) return; // Callback already run with Error. | ||
const registry = AggregatorRegistry.aggregate(request.responses); | ||
const promString = registry.metrics(); | ||
request.callback(null, promString); | ||
request.resolve(promString); | ||
} | ||
} | ||
} | ||
}); | ||
} else if (cluster.isWorker) { | ||
}); | ||
} | ||
} | ||
if (cluster.isWorker) { | ||
// Respond to master's requests for worker's local metrics. | ||
@@ -163,0 +181,0 @@ process.on('message', message => { |
@@ -67,9 +67,6 @@ /** | ||
this.name = config.name; | ||
this.hashMap = {}; | ||
this.labelNames = config.labelNames || []; | ||
if (this.labelNames.length === 0) { | ||
this.hashMap = createValue({}, 0); | ||
} | ||
this.reset(); | ||
@@ -100,2 +97,10 @@ this.help = config.help; | ||
/** | ||
* Reset counter | ||
* @returns {void} | ||
*/ | ||
reset() { | ||
return reset.call(this); | ||
} | ||
get() { | ||
@@ -121,2 +126,10 @@ return { | ||
const reset = function() { | ||
this.hashMap = {}; | ||
if (this.labelNames.length === 0) { | ||
this.hashMap = createValue({}, 0); | ||
} | ||
}; | ||
const inc = function(labels, hash) { | ||
@@ -143,2 +156,3 @@ return (value, timestamp) => { | ||
function createValue(hashMap, value, timestamp, labels, hash) { | ||
hash = hash || ''; | ||
timestamp = isDate(timestamp) | ||
@@ -145,0 +159,0 @@ ? timestamp.valueOf() |
@@ -67,6 +67,3 @@ /** | ||
this.labelNames = config.labelNames || []; | ||
this.hashMap = {}; | ||
if (this.labelNames.length === 0) { | ||
this.hashMap = createValue({}, 0, {}); | ||
} | ||
this.reset(); | ||
this.help = config.help; | ||
@@ -95,2 +92,10 @@ this.aggregator = config.aggregator || 'sum'; | ||
/** | ||
* Reset gauge | ||
* @returns {void} | ||
*/ | ||
reset() { | ||
return reset.call(this); | ||
} | ||
/** | ||
* Increment a gauge value | ||
@@ -231,2 +236,10 @@ * @param {object} labels - Object with labels where key is the label key and value is label value. Can only be one level deep | ||
function reset() { | ||
this.hashMap = {}; | ||
if (this.labelNames.length === 0) { | ||
this.hashMap = createValue({}, 0, {}); | ||
} | ||
} | ||
function convertLabelsAndValues(labels, value) { | ||
@@ -233,0 +246,0 @@ if (!isObject(labels)) { |
@@ -5,3 +5,6 @@ 'use strict'; | ||
const version = process.version; | ||
const versionSegments = version.slice(1).split('.').map(Number); | ||
const versionSegments = version | ||
.slice(1) | ||
.split('.') | ||
.map(Number); | ||
@@ -8,0 +11,0 @@ const NODE_VERSION_INFO = 'nodejs_version_info'; |
@@ -104,2 +104,8 @@ 'use strict'; | ||
resetMetrics() { | ||
for (const metric in this._metrics) { | ||
this._metrics[metric].reset(); | ||
} | ||
} | ||
get contentType() { | ||
@@ -106,0 +112,0 @@ return 'text/plain; version=0.0.4; charset=utf-8'; |
{ | ||
"name": "prom-client", | ||
"version": "10.1.1", | ||
"version": "10.2.0", | ||
"description": "Client for prometheus", | ||
@@ -37,7 +37,7 @@ "main": "index.js", | ||
"express": "^4.13.3", | ||
"husky": "^0.13.4", | ||
"jest": "^20.0.4", | ||
"husky": "^0.14.3", | ||
"jest": "^21.2.1", | ||
"lint-staged": "^4.0.0", | ||
"lolex": "^1.6.0", | ||
"prettier": "1.5.2", | ||
"lolex": "^2.1.3", | ||
"prettier": "1.7.4", | ||
"typescript": "^2.5.2" | ||
@@ -44,0 +44,0 @@ }, |
@@ -107,2 +107,10 @@ # Prometheus client for node.js [![Build Status](https://travis-ci.org/siimon/prom-client.svg?branch=master)](https://travis-ci.org/siimon/prom-client) [![Build status](https://ci.appveyor.com/api/projects/status/k2e0gwonkcee3lp9/branch/master?svg=true)](https://ci.appveyor.com/project/siimon/prom-client/branch/master) | ||
A counter can be reset manually. This removes the label-values combinations and | ||
initializes to 0. | ||
```js | ||
counter.reset(); | ||
counter.inc(); // Inc with 1 starting from 0 | ||
``` | ||
#### Gauge | ||
@@ -121,3 +129,10 @@ | ||
``` | ||
A gauge can be reset manually. This removes the label-values combinations and | ||
initializes to 0. | ||
```js | ||
gauge.reset(); | ||
gauge.inc(); // Inc with 1 starting from 0 | ||
``` | ||
There are some utilities for common use cases: | ||
@@ -166,3 +181,9 @@ | ||
``` | ||
A Histogram can be reset manually. This removes the label-values combinations and | ||
reinitializes the observations. | ||
```js | ||
histogram.reset(); | ||
``` | ||
#### Summary | ||
@@ -197,2 +218,9 @@ | ||
A Summary can be reset manually. This removes the label-values combinations and | ||
reinitializes the observations. | ||
```js | ||
summary.reset(); | ||
``` | ||
#### Labels | ||
@@ -306,2 +334,7 @@ | ||
##### Resetting metrics | ||
If you need to reset all metrics, you can use `register.resetMetrics()`. The metrics will remain present in the register and can be used without the need to | ||
instantiate them again, like you would need to do after `register.clear()`. | ||
##### Cluster metrics | ||
@@ -308,0 +341,0 @@ |
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
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
101747
2520
397