{"record":{"id":"60b6f473099df5ca","repo":"louthy/language-ext","slug":"activity-is-null","errorCode":null,"errorMessage":"Activity is null","messagePattern":"Activity is null","errorType":"exception","errorClass":"NullReferenceException","httpStatus":null,"severity":"error","filePath":"LanguageExt.Sys/Sys/Diag/Activity.cs","lineNumber":51,"sourceCode":"\n    public static K<M, Activity> startActivity(\n        string name,\n        ActivityKind activityKind,\n        HashMap<string, object> activityTags,\n        Seq<ActivityLink> activityLinks,\n        DateTimeOffset startTime,\n        ActivityContext? parentContext = null) =>\n        from src in activityIO\n        from cur in currentActivity\n        from act in use(src.StartActivity(\n                            name,\n                            activityKind,\n                            cur is null\n                                ? default\n                                : parentContext ?? cur.Context,\n                            activityTags,\n                            activityLinks,\n                            startTime).Map(a => a ?? throw new NullReferenceException(\"Activity is null\")))\n        select act;\n\n    /// <summary>\n    /// Creates a new activity if there are active listeners for it, using the specified name, activity kind, parent\n    /// activity context, tags, optional activity link and optional start time.\n    /// </summary>\n    /// <param name=\"name\">The operation name of the activity.</param>\n    /// <param name=\"operation\">The operation to whose activity will be traced</param>\n    /// <returns>The result of the `operation`</returns>\n    public static K<M, A> span<A>(string name, K<M, A> operation) =>\n        span(name, ActivityKind.Internal, default, default, DateTimeOffset.Now, operation);\n\n    /// <summary>\n    /// Creates a new activity if there are active listeners for it, using the specified name, activity kind, parent\n    /// activity context, tags, optional activity link and optional start time.\n    /// </summary>\n    /// <param name=\"name\">The operation name of the activity.</param>\n    /// <param name=\"activityKind\">The activity kind.</param>","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/louthy/language-ext/blob/2f0e3628242889774d4141960a35671a0280051f/LanguageExt.Sys/Sys/Diag/Activity.cs#L33-L69","documentation":"Activity.startActivity wraps System.Diagnostics.ActivitySource.StartActivity, which returns null when there is no listener registered for the activity source (i.e. no OpenTelemetry/ActivityListener is sampling that source). LanguageExt's Activity<M,RT> facade treats a null result as a failure and throws NullReferenceException(\"Activity is null\") instead of silently continuing without tracing.","triggerScenarios":"Calling Activity.span/startActivity (directly or via LanguageExt.Sys runtime effects) while no ActivityListener is registered for the ActivitySource name in the runtime's ActivitySourceIO — e.g. tracing enabled in code but the OTel SDK/listener not configured, or the source name filtered out by the listener.","commonSituations":"Running an Eff/Aff program with Activity.span in tests or local runs without OpenTelemetry instrumentation initialized; mismatch between the ActivitySource name registered in the runtime and the name the listener subscribes to; deploying with OTel SDK absent or SetSampler suppressing all (None sampler); calling startActivity manually instead of via span.","solutions":["Register an ActivityListener (or configure OpenTelemetry with AddSource(\"<source-name>\")) before running the span-wrapped operation, using the same source name the RT provides via ActivitySourceIO.","Use Activity.span rather than calling startActivity directly; if you call it directly, accept that a null Activity (no listener) is fatal in this facade and either guard with currentActivity/listener checks or avoid it.","Verify the OTel sampler is not set to Drop/None and that the listener's Source filter (ShouldListenTo) matches the source name.","If tracing is intentionally disabled, wrap span usage behind a flag so the traced code path is not executed without a listener."],"exampleFix":"// before\nvar program = Activity<IO, Runtime>.span(\"do-work\", work);\nprogram.Run(runtime); // throws \"Activity is null\" when no listener\n\n// after\nusing var listener = new ActivityListener\n{\n    ShouldListenTo = s => s.Name == \"my-source\",\n    Sample = (ref ActivityCreationOptions<ActivityContext> _) => ActivitySamplingResult.AllData,\n    ActivityStarted = _ => { }\n};\nActivitySource.AddActivityListener(listener);\nvar program = Activity<IO, Runtime>.span(\"do-work\", work);\nprogram.Run(runtime);","handlingStrategy":"try-catch","validationCode":"// Check that a listener exists before running traced code:\nvar hasListener = ActivitySource.HasListeners(); // System.Diagnostics helper (or track via ActivitySourceIO)\nif (!hasListener) { /* skip tracing or initialize OTel first */ }","typeGuard":"static bool CanTrace(Runtime rt) =>\n    ActivitySource.HasListeners() && rt.Source.Name != null;","tryCatchPattern":"try\n{\n    return program.Run(runtime); // program uses Activity.span\n}\ncatch (NullReferenceException e) when (e.Message == \"Activity is null\")\n{\n    // no ActivityListener registered for the source; run untraced or initialize OTel\n    return fallbackProgram.Run(runtime);\n}","preventionTips":["Always register an ActivityListener / OpenTelemetry AddSource for the runtime's ActivitySource name before running span-wrapped code.","Ensure the OTel sampler is not None/Drop and the listener's ShouldListenTo filter matches the source name.","Prefer Activity.span over raw startActivity so tracing setup is centralized.","Gate tracing behind configuration and log when tracing is requested but no listener is configured."],"tags":["csharp","languageext","opentelemetry","tracing","null-reference"],"backgroundTag":"missing-dependency","analyzedSha":"2f0e3628242889774d4141960a35671a0280051f","analyzedAt":"2026-09-15T03:31:55.716Z","contentChangedAt":"2026-09-15T03:31:55.716Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}