Skip to content

Commit cb5f314

Browse files
fix: add support of custom source in license usage (#383)
**Issue number: [ADDON-72793](https://splunk.atlassian.net/browse/ADDON-72793)** ## Summary Added support for custom source in license usage ### Changes function `events_ingested` has new optional parameter `license_usage_source`. If provided this value will be save as `modular_input_name`. ## Checklist * [x] I have performed a self-review of this change * [x] Changes have been tested * [x] Changes are documented * [x] PR title follows [conventional commit semantics](https://www.conventionalcommits.org/en/v1.0.0/)
1 parent 90bb447 commit cb5f314

File tree

3 files changed

+58
-5
lines changed

3 files changed

+58
-5
lines changed

solnlib/log.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ def events_ingested(
285285
index: str,
286286
account: str = None,
287287
host: str = None,
288+
license_usage_source: str = None,
288289
):
289290
"""Specific function to log the basic information of events ingested for
290291
the monitoring dashboard.
@@ -296,6 +297,7 @@ def events_ingested(
296297
sourcetype: Source type used to write event.
297298
n_events: Number of ingested events.
298299
index: Index used to write event.
300+
license_usage_source: source used to match data with license_usage.log.
299301
account: Account used to write event. (optional)
300302
host: Host used to write event. (optional)
301303
"""
@@ -310,7 +312,9 @@ def events_ingested(
310312

311313
result = {
312314
"action": "events_ingested",
313-
"modular_input_name": modular_input_name,
315+
"modular_input_name": license_usage_source
316+
if license_usage_source
317+
else modular_input_name,
314318
"sourcetype_ingested": sourcetype,
315319
"n_events": n_events,
316320
"event_input": input_name,

tests/integration/test_bulletin_rest_client.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,12 @@ def test_bulletin_rest_api():
5656
bulletin_client_1 = _build_bulletin_manager("msg_name_1", session_key)
5757
bulletin_client_2 = _build_bulletin_manager("msg_name_2", session_key)
5858

59+
# clear bulletin before tests
60+
_clear_bulletin()
61+
5962
bulletin_client_1.create_message(
6063
"new message to bulletin",
61-
capabilities=["apps_restore", "delete_messages"],
64+
capabilities=["apps_restore", "edit_roles"],
6265
roles=["admin"],
6366
)
6467

@@ -106,3 +109,13 @@ def test_bulletin_rest_api():
106109

107110
get_all_msg = bulletin_client_1.get_all_messages()
108111
assert len(get_all_msg["entry"]) == 0
112+
113+
114+
def _clear_bulletin():
115+
session_key = context.get_session_key()
116+
bulletin_client = _build_bulletin_manager("", session_key)
117+
118+
msg_to_del = [el["name"] for el in bulletin_client.get_all_messages()["entry"]]
119+
for msg in msg_to_del:
120+
endpoint = f"{bulletin_client.MESSAGES_ENDPOINT}/{msg}"
121+
bulletin_client._rest_client.delete(endpoint)

tests/unit/test_log.py

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,42 @@ def test_events_ingested_invalid_input():
243243
assert exp_msg == str(excinfo.value)
244244

245245

246+
def test_events_ingested_custom_license_usage():
247+
with mock.patch("logging.Logger") as mock_logger:
248+
log.events_ingested(
249+
mock_logger,
250+
"input_type://input_name",
251+
"sourcetype",
252+
5,
253+
"default",
254+
license_usage_source="custom:license:source",
255+
)
256+
257+
mock_logger.log.assert_called_once_with(
258+
logging.INFO,
259+
"action=events_ingested modular_input_name=custom:license:source sourcetype_ingested=sourcetype "
260+
"n_events=5 event_input=input_name event_index=default",
261+
)
262+
263+
with mock.patch("logging.Logger") as mock_logger:
264+
log.events_ingested(
265+
mock_logger,
266+
"demo://modular_input_name",
267+
"sourcetype",
268+
5,
269+
"default",
270+
host="abcd",
271+
account="test_acc",
272+
license_usage_source="custom:license:source:123",
273+
)
274+
275+
mock_logger.log.assert_called_once_with(
276+
logging.INFO,
277+
"action=events_ingested modular_input_name=custom:license:source:123 sourcetype_ingested=sourcetype n_"
278+
"events=5 event_input=modular_input_name event_index=default event_account=test_acc event_host=abcd",
279+
)
280+
281+
246282
def test_log_exceptions_full_msg():
247283
start_msg = "some msg before exception"
248284
with mock.patch("logging.Logger") as mock_logger:
@@ -321,9 +357,9 @@ def test_log_format(monkeypatch, tmp_path):
321357
log_content
322358
== dedent(
323359
"""
324-
2024-03-23 10:15:20,555 log_level=WARNING pid=1234 tid=MainThread file=test_file.py:test_func:123 | log 2
325-
2024-03-23 10:15:20,555 log_level=ERROR pid=1234 tid=MainThread file=test_file.py:test_func:123 | log 3
326-
""",
360+
2024-03-23 10:15:20,555 log_level=WARNING pid=1234 tid=MainThread file=test_file.py:test_func:123 | log 2
361+
2024-03-23 10:15:20,555 log_level=ERROR pid=1234 tid=MainThread file=test_file.py:test_func:123 | log 3
362+
""",
327363
).lstrip()
328364
)
329365

0 commit comments

Comments
 (0)