Skip to content

Instantly share code, notes, and snippets.

@mgwilliams
Created October 14, 2013 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgwilliams/bd9e94b89d6f2d88dd78 to your computer and use it in GitHub Desktop.
Save mgwilliams/bd9e94b89d6f2d88dd78 to your computer and use it in GitHub Desktop.
diff --git a/salt/returners/carbon_return.py b/salt/returners/carbon_return.py
index 98fb712..46a87ea 100644
--- a/salt/returners/carbon_return.py
+++ b/salt/returners/carbon_return.py
@@ -15,6 +15,7 @@ import socket
import logging
import time
import struct
+import collections
log = logging.getLogger(__name__)
@@ -44,6 +45,20 @@ def _send_picklemetrics(metrics, carbon_sock):
logging.debug('Sent {0} bytes to carbon'.format(sent_bytes))
+def _walk(path, value, metrics, timestamp):
+ if isinstance(value, collections.Mapping):
+ for key, val in value.items():
+ _walk('{0}.{1}'.format(path, key), val, metrics, timestamp)
+ else:
+ try:
+ val = float(value)
+ metrics.append((path, val, timestamp))
+ except TypeError:
+ raise
+ log.info('Error in carbon returner, when trying to'
+ 'convert metric:{0}, with val:{1}'.format(path, val))
+
+
def returner(ret):
'''
Return data to a remote carbon server using the text metric protocol
@@ -72,17 +87,12 @@ def returner(ret):
# Strip the hostname from the carbon base if we are returning from virt
# module since then we will get stable metric bases even if the VM is
# migrate from host to host
+ # TODO: Is this a good idea? Wouldn't the 'id' remain stable anyway?
if not metric_base.startswith('virt.'):
metric_base += '.' + _formatHostname(ret['id'])
metrics = []
- for name, vals in saltdata.items():
- for key, val in vals.items():
- # XXX: force datatype, needs typechecks, etc
- try:
- val = float(val)
- metrics.append((metric_base + '.' + _formatHostname(name) + '.' + key, val, timestamp))
- except TypeError:
- log.info('Error in carbon returner, when trying to convert metric:{0}, with val:{1}'.format(key, val))
+
+ _walk(metric_base, saltdata, metrics, timestamp)
def _send_textmetrics(metrics):
''' Use text protorocol to send metric over socket '''
@@ -97,7 +107,7 @@ def returner(ret):
if sent_bytes == 0:
log.error('Bytes sent 0, Connection reset?')
return
- logging.debug('Sent {0} bytes to carbon'.format(sent_bytes))
+ log.debug('Sent {0} bytes to carbon'.format(sent_bytes))
total_sent_bytes += sent_bytes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment