async def process_pdus_for_room(room_id: str): logger.debug("Processing PDUs for %s", room_id) try: await self.check_server_matches_acl(origin_host, room_id) except AuthError as e: logger.warning("Ignoring PDUs for room %s from banned server", room_id) for pdu in pdus_by_room[room_id]: event_id = pdu.event_id pdu_results[event_id] = e.error_dict() return for pdu in pdus_by_room[room_id]: event_id = pdu.event_id with pdu_process_time.time(): with nested_logging_context(event_id): try: await self._handle_received_pdu(origin, pdu) pdu_results[event_id] = {} except FederationError as e: logger.warning("Error handling PDU %s: %s", event_id, e) pdu_results[event_id] = {"error": str(e)} except Exception as e: f = failure.Failure() pdu_results[event_id] = {"error": str(e)} logger.error( "Failed to handle PDU %s", event_id, exc_info=(f.type, f.value, f.getTracebackObject()), )
def activate(self, span, finish_on_close): """ Makes a Span active. Args span (Span): the span that should become active. finish_on_close (Boolean): whether Span should be automatically finished when Scope.close() is called. Returns: Scope to control the end of the active period for *span*. It is a programming error to neglect to call Scope.close() on the returned instance. """ enter_logcontext = False ctx = current_context() if not ctx: # We don't want this scope to affect. logger.error("Tried to activate scope outside of loggingcontext") return Scope(None, span) elif ctx.scope is not None: # We want the logging scope to look exactly the same so we give it # a blank suffix ctx = nested_logging_context("") enter_logcontext = True scope = _LogContextScope(self, span, ctx, enter_logcontext, finish_on_close) ctx.scope = scope return scope
async def process_pdus_for_room(room_id: str): with nested_logging_context(room_id): logger.debug("Processing PDUs for %s", room_id) try: await self.check_server_matches_acl(origin_host, room_id) except AuthError as e: logger.warning( "Ignoring PDUs for room %s from banned server", room_id ) for pdu in pdus_by_room[room_id]: event_id = pdu.event_id pdu_results[event_id] = e.error_dict() return for pdu in pdus_by_room[room_id]: pdu_results[pdu.event_id] = await process_pdu(pdu)
async def process_pdu(pdu: EventBase) -> JsonDict: event_id = pdu.event_id with nested_logging_context(event_id): try: await self._handle_received_pdu(origin, pdu) return {} except FederationError as e: logger.warning("Error handling PDU %s: %s", event_id, e) return {"error": str(e)} except Exception as e: f = failure.Failure() logger.error( "Failed to handle PDU %s", event_id, exc_info=(f.type, f.value, f.getTracebackObject()), # type: ignore ) return {"error": str(e)}
def activate(self, span, finish_on_close): """ Makes a Span active. Args span (Span): the span that should become active. finish_on_close (Boolean): whether Span should be automatically finished when Scope.close() is called. Returns: Scope to control the end of the active period for *span*. It is a programming error to neglect to call Scope.close() on the returned instance. """ ctx = current_context() if not ctx: logger.error("Tried to activate scope outside of loggingcontext") return Scope(None, span) # type: ignore[arg-type] if ctx.scope is not None: # start a new logging context as a child of the existing one. # Doing so -- rather than updating the existing logcontext -- means that # creating several concurrent spans under the same logcontext works # correctly. ctx = nested_logging_context("") enter_logcontext = True else: # if there is no span currently associated with the current logcontext, we # just store the scope in it. # # This feels a bit dubious, but it does hack around a problem where a # span outlasts its parent logcontext (which would otherwise lead to # "Re-starting finished log context" errors). enter_logcontext = False scope = _LogContextScope(self, span, ctx, enter_logcontext, finish_on_close) ctx.scope = scope if enter_logcontext: ctx.__enter__() return scope
def test_nested_logging_context(self): with LoggingContext(request="foo"): nested_context = nested_logging_context(suffix="bar") self.assertEqual(nested_context.request, "foo-bar")