def test_using_context_logger_no_parent(): instance = TestContextClass() context_func = Mock(return_value={'some': 'context'}) wrapped = context_logger(context_func, instance.function) wrapped() context_func.assert_called_once()
def decorator(func): endpoint = ns.endpoint_for(operation) endpoint_path = graph.build_route_path(path, ns.prefix) if enable_cors: func = cross_origin(supports_credentials=True)(func) if enable_basic_auth or ns.enable_basic_auth: func = graph.basic_auth.required(func) if enable_context_logger and ns.controller is not None: func = context_logger( graph.request_context, func, parent=ns.controller, ) # set the opaque component data_func to look at the flask request context func = graph.opaque.initialize(graph.request_context)(func) if graph.route_metrics.enabled: func = graph.route_metrics(endpoint)(func) # keep audit decoration last (before registering the route) so that # errors raised by other decorators are captured in the audit trail if enable_audit: func = graph.audit(func) graph.app.route( endpoint_path, endpoint=endpoint, methods=[operation.value.method], )(func) return func
def wrap_handler(self, handler): """ Wrap handler with context logger. Ensures that all handler logger calls have access to opaque data. """ return context_logger( context_func=lambda *args, **kwargs: self.opaque, func=handler, parent=handler, )
def handle_message(self, media_type, message): """ Handle a single message. """ if message is None: self.logger.debug( "Skipping message with unparsed type: {}".format(media_type)) return False with self.opaque.initialize(self.sqs_message_context, message): try: sqs_message_handler = self.sqs_message_handler_registry[ media_type] except KeyError: # no handlers self.logger.debug( "Skipping message with no registered handler: {}".format( media_type)) return False # NB if possible, log with the handler's logger to make it easier # to tell which handler failed in the logs. try: logger = sqs_message_handler.logger except AttributeError: logger = self.logger try: handler_with_context = context_logger( self.sqs_message_context, sqs_message_handler, parent=sqs_message_handler, ) return handler_with_context(message) except SkipMessage as skipped: extra = self.sqs_message_context(message) extra.update(skipped.extra) logger.info( "Skipping message for reason: {}".format(str(skipped)), extra=extra, ) return False except Nack: logger.info("Nacking SQS message: {}".format(media_type, ), extra=self.sqs_message_context(message)) raise except Exception as error: logger.warning("Error handling SQS message: {}".format( media_type, ), exc_info=True, extra=self.sqs_message_context(message)) raise error
def decorator(func): endpoint = ns.endpoint_for(operation) endpoint_path = graph.build_route_path(path, ns.prefix) if enable_cors: func = cross_origin(supports_credentials=True)(func) if enable_basic_auth or ns.enable_basic_auth: func = graph.basic_auth.required(func) if enable_context_logger and ns.controller is not None: func = context_logger( graph.request_context, func, parent=ns.controller, ) # set the opaque component data_func to look at the flask request context func = graph.opaque.initialize(graph.request_context)(func) if enable_metrics or ns.enable_metrics: from microcosm_flask.metrics import StatusCodeClassifier tags = [f"endpoint:{endpoint}", "backend_type:microcosm_flask"] func = graph.metrics_counting( "route", tags=tags, classifier_cls=StatusCodeClassifier, )(func) func = graph.metrics_timing("route", tags=tags)(func) # keep audit decoration last (before registering the route) so that # errors raised by other decorators are captured in the audit trail if enable_audit: func = graph.audit(func) graph.app.route( endpoint_path, endpoint=endpoint, methods=[operation.value.method], )(func) return func
def invoke_handler(self, handler, media_type, content): """ Invoke handler with logging and error handling. """ logger = self.choose_logger(handler) try: handler_with_context = context_logger( self.sqs_message_context, handler, parent=handler, ) return handler_with_context(content) except SkipMessage as skipped: extra = self.sqs_message_context(content) extra.update(skipped.extra) logger.info( "Skipping message for reason: {}".format(str(skipped)), extra=extra, ) return False except Nack: logger.info( "Nacking SQS message: {}".format( media_type, ), extra=self.sqs_message_context(content) ) raise except Exception as error: logger.warning( "Error handling SQS message: {}".format( media_type, ), exc_info=True, extra=self.sqs_message_context(content) ) raise error