From af2985d31a4f8e4e333e7da93a9ed14836fdbe68 Mon Sep 17 00:00:00 2001 From: Matthew Wade Gordon Date: Tue, 8 Mar 2016 15:33:55 -0500 Subject: [PATCH] NOT WORKING - optional counter_rate recording This is in-progress code to optional disable the statsd counter_rate metrics (one for each counter). Salesforce doesn't want them. The test is largely finished, but still has a few things to clean up plus some documentation about how to actually run the tests is needed. --- exampleConfig.js | 2 +- lib/instrumental.js | 21 +++++++--- script/test | 2 + test/instrumental_test.js | 83 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 101 insertions(+), 7 deletions(-) create mode 100755 script/test create mode 100644 test/instrumental_test.js diff --git a/exampleConfig.js b/exampleConfig.js index 0d1dfa7..dc531ab 100644 --- a/exampleConfig.js +++ b/exampleConfig.js @@ -2,7 +2,7 @@ Example StatsD configuration for sending metrics to Instrumental. See the offical Statsd exampleConfig.js for more StatsD options. */ -{ +exports.config = { port: 8125 , backends: ["statsd-instrumental-backend" ] , debug: false diff --git a/lib/instrumental.js b/lib/instrumental.js index c17a85c..14e0e81 100644 --- a/lib/instrumental.js +++ b/lib/instrumental.js @@ -35,11 +35,12 @@ var error = false; var hostname = os.hostname(); var caChainPath = path.join("..", "certs"); var knownCerts = ["equifax", "geotrust", "rapidssl"]; -var key, host, port, timeout, flushInterval, secure, verifyCert, caChain; +var exports = module.exports = {}; // for testing +var key, host, port, timeout, flushInterval, secure, verifyCert, caChain, recordCounterRates; var instrumentalStats = {}; -function build_payload(metrics, time_stamp) { +exports.build_payload = function build_payload(metrics, time_stamp) { var payload = []; // Iterators: key and value. var k, v; @@ -52,9 +53,11 @@ function build_payload(metrics, time_stamp) { if(v !== 0) { payload.push(p.inc(k, v, time_stamp)); } - - var vps = metrics.counter_rates[k]; - payload.push(p.gauge_abs([k, "rate"].join("."), vps, time_stamp)); + console.log("config recordCounterRates: " + recordCounterRates) +// if(recordCounterRates) { + var vps = metrics.counter_rates[k]; + payload.push(p.gauge_abs([k, "rate"].join("."), vps, time_stamp)); +// } } // gauge item.time 7292 121820381 @@ -214,10 +217,16 @@ exports.init = function instrumental_init(startup_time, config, events) { key = config.instrumental.key; host = config.instrumental.host || "collector.instrumentalapp.com"; - // Default 10s timeout timeout = Number(config.instrumental.timeout || 10000); + // Record counter_rates by default + if(typeof(config.instrumental.record_counter_rates) == 'undefined'){ + recordCounterRates = true; + } else { + recordCounterRates = config.instrumental.record_counter_rates; + } + if(typeof(config.instrumental.secure) == 'undefined'){ secure = true; } else { diff --git a/script/test b/script/test new file mode 100755 index 0000000..713917b --- /dev/null +++ b/script/test @@ -0,0 +1,2 @@ +#!/bin/sh +tape test/*.js | faucet diff --git a/test/instrumental_test.js b/test/instrumental_test.js new file mode 100644 index 0000000..fdb3a22 --- /dev/null +++ b/test/instrumental_test.js @@ -0,0 +1,83 @@ +var test = require('tape'); +// import 'core-js' +//import { build_payload } from "../lib/instrumental.js"; +var instrumental = require("../lib/instrumental.js"); +var config = require("../exampleConfig.js").config; + +test('counter_rate should not report if disabled in configuration', function (t) { + var metrics = { + counters: { 'my.test.1': 2805 }, + counter_rates: { 'my.test.1': 280.5 } + }; + + // console.log("CONFIG: " + JSON.stringify(config)); + // console.log("INSTRUMENTAL: " + JSON.stringify(config.instrumental)); + + // Enable rate counters and ensure they are recorded + config.instrumental.recordCounterRates = true; + var payload = instrumental.build_payload(metrics); + + //var expected_payload = ["increment my.test.1 2805 ","gauge_absolute my.test.1.rate 280.5 "] + // t.equal(expected_payload, payload) + // t.fail("record_counter_rates should have a default") + + // TODO: What's with the fucking space on the end of this string? + t.assert(payload.indexOf("gauge_absolute my.test.1.rate 280.5 ") > -1, "Expected a rate metric, got: " + JSON.stringify(payload)) + + // Disable rate counters and ensure they are NOT recorded + config.instrumental.record_counter_rates = false; + var payload = instrumental.build_payload(metrics); + payload.forEach(function(instrumental_metric) { + if (instrumental_metric.indexOf("rate") > -1) { + t.fail("Should not be any rate metrics: " + instrumental_metric); + } else { + t.pass(); + } + }) + + +// [ 'gauge_absolute statsd.bad_lines_seen.rate 0 ', 'increment statsd.packets_received 581 ', 'gauge_absolute statsd.packets_received.rate 58.1 ', 'increment my.test.1 2805 ', 'gauge_absolute my.test.1.rate 280.5 ', 'gauge_absolute my.test.1.count_90 9 ', 'gauge_absolute my.test.1.mean_90 0.3333333333333333 ', 'gauge_absolute my.test.1.upper_90 1 ', 'gauge_absolute my.test.1.sum_90 3 ', 'gauge_absolute my.test.1.sum_squares_90 3 ', 'gauge_absolute my.test.1.std 0.4898979485566356 ', 'gauge_absolute my.test.1.upper 1 ', 'gauge_absolute my.test.1.lower 0 ', 'gauge_absolute my.test.1.count 10 ', 'gauge_absolute my.test.1.count_ps 1 ', 'gauge_absolute my.test.1.sum 4 ', 'gauge_absolute my.test.1.sum_squares 4 ', 'gauge_absolute my.test.1.mean 0.4 ', 'gauge_absolute my.test.1.median 0 ', 'gauge my.test.1 4 ', 'gauge statsd.timestamp_lag 0 ' ] + + + // assert payload is an array + // assert array does not contain any counter_rate metrics + + t.end(); +}); + + + +/* +var metrics = { + counters: + { 'statsd.bad_lines_seen': 0, + 'statsd.packets_received': 581, + 'my.test.1': 2805 }, +gauges: { 'my.test.1': 4, 'statsd.timestamp_lag': 0 }, +timers: { 'my.test.1': [ 0, 0, 0, 0, 0, 0, 1, 1, 1, 1 ] }, +timer_counters: { 'my.test.1': 10 }, +sets: {}, +counter_rates: + { 'statsd.bad_lines_seen': 0, + 'statsd.packets_received': 58.1, + 'my.test.1': 280.5 }, +timer_data: + { 'my.test.1': + { count_90: 9, + mean_90: 0.3333333333333333, + upper_90: 1, + sum_90: 3, + sum_squares_90: 3, + std: 0.4898979485566356, + upper: 1, + lower: 0, + count: 10, + count_ps: 1, + sum: 4, + sum_squares: 4, + mean: 0.4, + median: 0 } }, +pctThreshold: [ 90 ], +histogram: undefined, +statsd_metrics: { processing_time: 0 } }; +*/