mikejihbe / metrics

A metrics library for Node.js
574 stars 58 forks source link

ScheduledReporter with console, csv and graphite implementations. #29

Closed tolbertam closed 8 years ago

tolbertam commented 8 years ago

For #16, this introduces a ScheduledReporter interface that those wanting to create their own custom reporters can implement. It also includes reporters for console, csv, and graphite reporting. All reporters strive to represent the same behavior as the dropwizard metrics libraries reporters. One thing missing is the ability to choose the resolution of rates and durations, which currently resolve to seconds and milliseconds respectively (might be a nice future enhancement, or is that important enough to include here?).

ScheduledReporter has a start(intervalInMs) function for beginning reporting, which calls a report function based on the configured interval. It is up to the implementer to override the report method to perform the behavior they wish. To stop reporting, simply call stop. ScheduledReporter is also an EventEmitter so implementers may choose to emit custom events that can be used for logging and such (as is done in GraphiteReporter).

ConsoleReporter

A custom reporter that prints all metrics on console.log at a defined interval.

Example usage:

var ConsoleReporter = metrics.ConsoleReporter;
var report = new metrics.Report();
var consoleReporter = new ConsoleReporter(report);
consoleReporter.start(10000);

Example output:


Counters -----------------------------------------------------------------------
connected-to
             count = 3

Meters -------------------------------------------------------------------------
queued
             count = 8722
         mean rate = 12.11 events/second
     1-minute rate = 12.98 events/second
     1-minute rate = 12.11 events/second
    15-minute rate = 10.10 events/second

Timers -------------------------------------------------------------------------
UserRequests.requestTimer
             count = 17305
         mean rate = 24.02 events/second
     1-minute rate = 25.67 events/second
     1-minute rate = 23.10 events/second
    15-minute rate = 15.22 events/second
               min =  7.00 milliseconds
               max = 525.00 milliseconds
              mean = 156.85 milliseconds
            stddev = 67.13 milliseconds
              50% <= 143.50 milliseconds
              75% <= 191.00 milliseconds
              95% <= 254.00 milliseconds
              98% <= 292.84 milliseconds
              99% <= 337.26 milliseconds
            99.9% <= 512.80 milliseconds

CsvReporter

A custom reporter that will create a csv file for each metric that is appended to on the defined reporting interval.

Example usage:

var CsvReporter = metrics.CsvReporter;
var report = new metrics.Report();
csvReporter = new CsvReporter(report, '/path/to/directory');
csvReporter.start(10000);

Example csv contents:

--Example counter output--
t,count
1457889699.912,0
1457889710.013,3
1457889720.104,3
1457889730.15,3

--Example meter output--
t,count,mean_rate,m1_rate,m5_rate,m15_rate,rate_unit
1457889699.912,146,14.55342902711324,6,6,6,events/second
1457889710.013,273,13.559827149456117,7.393207200098882,6.306037751326734,6.103626933020923,events/second
1457889720.104,338,11.183165696135521,7.6955595971555155,6.415092206661059,6.14311683976342,events/second
1457889730.15,425,10.553237981724275,8.392187834638802,6.604962596749763,6.210067595403533,events/second
1457889740.216,537,10.66830896376351,9.025342597008544,6.790466006351998,6.276487285589423,events/second
1457889750.355,660,10.91342019974866,8.312062653915623,6.709337885440438,6.254706187757993,events/second

--Example timer output--
t,count,max,mean,min,stddev,p50,p75,p95,p98,p99,p999,mean_rate,m1_rate,m5_rate,m15_rate,rate_unit,duration_unit
1457889699.912,150,318,206.48666666666665,8,70.04233970414198,216.5,271.25,303.0499999999999,310,314.43000000000006,318,14.940239043824702,3.6,3.6,3.6,calls/second,millisecond
1457889710.013,405,318,165.6567901234568,8,65.9542538323502,158,217.5,287.49999999999994,299,309.94,318,20.11223121616924,5.93286512608212,4.106156134805884,3.77104490497006,calls/second,millisecond
1457889720.104,520,318,170.17115384615383,8,63.58172213618375,168,222,280.95,298.15999999999997,308.78999999999996,318,17.202593621807598,8.209550567635338,4.671558651067844,3.9663996858665898,calls/second,millisecond
1457889730.15,705,325,175.57304964539017,8,61.483743323753714,173,222,280,295,308.93999999999994,325,17.501613623951144,10.69634126087566,5.313143074188136,4.190126558940776,calls/second,millisecond
1457889740.216,932,325,177.95493562231772,8,58.53239215660678,177,220.75,277,293,307.66999999999996,325,18.51373631830913,12.856973450335554,5.933447908607099,4.410617071546225,calls/second,millisecond
1457889750.355,1183,325,177.90955198647484,8,56.79651184791325,178.5,219,274,290.41999999999996,303.97000000000025,324.797,19.559861774772244,12.066553482587318,5.988794286293632,4.445938313836983,calls/second,millisecond

GraphiteReporter

A custom reporter that sends metrics to a graphite server on the carbon tcp interface.

Example usage:

var GraphiteReporter = metrics.GraphiteReporter;
var report = new metrics.Report();
var graphiteReporter = new GraphiteReporter(report, 'my.app', '192.168.99.100', 2003);
graphiteReporter.start(30000);
// capture any log events to winston logger.
graphiteReporter.on('log', function(level, msg, ex) {
    log.log(level, msg, ex);
});

What is left is to add a basic test suite (I've validated it all with my application, but would be nice to have a repeatable suite of tests) and to update readme/docs where applicable. But before I do that, would like to get some feedback on the design/approach. My goal here was to introduce something extensible so others can add more reporters (either in this library proper, or in their own modules).

mikejihbe commented 8 years ago

Otherwise I think this looks great. Tests for it would definitely be appreciated

tolbertam commented 8 years ago

Otherwise I think this looks great. Tests for it would definitely be appreciated

Thanks for the review! I'll add some tests tonight and a brief section to the README.

tolbertam commented 8 years ago

Added some tests (naturally, found a bug :P) and an entry to the README (once merged I'll provide some more details on the wiki page w/r/t creating your own custom reporter and using the existing ones).

I noticed that test_all.js currently runs all tests at once, is that intentional? If you like I can incorporate a test framework like mocha for testing and set up travis to run continuous integration, any reservations against that? Can do that in a separate PR if you like.

mikejihbe commented 8 years ago

That would definitely be great. Let's make that a separate PR.

On Mon, Mar 14, 2016 at 8:07 PM, Andrew Tolbert notifications@github.com wrote:

Added some tests (naturally, found a bug :P) and an entry to the README (once merged I'll provide some more details on the wiki page w/r/t creating your own custom reporter and using the existing ones).

I noticed that test_all.js currently runs all tests at once, is that intentional? If you like I can incorporate framework like mocha for testing and set up travis to run continuous integration, any reservations against that? Can do that in a separate PR if you like.

— You are receiving this because you commented. Reply to this email directly or view it on GitHub: https://github.com/mikejihbe/metrics/pull/29#issuecomment-196630397

Mike Ihbe MustWin - Principal

mike@mustwin.com mikejihbe@gmail.com skype: mikeihbe Cell: 651.283.0815

tolbertam commented 8 years ago

Sounds good, I'll start working on that and will create a PR sometime within the next few days. Thanks for merging :+1: