Skip to content

Commit 5eeb369

Browse files
committed
add test of new initialize logic
1 parent a749967 commit 5eeb369

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

scout_apm_logging/handler.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,6 @@ def emit(self, record):
6969
print(f"Failed to initialize ScoutOtelHandler: {e}")
7070
return
7171

72-
if not self.otel_handler:
73-
return
74-
7572
if getattr(self._handling_log, "value", False):
7673
# We're already handling a log message, don't get the TrackedRequest
7774
return self.otel_handler.emit(record)

tests/unit/test_handler.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,41 @@ def test_get_ingest_key_not_set(mock_scout_config, otel_scout_handler):
204204
mock_scout_config.value.return_value = None
205205
with pytest.raises(ValueError, match="SCOUT_LOGS_INGEST_KEY is not set"):
206206
otel_scout_handler._get_ingest_key()
207+
208+
209+
def test_initialize_only_once(otel_scout_handler):
210+
# First initialization happens in fixture
211+
initial_service_name = otel_scout_handler.service_name
212+
213+
# Try to initialize again
214+
otel_scout_handler._initialize()
215+
216+
# Service name should not change since second initialization should return early
217+
assert otel_scout_handler.service_name == initial_service_name
218+
219+
220+
def test_emit_handles_initialization_failure():
221+
with patch("scout_apm_logging.handler.scout_config") as mock_scout_config:
222+
mock_scout_config.value.return_value = (
223+
None # This will cause _get_ingest_key to fail
224+
)
225+
ScoutOtelHandler._class_initialized = False
226+
227+
handler = ScoutOtelHandler(service_name="test-service")
228+
229+
with patch("sys.stdout", new_callable=io.StringIO) as mock_stdout:
230+
record = logging.LogRecord(
231+
name="test",
232+
level=logging.INFO,
233+
pathname="",
234+
lineno=0,
235+
msg="Test message",
236+
args=(),
237+
exc_info=None,
238+
)
239+
handler.emit(record)
240+
241+
assert (
242+
"Failed to initialize ScoutOtelHandler: "
243+
"SCOUT_LOGS_INGEST_KEY is not set" in mock_stdout.getvalue()
244+
)

0 commit comments

Comments
 (0)