Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Carbon backend connections persistent #131

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
23 changes: 17 additions & 6 deletions graphios_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@ def __init__(self, cfg):
self.carbon_plaintext = cfg['carbon_plaintext']
except:
self.carbon_plaintext = False

self.connect()

def convert_messages(self, metrics):
"""
Expand Down Expand Up @@ -356,13 +358,11 @@ def fix_string(self, my_string):
my_string = my_string.replace(char, self.replacement_character)
return my_string

def send(self, metrics):
def connect(self):
"""
Connect to the Carbon server
Send the metrics
Connect to the Carbon server[s]
"""
ret = 0
sock = socket.socket()
self.socks = []
servers = self.carbon_servers.split(",")
for serv in servers:
if ":" in serv:
Expand All @@ -376,12 +376,20 @@ def send(self, metrics):
port = 2004
self.log.debug("Connecting to carbon at %s:%s" % (server, port))
try:
sock = socket.socket()
sock.connect((socket.gethostbyname(server), port))
self.socks.append(sock)
self.log.debug("connected")
except Exception, ex:
self.log.warning("Can't connect to carbon: %s:%s %s" % (
server, port, ex))

def send(self, metrics):
"""
Send the metrics
"""
ret = 0
for sock in self.socks:
messages = self.convert_messages(metrics)
try:
for message in messages:
Expand All @@ -392,9 +400,12 @@ def send(self, metrics):
return 0
# this only gets returned if nothing failed.
ret += len(metrics)
sock.close()
return ret

def __del(self):
for sock in self.socks:
sock.close()


# ###########################################################
# #### statsd backend #######################################
Expand Down