Skip to content

Commit 0514f56

Browse files
chore: merge develop into main (#384)
2 parents 0eee07c + f02c2a3 commit 0514f56

File tree

5 files changed

+91
-5
lines changed

5 files changed

+91
-5
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
[tool.poetry]
1818
name = "solnlib"
19-
version = "5.1.2"
19+
version = "5.2.0-beta.1"
2020
description = "The Splunk Software Development Kit for Splunk Solutions"
2121
authors = ["Splunk <addonfactory@splunk.com>"]
2222
license = "Apache-2.0"

solnlib/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,4 @@
5656
"utils",
5757
]
5858

59-
__version__ = "5.1.2"
59+
__version__ = "5.2.0-beta.1"

solnlib/log.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class Logs(metaclass=Singleton):
7979
_default_directory = None
8080
_default_namespace = None
8181
_default_log_format = (
82-
"%(asctime)s %(levelname)s pid=%(process)d tid=%(threadName)s "
82+
"%(asctime)s log_level=%(levelname)s pid=%(process)d tid=%(threadName)s "
8383
"file=%(filename)s:%(funcName)s:%(lineno)d | %(message)s"
8484
)
8585
_default_log_level = logging.INFO
@@ -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: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@
1818
import json
1919
import multiprocessing
2020
import os
21+
import re
2122
import shutil
2223
import threading
2324
import traceback
2425
import time
26+
from textwrap import dedent
27+
2528
import pytest
2629
from unittest import mock
2730

@@ -240,6 +243,42 @@ def test_events_ingested_invalid_input():
240243
assert exp_msg == str(excinfo.value)
241244

242245

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+
243282
def test_log_exceptions_full_msg():
244283
start_msg = "some msg before exception"
245284
with mock.patch("logging.Logger") as mock_logger:
@@ -300,3 +339,33 @@ class AddonComplexError(Exception):
300339
mock_logger.log.assert_called_with(
301340
logging.ERROR, f"exc_l={result} \n{traceback.format_exc()}\n"
302341
)
342+
343+
344+
def test_log_format(monkeypatch, tmp_path):
345+
log_file = tmp_path / "logging_levels.log"
346+
347+
monkeypatch.setattr(log.Logs, "_get_log_file", lambda _, name: str(log_file))
348+
349+
logger = log.Logs().get_logger("logging_levels")
350+
351+
logger.warning("log 2")
352+
logger.error("log 3")
353+
354+
log_content = transform_log(log_file.read_text())
355+
356+
assert (
357+
log_content
358+
== dedent(
359+
"""
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+
""",
363+
).lstrip()
364+
)
365+
366+
367+
def transform_log(log: str) -> str:
368+
log = re.sub(r"pid=\d+", "pid=1234", log)
369+
log = re.sub(r"file=[^ ]+", "file=test_file.py:test_func:123", log)
370+
log = re.sub(r"\d{4}-\d\d-\d\d \d\d[^ ]+", "2024-03-23 10:15:20,555", log)
371+
return log

0 commit comments

Comments
 (0)