Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: remove all timestamp support #333

Merged
merged 1 commit into from
Feb 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 0 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ active handles, GC and Node.js version. See what metrics there are in
- `gcDurationBuckets` with custom buckets for GC duration histogram. Default buckets of GC duration histogram are `[0.001, 0.01, 0.1, 1, 2, 5]` (in seconds).
- `eventLoopMonitoringPrecision` with sampling rate in milliseconds. Must be greater than zero. Default: 10.


To register metrics to another registry, pass it in as `register`:

```js
Expand Down Expand Up @@ -89,17 +88,6 @@ const prefix = 'my_application_';
collectDefaultMetrics({ prefix });
```

To disable metric timestamps set `timestamps` to `false` (You can find the list of metrics that support this feature in `test/defaultMetricsTest.js`):

```js
const client = require('prom-client');

const collectDefaultMetrics = client.collectDefaultMetrics;

// Probe every 5th second.
collectDefaultMetrics({ timestamps: false });
```

You can get the full list of metrics by inspecting
`client.collectDefaultMetrics.metricsList`.

Expand Down Expand Up @@ -338,22 +326,6 @@ Default labels will be overridden if there is a name conflict.

`register.clear()` will clear default labels.

### Timestamps

Counter and gauge metrics can take a timestamp argument after the value
argument. This argument must be a Date or a number (milliseconds since Unix
epoch, i.e. 1970-01-01 00:00:00 UTC, excluding leap seconds).

```js
gauge.set(100, 1485531442231); // Set gauge value and timestamp as milliseconds since Unix epoch
gauge.set(100, Date.now()); // Set gauge value and timestamp as milliseconds since Unix epoch
gauge.set(100, new Date()); // Set gauge value and timestamp as Date
gauge.set({ method: 'GET', statusCode: '200' }, 100, new Date()); // Set gauge value and timestamp with labels
gauge.labels('GET', '200').set(100, new Date()); // Same as above

counter.inc(1, new Date()); // Increment counter with timestamp
```

### Multiple registries

By default, metrics are automatically registered to the global registry (located
Expand Down Expand Up @@ -403,9 +375,6 @@ AggregatorRegistry.setRegistries([registry1, registry2]);
You can get all metrics by running `register.metrics()`, which will output a
string for prometheus to consume.

`register.metrics()` takes an optional object with a `timestamps` field. Setting
this to false will strip timestamps from the string.

#### Getting a single metric for Prometheus displaying

If you need to output a single metric for Prometheus, you can use
Expand Down Expand Up @@ -457,9 +426,6 @@ register.clusterMetrics((err, metrics) => {
It is possible to push metrics via a
[Pushgateway](https://github.com/prometheus/pushgateway).

Note that timestamps will be stripped before the metrics are pushed, since
pushgateway >= 0.4 does not accept timestamps.

```js
const client = require('prom-client');
let gateway = new client.Pushgateway('http://127.0.0.1:9091');
Expand Down
49 changes: 13 additions & 36 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
// Type definitions for prom-client
// Definitions by: Simon Nyberg http://twitter.com/siimon_nyberg

/**
* Options pass to Registry.metrics()
*/
export interface MetricsOpts {
/**
* Whether to include timestamps in the output, defaults to true
*/
timestamps?: boolean;
}

/**
* Container for all registered metrics
*/
export class Registry {
/**
* Get string representation for all metrics
*/
metrics(opts?: MetricsOpts): string;
metrics(): string;

/**
* Remove all metrics from the registry
Expand Down Expand Up @@ -172,16 +162,14 @@ export class Counter<T extends string> {
* Increment for given labels
* @param labels Object with label keys and values
* @param value The number to increment with
* @param timestamp Timestamp to associate the time series with
*/
inc(labels: LabelValues<T>, value?: number, timestamp?: number | Date): void;
inc(labels: LabelValues<T>, value?: number): void;

/**
* Increment with value
* @param value The value to increment with
* @param timestamp Timestamp to associate the time series with
*/
inc(value?: number, timestamp?: number | Date): void;
inc(value?: number): void;

/**
* Return the child for given labels
Expand All @@ -207,9 +195,8 @@ export namespace Counter {
/**
* Increment with value
* @param value The value to increment with
* @param timestamp Timestamp to associate the time series with
*/
inc(value?: number, timestamp?: number | Date): void;
inc(value?: number): void;
}
}

Expand All @@ -234,46 +221,40 @@ export class Gauge<T extends string> {
* Increment gauge for given labels
* @param labels Object with label keys and values
* @param value The value to increment with
* @param timestamp Timestamp to associate the time series with
*/
inc(labels: LabelValues<T>, value?: number, timestamp?: number | Date): void;
inc(labels: LabelValues<T>, value?: number): void;

/**
* Increment gauge
* @param value The value to increment with
* @param timestamp Timestamp to associate the time series with
*/
inc(value?: number, timestamp?: number | Date): void;
inc(value?: number): void;

/**
* Decrement gauge
* @param labels Object with label keys and values
* @param value Value to decrement with
* @param timestamp Timestamp to associate the time series with
*/
dec(labels: LabelValues<T>, value?: number, timestamp?: number | Date): void;
dec(labels: LabelValues<T>, value?: number): void;

/**
* Decrement gauge
* @param value The value to decrement with
* @param timestamp Timestamp to associate the time series with
*/
dec(value?: number, timestamp?: number | Date): void;
dec(value?: number): void;

/**
* Set gauge value for labels
* @param labels Object with label keys and values
* @param value The value to set
* @param timestamp Timestamp to associate the time series with
*/
set(labels: LabelValues<T>, value: number, timestamp?: number | Date): void;
set(labels: LabelValues<T>, value: number): void;

/**
* Set gauge value
* @param value The value to set
* @param timestamp Timestamp to associate the time series with
*/
set(value: number, timestamp?: number | Date): void;
set(value: number): void;

/**
* Set gauge value to current epoch time in ms
Expand Down Expand Up @@ -312,23 +293,20 @@ export namespace Gauge {
/**
* Increment gauge with value
* @param value The value to increment with
* @param timestamp Timestamp to associate the time series with
*/
inc(value?: number, timestamp?: number | Date): void;
inc(value?: number): void;

/**
* Decrement with value
* @param value The value to decrement with
* @param timestamp Timestamp to associate the time series with
*/
dec(value?: number, timestamp?: number | Date): void;
dec(value?: number): void;

/**
* Set gauges value
* @param value The value to set
* @param timestamp Timestamp to associate the time series with
*/
set(value: number, timestamp?: number | Date): void;
set(value: number): void;

/**
* Set gauge value to current epoch time in ms
Expand Down Expand Up @@ -590,7 +568,6 @@ export function exponentialBuckets(
): number[];

export interface DefaultMetricsCollectorConfiguration {
timestamps?: boolean;
register?: Registry;
prefix?: string;
gcDurationBuckets?: number[];
Expand Down
25 changes: 6 additions & 19 deletions lib/counter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const util = require('util');
const { globalRegistry } = require('./registry');
const type = 'counter';
const {
isDate,
getPropertiesFromObj,
hashObject,
isObject,
Expand Down Expand Up @@ -70,16 +69,15 @@ class Counter {
* Increment counter
* @param {object} labels - What label you want to be incremented
* @param {Number} value - Value to increment, if omitted increment with 1
* @param {(Number|Date)} timestamp - Timestamp to set the counter to
* @returns {void}
*/
inc(labels, value, timestamp) {
inc(labels, value) {
if (!isObject(labels)) {
return inc.call(this, null)(labels, value);
}

const hash = hashObject(labels);
return inc.call(this, labels, hash)(value, timestamp);
return inc.call(this, labels, hash)(value);
}

/**
Expand Down Expand Up @@ -124,15 +122,10 @@ const reset = function() {
};

const inc = function(labels, hash) {
return (value, timestamp) => {
return value => {
if (value && !Number.isFinite(value)) {
throw new TypeError(`Value is not a valid number: ${util.format(value)}`);
}
if (timestamp && !isDate(timestamp) && !Number.isFinite(timestamp)) {
throw new TypeError(
`Timestamp is not a valid date or number: ${util.format(timestamp)}`
);
}
if (value < 0) {
throw new Error('It is not possible to decrease a counter');
}
Expand All @@ -142,22 +135,16 @@ const inc = function(labels, hash) {

const incValue = value === null || value === undefined ? 1 : value;

this.hashMap = setValue(this.hashMap, incValue, timestamp, labels, hash);
this.hashMap = setValue(this.hashMap, incValue, labels, hash);
};
};

function setValue(hashMap, value, timestamp, labels, hash) {
function setValue(hashMap, value, labels, hash) {
hash = hash || '';
timestamp = isDate(timestamp)
? timestamp.valueOf()
: Number.isFinite(timestamp)
? timestamp
: undefined;
if (hashMap[hash]) {
hashMap[hash].value += value;
hashMap[hash].timestamp = timestamp;
} else {
hashMap[hash] = { value, labels: labels || {}, timestamp };
hashMap[hash] = { value, labels: labels || {} };
}
return hashMap;
}
Expand Down
11 changes: 2 additions & 9 deletions lib/defaultMetrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,10 @@ const metricsList = Object.keys(metrics);

module.exports = function collectDefaultMetrics(config) {
if (config !== null && config !== undefined && !isObject(config)) {
throw new Error('config must be null, undefined, or an object');
throw new TypeError('config must be null, undefined, or an object');
}

config = Object.assign(
{
timestamps: true,
eventLoopMonitoringPrecision: 10,
timeout: 10000
},
config
);
config = Object.assign({ eventLoopMonitoringPrecision: 10 }, config);

const registry = config.register || globalRegistry;
const last = registry
Expand Down
Loading