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

Remove manual rate limiting and fix GlobalTracer reset #22

Merged
merged 2 commits into from
Jan 15, 2024
Merged
Changes from all commits
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
23 changes: 5 additions & 18 deletions src/Profiler/DatadogProfiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,9 @@ class DatadogProfiler implements ProfilerInterface

private LoggerInterface $logger;

private float $sampleRate = 1.0;

public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;

$sampleRateString = getenv('DD_TRACE_SAMPLE_RATE');
if (is_numeric($sampleRateString)) {
$sampleRate = (float) $sampleRateString;
$this->sampleRate = $sampleRate;
}
}

public function start(string $name, ?string $kind = null): void
Expand All @@ -41,10 +33,6 @@ 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 @@ -98,14 +86,13 @@ public function stopAndIgnore(): void
return;
}

GlobalTracer::set(new Tracer());
}
$this->scope = null;

private function rateLimited(): bool
{
$randomFloat = mt_rand() / mt_getrandmax(); // between 0 and 1
// https://github.com/DataDog/dd-trace-php/issues/1533#issuecomment-1059211743
ini_set('datadog.trace.enabled', '0');
ini_set('datadog.trace.enabled', '1');

return $randomFloat > $this->sampleRate;
GlobalTracer::set(new Tracer());
}

private function isEnabled(): bool
Expand Down
Loading