Skip to content

Commit

Permalink
Adjust filtering processor example.
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeBlanch committed Oct 27, 2022
1 parent c5cb95e commit 2ea306c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 24 deletions.
29 changes: 12 additions & 17 deletions docs/trace/extending-the-sdk/MyFilteringProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,32 @@
using System;
using System.Diagnostics;
using OpenTelemetry;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;

/// <summary>
/// A custom processor for filtering <see cref="Activity"/> instances.
/// </summary>
/// <remarks>
/// Note: <see cref="CompositeProcessor{T}"/> is used as the base class because
/// the SDK needs to understand that <c>MyFilteringProcessor</c> wraps an inner
/// processor. Without that understanding some features such as <see
/// cref="Resource"/> would be unavailable because the SDK needs to push state
/// about the parent <see cref="TracerProvider"/> to all processors in the
/// chain.
/// </remarks>
internal sealed class MyFilteringProcessor : CompositeProcessor<Activity>
internal sealed class MyFilteringProcessor : BaseProcessor<Activity>
{
private readonly Func<Activity, bool> filter;

public MyFilteringProcessor(BaseProcessor<Activity> processor, Func<Activity, bool> filter)
: base(new[] { processor })
/// <summary>
/// Initializes a new instance of the <see cref="MyFilteringProcessor"/>
/// class.
/// </summary>
/// <param name="filter">Function used to test if an <see cref="Activity"/>
/// should be recorded or dropped. Return <see langword="true"/> to record
/// or <see langword="false"/> to drop.</param>
public MyFilteringProcessor(Func<Activity, bool> 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;
}
}
}
13 changes: 6 additions & 7 deletions docs/trace/extending-the-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
```

Expand Down

0 comments on commit 2ea306c

Please sign in to comment.