Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[docs] Adjust filtering processor guidance #3834

Merged
merged 2 commits into from
Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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