Skip to content

Commit

Permalink
Respect Datadog DD_TRACE_SAMPLE_RATE rate limiting environment variable
Browse files Browse the repository at this point in the history
  • Loading branch information
radostyle committed Nov 29, 2023
1 parent 03e6b33 commit ec56114
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/Profiler/DatadogProfiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -89,6 +102,15 @@ public function stopAndIgnore(): void
GlobalTracer::set(new Tracer());
}

private function rateLimited(): bool
{
if($this->sampleRate) {

Check failure on line 107 in src/Profiler/DatadogProfiler.php

View workflow job for this annotation

GitHub Actions / PHP 8.0 - Symfony 4.4

Only booleans are allowed in an if condition, float|null given.

Check failure on line 107 in src/Profiler/DatadogProfiler.php

View workflow job for this annotation

GitHub Actions / PHP 8.0 - Symfony 5.4

Only booleans are allowed in an if condition, float|null given.

Check failure on line 107 in src/Profiler/DatadogProfiler.php

View workflow job for this annotation

GitHub Actions / PHP 8.0 - Symfony 6.0

Only booleans are allowed in an if condition, float|null given.

Check failure on line 107 in src/Profiler/DatadogProfiler.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 - Symfony 4.4

Only booleans are allowed in an if condition, float|null given.

Check failure on line 107 in src/Profiler/DatadogProfiler.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 - Symfony 5.4

Only booleans are allowed in an if condition, float|null given.

Check failure on line 107 in src/Profiler/DatadogProfiler.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 - Symfony 6.0

Only booleans are allowed in an if condition, float|null given.

Check failure on line 107 in src/Profiler/DatadogProfiler.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 - Symfony 6.1

Only booleans are allowed in an if condition, float|null given.
$randomFloat = mt_rand() / mt_getrandmax(); // between 0 and 1
return $randomFloat > $this->sampleRate;
}
return false;
}

private function isEnabled(): bool
{
return \extension_loaded('ddtrace')
Expand Down

0 comments on commit ec56114

Please sign in to comment.