Skip to content

Commit 5d38560

Browse files
authored
Doc updates logging reporter (#45)
* Update docs and add Logging Reporter * Update example output * Update method to be protected * Yarn Format * pr feed back * localhost -> yaorg.github.io
1 parent 0cc35d1 commit 5d38560

File tree

11 files changed

+200
-27
lines changed

11 files changed

+200
-27
lines changed

packages/measured-core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"main": "./lib/index.js",
1313
"scripts": {
1414
"clean": "rm -fr build",
15-
"format": "prettier --write '**/*.{ts,js}'",
15+
"format": "prettier --write './lib/**/*.{ts,js}'",
1616
"lint": "eslint lib --ext .js",
1717
"test:node": "mocha './test/**/test-*.js'",
1818
"test:node:coverage": "nyc --report-dir build/coverage/ --reporter=html --reporter=text mocha './test/**/test-*.js'",

packages/measured-node-metrics/lib/utils/CpuUtils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module.exports = {
1616

1717
cpus.forEach(cpu => {
1818
//Total up the time in the cores tick
19-
Object.keys(cpu.times).forEach((type) => {
19+
Object.keys(cpu.times).forEach(type => {
2020
totalTick += cpu.times[type];
2121
});
2222
//Total up the idle time of the core
@@ -37,6 +37,6 @@ module.exports = {
3737
const idleDifference = endMeasure.idle - startMeasure.idle;
3838
const totalDifference = endMeasure.total - startMeasure.total;
3939
//Calculate the average percentage CPU usage
40-
return Math.ceil(100 - (100 * idleDifference / totalDifference));
40+
return Math.ceil(100 - 100 * idleDifference / totalDifference);
4141
}
4242
};

packages/measured-node-metrics/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"main": "./lib/index.js",
1313
"scripts": {
1414
"clean": "rm -fr build",
15-
"format": "prettier --write '**/*.{ts,js}'",
15+
"format": "prettier --write './lib/**/*.{ts,js}'",
1616
"lint": "eslint lib --ext .js",
1717
"test:node": "mocha './test/**/test-*.js'",
1818
"test:node:coverage": "nyc --report-dir build/coverage/ --reporter=html --reporter=text mocha './test/**/test-*.js'",

packages/measured-reporting/README.md

Lines changed: 81 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,94 @@ npm install measured-reporting
1515
### [Self Reporting Metrics Registry](https://yaorg.github.io/node-measured/SelfReportingMetricsRegistry.html)
1616
A dimensional aware self-reporting metrics registry, just supply this class with a reporter implementation at instantiation and this is all you need to instrument application level metrics in your app.
1717

18+
See the [SelfReportingMetricsRegistryOptions](http://yaorg.github.io/node-measured/build/docs/packages/measured-reporting/global.html#SelfReportingMetricsRegistryOptions) for advanced configuration.
19+
20+
```javascript
21+
const { SelfReportingMetricsRegistry, LoggingReporter } = require('measured-reporting');
22+
const registry = new SelfReportingMetricsRegistry(new LoggingReporter({
23+
defaultDimensions: {
24+
hostname: os.hostname()
25+
}
26+
}));
27+
28+
// The metric will flow through LoggingReporter#_reportMetrics(metrics) every 10 seconds by default
29+
const myCounter = registry.getOrCreateCounter('my-counter');
30+
31+
```
32+
1833
### [Reporter Abstract Class](https://yaorg.github.io/node-measured/Reporter.html)
19-
The base class for reporter implementations. This class is extended and the [_reportMetrics(metrics)](https://yaorg.github.io/node-measured/Reporter.html#_reportMetrics__anchor) method gets overridden to create an data aggregator specific reporter. See the [SignalFx Reporter](../measured-signalfx-reporter/) for an example implementation.
34+
Extend this class and override the [_reportMetrics(metrics)](https://yaorg.github.io/node-measured/Reporter.html#_reportMetrics__anchor) method to create a vendor specific reporter implementation.
2035

36+
See the [ReporterOptions](http://yaorg.github.io/node-measured/build/docs/packages/measured-reporting/global.html#ReporterOptions) for advanced configuration.
37+
38+
#### Current Implementations
39+
- [SignalFx Reporter](https://yaorg.github.io/node-measured/SignalFxMetricsReporter.html) in the `measured-signalfx-reporter` package.
40+
- reports metrics to SignalFx.
41+
- [Logging Reporter](https://yaorg.github.io/node-measured/LoggingReporter.html) in the `measured-reporting` package.
42+
- A reporter impl that simply logs the metrics via the Logger
43+
44+
#### Creating an anonymous Implementation
2145
You can technically create an anonymous instance of this, see the following example.
2246
```javascript
23-
// Create anonymous console logging Reporter instance.
24-
const reporter = new class extends Reporter {
47+
const os = require('os');
48+
const process = require('process');
49+
const { SelfReportingMetricsRegistry, Reporter } = require('measured-reporting');
50+
51+
// Create a self reporting registry with an anonymous Reporter instance;
52+
const registry = new SelfReportingMetricsRegistry(
53+
new class extends Reporter {
54+
constructor() {
55+
super({
56+
defaultDimensions: {
57+
hostname: os.hostname(),
58+
env: process.env['NODE_ENV'] ? process.env['NODE_ENV'] : 'unset'
59+
}
60+
})
61+
}
62+
2563
_reportMetrics(metrics) {
26-
metrics.forEach(metric => console.log(JSON.stringify(metric.toJSON())))
64+
metrics.forEach(metric => {
65+
console.log(JSON.stringify({
66+
metricName: metric.name,
67+
dimensions: this._getDimensions(metric),
68+
data: metric.metricImpl.toJSON()
69+
}))
70+
});
2771
}
28-
};
72+
}()
73+
);
74+
75+
// create a gauge that reports the process uptime every second
76+
const processUptimeGauge = registry.getOrCreateGauge('node.process.uptime', () => process.uptime(), {}, 1);
77+
```
78+
79+
Example output:
80+
```bash
81+
APP5HTD6ACCD8C:foo jfiel2$ NODE_ENV=development node index.js | bunyan
82+
[2018-06-06T23:39:49.678Z] INFO: Reporter/9526 on APP5HTD6ACCD8C: {"metricName":"node.process.uptime","dimensions":{"hostname":"APP5HTD6ACCD8C","env":"development"},"data":0.092}
83+
[2018-06-06T23:39:50.685Z] INFO: Reporter/9526 on APP5HTD6ACCD8C: {"metricName":"node.process.uptime","dimensions":{"hostname":"APP5HTD6ACCD8C","env":"development"},"data":1.099}
84+
[2018-06-06T23:39:51.690Z] INFO: Reporter/9526 on APP5HTD6ACCD8C: {"metricName":"node.process.uptime","dimensions":{"hostname":"APP5HTD6ACCD8C","env":"development"},"data":2.104}
85+
[2018-06-06T23:39:52.691Z] INFO: Reporter/9526 on APP5HTD6ACCD8C: {"metricName":"node.process.uptime","dimensions":{"hostname":"APP5HTD6ACCD8C","env":"development"},"data":3.105}
86+
[2018-06-06T23:39:53.692Z] INFO: Reporter/9526 on APP5HTD6ACCD8C: {"metricName":"node.process.uptime","dimensions":{"hostname":"APP5HTD6ACCD8C","env":"development"},"data":4.106}
87+
```
88+
89+
90+
Consider creating a proper class and contributing it back to Measured if it is generic and sharable.
91+
92+
### [Logging Reporter Class](https://yaorg.github.io/node-measured/LoggingReporter.html)
93+
A simple reporter that logs the metrics via the Logger.
94+
95+
See the [ReporterOptions](http://yaorg.github.io/node-measured/build/docs/packages/measured-reporting/global.html#ReporterOptions) for advanced configuration.
96+
97+
```javascript
98+
const { SelfReportingMetricsRegistry, LoggingReporter } = require('measured-reporting');
99+
const registry = new SelfReportingMetricsRegistry(new LoggingReporter({
100+
logger: myLogerImpl, // defaults to new bunyan logger if not supplied
101+
defaultDimensions: {
102+
hostname: require('os').hostname()
103+
}
104+
}));
29105
```
30-
It would of course be better to create a proper class and contribute it back as a new package for measured if it is generic and sharable.
31106

32107
## What are dimensions?
33108
As described by Signal Fx:

packages/measured-reporting/lib/index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const SelfReportingMetricsRegistry = require('./registries/SelfReportingMetricsRegistry');
22
const Reporter = require('./reporters/Reporter');
3+
const LoggingReporter = require('./reporters/LoggingReporter');
34
const inputValidators = require('./validators/inputValidators');
45

56
/**
@@ -8,14 +9,30 @@ const inputValidators = require('./validators/inputValidators');
89
*/
910
module.exports = {
1011
/**
12+
* The Self Reporting Metrics Registry Class.
13+
*
1114
* @type {SelfReportingMetricsRegistry}
1215
*/
1316
SelfReportingMetricsRegistry,
17+
1418
/**
19+
* The abstract / base Reporter class.
20+
*
1521
* @type {Reporter}
1622
*/
1723
Reporter,
24+
25+
/**
26+
* The basic included reference reporter, simply logs the metrics.
27+
* See {ReporterOptions} for options.
28+
*
29+
* @type {LoggingReporter}
30+
*/
31+
LoggingReporter,
32+
1833
/**
34+
* Various Input Validation functions.
35+
*
1936
* @type {inputValidators}
2037
*/
2138
inputValidators

packages/measured-reporting/lib/registries/SelfReportingMetricsRegistry.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ class SelfReportingMetricsRegistry {
120120
* Creates a {@link Histogram} or gets the existing Histogram for a given name and dimension combo
121121
*
122122
* @param {string} name The Metric name
123-
* @param {Dimensions} dimensions any custom {@link Dimensions} for the Metric
124-
* @param {number} publishingIntervalInSeconds a optional custom publishing interval
123+
* @param {Dimensions} [dimensions] any custom {@link Dimensions} for the Metric
124+
* @param {number} [publishingIntervalInSeconds] a optional custom publishing interval
125125
* @return {Histogram}
126126
*/
127127
getOrCreateHistogram(name, dimensions, publishingIntervalInSeconds) {
@@ -143,8 +143,8 @@ class SelfReportingMetricsRegistry {
143143
* Creates a {@link Meter} or gets the existing Meter for a given name and dimension combo
144144
*
145145
* @param {string} name The Metric name
146-
* @param {Dimensions} dimensions any custom {@link Dimensions} for the Metric
147-
* @param {number} publishingIntervalInSeconds a optional custom publishing interval
146+
* @param {Dimensions} [dimensions] any custom {@link Dimensions} for the Metric
147+
* @param {number} [publishingIntervalInSeconds] a optional custom publishing interval
148148
* @return {Meter}
149149
*/
150150
getOrCreateMeter(name, dimensions, publishingIntervalInSeconds) {
@@ -165,8 +165,8 @@ class SelfReportingMetricsRegistry {
165165
* Creates a {@link Counter} or gets the existing Counter for a given name and dimension combo
166166
*
167167
* @param {string} name The Metric name
168-
* @param {Dimensions} dimensions any custom {@link Dimensions} for the Metric
169-
* @param {number} publishingIntervalInSeconds a optional custom publishing interval
168+
* @param {Dimensions} [dimensions] any custom {@link Dimensions} for the Metric
169+
* @param {number} [publishingIntervalInSeconds] a optional custom publishing interval
170170
* @return {Counter}
171171
*/
172172
getOrCreateCounter(name, dimensions, publishingIntervalInSeconds) {
@@ -188,8 +188,8 @@ class SelfReportingMetricsRegistry {
188188
* Creates a {@link Timer} or gets the existing Timer for a given name and dimension combo.
189189
*
190190
* @param {string} name The Metric name
191-
* @param {Dimensions} dimensions any custom {@link Dimensions} for the Metric
192-
* @param {number} publishingIntervalInSeconds a optional custom publishing interval
191+
* @param {Dimensions} [dimensions] any custom {@link Dimensions} for the Metric
192+
* @param {number} [publishingIntervalInSeconds] a optional custom publishing interval
193193
* @return {Timer}
194194
*/
195195
getOrCreateTimer(name, dimensions, publishingIntervalInSeconds) {
@@ -211,8 +211,8 @@ class SelfReportingMetricsRegistry {
211211
* Creates a {@link SettableGauge} or gets the existing SettableGauge for a given name and dimension combo.
212212
*
213213
* @param {string} name The Metric name
214-
* @param {Dimensions} dimensions any custom {@link Dimensions} for the Metric
215-
* @param {number} publishingIntervalInSeconds a optional custom publishing interval
214+
* @param {Dimensions} [dimensions] any custom {@link Dimensions} for the Metric
215+
* @param {number} [publishingIntervalInSeconds] a optional custom publishing interval
216216
* @return {SettableGauge}
217217
*/
218218
getOrCreateSettableGauge(name, dimensions, publishingIntervalInSeconds) {
@@ -236,11 +236,17 @@ class SelfReportingMetricsRegistry {
236236
* @param {string} name The Metric name.
237237
* @param {function} valueProducingPromiseCallback.
238238
* @param {number} cachedGaugeUpdateIntervalInSeconds.
239-
* @param {Dimensions} dimensions any custom {@link Dimensions} for the Metric.
240-
* @param {number} publishingIntervalInSeconds a optional custom publishing interval.
239+
* @param {Dimensions} [dimensions] any custom {@link Dimensions} for the Metric.
240+
* @param {number} [publishingIntervalInSeconds] a optional custom publishing interval.
241241
* @return {CachedGauge}
242242
*/
243-
getOrCreateCachedGauge(name, valueProducingPromiseCallback, cachedGaugeUpdateIntervalInSeconds, dimensions, publishingIntervalInSeconds) {
243+
getOrCreateCachedGauge(
244+
name,
245+
valueProducingPromiseCallback,
246+
cachedGaugeUpdateIntervalInSeconds,
247+
dimensions,
248+
publishingIntervalInSeconds
249+
) {
244250
validateCachedGaugeOptions(name, valueProducingPromiseCallback, dimensions, publishingIntervalInSeconds);
245251

246252
let cachedGauge;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const Reporter = require('./Reporter');
2+
3+
/**
4+
* A reporter impl that simply logs the metrics via the Logger.
5+
*
6+
* @example
7+
* const { SelfReportingMetricsRegistry, LoggingReporter } = require('measured-reporting');
8+
* const registry = new SelfReportingMetricsRegistry(new LoggingReporter());
9+
*
10+
* @extends {Reporter}
11+
*/
12+
class LoggingReporter extends Reporter {
13+
/**
14+
* Logs the metrics via the inherited logger instance.
15+
* @param {MetricWrapper[]} metrics
16+
* @protected
17+
*/
18+
_reportMetrics(metrics) {
19+
metrics.forEach(metric => {
20+
this._log.info(
21+
JSON.stringify({
22+
metricName: metric.name,
23+
dimensions: this._getDimensions(metric),
24+
data: metric.metricImpl.toJSON()
25+
})
26+
);
27+
});
28+
}
29+
}
30+
31+
module.exports = LoggingReporter;

packages/measured-reporting/lib/reporters/Reporter.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,50 @@ const DEFAULT_REPORTING_INTERVAL_IN_SECONDS = 10;
88
* The abstract reporter that specific implementations can extend to create a Self Reporting Metrics Registry Reporter.
99
*
1010
* {@link SelfReportingMetricsRegistry}
11+
*
12+
* @example
13+
* const os = require('os');
14+
* const process = require('process');
15+
* const { SelfReportingMetricsRegistry, Reporter } = require('measured-reporting');
16+
*
17+
* // Create a self reporting registry with a named anonymous reporter instance;
18+
* const registry = new SelfReportingMetricsRegistry(
19+
* new class ConsoleReporter extends Reporter {
20+
* constructor() {
21+
* super({
22+
* defaultDimensions: {
23+
* hostname: os.hostname(),
24+
* env: process.env['NODE_ENV'] ? process.env['NODE_ENV'] : 'unset'
25+
* }
26+
* })
27+
* }
28+
*
29+
* _reportMetrics(metrics) {
30+
* metrics.forEach(metric => {
31+
* console.log(JSON.stringify({
32+
* metricName: metric.name,
33+
* dimensions: this._getDimensions(metric),
34+
* data: metric.metricImpl.toJSON()
35+
* }))
36+
* });
37+
* }
38+
* }()
39+
* );
40+
*
41+
* @example
42+
* // Create a regular class that extends Reporter
43+
* class LoggingReporter extends Reporter {
44+
* _reportMetrics(metrics) {
45+
* metrics.forEach(metric => {
46+
* this._log.info(JSON.stringify({
47+
* metricName: metric.name,
48+
* dimensions: this._getDimensions(metric),
49+
* data: metric.metricImpl.toJSON()
50+
* }))
51+
* });
52+
* }
53+
* }
54+
*
1155
* @abstract
1256
*/
1357
class Reporter {

packages/measured-reporting/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"main": "./lib/index.js",
1313
"scripts": {
1414
"clean": "rm -fr build",
15-
"format": "prettier --write '**/*.{ts,js}'",
15+
"format": "prettier --write './lib/**/*.{ts,js}'",
1616
"lint": "eslint lib --ext .js",
1717
"test:node": "mocha './test/**/test-*.js'",
1818
"test:node:coverage": "nyc --report-dir build/coverage/ --reporter=html --reporter=text mocha './test/**/test-*.js'",

packages/measured-reporting/test/unit/reporters/test-Reporter.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,9 @@ describe('Reporter', () => {
118118
it('Can be used to create an anonymous instance of a reporter', () => {
119119
const anonymousReporter = new class extends Reporter {
120120
_reportMetrics(metrics) {
121-
metrics.forEach(metric => console.log(JSON.stringify(metric)))
121+
metrics.forEach(metric => console.log(JSON.stringify(metric)));
122122
}
123-
};
123+
}();
124124

125125
validateReporterInstance(anonymousReporter);
126126
});

0 commit comments

Comments
 (0)