| import datetime |
| import functools |
| import logging |
| from advanced_workflow_orchestrator import AdvancedWorkflowOrchestrator |
| from analytics.collector import AsyncAnalyticsCollector |
|
|
| logger = logging.getLogger(__name__) |
|
|
| def activate_analytics(orchestrator_instance: AdvancedWorkflowOrchestrator): |
| """ |
| Monkey-patches the AdvancedWorkflowOrchestrator's execution method |
| to automatically record execution metrics. |
| """ |
| logger.info("🧬 Activating Workflow DNA (Analytics Instrumentation) for AdvancedWorkflowOrchestrator...") |
| |
| |
| |
| |
| original_method = orchestrator_instance._execute_workflow_step |
| |
| |
| async def instrumented_execute_step(self, workflow, step_id, context): |
| start_time = datetime.datetime.now() |
| |
| |
| step = next((s for s in workflow.steps if s.step_id == step_id), None) |
| step_type = step.step_type.value if step and hasattr(step.step_type, 'value') else "unknown" |
| if step_type == "unknown" and step: |
| step_type = str(step.step_type) |
|
|
| status = "COMPLETED" |
| error = None |
| |
| try: |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| await original_method(workflow, step_id, context) |
| |
| |
| result = context.results.get(step_id) |
| if result and result.get("status") == "failed": |
| status = "FAILED" |
| error = result.get("error") |
| |
| except Exception as e: |
| status = "FAILED" |
| error = str(e) |
| raise e |
| |
| finally: |
| end_time = datetime.datetime.now() |
| |
| |
| |
| |
| |
| |
| execution_id = getattr(context, 'workflow_id', 'unknown') |
| |
| |
| workflow_def_id = context.input_data.get("_ui_workflow_id") if context.input_data else None |
| |
| if not workflow_def_id: |
| workflow_def_id = "ad-hoc" |
|
|
| if execution_id != "unknown": |
| await AsyncAnalyticsCollector.get_instance().log_step( |
| execution_id=execution_id, |
| workflow_id=workflow_def_id, |
| step_id=step_id, |
| step_type=step_type, |
| start_time=start_time, |
| end_time=end_time, |
| status=status, |
| error=error, |
| results=context.results.get(step_id) if hasattr(context, 'results') else None |
| ) |
| |
| |
| |
| |
| |
| orchestrator_instance._execute_workflow_step = functools.partial(instrumented_execute_step, orchestrator_instance) |
| |
| logger.info("✅ Workflow DNA Active: Instrumentation applied to AdvancedWorkflowOrchestrator.") |
|
|