Skip to content

Commit

Permalink
v0.5.9 - graphite-reconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Wilhelm committed Feb 15, 2014
2 parents 00677e6 + 03ac083 commit 2a43bac
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.5.9

* collectors/graphite - feature: reconnect and resend

# 0.5.8

* collectors/memory - calculate percent util
Expand Down
2 changes: 1 addition & 1 deletion lib/panoptimon/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Copyright (C) 2012-2014 Cisco, Inc.

module Panoptimon
VERSION = "0.5.8"
VERSION = "0.5.9"
end
31 changes: 29 additions & 2 deletions plugins/graphite/graphite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,34 @@

prefix.gsub!(/<%= *(host|domain) *%>/) { hostname[$1] }

socket = TCPSocket.open(host, port)
# attempt to reconnect and resend
# (but avoid over-zealous connect attempts or memory leaks)
# (drops metrics if resend buffer is too large)
writer = ->(){
buffer = []
defer = ->(line) {
buffer.push(line)
buffer = buffer.drop(50) if buffer.length > 100
}
connect = ->() {
return if buffer.length > 3 and not(buffer.length % 10 == 0)
socket = TCPSocket.open(host, port)
socket.write(buffer.slice!(0..buffer.length-1).join('')) \
if buffer.length > 0
return socket
}
socket = connect[] # connect early -> fail early
->(line) {
begin
socket ||= connect[] or return nil.tap { defer.call(line) }
socket.write(line)
rescue => err
warn "graphite error: #{err}"
socket = nil
defer.call(line)
end
}
}[]

->(metrics) {
t = Time.now.to_i
Expand All @@ -31,6 +58,6 @@
metric.gsub!(/\|/, '.')

stat = "#{prefix}.#{metric} #{metrics[k]} #{t}"
socket.write("#{stat}\n")
writer.call("#{stat}\n")
}
}

0 comments on commit 2a43bac

Please sign in to comment.