|
17 | 17 | using System; |
18 | 18 | using System.Diagnostics; |
19 | 19 | using OpenTelemetry; |
20 | | -using OpenTelemetry.Resources; |
21 | | -using OpenTelemetry.Trace; |
22 | 20 |
|
23 | 21 | /// <summary> |
24 | 22 | /// A custom processor for filtering <see cref="Activity"/> instances. |
25 | 23 | /// </summary> |
26 | | -/// <remarks> |
27 | | -/// Note: <see cref="CompositeProcessor{T}"/> is used as the base class because |
28 | | -/// the SDK needs to understand that <c>MyFilteringProcessor</c> wraps an inner |
29 | | -/// processor. Without that understanding some features such as <see |
30 | | -/// cref="Resource"/> would be unavailable because the SDK needs to push state |
31 | | -/// about the parent <see cref="TracerProvider"/> to all processors in the |
32 | | -/// chain. |
33 | | -/// </remarks> |
34 | | -internal sealed class MyFilteringProcessor : CompositeProcessor<Activity> |
| 24 | +internal sealed class MyFilteringProcessor : BaseProcessor<Activity> |
35 | 25 | { |
36 | 26 | private readonly Func<Activity, bool> filter; |
37 | 27 |
|
38 | | - public MyFilteringProcessor(BaseProcessor<Activity> processor, Func<Activity, bool> filter) |
39 | | - : base(new[] { processor }) |
| 28 | + /// <summary> |
| 29 | + /// Initializes a new instance of the <see cref="MyFilteringProcessor"/> |
| 30 | + /// class. |
| 31 | + /// </summary> |
| 32 | + /// <param name="filter">Function used to test if an <see cref="Activity"/> |
| 33 | + /// should be recorded or dropped. Return <see langword="true"/> to record |
| 34 | + /// or <see langword="false"/> to drop.</param> |
| 35 | + public MyFilteringProcessor(Func<Activity, bool> filter) |
40 | 36 | { |
41 | 37 | this.filter = filter ?? throw new ArgumentNullException(nameof(filter)); |
42 | 38 | } |
43 | 39 |
|
44 | 40 | public override void OnEnd(Activity activity) |
45 | 41 | { |
46 | | - // Call the underlying processor |
47 | | - // only if the Filter returns true. |
48 | | - if (this.filter(activity)) |
| 42 | + // Bypass export if the Filter returns false. |
| 43 | + if (!this.filter(activity)) |
49 | 44 | { |
50 | | - base.OnEnd(activity); |
| 45 | + activity.ActivityTraceFlags &= ~ActivityTraceFlags.Recorded; |
51 | 46 | } |
52 | 47 | } |
53 | 48 | } |
0 commit comments