Skip to content

Commit

Permalink
Merge pull request #7 from Goodluckhf/self-metrics
Browse files Browse the repository at this point in the history
default  nodejs metrics
  • Loading branch information
Goodluckhf authored Oct 8, 2019
2 parents 2be73ea + 72a2b7a commit 3200a27
Show file tree
Hide file tree
Showing 19 changed files with 2,261 additions and 1,901 deletions.
30 changes: 30 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module.exports = {
env: {
node: true,
},
extends: ['eslint-config-airbnb-base', 'plugin:prettier/recommended'],
parser: 'babel-eslint',
rules: {
'no-param-reassign': 0,
'no-unused-expressions': 0,
'no-underscore-dangle': [
'error',
{
allowAfterThis: true,
},
],
'spaced-comment': 0,
},
overrides: [
{
files: ['*.server.test.js'],
env: {
mocha: true,
},
rules: {
'no-underscore-dangle': 'off',
'no-new': 'off',
},
},
],
};
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"singleQuote": true,
"trailingComma": "all"
}
57 changes: 35 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,45 +19,56 @@
Convenient wrapper for prom-client to expose product's metrics to prometheus

## Initialize

You just have to initialize `Facade` class and use it as a `Singletone`:

```javascript
const { UMetrics, PullTransport } = require('umetrics');

// Any logger with this interface
const logger = {
info(msg) {
console.log(msg);
},
warn(msg) {
console.log(msg);
},
error(msg) {
console.log(msg);
},
info(msg) {
console.log(msg);
},
warn(msg) {
console.log(msg);
},
error(msg) {
console.log(msg);
},
};

// And just initialize Singletone once
const uMetrics = new UMetrics(new PullTransport(logger, 3000), {
prefix: 'test',
prefix: 'test',
});

uMetrics.start();
````
```

Available options:

- `prefix` - prefix for all metric names (by default null)
- `labels` - default labels for all metrics should be type: `{ [labelName: string]: any}`
- `nodejsMetricsEnabled` - turn off/on export of [default metrics](https://github.com/siimon/prom-client#default-metrics) (by default false)
- `nodejsMetricsInterval` - interval of scrapping default metrics (7000 ms by default)

Then in your code you have to register new metric name:

```javascript
uMetrics.register(uMetrics.Metrics.Gauge, 'someMetricName', {
ttl : 60 * 1000,
labels: ['some_label'],
ttl: 60 * 1000,
labels: ['some_label'],
});
```
`labels` - you have register label names before setting their values
`ttl` - metric's time to live (milliseconds)

`ttl` parameter is very useful. because usually if you use prometheus, you want to watch time series.
So you expect that that data will be reset evenly
`labels` - you have register label names before setting their values
`ttl` - metric's time to live (milliseconds) - deprecated

## Using

And use it to collect metrics:

```javascript
// Yes, you can write the name of metric here
// Inside it' realised with proxy, so you can use it like this
Expand All @@ -68,24 +79,26 @@ uMetrics.someMetricName.set(10, { some_label: 'label_value' });
```

Best practise is to wrap with try/catch to secure from uncaught exceptions

```javascript
try {
uMetrics.someMetricName.inc(1);
uMetrics.someMetricName.inc(1);
} catch (error) {
// logging the error here
// logging the error here
}
```

You cant inject another transport. Out of the box you have `PullTransport` and `PushTransport`

```javascript
// You need push transport for scripts which run for not long period
// For example cron
new PushTransport(logger, {
url: 'pushgatewayurl',
interval: 2000, //ms
url: 'pushgatewayurl',
interval: 2000, //ms
});
```

See https://prometheus.io/docs/practices/pushing/

Now we have only one Metric type `GaugeMetric`. Welcome for contribution!

Loading

0 comments on commit 3200a27

Please sign in to comment.