|
| 1 | +using System.Diagnostics; |
| 2 | + |
| 3 | +namespace ModularPipelines.Tracing; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Provides Activity-based tracing for module execution. |
| 7 | +/// </summary> |
| 8 | +/// <remarks> |
| 9 | +/// This class provides distributed tracing support using System.Diagnostics.Activity, |
| 10 | +/// enabling integration with OpenTelemetry and other APM tools. |
| 11 | +/// |
| 12 | +/// Phase 1 (current): Foundation - provides ActivitySource for module execution tracing |
| 13 | +/// alongside existing AsyncLocal pattern for backward compatibility. |
| 14 | +/// |
| 15 | +/// Future phases will gradually transition ambient context from AsyncLocal to Activity. |
| 16 | +/// </remarks> |
| 17 | +public static class ModuleActivityTracing |
| 18 | +{ |
| 19 | + /// <summary> |
| 20 | + /// Tag key for the module type name. |
| 21 | + /// </summary> |
| 22 | + public const string ModuleTypeTag = "modular_pipelines.module.type"; |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Tag key for the module type's full name (including namespace). |
| 26 | + /// </summary> |
| 27 | + public const string ModuleTypeFullNameTag = "modular_pipelines.module.type_full"; |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Tag key for the module execution status. |
| 31 | + /// </summary> |
| 32 | + public const string ModuleStatusTag = "modular_pipelines.module.status"; |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Tag key for exception type when a module fails. |
| 36 | + /// </summary> |
| 37 | + public const string ExceptionTypeTag = "exception.type"; |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Tag key for exception message when a module fails. |
| 41 | + /// </summary> |
| 42 | + public const string ExceptionMessageTag = "exception.message"; |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// The ActivitySource for ModularPipelines module execution. |
| 46 | + /// </summary> |
| 47 | + /// <remarks> |
| 48 | + /// Listeners can subscribe to this source to receive module execution spans. |
| 49 | + /// The source name follows the recommended namespace-based naming convention. |
| 50 | + /// </remarks> |
| 51 | + public static readonly ActivitySource Source = new( |
| 52 | + name: "ModularPipelines.Modules", |
| 53 | + version: "1.0.0"); |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// Starts a new Activity for module execution. |
| 57 | + /// </summary> |
| 58 | + /// <param name="moduleType">The type of the module being executed.</param> |
| 59 | + /// <returns>The started Activity, or null if no listeners are registered.</returns> |
| 60 | + public static Activity? StartModuleActivity(Type moduleType) |
| 61 | + { |
| 62 | + var activity = Source.StartActivity( |
| 63 | + name: $"Module.{moduleType.Name}", |
| 64 | + kind: ActivityKind.Internal); |
| 65 | + |
| 66 | + if (activity is not null) |
| 67 | + { |
| 68 | + activity.SetTag(ModuleTypeTag, moduleType.Name); |
| 69 | + activity.SetTag(ModuleTypeFullNameTag, moduleType.FullName); |
| 70 | + } |
| 71 | + |
| 72 | + return activity; |
| 73 | + } |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// Records a successful module completion on the activity. |
| 77 | + /// </summary> |
| 78 | + /// <param name="activity">The activity to update.</param> |
| 79 | + public static void RecordSuccess(Activity? activity) |
| 80 | + { |
| 81 | + activity?.SetTag(ModuleStatusTag, "Successful"); |
| 82 | + activity?.SetStatus(ActivityStatusCode.Ok); |
| 83 | + } |
| 84 | + |
| 85 | + /// <summary> |
| 86 | + /// Records a skipped module on the activity. |
| 87 | + /// </summary> |
| 88 | + /// <param name="activity">The activity to update.</param> |
| 89 | + public static void RecordSkipped(Activity? activity) |
| 90 | + { |
| 91 | + activity?.SetTag(ModuleStatusTag, "Skipped"); |
| 92 | + activity?.SetStatus(ActivityStatusCode.Ok, "Module was skipped"); |
| 93 | + } |
| 94 | + |
| 95 | + /// <summary> |
| 96 | + /// Records a failed module execution on the activity. |
| 97 | + /// </summary> |
| 98 | + /// <param name="activity">The activity to update.</param> |
| 99 | + /// <param name="exception">The exception that caused the failure.</param> |
| 100 | + public static void RecordFailure(Activity? activity, Exception exception) |
| 101 | + { |
| 102 | + if (activity is null) |
| 103 | + { |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + activity.SetTag(ModuleStatusTag, "Failed"); |
| 108 | + activity.SetTag(ExceptionTypeTag, exception.GetType().FullName); |
| 109 | + activity.SetTag(ExceptionMessageTag, exception.Message); |
| 110 | + activity.SetStatus(ActivityStatusCode.Error, exception.Message); |
| 111 | + } |
| 112 | +} |
0 commit comments