Skip to content

Commit

Permalink
Merge 8074031 into fab57f8
Browse files Browse the repository at this point in the history
  • Loading branch information
pilamb committed Nov 21, 2018
2 parents fab57f8 + 8074031 commit 143fcc0
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 7 deletions.
6 changes: 4 additions & 2 deletions pyms/flask/app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def create_app(self):
all libraries like Swagger, database,
the trace system...
return the app and the database objects.
:return:
:return: application
"""
config = get_conf(service=self.service)
app = connexion.App(__name__, specification_dir=os.path.join(self.path, 'swagger'))
Expand All @@ -43,8 +43,10 @@ def create_app(self):
application.register_blueprint(healthcheck_blueprint)
self.init_libs(application)
# Inject Modules
formatter = CustomJsonFormatter('(timestamp) (level) (name) (module) (funcName) (lineno) (message)')
formatter = CustomJsonFormatter('(timestamp) (level) (name) (module) (funcName) (lineno) (message) (traceback)')

if not application.config["TESTING"]:

log_handler = logging.StreamHandler()

application.tracer = FlaskTracer(init_jaeger_tracer(), True, application)
Expand Down
4 changes: 4 additions & 0 deletions pyms/logger/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ def add_fields(self, log_record, record, message_dict):
else:
log_record['severity'] = record.levelname
log_record["service"] = self.service_name
severity = log_record.get('severity')
if severity in ['ERROR', 'CRITICAL'] and log_record.get('exc_info'):
log_record['traceback'] = log_record.get('exc_info')
del log_record['exc_info']

# Add traces
if self.tracer:
Expand Down
3 changes: 2 additions & 1 deletion pyms/tracer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ def init_jaeger_tracer():
'propagation': 'b3',
'logging': True,
}, service_name="data-connection")
return config.initialize_tracer()
return config.initialize_tracer()

44 changes: 44 additions & 0 deletions tests/test_logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import logging
import os
import unittest
import sys

from pyms.constants import CONFIGMAP_FILE_ENVIRONMENT
from pyms.flask.app import Microservice


class TracerTest(unittest.TestCase):

def setUp(self):
os.environ[CONFIGMAP_FILE_ENVIRONMENT] = os.path.join(
os.path.dirname(os.path.abspath(__file__)), "config-tests.yml"
)
self.ms = Microservice(service="my-ms", path=__file__)
self.app = self.ms.create_app()
self.client = self.app.test_client()
self.stream_handler = logging.StreamHandler(sys.stdout)

def test_using_info_level(self):
expected_result = ['INFO:flask.app:SendingData']
self.app.logger.addHandler(self.stream_handler)
with self.assertLogs(level='INFO') as cm:
with self.app.app_context():
self.app.logger.info("SendingData")
self.assertEqual(expected_result, cm.output)

def exception_raiser(self):
raise ZeroDivisionError

def test_raising_exception(self):
self.stream_handler = logging.StreamHandler(sys.stdout)
self.app.logger.addHandler(self.stream_handler)
with self.assertLogs(level='ERROR') as cm:
with self.app.app_context():
with self.assertRaises(ZeroDivisionError) as context:
self.app.logger.error("DivisionByZero")
self.exception_raiser()
self.assertEqual('division by zero', str(context.exception))
self.assertEqual(cm.output, ['ERROR:flask.app:DivisionByZero'])

def tearDown(self):
self.app.logger.removeHandler(self.stream_handler)
4 changes: 0 additions & 4 deletions tests/tests_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@ def test_example_test_file_not_exists(self):
with self.assertRaises(ConfigDoesNotFoundException):
config = ConfFile(path="path/not/exist.yml")

def test_example_test_yaml_file(self):
config = ConfFile(path=os.path.join(self.BASE_DIR, "config-tests.yml"))
self.assertEqual(config.my_ms.test_var, "general")

def test_example_test_json_file(self):
config = ConfFile(path=os.path.join(self.BASE_DIR, "config-tests.json"))
self.assertEqual(config.my_ms.test_var, "general")

0 comments on commit 143fcc0

Please sign in to comment.