From a90d237a23a7eaf6e08c5ccf2b679e5ef6658749 Mon Sep 17 00:00:00 2001 From: Bob Renwick Date: Thu, 14 Apr 2016 14:47:33 +0100 Subject: [PATCH] add new config so that you can specify which data to index We've decided that we don't need the raw `timer` data when we have the `timerData` data too. This keeps the default as indexing everything but allows users of the backend to specify the types that they want to index. The only thing that users need to be careful of, is if they choose to override the setting then they need to use any custom names they've given each data type. --- lib/elasticsearch.js | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/lib/elasticsearch.js b/lib/elasticsearch.js index c067e1b..3ff55d8 100644 --- a/lib/elasticsearch.js +++ b/lib/elasticsearch.js @@ -4,7 +4,7 @@ * To enable this backend, include 'elastic' in the backends * configuration array: * - * backends: ['./backends/elastic'] + * backends: ['./backends/elastic'] * (if the config file is in the statsd folder) * * A sample configuration can be found in exampleElasticConfig.js @@ -33,9 +33,14 @@ var elasticIndex; var elasticIndexTimestamp; var elasticCountType; var elasticTimerType; +var elasticTypesToIndex; var elasticStats = {}; +var should_index_type = function should_index_type(type) { + return elasticTypesToIndex.indexOf(type) >= 0; +} + var es_bulk_insert = function elasticsearch_bulk_insert(listCounters, listTimers, listTimerData, listGaugeData) { @@ -147,24 +152,33 @@ var flush_stats = function elastic_flush(ts, metrics) { var gauges = metrics.gauges; var pctThreshold = metrics.pctThreshold; */ + if (should_index_type(elasticCountType) >= 0) { + for (key in metrics.counters) { + numStats += fm.counters(key, metrics.counters[key], ts, array_counts); + } - for (key in metrics.counters) { - numStats += fm.counters(key, metrics.counters[key], ts, array_counts); } - for (key in metrics.timers) { - numStats += fm.timers(key, metrics.timers[key], ts, array_timers); + if (should_index_type(elasticTimerType)) { + for (key in metrics.timers) { + numStats += fm.timers(key, metrics.timers[key], ts, array_timers); + } } - if (array_timers.length > 0) { - for (key in metrics.timer_data) { - fm.timer_data(key, metrics.timer_data[key], ts, array_timer_data); + if (should_index_type(elasticTimerDataType)) { + if (array_timers.length > 0) { + for (key in metrics.timer_data) { + fm.timer_data(key, metrics.timer_data[key], ts, array_timer_data); + } } } - for (key in metrics.gauges) { - numStats += fm.gauges(key, metrics.gauges[key], ts, array_gauges); + if (should_index_type(elasticGaugeDataType)) { + for (key in metrics.gauges) { + numStats += fm.gauges(key, metrics.gauges[key], ts, array_gauges); + } } + if (debug) { lg.log('metrics:'); lg.log( JSON.stringify(metrics) ); @@ -200,6 +214,7 @@ exports.init = function elasticsearch_init(startup_time, config, events, logger) elasticTimerDataType = configEs.timerDataType || elasticTimerType + '_stats'; elasticGaugeDataType = configEs.gaugeDataType || 'gauge'; elasticFormatter = configEs.formatter || 'default_format'; + elasticTypesToIndex = configEs.typesToIndex || [elasticCountType, elasticTimerType, elasticTimerDataType, elasticGaugeDataType]; fm = require('./' + elasticFormatter + '.js') if (debug) {