Created converters 57/2357/4
authormbeierl <mark.beierl@emc.com>
Thu, 8 Oct 2015 02:18:19 +0000 (19:18 -0700)
committerMark Beierl <mark.beierl@emc.com>
Thu, 15 Oct 2015 18:49:30 +0000 (18:49 +0000)
JIRA: STORPERF-6

Change-Id: Idba1718a8ad8243872ca100ed10b1c457221d147
Signed-off-by: mbeierl <mark.beierl@emc.com>
storperf/carbon/__init__.py [new file with mode: 0644]
storperf/carbon/converter.py [new file with mode: 0644]
tests/__init__.py [new file with mode: 0644]
tests/carbon/__init__.py [new file with mode: 0644]
tests/carbon/emitter_test.py [new file with mode: 0644]

diff --git a/storperf/carbon/__init__.py b/storperf/carbon/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/storperf/carbon/converter.py b/storperf/carbon/converter.py
new file mode 100644 (file)
index 0000000..4ad5be2
--- /dev/null
@@ -0,0 +1,45 @@
+##############################################################################
+# Copyright (c) 2015 EMC and others.
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+##############################################################################
+
+class JSONToCarbon(object):
+    def __init__(self):
+        pass
+    
+    def convert_to_dictionary(self, json_object, prefix=None):
+        self.timestamp = str(json_object['timestamp'])
+        self.flat_dictionary = {}
+        self.resurse_to_flat_dictionary(json_object, prefix)
+        return self.flat_dictionary
+
+    def resurse_to_flat_dictionary(self, json, prefix=None):
+        if type(json) == dict:
+            for k, v in json.items():
+                if prefix is None:
+                    key = k.decode("utf-8").replace(" ", "_")
+                else:
+                    key = prefix + "." + k.decode("utf-8").replace(" ", "_")
+                if hasattr(v, '__iter__'):
+                    self.resurse_to_flat_dictionary(v, key)
+                else:
+                    self.flat_dictionary[key] = str(v).replace(" ", "_") + " " + self.timestamp
+        elif type(json) == list:
+            index = 0
+            for v in json:
+                index += 1
+                if hasattr(v, '__iter__'):
+                    self.resurse_to_flat_dictionary(v, prefix+"."+str(index))
+                else:
+                    if prefix is None:
+                        self.flat_dictionary[index] = str(v).replace(" ", "_")
+                        + " " + self.timestamp
+                    else:
+                        key = prefix + "." + index;
+                        self.flat_dictionary[key] = str(v).replace(" ", "_") + " " + self.timestamp
+        else:
+            self.flat_dictionary[json] = self.timestamp
diff --git a/tests/__init__.py b/tests/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tests/carbon/__init__.py b/tests/carbon/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tests/carbon/emitter_test.py b/tests/carbon/emitter_test.py
new file mode 100644 (file)
index 0000000..5caba61
--- /dev/null
@@ -0,0 +1,65 @@
+##############################################################################
+# Copyright (c) 2015 EMC and others.
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+##############################################################################
+
+import unittest
+
+import json
+import SocketServer
+import threading
+from time import sleep
+
+import carbon.converter
+import carbon.emitter
+
+class MetricsHandler(SocketServer.BaseRequestHandler):
+
+    def handle(self):
+        # Echo the back to the client
+        CarbonMetricTransmitterTest.response = self.request.recv(1024)
+        return
+
+class MetricsServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
+    pass
+
+class CarbonMetricTransmitterTest(unittest.TestCase):
+    listen_port = 0
+    response = None
+    
+    def setUp(self):
+        
+        address = ('localhost', 0)
+        server = MetricsServer(address, MetricsHandler)
+        ip, self.listen_port = server.server_address
+    
+        t = threading.Thread(target=server.serve_forever)
+        t.setDaemon(True) 
+        t.start()
+    
+    def test_transmit_metrics(self):
+
+        testconv = carbon.converter.JSONToCarbon()
+        json_object = json.loads("""{"timestamp" : "12345"}""")
+        result = testconv.convert_to_dictionary(json_object, "host.run-name")
+
+        emitter = carbon.emitter.CarbonMetricTransmitter()
+        emitter.carbon_port = self.listen_port
+        emitter.transmit_metrics(result)
+        
+        count = 0
+        
+        while (CarbonMetricTransmitterTest.response == None and count < 10):
+            count += 1
+            sleep(0.1)
+        
+        self.assertEqual("host.run-name.timestamp 12345 12345\n", 
+                         CarbonMetricTransmitterTest.response, 
+                         CarbonMetricTransmitterTest.response)
+
+if __name__ == '__main__':
+    unittest.main()
\ No newline at end of file