diff --git a/docs/trace/extending-the-sdk/MyFilteringProcessor.cs b/docs/trace/extending-the-sdk/MyFilteringProcessor.cs index ed74678decf..5c1230fcac7 100644 --- a/docs/trace/extending-the-sdk/MyFilteringProcessor.cs +++ b/docs/trace/extending-the-sdk/MyFilteringProcessor.cs @@ -17,37 +17,32 @@ using System; using System.Diagnostics; using OpenTelemetry; -using OpenTelemetry.Resources; -using OpenTelemetry.Trace; /// /// A custom processor for filtering instances. /// -/// -/// Note: is used as the base class because -/// the SDK needs to understand that MyFilteringProcessor wraps an inner -/// processor. Without that understanding some features such as would be unavailable because the SDK needs to push state -/// about the parent to all processors in the -/// chain. -/// -internal sealed class MyFilteringProcessor : CompositeProcessor +internal sealed class MyFilteringProcessor : BaseProcessor { private readonly Func filter; - public MyFilteringProcessor(BaseProcessor processor, Func filter) - : base(new[] { processor }) + /// + /// Initializes a new instance of the + /// class. + /// + /// Function used to test if an + /// should be recorded or dropped. Return to record + /// or to drop. + public MyFilteringProcessor(Func filter) { this.filter = filter ?? throw new ArgumentNullException(nameof(filter)); } public override void OnEnd(Activity activity) { - // Call the underlying processor - // only if the Filter returns true. - if (this.filter(activity)) + // Bypass export if the Filter returns false. + if (!this.filter(activity)) { - base.OnEnd(activity); + activity.ActivityTraceFlags &= ~ActivityTraceFlags.Recorded; } } } diff --git a/docs/trace/extending-the-sdk/README.md b/docs/trace/extending-the-sdk/README.md index b95f8225c4b..da2a6cfae7f 100644 --- a/docs/trace/extending-the-sdk/README.md +++ b/docs/trace/extending-the-sdk/README.md @@ -270,20 +270,19 @@ capabilities such as offering easy access to more context (library specific). ### Filtering Processor Another common use case of writing custom processor is to filter Activities from -being exported. Such a "FilteringProcessor" can be written as a wrapper around -an underlying processor. An example "FilteringProcessor" is shown +being exported. Such a "FilteringProcessor" can be written to toggle the +`Activity.Recorded` flag. An example "FilteringProcessor" is shown [here](./MyFilteringProcessor.cs). -When using such a filtering processor, instead of using extension method to -register the exporter, they must be registered manually as shown below: +When using such a filtering processor if should be registered BEFORE the +processor containing the exporter which should be bypassed: ```csharp using var tracerProvider = Sdk.CreateTracerProviderBuilder() .SetSampler(new MySampler()) .AddSource("OTel.Demo") - .AddProcessor(new MyFilteringProcessor( - new SimpleActivityExportProcessor(new MyExporter("ExporterX")), - (act) => true)) + .AddProcessor(new MyFilteringProcessor(activity => true)) + .AddProcessor(new SimpleActivityExportProcessor(new MyExporter("ExporterX"))) .Build(); ```