-
Notifications
You must be signed in to change notification settings - Fork 217
feat(sdk): add custom sampler registry support #1867
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace OpenTelemetry\SDK\Trace\Sampler; | ||
|
|
||
| use OpenTelemetry\SDK\Trace\SamplerInterface; | ||
|
|
||
| class AlwaysOffSamplerFactory implements SamplerFactoryInterface | ||
| { | ||
| #[\Override] | ||
| public function create(): SamplerInterface | ||
| { | ||
| return new AlwaysOffSampler(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace OpenTelemetry\SDK\Trace\Sampler; | ||
|
|
||
| use OpenTelemetry\SDK\Trace\SamplerInterface; | ||
|
|
||
| class AlwaysOnSamplerFactory implements SamplerFactoryInterface | ||
| { | ||
| #[\Override] | ||
| public function create(): SamplerInterface | ||
| { | ||
| return new AlwaysOnSampler(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace OpenTelemetry\SDK\Trace\Sampler; | ||
|
|
||
| use OpenTelemetry\SDK\Trace\SamplerInterface; | ||
|
|
||
| class ParentBasedAlwaysOffSamplerFactory implements SamplerFactoryInterface | ||
| { | ||
| #[\Override] | ||
| public function create(): SamplerInterface | ||
| { | ||
| return new ParentBased(new AlwaysOffSampler()); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace OpenTelemetry\SDK\Trace\Sampler; | ||
|
|
||
| use OpenTelemetry\SDK\Trace\SamplerInterface; | ||
|
|
||
| class ParentBasedAlwaysOnSamplerFactory implements SamplerFactoryInterface | ||
| { | ||
| #[\Override] | ||
| public function create(): SamplerInterface | ||
| { | ||
| return new ParentBased(new AlwaysOnSampler()); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace OpenTelemetry\SDK\Trace\Sampler; | ||
|
|
||
| use OpenTelemetry\SDK\Common\Configuration\Configuration; | ||
| use OpenTelemetry\SDK\Common\Configuration\Variables; | ||
| use OpenTelemetry\SDK\Trace\SamplerInterface; | ||
|
|
||
| class ParentBasedTraceIdRatioSamplerFactory implements SamplerFactoryInterface | ||
| { | ||
| #[\Override] | ||
| public function create(): SamplerInterface | ||
| { | ||
| $ratio = Configuration::getRatio(Variables::OTEL_TRACES_SAMPLER_ARG); | ||
|
|
||
| return new ParentBased(new TraceIdRatioBasedSampler($ratio)); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace OpenTelemetry\SDK\Trace\Sampler; | ||
|
|
||
| use OpenTelemetry\SDK\Trace\SamplerInterface; | ||
|
|
||
| interface SamplerFactoryInterface | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer if we use If we want to start with just samplers: class SamplerFactory
{
public function create(): SamplerInterface
{
$registry = new EnvComponentLoaderRegistry();
foreach (ServiceLoader::load(EnvComponentLoader::class) as $loader) {
$registry->register($loader);
}
$env = new EnvResolver();
$context = new Context();
$samplerName = $env->string(Variables::OTEL_TRACES_SAMPLER) ?? Defaults::OTEL_TRACES_SAMPLER;
return $registry->load(SamplerInterface::class, $samplerName, $env, $context);
}
}/**
* @implements EnvComponentLoader<SamplerInterface>
*/
final class SamplerLoaderTraceIdRatioBased implements EnvComponentLoader
{
#[\Override]
public function load(EnvResolver $env, EnvComponentLoaderRegistry $registry, Context $context): SamplerInterface
{
return new TraceIdRatioBasedSampler($env->numeric(Variables::OTEL_TRACES_SAMPLER_ARG, max: 1) ?? 1.);
}
#[\Override]
public function name(): string
{
return 'traceidratio';
}
}# _register.php
ServiceLoader::register(EnvComponentLoader::class, SamplerLoaderTraceIdRatioBased::class);
// ... |
||
| { | ||
| public function create(): SamplerInterface; | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,20 @@ | ||||||
| <?php | ||||||
|
|
||||||
| declare(strict_types=1); | ||||||
|
|
||||||
| namespace OpenTelemetry\SDK\Trace\Sampler; | ||||||
|
|
||||||
| use OpenTelemetry\SDK\Common\Configuration\Configuration; | ||||||
| use OpenTelemetry\SDK\Common\Configuration\Variables; | ||||||
| use OpenTelemetry\SDK\Trace\SamplerInterface; | ||||||
|
|
||||||
| class TraceIdRatioBasedSamplerFactory implements SamplerFactoryInterface | ||||||
| { | ||||||
| #[\Override] | ||||||
| public function create(): SamplerInterface | ||||||
| { | ||||||
| $ratio = Configuration::getRatio(Variables::OTEL_TRACES_SAMPLER_ARG); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| return new TraceIdRatioBasedSampler($ratio); | ||||||
| } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| \OpenTelemetry\SDK\Registry::registerSamplerFactory('always_on', \OpenTelemetry\SDK\Trace\Sampler\AlwaysOnSamplerFactory::class); | ||
| \OpenTelemetry\SDK\Registry::registerSamplerFactory('always_off', \OpenTelemetry\SDK\Trace\Sampler\AlwaysOffSamplerFactory::class); | ||
| \OpenTelemetry\SDK\Registry::registerSamplerFactory('traceidratio', \OpenTelemetry\SDK\Trace\Sampler\TraceIdRatioBasedSamplerFactory::class); | ||
| \OpenTelemetry\SDK\Registry::registerSamplerFactory('parentbased_always_on', \OpenTelemetry\SDK\Trace\Sampler\ParentBasedAlwaysOnSamplerFactory::class); | ||
| \OpenTelemetry\SDK\Registry::registerSamplerFactory('parentbased_always_off', \OpenTelemetry\SDK\Trace\Sampler\ParentBasedAlwaysOffSamplerFactory::class); | ||
| \OpenTelemetry\SDK\Registry::registerSamplerFactory('parentbased_traceidratio', \OpenTelemetry\SDK\Trace\Sampler\ParentBasedTraceIdRatioSamplerFactory::class); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace OpenTelemetry\Tests\Unit\SDK\Trace\Sampler; | ||
|
|
||
| use OpenTelemetry\SDK\Trace\Sampler\AlwaysOffSampler; | ||
| use OpenTelemetry\SDK\Trace\Sampler\AlwaysOffSamplerFactory; | ||
| use PHPUnit\Framework\Attributes\CoversClass; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| #[CoversClass(AlwaysOffSamplerFactory::class)] | ||
| class AlwaysOffSamplerFactoryTest extends TestCase | ||
| { | ||
| public function test_create(): void | ||
| { | ||
| $factory = new AlwaysOffSamplerFactory(); | ||
|
|
||
| $this->assertInstanceOf(AlwaysOffSampler::class, $factory->create()); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace OpenTelemetry\Tests\Unit\SDK\Trace\Sampler; | ||
|
|
||
| use OpenTelemetry\SDK\Trace\Sampler\AlwaysOnSampler; | ||
| use OpenTelemetry\SDK\Trace\Sampler\AlwaysOnSamplerFactory; | ||
| use PHPUnit\Framework\Attributes\CoversClass; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| #[CoversClass(AlwaysOnSamplerFactory::class)] | ||
| class AlwaysOnSamplerFactoryTest extends TestCase | ||
| { | ||
| public function test_create(): void | ||
| { | ||
| $factory = new AlwaysOnSamplerFactory(); | ||
|
|
||
| $this->assertInstanceOf(AlwaysOnSampler::class, $factory->create()); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.