Skip to content

Commit a1f8c41

Browse files
committed
Update the DSC’s sample_rate if a traces_sampler is used
1 parent f2fcab6 commit a1f8c41

2 files changed

Lines changed: 9 additions & 3 deletions

File tree

src/State/Hub.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,14 @@ public function startTransaction(TransactionContext $context, array $customSampl
276276

277277
if ($tracesSampler !== null) {
278278
$sampleRate = $tracesSampler($samplingContext);
279-
280279
$sampleSource = 'config:traces_sampler';
280+
281+
$dynamicSamplingContext = $context->getMetadata()->getDynamicSamplingContext();
282+
if ($dynamicSamplingContext !== null) {
283+
// If a tracesSampler is defined,
284+
// always overwrite the sample_rate in the DSC
285+
$dynamicSamplingContext->set('sample_rate', (string) $sampleRate, true);
286+
}
281287
} else {
282288
$parentSampleRate = $context->getMetadata()->getParentSamplingRate();
283289
if ($parentSampleRate !== null) {

src/Tracing/DynamicSamplingContext.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ private function __construct()
4040
* @param string $key the list member key
4141
* @param string $value the list member value
4242
*/
43-
public function set(string $key, string $value): self
43+
public function set(string $key, string $value, bool $forceOverwrite = false): self
4444
{
45-
if ($this->isFrozen) {
45+
if ($this->isFrozen && !$forceOverwrite) {
4646
return $this;
4747
}
4848

0 commit comments

Comments
 (0)