Explorar o código

Merge pull request #35 from greg-junkietech/feature/#34-prometheus-export-format

implemented feature #34: introduced view output format prometheus
Andrey %!s(int64=4) %!d(string=hai) anos
pai
achega
4d77c7ea2d

+ 20 - 2
metrixpp/ext/std/tools/view.py

@@ -11,6 +11,7 @@ import sys
 from metrixpp.mpp import api
 from metrixpp.mpp import utils
 from metrixpp.mpp import cout
+from metrixpp.mpp import promout
 
 DIGIT_COUNT = 8
 
@@ -22,9 +23,9 @@ class Plugin(api.Plugin, api.IConfigurable, api.IRunable):
 
     def declare_configuration(self, parser):
         self.parser = parser
-        parser.add_option("--format", "--ft", default='txt', choices=['txt', 'xml', 'python'],
+        parser.add_option("--format", "--ft", default='txt', choices=['txt', 'xml', 'python', 'prometheus'],
                           help="Format of the output data. "
-                          "Possible values are 'xml', 'txt' or 'python' [default: %default]")
+                          "Possible values are 'xml', 'txt', 'python' or 'prometheus' [default: %default]")
         parser.add_option("--nest-regions", "--nr", action="store_true", default=False,
                           help="If the option is set (True), data for regions is exported in the form of a tree. "
                           "Otherwise, all regions are exported in plain list. [default: %default]")
@@ -122,6 +123,8 @@ def export_to_str(out_format, paths, loader, loader_prev, nest_regions, dist_col
 
         if out_format == 'txt':
             cout_txt(data, loader)
+        elif out_format == 'prometheus':
+            cout_prom(data, loader)
         elif out_format == 'xml':
             result += utils.serialize_to_xml(data, root_name = "data", digitCount = DIGIT_COUNT) + "\n"
         elif out_format == 'python':
@@ -657,3 +660,18 @@ def cout_txt(data, loader):
                 "Directory content:",
                 details)
     
+def cout_prom(data, loader):
+    
+    for namespace in sorted(list(data['aggregated-data'].keys())):
+        for field in sorted(list(data['aggregated-data'][namespace].keys())):
+            details = []
+            for attr in ['avg', 'min', 'max', 'total']:
+                if isinstance(data['aggregated-data'][namespace][field][attr], float):
+                    # round the data to reach same results on platforms with different precision
+                    details.append((attr, str(round(data['aggregated-data'][namespace][field][attr], DIGIT_COUNT))))
+                else:
+                    details.append((attr, str(data['aggregated-data'][namespace][field][attr])))
+
+            promout.notify(path = data['info']['path'],
+                    metric = namespace + "." + field,
+                    details = details)

+ 20 - 0
metrixpp/mpp/promout.py

@@ -0,0 +1,20 @@
+#
+#    Metrix++, Copyright 2009-2019, Metrix++ Project
+#    Link: https://github.com/metrixplusplus/metrixplusplus
+#    
+#    This file is a part of Metrix++ Tool.
+#    
+
+import re
+
+def notify(path, metric, details, region=""):
+    notification = ""
+
+    for each in details:
+        if str(each[1]) != 'None':
+            if region:
+                notification += ("{metric} {{file=\"{path}\", region=\"{region}\"}} {value}\n".format(metric=re.sub(r'^_', '', re.sub(r'[\.\:]', '_', metric + "." + str(each[0]))), value=str(each[1]), path=path, region=region))
+            else:
+                notification += ("{metric} {{file=\"{path}\"}} {value}\n".format(metric=re.sub(r'^_', '', re.sub(r'[\.\:]', '_', metric + "." + str(each[0]))), value=str(each[1]), path=path, region=region))
+        
+    print(notification)

+ 3 - 0
metrixpp/tests/general/test_basic.py

@@ -227,6 +227,9 @@ class Test(tests.common.TestCase):
         runner = tests.common.ToolRunner('view', ['--format=xml'], prefix='xml')
         self.assertExec(runner.run())
         
+        runner = tests.common.ToolRunner('view', ['--format=prometheus'], prefix='prometheus')
+        self.assertExec(runner.run())
+        
         runner = tests.common.ToolRunner('collect',
                                          ['--std.code.complexity.cyclomatic'],
                                          prefix='nest',

+ 1 - 1
metrixpp/tests/general/test_basic/test_help_view_default_stdout.gold.txt

@@ -18,7 +18,7 @@ Options:
                         'METRIXPLUSPLUS_LOG_LEVEL' if set. [default: INFO]
   --format=FORMAT, --ft=FORMAT
                         Format of the output data. Possible values are 'xml',
-                        'txt' or 'python' [default: txt]
+                        'txt', 'python' or 'prometheus' [default: txt]
   --nest-regions, --nr  If the option is set (True), data for regions is
                         exported in the form of a tree. Otherwise, all regions
                         are exported in plain list. [default: False]

+ 6 - 0
metrixpp/tests/general/test_basic/test_view_format_view_prometheus_stdout.gold.txt

@@ -0,0 +1,6 @@
+std_code_complexity_cyclomatic_avg {file="./"} 1.375
+std_code_complexity_cyclomatic_min {file="./"} 1
+std_code_complexity_cyclomatic_max {file="./"} 3
+std_code_complexity_cyclomatic_total {file="./"} 11.0
+
+