-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Core] [Logging] | Integration logs not being ingested (#1120)
# Description **What:** - Fix logs not being ingested on integrations fails or exceptions. **Why:** - The logger flush method is spawning a new thread that is not awaited when the program exists. - The logger accepts 'exc_info' that cannot be serialized. - Upon shutdown of the program the logger writes a message that needs to be awaited in order to be sent to Port API. **How:** - Added method - 'wait_for_lingering_threads' to 'HTTPMemoryHandler' that will wait for running threads. 'wait_for_lingering_threads' is invoked via the 'SignalHandler' upon exit. - Modified method - '_serialize_record' to look for 'exc_info' in each log and serialize it using 'traceback'. - Added call - 'logger.complete' so log that is sent on exit of program will be sent to Port API. ## Type of change Please leave one option from the following and delete the rest: - [x] Bug fix (non-breaking change which fixes an issue) <h4> All tests should be run against the port production environment(using a testing org). </h4> ### Core testing checklist - [ ] Integration able to create all default resources from scratch - [ ] Resync finishes successfully - [ ] Resync able to create entities - [ ] Resync able to update entities - [ ] Resync able to detect and delete entities - [ ] Scheduled resync able to abort existing resync and start a new one - [ ] Tested with at least 2 integrations from scratch - [ ] Tested with Kafka and Polling event listeners - [ ] Tested deletion of entities that don't pass the selector ## Screenshots Include screenshots from your environment showing how the resources of the integration will look. ## API Documentation Provide links to the API documentation used for this integration. --------- Co-authored-by: Ivan Kalinovski <[email protected]> Co-authored-by: Shalev Avhar <[email protected]> Co-authored-by: Shalev Avhar <[email protected]> Co-authored-by: Tom Tankilevitch <[email protected]> Co-authored-by: Port Bot <[email protected]> Co-authored-by: GitHub Action <[email protected]>
- Loading branch information
1 parent
ddf2d8b
commit 0faaf0d
Showing
7 changed files
with
106 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
from port_ocean.log.handlers import _serialize_record | ||
from loguru import logger | ||
from logging import LogRecord | ||
from queue import Queue | ||
from logging.handlers import QueueHandler | ||
from typing import Callable, Any | ||
|
||
|
||
log_message = "This is a test log message." | ||
exception_grouop_message = "Test Exception group" | ||
exception_message = "Test Exception" | ||
expected_keys = ["message", "level", "timestamp", "extra"] | ||
|
||
|
||
def test_serialize_record_log_shape() -> None: | ||
record = log_record( | ||
lambda: logger.exception( | ||
log_message, | ||
exc_info=None, | ||
) | ||
) | ||
serialized_record = _serialize_record(record) | ||
assert all(key in serialized_record for key in expected_keys) | ||
assert log_message in serialized_record.get("message", None) | ||
|
||
|
||
def test_serialize_record_exc_info_single_exception() -> None: | ||
record = log_record( | ||
lambda: logger.exception( | ||
log_message, | ||
exc_info=ExceptionGroup( | ||
exception_grouop_message, [Exception(exception_message)] | ||
), | ||
) | ||
) | ||
serialized_record = _serialize_record(record) | ||
exc_info = assert_extra(serialized_record.get("extra", {})) | ||
assert exception_grouop_message in exc_info | ||
assert exception_message in exc_info | ||
|
||
|
||
def test_serialize_record_exc_info_group_exception() -> None: | ||
record = log_record( | ||
lambda: logger.exception(log_message, exc_info=Exception(exception_message)) | ||
) | ||
serialized_record = _serialize_record(record) | ||
exc_info = assert_extra(serialized_record.get("extra", {})) | ||
assert exception_message in exc_info | ||
|
||
|
||
def assert_extra(extra: dict[str, Any]) -> str: | ||
exc_info = extra.get("exc_info", None) | ||
assert type(exc_info) is str | ||
return exc_info | ||
|
||
|
||
def log_record(cb: Callable[[], None]) -> LogRecord: | ||
queue = Queue[LogRecord]() | ||
queue_handler = QueueHandler(queue) | ||
logger_id = logger.add( | ||
queue_handler, | ||
level="DEBUG", | ||
format="{message}", | ||
diagnose=False, | ||
enqueue=True, | ||
) | ||
cb() | ||
logger.complete() | ||
logger.remove(logger_id) | ||
record = queue.get() | ||
return record |