async def test_with_context(self): test_event = EventContext(id="my_id", flow_id="my_flow_id", name="test") async with EventContextHolder.with_async_context( test_event.id, test_event.flow_id, test_event.event_name, test_event.event_version) as event_context: self.assertEqual(event_context, test_event) self.assertEqual(EventContextHolder.get(), test_event) self.assertEqual(EventContextHolder.get(), EventContext())
async def event_1(): second_event = EventContext(id=uuid4(), flow_id=uuid4(), name="test") async with EventContextHolder.with_async_context( second_event.id, second_event.flow_id, second_event.event_name, second_event.event_version, ) as event_context: assert EventContextHolder.get( ) == event_context, "EventContext was wrongly set" _events.append({1: EventContextHolder.get()})
async def event_0(): first_event = EventContext(id=uuid4(), flow_id=uuid4(), name="test") async with EventContextHolder.with_async_context( first_event.id, first_event.flow_id, first_event.event_name, first_event.event_version, ) as event_context: assert EventContextHolder.get( ) == event_context, "EventContext was wrongly set" _events.append({0: EventContextHolder.get()}) await asyncio.sleep(0.2)
def log(self, level, msg, *args, **kwargs): if self.isEnabledFor(level): event_context = EventContextHolder.get() _msg = dict( severity=logging.getLevelName(level), logmessage=msg, EventID=event_context.id, FlowID=event_context.flow_id, UserId=event_context.user_id, Operation="{}:v{}".format(event_context.event_name, event_context.event_version), logger=self.klass, LoggerName=self.logger.name, logdate=dt.utcnow().isoformat(), ApplicationVersion=self.version, ) extra = kwargs.pop("extra", None) if extra: if isinstance(extra, dict): extra = json.dumps(extra) if not isinstance(extra, str): extra_type = type(extra) raise TypeError( "Extra param needs to be dict or str, not {}".format(extra_type) ) _msg["extra"] = extra if level == logging.ERROR and kwargs.get("exc_info"): args = tuple() fmt = logging.Formatter() _exc = sys.exc_info() _msg["stackTrace"] = fmt.formatException(_exc) kwargs["exc_info"] = False msg = json.dumps(_msg) self.logger.log(level, msg, *args, **kwargs)
def test_build_event_with_context(self): context = EventContext(id="my-id", flow_id="my-flow-id", name="test") EventContextHolder.set(context) event = { "name": "event:name", "version": 1, "payload": { "test": "test" }, "id": "id-test", "flow_id": "flow-id-test", } builded_event = self.client.build_request_event(**event) self.assertEqual(context.id, builded_event.id) self.assertEqual(context.flow_id, builded_event.flow_id) EventContextHolder.clean()
def process_event(cls, raw_event: str) -> str: event = None try: event: Event = cls.parse_event(raw_event) with EventContextHolder.with_context(event.id, event.flow_id, event.name) as _: event_handler: EventHandler = EventDiscovery.get( event.name, event.version) response: ResponseEvent = event_handler.handle(event) return response.to_json() except EventException as exception: return EventBuilder.error_for(exception, event).to_json()
async def event_2(): _events.append({2: EventContextHolder.get()})