From ec56114e46e4ce923a2dffe36e89fdd4157697fc Mon Sep 17 00:00:00 2001 From: Jonathan Alvarado Date: Wed, 29 Nov 2023 09:50:30 -0600 Subject: [PATCH 1/2] Respect Datadog DD_TRACE_SAMPLE_RATE rate limiting environment variable --- src/Profiler/DatadogProfiler.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/Profiler/DatadogProfiler.php b/src/Profiler/DatadogProfiler.php index 2ca68cc..e510d3f 100644 --- a/src/Profiler/DatadogProfiler.php +++ b/src/Profiler/DatadogProfiler.php @@ -21,10 +21,19 @@ class DatadogProfiler implements ProfilerInterface private ?Scope $scope = null; private LoggerInterface $logger; + private ?float $sampleRate; public function __construct(LoggerInterface $logger) { $this->logger = $logger; + + $sampleRateString = getenv('DD_TRACE_SAMPLE_RATE'); + if(is_numeric($sampleRateString)) { + $sampleRate = floatval($sampleRateString); + $this->sampleRate = $sampleRate; + } else { + $this->sampleRate = null; + } } public function start(string $name, ?string $kind = null): void @@ -33,6 +42,10 @@ public function start(string $name, ?string $kind = null): void return; } + if($this->rateLimited()) { + return; + } + if (dd_trace_env_config('DD_TRACE_GENERATE_ROOT_SPAN')) { $this->logger->error( sprintf('You should set DD_TRACE_GENERATE_ROOT_SPAN=0 when using %s.', self::class) @@ -89,6 +102,15 @@ public function stopAndIgnore(): void GlobalTracer::set(new Tracer()); } + private function rateLimited(): bool + { + if($this->sampleRate) { + $randomFloat = mt_rand() / mt_getrandmax(); // between 0 and 1 + return $randomFloat > $this->sampleRate; + } + return false; + } + private function isEnabled(): bool { return \extension_loaded('ddtrace') From ae641fa07aad7a3c93c8557a108a5f078be25933 Mon Sep 17 00:00:00 2001 From: Jonathan Alvarado Date: Wed, 29 Nov 2023 10:21:47 -0600 Subject: [PATCH 2/2] Set default to 1.0 --- src/Profiler/DatadogProfiler.php | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/Profiler/DatadogProfiler.php b/src/Profiler/DatadogProfiler.php index e510d3f..ea478a4 100644 --- a/src/Profiler/DatadogProfiler.php +++ b/src/Profiler/DatadogProfiler.php @@ -21,7 +21,8 @@ class DatadogProfiler implements ProfilerInterface private ?Scope $scope = null; private LoggerInterface $logger; - private ?float $sampleRate; + + private float $sampleRate = 1.0; public function __construct(LoggerInterface $logger) { @@ -31,8 +32,6 @@ public function __construct(LoggerInterface $logger) if(is_numeric($sampleRateString)) { $sampleRate = floatval($sampleRateString); $this->sampleRate = $sampleRate; - } else { - $this->sampleRate = null; } } @@ -104,11 +103,8 @@ public function stopAndIgnore(): void private function rateLimited(): bool { - if($this->sampleRate) { - $randomFloat = mt_rand() / mt_getrandmax(); // between 0 and 1 - return $randomFloat > $this->sampleRate; - } - return false; + $randomFloat = mt_rand() / mt_getrandmax(); // between 0 and 1 + return $randomFloat > $this->sampleRate; } private function isEnabled(): bool