Skip to content

Commit

Permalink
add test of new initialize logic
Browse files Browse the repository at this point in the history
  • Loading branch information
quinnmil committed Jan 23, 2025
1 parent 46a3f1b commit e157fd0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
3 changes: 0 additions & 3 deletions scout_apm_logging/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,6 @@ def emit(self, record):
print(f"Failed to initialize ScoutOtelHandler: {e}")
return

if not self.otel_handler:
return

if getattr(self._handling_log, "value", False):
# We're already handling a log message, don't get the TrackedRequest
return self.otel_handler.emit(record)
Expand Down
38 changes: 38 additions & 0 deletions tests/unit/test_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,41 @@ def test_get_ingest_key_not_set(mock_scout_config, otel_scout_handler):
mock_scout_config.value.return_value = None
with pytest.raises(ValueError, match="SCOUT_LOGS_INGEST_KEY is not set"):
otel_scout_handler._get_ingest_key()


def test_initialize_only_once(otel_scout_handler):
# First initialization happens in fixture
initial_service_name = otel_scout_handler.service_name

# Try to initialize again
otel_scout_handler._initialize()

# Service name should not change since second initialization should return early
assert otel_scout_handler.service_name == initial_service_name


def test_emit_handles_initialization_failure():
with patch("scout_apm_logging.handler.scout_config") as mock_scout_config:
mock_scout_config.value.return_value = (
None # This will cause _get_ingest_key to fail
)
ScoutOtelHandler._class_initialized = False

handler = ScoutOtelHandler(service_name="test-service")

with patch("sys.stdout", new_callable=io.StringIO) as mock_stdout:
record = logging.LogRecord(
name="test",
level=logging.INFO,
pathname="",
lineno=0,
msg="Test message",
args=(),
exc_info=None,
)
handler.emit(record)

assert (
"Failed to initialize ScoutOtelHandler: "
"SCOUT_LOGS_INGEST_KEY is not set" in mock_stdout.getvalue()
)

0 comments on commit e157fd0

Please sign in to comment.