refactor: Add Activity-based tracing for module execution (Phase 1)#1466
Conversation
SummaryThis PR adds Activity-based distributed tracing infrastructure for module execution as Phase 1 of migrating from AsyncLocal ambient context to Activity-based patterns. Critical IssuesNone found ✅ Suggestions1. Activity Status Placement Could Cause Resource LeakThe Activity status recording logic is inside the try block at lines 162-172 in ModuleRunner.cs. If an exception occurs during status recording (though unlikely), the finally block won't execute, potentially preventing AsyncLocal cleanup. Recommendation: Move the Activity status recording to the finally block to ensure it always runs and doesn't interfere with the critical AsyncLocal cleanup: try
{
ModuleLogger.Values.Value = logger;
ModuleLogger.CurrentModuleType.Value = moduleType;
await ExecuteModuleLifecycle(moduleState, scopedServiceProvider, pipelineContext, executionContext, moduleContext, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
// Record failure on the Activity before re-throwing
ModuleActivityTracing.RecordFailure(activity, ex);
throw;
}
finally
{
// Record success/skip status if no exception
if (executionContext.Status == Enums.Status.Skipped)
{
ModuleActivityTracing.RecordSkipped(activity);
}
else if (activity?.Status != ActivityStatusCode.Error)
{
ModuleActivityTracing.RecordSuccess(activity);
}
// Clear AsyncLocal to prevent potential leaks
ModuleLogger.Values.Value = null;
ModuleLogger.CurrentModuleType.Value = null;
}2. Minor: Consider Using ActivityKind.ConsumerIn ModuleActivityTracing.cs line 63, the Activity is created with Verdict✅ APPROVE - No critical issues This is a solid foundation for Phase 1. The implementation correctly:
The suggestion above is optional but would make the code more resilient to edge cases during the transition period. |
Add System.Diagnostics.Activity-based tracing alongside existing AsyncLocal pattern for backward compatibility. This enables integration with OpenTelemetry and other APM tools. Changes: - Add ModuleActivityTracing.cs with ActivitySource for module execution - Update ModuleRunner to wrap execution in Activity scope - Record success/skip/failure status on Activity with appropriate tags This is Phase 1 (foundation, non-breaking) of the AsyncLocal refactor. Fixes #1461 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
266b836 to
5963b5f
Compare
SummaryThis PR adds Activity-based distributed tracing infrastructure for module execution as Phase 1 of migrating from AsyncLocal ambient context to Activity-based patterns. Critical IssuesNone found ✅ SuggestionsThe existing github-actions review raises a good point about exception handling, but the suggested refactoring is actually unnecessary. The current implementation is correct:
The current code structure is actually the right pattern - status recording happens in the "happy path" of the try block, and failure recording happens in the catch before re-throw. This is cleaner than checking activity status in the finally block. Minor: Consider adding XML docs to public constantsThe public tag constants in Previous Review StatusThe github-actions bot suggested moving Activity status recording to the finally block, but this is unnecessary given that the finally block will always execute regardless. The current structure is correct and follows standard try-catch-finally patterns. Verdict✅ APPROVE - No critical issues This is a well-structured Phase 1 implementation that:
|
Summary
ModuleActivityTracing.cswithActivitySourcefor distributed tracing of module executionModuleRunnerto wrap module execution in an Activity scopeThis is Phase 1 (foundation, non-breaking) of the AsyncLocal refactor described in issue #1461. The Activity-based tracing runs alongside the existing AsyncLocal pattern for full backward compatibility.
Details
New file:
src/ModularPipelines/Tracing/ModuleActivityTracing.csActivitySourcenamed "ModularPipelines.Modules" for module execution spansModified:
src/ModularPipelines/Engine/Execution/ModuleRunner.csExecuteModuleWithPipelinein an Activity scope using the newModuleActivityTracingBenefits
Test plan
Fixes #1461
🤖 Generated with Claude Code