Skip to content

Commit e11a407

Browse files
committed
simplify try logic
1 parent 5eeb369 commit e11a407

File tree

2 files changed

+46
-80
lines changed

2 files changed

+46
-80
lines changed

scout_apm_logging/handler.py

Lines changed: 46 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -60,57 +60,52 @@ def setup_logging(self):
6060
)
6161

6262
def emit(self, record):
63-
try:
64-
# Initialize here to ensure that required configuration variables are loaded
65-
if not ScoutOtelHandler._class_initialized:
66-
try:
67-
self._initialize()
68-
except Exception as e:
69-
print(f"Failed to initialize ScoutOtelHandler: {e}")
70-
return
71-
72-
if getattr(self._handling_log, "value", False):
73-
# We're already handling a log message, don't get the TrackedRequest
74-
return self.otel_handler.emit(record)
75-
76-
self._handling_log.value = True
77-
scout_request = TrackedRequest.instance()
78-
79-
if scout_request:
80-
operation_detail = get_operation_detail(scout_request)
81-
if operation_detail:
82-
setattr(
83-
record,
84-
operation_detail.entrypoint_attribute,
85-
operation_detail.name,
86-
)
87-
88-
# Add Scout-specific attributes to the log record
89-
record.scout_request_id = scout_request.request_id
90-
record.scout_start_time = scout_request.start_time.isoformat()
91-
# Add duration if the request is completed
92-
if scout_request.end_time:
93-
record.scout_end_time = scout_request.end_time.isoformat()
94-
record.scout_duration = (
95-
scout_request.end_time - scout_request.start_time
96-
).total_seconds()
97-
98-
setattr(record, "service.name", self.service_name)
99-
100-
# Add tags
101-
for key, value in scout_request.tags.items():
102-
setattr(record, f"scout_tag_{key}", value)
103-
104-
# Add the current span's operation if available
105-
current_span = scout_request.current_span()
106-
if current_span:
107-
record.scout_current_operation = current_span.operation
108-
109-
self.otel_handler.emit(record)
110-
except Exception as e:
111-
print(f"Error in ScoutOtelHandler.emit: {e}")
112-
finally:
113-
self._handling_log.value = False
63+
if not ScoutOtelHandler._class_initialized:
64+
try:
65+
self._initialize()
66+
except Exception as e:
67+
print(f"Failed to initialize ScoutOtelHandler: {e}")
68+
return
69+
70+
if getattr(self._handling_log, "value", False):
71+
# We're already handling a log message, don't get the TrackedRequest
72+
return self.otel_handler.emit(record)
73+
74+
self._handling_log.value = True
75+
scout_request = TrackedRequest.instance()
76+
77+
if scout_request:
78+
operation_detail = get_operation_detail(scout_request)
79+
if operation_detail:
80+
setattr(
81+
record,
82+
operation_detail.entrypoint_attribute,
83+
operation_detail.name,
84+
)
85+
86+
# Add Scout-specific attributes to the log record
87+
record.scout_request_id = scout_request.request_id
88+
record.scout_start_time = scout_request.start_time.isoformat()
89+
# Add duration if the request is completed
90+
if scout_request.end_time:
91+
record.scout_end_time = scout_request.end_time.isoformat()
92+
record.scout_duration = (
93+
scout_request.end_time - scout_request.start_time
94+
).total_seconds()
95+
96+
setattr(record, "service.name", self.service_name)
97+
98+
# Add tags
99+
for key, value in scout_request.tags.items():
100+
setattr(record, f"scout_tag_{key}", value)
101+
102+
# Add the current span's operation if available
103+
current_span = scout_request.current_span()
104+
if current_span:
105+
record.scout_current_operation = current_span.operation
106+
107+
self.otel_handler.emit(record)
108+
self._handling_log.value = False
114109

115110
def close(self):
116111
if self.logger_provider:

tests/unit/test_handler.py

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -141,35 +141,6 @@ def test_emit_already_handling_log(otel_scout_handler):
141141
otel_scout_handler._handling_log.value = False
142142

143143

144-
def test_emit_exception_handling(otel_scout_handler):
145-
with patch(
146-
"scout_apm_logging.handler.TrackedRequest"
147-
) as mock_tracked_request, patch(
148-
"sys.stdout", new_callable=io.StringIO
149-
) as mock_stdout:
150-
151-
mock_tracked_request.instance.side_effect = Exception("Test exception")
152-
153-
record = logging.LogRecord(
154-
name="test",
155-
level=logging.INFO,
156-
pathname="",
157-
lineno=0,
158-
msg="Test message",
159-
args=(),
160-
exc_info=None,
161-
)
162-
163-
otel_scout_handler.emit(record)
164-
165-
# Check that the exception was caught and the error message was printed
166-
assert (
167-
"Error in ScoutOtelHandler.emit: Test exception" in mock_stdout.getvalue()
168-
)
169-
170-
otel_scout_handler._handling_log.value = False
171-
172-
173144
def test_close(otel_scout_handler):
174145
with patch.object(otel_scout_handler.logger_provider, "shutdown") as mock_shutdown:
175146
otel_scout_handler.close()

0 commit comments

Comments
 (0)