summaryrefslogtreecommitdiffstats
path: root/nfvbench/fluentd.py
diff options
context:
space:
mode:
authorKerim Gokarslan <kgokarsl@cisco.com>2017-08-31 12:39:07 -0700
committerKerim Gokarslan <kgokarsl@cisco.com>2017-08-31 13:50:57 -0700
commit00410ac4f2b167af4d6cd01072c791ac6804ce5a (patch)
treeeb70742585ca2dc4b45d2e5311075eb7b4caaaa5 /nfvbench/fluentd.py
parentfd72e102b0d6b6b62f72a06ad950e7959c54d7ef (diff)
NFVBENCH-12 Add run summary to fluentd stream
NFVBENCH-14 Fluentd server IP adress from the config is not picked up when configured NFVBENCH-16 At the start of each run send a new fluent record with the config Change-Id: I2d76ecee5b1b93dad4eeccd67d5ed906a0a8faba Signed-off-by: Kerim Gokarslan <kgokarsl@cisco.com>
Diffstat (limited to 'nfvbench/fluentd.py')
-rw-r--r--nfvbench/fluentd.py61
1 files changed, 59 insertions, 2 deletions
diff --git a/nfvbench/fluentd.py b/nfvbench/fluentd.py
index 683d4ce..5bfcebe 100644
--- a/nfvbench/fluentd.py
+++ b/nfvbench/fluentd.py
@@ -16,6 +16,7 @@ from datetime import datetime
from fluent import sender
import logging
+
class FluentLogHandler(logging.Handler):
'''This is a minimalist log handler for use with Fluentd
@@ -26,17 +27,25 @@ class FluentLogHandler(logging.Handler):
- the runlogdate (to tie multiple run-related logs together)
The timestamp is retrieved by the fluentd library.
'''
+
def __init__(self, tag, fluentd_ip='127.0.0.1', fluentd_port=24224):
logging.Handler.__init__(self)
self.tag = tag
self.formatter = logging.Formatter('%(message)s')
- self.sender = sender.FluentSender(self.tag, port=fluentd_port)
- self.start_new_run()
+ self.sender = sender.FluentSender(self.tag, host=fluentd_ip, port=fluentd_port)
+ self.runlogdate = 0
+ self.__warning_counter = 0
+ self.__error_counter = 0
def start_new_run(self):
'''Delimitate a new run in the stream of records with a new timestamp
'''
self.runlogdate = str(datetime.now())
+ # reset counters
+ self.__warning_counter = 0
+ self.__error_counter = 0
+ # send start record
+ self.__send_start_record()
def emit(self, record):
data = {
@@ -44,4 +53,52 @@ class FluentLogHandler(logging.Handler):
"loglevel": record.levelname,
"message": self.formatter.format(record)
}
+ self.__update_stats(record.levelno)
self.sender.emit(None, data)
+
+ # send START record for each run
+ def __send_start_record(self):
+ data = {
+ "runlogdate": self.runlogdate,
+ "loglevel": "START",
+ "message": "NFVBENCH run is started",
+ "numloglevel": 0,
+ "numerrors": 0,
+ "numwarnings": 0
+ }
+ self.sender.emit(None, data)
+
+ # send stats related to the current run and reset state for a new run
+ def send_run_summary(self, run_summary_required):
+ if run_summary_required or self.__get_highest_level() == logging.ERROR:
+ data = {
+ "runlogdate": self.runlogdate,
+ "loglevel": "RUN_SUMMARY",
+ "message": self.__get_highest_level_desc(),
+ "numloglevel": self.__get_highest_level(),
+ "numerrors": self.__error_counter,
+ "numwarnings": self.__warning_counter
+ }
+ self.sender.emit(None, data)
+
+ def __get_highest_level(self):
+ if self.__error_counter > 0:
+ return logging.ERROR
+ elif self.__warning_counter > 0:
+ return logging.WARNING
+ return logging.INFO
+
+ def __get_highest_level_desc(self):
+ highest_level = self.__get_highest_level()
+ if highest_level == logging.INFO:
+ return "GOOD RUN"
+ elif highest_level == logging.WARNING:
+ return "RUN WITH WARNINGS"
+ else:
+ return "RUN WITH ERRORS"
+
+ def __update_stats(self, levelno):
+ if levelno == logging.WARNING:
+ self.__warning_counter += 1
+ elif levelno == logging.ERROR:
+ self.__error_counter += 1