Skip to content

Commit ec864e2

Browse files
committed
simplify try logic
1 parent e157fd0 commit ec864e2

File tree

1 file changed

+46
-51
lines changed

1 file changed

+46
-51
lines changed

scout_apm_logging/handler.py

+46-51
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:

0 commit comments

Comments
 (0)