gemini-code-assist[bot] commented on code in PR #37413:
URL: https://github.com/apache/beam/pull/37413#discussion_r2765276107


##########
sdks/python/apache_beam/runners/worker/log_handler.py:
##########
@@ -134,9 +134,12 @@ def emit(self, record: logging.LogRecord) -> None:
     log_entry.timestamp.nanos = int(nanoseconds)
     if record.exc_info:
       log_entry.trace = ''.join(traceback.format_exception(*record.exc_info))
-    instruction_id = statesampler.get_current_instruction_id()
-    if instruction_id:
-      log_entry.instruction_id = instruction_id
+    if hasattr(record, 'instruction_id'):
+      log_entry.instruction_id = record.instruction_id
+    if not log_entry.instruction_id:
+      instruction_id = statesampler.get_current_instruction_id()
+      if instruction_id:
+        log_entry.instruction_id = instruction_id

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   This logic for setting `instruction_id` can be simplified to be more concise 
and readable by using `getattr` and consolidating the checks.
   
   ```python
       instruction_id = getattr(record, 'instruction_id', None)
       if not instruction_id:
         instruction_id = statesampler.get_current_instruction_id()
       if instruction_id:
         log_entry.instruction_id = instruction_id
   ```



##########
sdks/python/apache_beam/runners/worker/bundle_processor.py:
##########
@@ -1159,8 +1160,13 @@ def __init__(
         from apache_beam.runners.worker.sdk_worker_main import 
terminate_sdk_harness
         terminate_sdk_harness()
 
-    for op in reversed(self.ops.values()):
-      op.setup(self.data_sampler)
+      if instruction_id:
+        with statesampler.instruction_id(instruction_id):
+          for op in reversed(self.ops.values()):
+            op.setup(self.data_sampler)
+      else:
+        for op in reversed(self.ops.values()):
+          op.setup(self.data_sampler)

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   The loop for setting up operations is duplicated in both the `if` and `else` 
blocks. This can be refactored to avoid code repetition and improve 
maintainability. A common pattern for this is to use a conditional context 
manager.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to