Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect Datadog DD_TRACE_SAMPLE_RATE rate limiting environment variable #19

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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;
magikid marked this conversation as resolved.
Show resolved Hide resolved
}
}

public function start(string $name, ?string $kind = null): void
Expand All @@ -33,6 +42,10 @@
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 @@
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
Loading