-
Notifications
You must be signed in to change notification settings - Fork 893
Expand file tree
/
Copy pathParentBasedSampler.cs
More file actions
108 lines (97 loc) · 4.93 KB
/
Copy pathParentBasedSampler.cs
File metadata and controls
108 lines (97 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
using System.Diagnostics;
using OpenTelemetry.Internal;
namespace OpenTelemetry.Trace;
/// <summary>
/// Sampler implementation which by default will take a sample if parent Activity is sampled.
/// Otherwise, samples root traces according to the specified root sampler.
/// </summary>
/// <remarks>
/// The default behavior can be customized by providing additional samplers to be invoked for different
/// combinations of local/remote parent and its sampling decision.
/// See <see cref="ParentBasedSampler(Sampler, Sampler, Sampler, Sampler, Sampler)"/>.
/// </remarks>
public sealed class ParentBasedSampler : Sampler
{
private readonly Sampler rootSampler;
private readonly Sampler remoteParentSampled;
private readonly Sampler remoteParentNotSampled;
private readonly Sampler localParentSampled;
private readonly Sampler localParentNotSampled;
/// <summary>
/// Initializes a new instance of the <see cref="ParentBasedSampler"/> class.
/// </summary>
/// <param name="rootSampler">The <see cref="Sampler"/> to be called for root span/activity.</param>
public ParentBasedSampler(Sampler rootSampler)
{
Guard.ThrowIfNull(rootSampler);
this.rootSampler = rootSampler;
#pragma warning disable CA1062 // Validate arguments of public methods - needed for netstandard2.1
this.Description = $"ParentBased{{{rootSampler.Description}}}";
#pragma warning restore CA1062 // Validate arguments of public methods - needed for netstandard2.1
this.remoteParentSampled = AlwaysOnSampler.Instance;
this.remoteParentNotSampled = AlwaysOffSampler.Instance;
this.localParentSampled = AlwaysOnSampler.Instance;
this.localParentNotSampled = AlwaysOffSampler.Instance;
}
/// <summary>
/// Initializes a new instance of the <see cref="ParentBasedSampler"/> class with ability to delegate
/// sampling decision to one of the inner samplers provided.
/// </summary>
/// <param name="rootSampler">The <see cref="Sampler"/> to be called for root span/activity.</param>
/// <param name="remoteParentSampled">
/// A <see cref="Sampler"/> to delegate sampling decision to in case of
/// remote parent (<see cref="ActivityContext.IsRemote"/> == true) with <see cref="ActivityTraceFlags.Recorded"/> flag == true.
/// Default: <see cref="AlwaysOnSampler"/>.
/// </param>
/// <param name="remoteParentNotSampled">
/// A <see cref="Sampler"/> to delegate sampling decision to in case of
/// remote parent (<see cref="ActivityContext.IsRemote"/> == true) with <see cref="ActivityTraceFlags.Recorded"/> flag == false.
/// Default: <see cref="AlwaysOffSampler"/>.
/// </param>
/// <param name="localParentSampled">
/// A <see cref="Sampler"/> to delegate sampling decision to in case of
/// local parent (<see cref="ActivityContext.IsRemote"/> == false) with <see cref="ActivityTraceFlags.Recorded"/> flag == true.
/// Default: <see cref="AlwaysOnSampler"/>.
/// </param>
/// <param name="localParentNotSampled">
/// A <see cref="Sampler"/> to delegate sampling decision to in case of
/// local parent (<see cref="ActivityContext.IsRemote"/> == false) with <see cref="ActivityTraceFlags.Recorded"/> flag == false.
/// Default: <see cref="AlwaysOffSampler"/>.
/// </param>
public ParentBasedSampler(
Sampler rootSampler,
Sampler? remoteParentSampled = null,
Sampler? remoteParentNotSampled = null,
Sampler? localParentSampled = null,
Sampler? localParentNotSampled = null)
: this(rootSampler)
{
this.remoteParentSampled = remoteParentSampled ?? AlwaysOnSampler.Instance;
this.remoteParentNotSampled = remoteParentNotSampled ?? AlwaysOffSampler.Instance;
this.localParentSampled = localParentSampled ?? AlwaysOnSampler.Instance;
this.localParentNotSampled = localParentNotSampled ?? AlwaysOffSampler.Instance;
}
/// <inheritdoc />
public override SamplingResult ShouldSample(in SamplingParameters samplingParameters)
{
var parentContext = samplingParameters.ParentContext;
if (parentContext.TraceId == default)
{
// If no parent, use the rootSampler to determine sampling.
return this.rootSampler.ShouldSample(samplingParameters);
}
// Is parent sampled?
if ((parentContext.TraceFlags & ActivityTraceFlags.Recorded) != 0)
{
return parentContext.IsRemote
? this.remoteParentSampled.ShouldSample(samplingParameters)
: this.localParentSampled.ShouldSample(samplingParameters);
}
// If parent is not sampled => delegate to the "not sampled" inner samplers.
return parentContext.IsRemote
? this.remoteParentNotSampled.ShouldSample(samplingParameters)
: this.localParentNotSampled.ShouldSample(samplingParameters);
}
}