@@ -17,13 +17,14 @@ internal class TraceSampler : ITraceSampler
17
17
private static readonly IDatadogLogger Log = DatadogLogging . GetLoggerFor < TraceSampler > ( ) ;
18
18
19
19
private readonly IRateLimiter _limiter ;
20
- private readonly List < ISamplingRule > _rules = [ ] ;
20
+ private readonly List < ISamplingRule > _rules ;
21
+ private readonly AgentSamplingRule ? _agentSamplingRule ;
21
22
22
- private AgentSamplingRule ? _agentSamplingRule ;
23
-
24
- public TraceSampler ( IRateLimiter limiter )
23
+ public TraceSampler ( IRateLimiter limiter , List < ISamplingRule > rules , AgentSamplingRule ? agentSamplingRule )
25
24
{
26
25
_limiter = limiter ;
26
+ _rules = [ ..rules ] ;
27
+ _agentSamplingRule = agentSamplingRule ;
27
28
}
28
29
29
30
public void SetDefaultSampleRates ( IReadOnlyDictionary < string , float > sampleRates )
@@ -48,57 +49,6 @@ public SamplingDecision MakeSamplingDecision(Span span)
48
49
return SamplingDecision . Default ;
49
50
}
50
51
51
- /// <summary>
52
- /// Register a new sampling rule. To register a <see cref="AgentSamplingRule"/>,
53
- /// use <see cref="RegisterAgentSamplingRule"/> instead.
54
- /// </summary>
55
- /// <remarks>
56
- /// The order that rules are registered is important, as they are evaluated in order.
57
- /// The first rule that matches will be used to determine the sampling rate.
58
- /// </remarks>
59
- public void RegisterRule ( ISamplingRule rule )
60
- {
61
- _rules . Add ( rule ) ;
62
- }
63
-
64
- /// <summary>
65
- /// Register new sampling rules. To register a <see cref="AgentSamplingRule"/>,
66
- /// use <see cref="RegisterAgentSamplingRule"/> instead.
67
- /// </summary>
68
- /// <remarks>
69
- /// The order that rules are registered is important, as they are evaluated in order.
70
- /// The first rule that matches will be used to determine the sampling rate.
71
- /// </remarks>
72
- public void RegisterRules ( IEnumerable < ISamplingRule > rules )
73
- {
74
- _rules . AddRange ( rules ) ;
75
- }
76
-
77
- /// <summary>
78
- /// Register a new agent sampling rule. This rule should be registered last,
79
- /// after any calls to <see cref="RegisterRule"/> or <see cref="RegisterRules"/>.
80
- /// </summary>
81
- /// <remarks>
82
- /// The order that rules are registered is important, as they are evaluated in order.
83
- /// The first rule that matches will be used to determine the sampling rate.
84
- /// </remarks>
85
- public void RegisterAgentSamplingRule ( AgentSamplingRule rule )
86
- {
87
- // only register the one AgentSamplingRule
88
- if ( Interlocked . Exchange ( ref _agentSamplingRule , rule ) == null )
89
- {
90
- // keep a reference to this rule so we can call SetDefaultSampleRates() later
91
- // to update the agent sampling rates
92
- _agentSamplingRule = rule ;
93
-
94
- RegisterRule ( rule ) ;
95
- }
96
- else
97
- {
98
- Log . Warning ( "AgentSamplingRule already registered. Ignoring additional registration." ) ;
99
- }
100
- }
101
-
102
52
// used for testing
103
53
internal IReadOnlyList < ISamplingRule > GetRules ( )
104
54
{
@@ -137,5 +87,66 @@ private SamplingDecision MakeSamplingDecision(Span span, float rate, string mech
137
87
138
88
return new SamplingDecision ( priority , mechanism , rate , limiterRate ) ;
139
89
}
90
+
91
+ public class Builder ( IRateLimiter limiter )
92
+ {
93
+ private readonly IRateLimiter _limiter = limiter ;
94
+ private readonly List < ISamplingRule > _rules = [ ] ;
95
+ private AgentSamplingRule ? _agentSamplingRule ;
96
+
97
+ public TraceSampler Build ( )
98
+ {
99
+ return new TraceSampler ( _limiter , _rules , _agentSamplingRule ) ;
100
+ }
101
+
102
+ /// <summary>
103
+ /// Register a new sampling rule. To register a <see cref="AgentSamplingRule"/>,
104
+ /// use <see cref="RegisterAgentSamplingRule"/> instead.
105
+ /// </summary>
106
+ /// <remarks>
107
+ /// The order that rules are registered is important, as they are evaluated in order.
108
+ /// The first rule that matches will be used to determine the sampling rate.
109
+ /// </remarks>
110
+ public void RegisterRule ( ISamplingRule rule )
111
+ {
112
+ _rules . Add ( rule ) ;
113
+ }
114
+
115
+ /// <summary>
116
+ /// Register new sampling rules. To register a <see cref="AgentSamplingRule"/>,
117
+ /// use <see cref="RegisterAgentSamplingRule"/> instead.
118
+ /// </summary>
119
+ /// <remarks>
120
+ /// The order that rules are registered is important, as they are evaluated in order.
121
+ /// The first rule that matches will be used to determine the sampling rate.
122
+ /// </remarks>
123
+ public void RegisterRules ( IEnumerable < ISamplingRule > rules )
124
+ {
125
+ _rules . AddRange ( rules ) ;
126
+ }
127
+
128
+ /// <summary>
129
+ /// Register a new agent sampling rule. This rule should be registered last,
130
+ /// after any calls to <see cref="RegisterRule"/> or <see cref="RegisterRules"/>.
131
+ /// </summary>
132
+ /// <remarks>
133
+ /// The order that rules are registered is important, as they are evaluated in order.
134
+ /// The first rule that matches will be used to determine the sampling rate.
135
+ /// </remarks>
136
+ public void RegisterAgentSamplingRule ( AgentSamplingRule rule )
137
+ {
138
+ // only register the one AgentSamplingRule
139
+ // keep a reference to this rule so we can call SetDefaultSampleRates() later
140
+ // to update the agent sampling rates
141
+ if ( Interlocked . Exchange ( ref _agentSamplingRule , rule ) == null )
142
+ {
143
+ RegisterRule ( rule ) ;
144
+ }
145
+ else
146
+ {
147
+ Log . Warning ( "AgentSamplingRule already registered. Ignoring additional registration." ) ;
148
+ }
149
+ }
150
+ }
140
151
}
141
152
}
0 commit comments