Skip to content
This repository has been archived by the owner on Dec 1, 2023. It is now read-only.

send multiple stats in one udp package #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 40 additions & 11 deletions backends/gossip_girl.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,34 @@ function GossipGirl(startupTime, config, emitter) {
var self = this
self.config = config.gossip_girl || []
self.statsd_config = config
self.ignorable = [ "statsd.packets_received", "statsd.bad_lines_seen", "statsd.packet_process_time" ]
self.ignorable = [ "statsd.packets_received", "statsd.bad_lines_seen", "statsd.packet_process_time", "statsd.timestamp_lag" ]

emitter.on('flush', function(time_stamp, metrics) { self.process(time_stamp, metrics); })
}

GossipGirl.prototype.gossip = function(packet, host, port) {
var self = this
self.sock.send(packet, 0, packet.length, port, host, function(err,bytes) {
var buffer = new Buffer(packet)
self.sock.send(buffer, 0, buffer.length, port, host, function(err,bytes) {
if (err) {
console.log(err)
}
})
}

GossipGirl.prototype.format = function (key, value, suffix) {
return new Buffer("'" + key + "':" + value + "|" + suffix)
return "'" + key + "':" + value + "|" + suffix + "\n"
}

GossipGirl.prototype.process = function(time_stamp, metrics) {
var self = this
hosts = self.config
var stats, packet
var stats, packetTmp, packet = "", countStats = 0, countPacket = 0, countValues = 0

var stats_map = {
counters: { data: metrics.counters, suffix: "c", name: "counter" },
gauges: { data: metrics.gauges, suffix: "g", name: "gauge" },
timers: { data: metrics.timers, suffix: "ms", name: "timer" }
counters: { data: metrics.counters, suffix: "c", name: "counter" },
gauges: { data: metrics.gauges, suffix: "g", name: "gauge" },
timers: { data: metrics.timers, suffix: "ms", name: "timer" }
}

self.sock = dgram.createSocket("udp4")
Expand All @@ -40,14 +41,42 @@ GossipGirl.prototype.process = function(time_stamp, metrics) {
stats = stats_map[type]
for (key in stats.data) {
if (self.ignorable.indexOf(key) >= 0) continue
packet = self.format(key, stats.data[key], stats.suffix)

packetTmp = self.format(key, stats.data[key], stats.suffix)

if (self.statsd_config.dumpMessages) {
util.log ("Gossiping about " + stats.name + ": " + packet)
}

self.gossip(packet, hosts[i].host, hosts[i].port)
}
countStats++
countValues += stats.data[key]
if (!self.config[i].packageJoin) {
self.gossip(packetTmp, hosts[i].host, hosts[i].port);
countPacket++
} else {
if (packet.length + packetTmp.length > self.config[i].packageLimit) {
setTimeout(function(packet, hosts, i){
self.gossip(packet, hosts[i].host, hosts[i].port)
}, self.config[i].delay * countPacket, packet, hosts, i);
packet = packetTmp;
countPacket++
} else {
packet = packet + packetTmp;
}
}
}
if (packet.length > 0) {
setTimeout(function(packet, hosts, i){
self.gossip(packet, hosts[i].host, hosts[i].port)
}, self.config[i].delay * countPacket, packet, hosts, i);
countPacket++
}
if (self.config[i].verbose) {
util.log ("Gossiping " + type + " : " + countStats + "(" + countValues + ") stats in " + countPacket + " packages")
}
countPacket = 0
countStats = 0
countValues = 0
packet = ""
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion exampleGossipConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ Optional Variables:
"gossip_girl": [
{
"host": "observer.example.com",
"port": 8125
"port": 8125,
"packageJoin": true,
"packageLimit": 8192,
"verbose": true,
"delay": 20
}
],
"flushInterval": 1000,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"keywords" : "statsd",
"author" : "Zo Obradovic <[email protected]>",
"main" : "./backends/gossip_girl.js",
"version" : "1.0.1",
"version" : "1.0.2",
"engines" : {
"node" : ">=0.6"
},
Expand Down