Skip to content

Commit

Permalink
Merge pull request #1 from Goodluckhf/dev
Browse files Browse the repository at this point in the history
Fixes to build and little testing
  • Loading branch information
Goodluckhf authored Oct 23, 2018
2 parents e3f3146 + 450375f commit 97634c6
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 10 deletions.
31 changes: 22 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# uMetrics
![Travis](https://img.shields.io/travis/Goodluckhf/uMetrics/master.svg?style=flat-square)
![Coveralls github branch](https://img.shields.io/coveralls/github/Goodluckhf/uMetrics/master.svg?style=flat-square)
![node](https://img.shields.io/node/v/uMetrics.svg?style=flat-square)
![npm](https://img.shields.io/npm/v/uMetrics.svg?style=flat-square)
![node](https://img.shields.io/node/v/umetrics.svg?style=flat-square)
![npm](https://img.shields.io/npm/v/umetrics.svg?style=flat-square)

![GitHub top language](https://img.shields.io/github/languages/top/Goodluckhf/uMetrics.svg?style=flat-square)
![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/Goodluckhf/uMetrics.svg?style=flat-square)
Expand All @@ -15,6 +15,7 @@

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');
Expand Down Expand Up @@ -42,23 +43,35 @@ uMetrics.start();
Then in your code you have to register new metric name:
```javascript
uMetrics.register(uMetrics.Metrics.Gauge, 'someMetricName', {
ttl : 60 * 1000, // each time this metric will be reset
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
## 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
uMetrics.someMetricName.inc(1, { some_label: 'label_value' });

// You can set value
uMetrics.someMetricName.set(10, { some_label: 'label_value' });
```

Best practise is to wrap with try/catch to secure from uncaught exceptions
```javascript
try {
// Yes, you can write the name of metric here
// Inside it' realised with proxy, so you can use it like this
uMetrics.someMetricName.inc(1, { some_label: 'label_value' });
uMetrics.someMetricName.inc(1);
} catch (error) {
// here just logging error
// logging the error here
}
```
Best practise to wrap with try/catch to secure from uncaught exceptions


You cant inject another transport. Out of the box you have `PullTransport` and `PushTransport`
```javascript
Expand Down
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import UMetrics from './UMetrics';
import Transport from './transport/Transport';
import PullTransport from './transport/PullTransport';
import PushTransport from './transport/PushTransport';
import Metric from './metric/Metric';
import GaugeMetric from './metric/GaugeMetric';

export {
UMetrics,
Transport,
PushTransport,
PullTransport,
GaugeMetric,
Metric,
};
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,8 @@
}
]
}
}
},
"files": [
"dist/**/*.*"
]
}
15 changes: 15 additions & 0 deletions transport/PushTransport.server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,20 @@ describe('PushTransport', () => {
const pushTransport = new PushTransport({}, { url: 'test.com' });
expect(pushTransport).to.have.property('start');
});

it('Should push metrics with an interval', (cb) => {
const pushTransport = new PushTransport({}, { url: 'test.com', interval: 500 });
let pushTimes = 0;
pushTransport.gateway = {
pushAdd() {
pushTimes += 1;
}
};
pushTransport.start();
setTimeout(() => {
expect(pushTimes).to.be.equals(2);
cb();
}, 1100).unref();
});
});

0 comments on commit 97634c6

Please sign in to comment.