Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions solnlib/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,18 +252,32 @@ def modular_input_end(logger: logging.Logger, modular_input_name: str):


def events_ingested(
logger: logging.Logger, modular_input_name: str, sourcetype: str, n_events: int
logger: logging.Logger,
modular_input_name: str,
sourcetype: str,
n_events: int,
index: str = None,
account: str = None,
host: str = None,
):
"""Specific function to log the number of events ingested."""
log_event(
logger,
{
"action": "events_ingested",
"modular_input_name": modular_input_name,
"sourcetype_ingested": sourcetype,
"n_events": n_events,
},
)
"""Specific function to log the basic information of events ingested."""

result = {
"action": "events_ingested",
"modular_input_name": modular_input_name,
"sourcetype_ingested": sourcetype,
"n_events": n_events,
}
if index:
result["event_index"] = index
if account:
result["event_account"] = account
if host:
result["event_host"] = host
if "://" in modular_input_name:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we specify the format in the doc-string and throw a ValueError exception if the format is not what we expect?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't want to force users to use specific format, because someone may already be using this function and will want it to stay as it is

input_name = modular_input_name.split("/")[-1]
result["event_input"] = input_name
log_event(logger, result)


def log_exception(
Expand Down
34 changes: 34 additions & 0 deletions tests/unit/test_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,40 @@ def test_events_ingested():
"action=events_ingested modular_input_name=modular_input_name sourcetype_ingested=sourcetype n_events=5",
)

with mock.patch("logging.Logger") as mock_logger:
log.events_ingested(
mock_logger,
"demo://modular_input_name",
"sourcetype",
5,
index="default",
host="abcd",
account="test_acc",
)

mock_logger.log.assert_called_once_with(
logging.INFO,
"action=events_ingested modular_input_name=demo://modular_input_name sourcetype_ingested=sourcetype n_"
"events=5 event_index=default event_account=test_acc event_host=abcd event_input=modular_input_name",
)

with mock.patch("logging.Logger") as mock_logger:
log.events_ingested(
mock_logger,
"modular_input_name",
"sourcetype",
5,
index="default",
host="abcd",
account="test_acc",
)

mock_logger.log.assert_called_once_with(
logging.INFO,
"action=events_ingested modular_input_name=modular_input_name sourcetype_ingested=sourcetype n_"
"events=5 event_index=default event_account=test_acc event_host=abcd",
)


def test_log_exceptions_full_msg():
start_msg = "some msg before exception"
Expand Down