@@ -129,15 +129,16 @@ SamplingDecision ApmDisabledTraceSampler::decide(const SpanData& span_data) {
129129 SamplingDecision decision;
130130 decision.origin = SamplingDecision::Origin::LOCAL;
131131
132+ auto now = clock_ ();
133+ uint64_t num_allowed;
132134 if (span_data.tags .find (tags::internal::trace_source) !=
133135 span_data.tags .end ()) {
134136 decision.mechanism = static_cast <int >(SamplingMechanism::APP_SEC);
135137 decision.priority = static_cast <int >(SamplingPriority::USER_KEEP);
138+ last_kept_.store (now.wall , std::memory_order_relaxed);
139+ num_allowed = num_allowed_.fetch_add (1 , std::memory_order_relaxed) + 1 ;
136140 } else {
137- auto now = clock_ ();
138141 auto last_kept = last_kept_.load (std::memory_order_relaxed);
139- auto num_asked = num_asked_.fetch_add (1 , std::memory_order_relaxed) + 1 ;
140- uint64_t num_allowed;
141142 if (now.wall - last_kept >= INTERVAL) {
142143 if (last_kept_.compare_exchange_strong (last_kept, now.wall )) {
143144 decision.priority = static_cast <int >(SamplingPriority::USER_KEEP);
@@ -151,15 +152,16 @@ SamplingDecision ApmDisabledTraceSampler::decide(const SpanData& span_data) {
151152 decision.priority = static_cast <int >(SamplingPriority::USER_DROP);
152153 num_allowed = num_allowed_.load (std::memory_order_relaxed);
153154 }
155+ }
154156
155- decision.limiter_max_per_second = ALLOWED_PER_SECOND;
156- double effective_rate = static_cast <double >(num_allowed) / num_asked;
157- if (effective_rate > 1.0 ) {
158- // can happen due to the relaxed atomic operations
159- effective_rate = 1.0 ;
160- }
161- decision.limiter_effective_rate = Rate::from (effective_rate).value ();
157+ auto num_asked = num_asked_.fetch_add (1 , std::memory_order_relaxed) + 1 ;
158+ decision.limiter_max_per_second = ALLOWED_PER_SECOND;
159+ double effective_rate = static_cast <double >(num_allowed) / num_asked;
160+ if (effective_rate > 1.0 ) {
161+ // can happen due to the relaxed atomic operations
162+ effective_rate = 1.0 ;
162163 }
164+ decision.limiter_effective_rate = Rate::from (effective_rate).value ();
163165
164166 return decision;
165167}
0 commit comments