Skip to content

Commit

Permalink
simplify try logic
Browse files Browse the repository at this point in the history
  • Loading branch information
quinnmil committed Jan 24, 2025
1 parent 5eeb369 commit e11a407
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 80 deletions.
97 changes: 46 additions & 51 deletions scout_apm_logging/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,57 +60,52 @@ def setup_logging(self):
)

def emit(self, record):
try:
# Initialize here to ensure that required configuration variables are loaded
if not ScoutOtelHandler._class_initialized:
try:
self._initialize()
except Exception as e:
print(f"Failed to initialize ScoutOtelHandler: {e}")
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)

self._handling_log.value = True
scout_request = TrackedRequest.instance()

if scout_request:
operation_detail = get_operation_detail(scout_request)
if operation_detail:
setattr(
record,
operation_detail.entrypoint_attribute,
operation_detail.name,
)

# Add Scout-specific attributes to the log record
record.scout_request_id = scout_request.request_id
record.scout_start_time = scout_request.start_time.isoformat()
# Add duration if the request is completed
if scout_request.end_time:
record.scout_end_time = scout_request.end_time.isoformat()
record.scout_duration = (
scout_request.end_time - scout_request.start_time
).total_seconds()

setattr(record, "service.name", self.service_name)

# Add tags
for key, value in scout_request.tags.items():
setattr(record, f"scout_tag_{key}", value)

# Add the current span's operation if available
current_span = scout_request.current_span()
if current_span:
record.scout_current_operation = current_span.operation

self.otel_handler.emit(record)
except Exception as e:
print(f"Error in ScoutOtelHandler.emit: {e}")
finally:
self._handling_log.value = False
if not ScoutOtelHandler._class_initialized:
try:
self._initialize()
except Exception as e:
print(f"Failed to initialize ScoutOtelHandler: {e}")
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)

self._handling_log.value = True
scout_request = TrackedRequest.instance()

if scout_request:
operation_detail = get_operation_detail(scout_request)
if operation_detail:
setattr(
record,
operation_detail.entrypoint_attribute,
operation_detail.name,
)

# Add Scout-specific attributes to the log record
record.scout_request_id = scout_request.request_id
record.scout_start_time = scout_request.start_time.isoformat()
# Add duration if the request is completed
if scout_request.end_time:
record.scout_end_time = scout_request.end_time.isoformat()
record.scout_duration = (
scout_request.end_time - scout_request.start_time
).total_seconds()

setattr(record, "service.name", self.service_name)

# Add tags
for key, value in scout_request.tags.items():
setattr(record, f"scout_tag_{key}", value)

# Add the current span's operation if available
current_span = scout_request.current_span()
if current_span:
record.scout_current_operation = current_span.operation

self.otel_handler.emit(record)
self._handling_log.value = False

def close(self):
if self.logger_provider:
Expand Down
29 changes: 0 additions & 29 deletions tests/unit/test_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,35 +141,6 @@ def test_emit_already_handling_log(otel_scout_handler):
otel_scout_handler._handling_log.value = False


def test_emit_exception_handling(otel_scout_handler):
with patch(
"scout_apm_logging.handler.TrackedRequest"
) as mock_tracked_request, patch(
"sys.stdout", new_callable=io.StringIO
) as mock_stdout:

mock_tracked_request.instance.side_effect = Exception("Test exception")

record = logging.LogRecord(
name="test",
level=logging.INFO,
pathname="",
lineno=0,
msg="Test message",
args=(),
exc_info=None,
)

otel_scout_handler.emit(record)

# Check that the exception was caught and the error message was printed
assert (
"Error in ScoutOtelHandler.emit: Test exception" in mock_stdout.getvalue()
)

otel_scout_handler._handling_log.value = False


def test_close(otel_scout_handler):
with patch.object(otel_scout_handler.logger_provider, "shutdown") as mock_shutdown:
otel_scout_handler.close()
Expand Down

0 comments on commit e11a407

Please sign in to comment.