|
| 1 | +/* |
| 2 | + * Flush stats to ElasticSearch (http://www.elasticsearch.org/) |
| 3 | + * |
| 4 | + * To enable this backend, include 'elastic' in the backends |
| 5 | + * configuration array: |
| 6 | + * |
| 7 | + * backends: ['./backends/elastic'] |
| 8 | + * (if the config file is in the statsd folder) |
| 9 | + * |
| 10 | + * A sample configuration can be found in exampleElasticConfig.js |
| 11 | + * |
| 12 | + * This backend supports the following config options: |
| 13 | + * |
| 14 | + * elasticHost: hostname or IP of ElasticSearch server |
| 15 | + * elasticPort: port of Elastic Search Server |
| 16 | + * elasticIndex: Index of the ElasticSearch server where the metrics are to be saved (_index) |
| 17 | + * elasticIndexType: The type of the index to be saved with (_type) |
| 18 | + * elasticFlushInterval: Right now, not being used and is the same as flushInterval, but hope to use this to make the logging to ES over a longer interval than for graphite |
| 19 | + */ |
| 20 | + |
| 21 | +var net = require('net'), |
| 22 | + util = require('util'), |
| 23 | + http = require('http'); |
| 24 | + |
| 25 | +var debug; |
| 26 | +var flushInterval; |
| 27 | +var elasticHost; |
| 28 | +var elasticPort; |
| 29 | +var elasticIndex; |
| 30 | +var elasticIndexType; |
| 31 | + |
| 32 | +var elasticStats = {}; |
| 33 | + |
| 34 | +var post_stats = function elastic_post_stats(statString) { |
| 35 | + if (elasticHost) { |
| 36 | + try { |
| 37 | + var elastic = require('../utils/httpReq') |
| 38 | + elasticUrl = 'http://' + elasticHost + ':' + elasticPort + '/' + elasticIndex + '/' + elasticIndexType ; |
| 39 | + elastic.urlReq(elasticUrl,{ |
| 40 | + method: 'POST', |
| 41 | + params: statString.toString() |
| 42 | + }, function(body, res){ |
| 43 | + |
| 44 | + }); |
| 45 | + |
| 46 | + |
| 47 | + } catch(e){ |
| 48 | + if (debug) { |
| 49 | + util.log(e); |
| 50 | + } |
| 51 | + elasticStats.last_exception = Math.round(new Date().getTime() / 1000); |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +var flush_stats = function elastic_flush(ts, metrics) { |
| 57 | + var statString = ''; |
| 58 | + var numStats = 0; |
| 59 | + var key; |
| 60 | + var message_array = new Array(); |
| 61 | + |
| 62 | + ts = new Date() |
| 63 | + var counters = metrics.counters; |
| 64 | + var gauges = metrics.gauges; |
| 65 | + var timers = metrics.timers; |
| 66 | + var pctThreshold = metrics.pctThreshold; |
| 67 | + |
| 68 | + for (key in counters) { |
| 69 | + var value = counters[key]; |
| 70 | + var valuePerSecond = value / (flushInterval / 1000); // calculate "per second" rate |
| 71 | + |
| 72 | + //statString += 'stats.' + key + ' ' + valuePerSecond + ' ' + "\n"; |
| 73 | + //statString += 'stats_counts.' + key + ' ' + value + ' ' + "\n"; |
| 74 | + message_array.push(create_json(key + '.persecond',valuePerSecond,ts)); |
| 75 | + message_array.push(create_json(key,value,ts)); |
| 76 | + |
| 77 | + numStats += 1; |
| 78 | + } |
| 79 | + |
| 80 | + for (key in timers) { |
| 81 | + if (timers[key].length > 0) { |
| 82 | + var values = timers[key].sort(function (a,b) { return a-b; }); |
| 83 | + var count = values.length; |
| 84 | + var min = values[0]; |
| 85 | + var max = values[count - 1]; |
| 86 | + |
| 87 | + var mean = min; |
| 88 | + var maxAtThreshold = max; |
| 89 | + |
| 90 | + var message = ""; |
| 91 | + |
| 92 | + var key2; |
| 93 | + |
| 94 | + for (key2 in pctThreshold) { |
| 95 | + var pct = pctThreshold[key2]; |
| 96 | + if (count > 1) { |
| 97 | + var thresholdIndex = Math.round(((100 - pct) / 100) * count); |
| 98 | + var numInThreshold = count - thresholdIndex; |
| 99 | + var pctValues = values.slice(0, numInThreshold); |
| 100 | + maxAtThreshold = pctValues[numInThreshold - 1]; |
| 101 | + |
| 102 | + // average the remaining timings |
| 103 | + var sum = 0; |
| 104 | + for (var i = 0; i < numInThreshold; i++) { |
| 105 | + sum += pctValues[i]; |
| 106 | + } |
| 107 | + |
| 108 | + mean = sum / numInThreshold; |
| 109 | + } |
| 110 | + |
| 111 | + var clean_pct = '' + pct; |
| 112 | + clean_pct.replace('.', '_'); |
| 113 | + message_array.push(create_json(key + '.timers.mean_' + clean_pct ,mean,ts)); |
| 114 | + message_array.push(create_json(key + '.timers.mean_' + clean_pct ,maxAtThreshold,ts)); |
| 115 | + } |
| 116 | + |
| 117 | + message_array.push(create_json(key + '.timers.upper' ,max,ts)); |
| 118 | + message_array.push(create_json(key + '.timers.lower' ,min,ts)); |
| 119 | + message_array.push(create_json(key + '.timers.count' ,count,ts)); |
| 120 | + //statString += message; |
| 121 | + |
| 122 | + numStats += 1; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + for (key in gauges) { |
| 127 | + message_array.push(create_json(key + '.gauges' , gauges[key],ts)); |
| 128 | + numStats += 1; |
| 129 | + } |
| 130 | + |
| 131 | + for(var i = 0; i < message_array.length; i++ ) { |
| 132 | + post_stats(message_array[i].toString()) |
| 133 | + }; |
| 134 | + |
| 135 | +}; |
| 136 | + |
| 137 | +var create_json = function create_elastic_json(entity, value, timestamp){ |
| 138 | + result = {"entity" : entity , "value" : value , "@timestamp" : timestamp} |
| 139 | + return JSON.stringify(result); |
| 140 | +}; |
| 141 | + |
| 142 | + |
| 143 | +var elastic_backend_status = function graphite_status(writeCb) { |
| 144 | + for (stat in elasticStats) { |
| 145 | + writeCb(null, 'elastic', stat, elasticStats[stat]); |
| 146 | + } |
| 147 | +}; |
| 148 | + |
| 149 | +exports.init = function graphite_init(startup_time, config, events) { |
| 150 | + debug = config.debug; |
| 151 | + elasticHost = config.elasticHost; |
| 152 | + elasticPort = config.elasticPort; |
| 153 | + elasticIndex = config.elasticIndex; |
| 154 | + elasticIndexType = config.elasticIndexType; |
| 155 | + |
| 156 | + elasticStats.last_flush = startup_time; |
| 157 | + elasticStats.last_exception = startup_time; |
| 158 | + |
| 159 | + flushInterval = config.flushInterval; |
| 160 | + |
| 161 | + events.on('flush', flush_stats); |
| 162 | + events.on('status', elastic_backend_status); |
| 163 | + |
| 164 | + return true; |
| 165 | +}; |
| 166 | + |
0 commit comments