-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add sampler configuration (#8)
contributors: @mmross
- Loading branch information
Showing
8 changed files
with
235 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Auxmoney\OpentracingBundle\Factory; | ||
|
||
use Exception; | ||
use Zipkin\Sampler; | ||
|
||
interface SamplerFactory | ||
{ | ||
/** | ||
* @throws Exception | ||
* @param mixed $samplerValue | ||
*/ | ||
public function createSampler(string $samplerClass, $samplerValue): Sampler; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Auxmoney\OpentracingBundle\Factory; | ||
|
||
use Exception; | ||
use Zipkin\Sampler; | ||
use Zipkin\Samplers\BinarySampler; | ||
use Zipkin\Samplers\PercentageSampler; | ||
|
||
final class ZipkinSamplerFactory implements SamplerFactory | ||
{ | ||
/** | ||
* @inheritDoc | ||
* @SuppressWarnings(PHPMD.StaticAccess) | ||
*/ | ||
public function createSampler(string $samplerClass, $samplerValue): Sampler | ||
{ | ||
if ($samplerClass === BinarySampler::class) { | ||
return $this->createBinarySampler($samplerValue); | ||
} | ||
|
||
if ($samplerClass === PercentageSampler::class) { | ||
return PercentageSampler::create($samplerValue); | ||
} | ||
|
||
throw new Exception(sprintf('unknown sampler class %s given', $samplerClass)); | ||
} | ||
|
||
/** | ||
* @param mixed $samplerValue | ||
* @SuppressWarnings(PHPMD.StaticAccess) | ||
* @throws Exception | ||
*/ | ||
private function createBinarySampler($samplerValue): BinarySampler | ||
{ | ||
if (!is_bool($samplerValue)) { | ||
throw new Exception( | ||
sprintf('sampler value for the binary sampler must be an boolean, %s given', gettype($samplerValue)) | ||
); | ||
} | ||
|
||
if ($samplerValue) { | ||
return BinarySampler::createAsAlwaysSample(); | ||
} | ||
|
||
return BinarySampler::createAsNeverSample(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Auxmoney\OpentracingBundle\Factory; | ||
|
||
use Exception; | ||
use PHPUnit\Framework\TestCase; | ||
use Zipkin\Samplers\BinarySampler; | ||
use Zipkin\Samplers\PercentageSampler; | ||
|
||
class ZipkinSamplerFactoryTest extends TestCase | ||
{ | ||
private $subject; | ||
|
||
public function setUp() | ||
{ | ||
$this->subject = new ZipkinSamplerFactory(); | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
public function testCreateActiveBinarySampler(): void | ||
{ | ||
$binarySampler = $this->subject->createSampler(BinarySampler::class, true); | ||
self::assertTrue($binarySampler->isSampled('dummyTraceId')); | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
public function testCreateInactiveBinarySampler(): void | ||
{ | ||
$binarySampler = $this->subject->createSampler(BinarySampler::class, false); | ||
self::assertFalse($binarySampler->isSampled('dummyTraceId')); | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
public function testCreateBinarySamplerWithoutStringValue(): void | ||
{ | ||
self::expectException(Exception::class); | ||
self::expectExceptionMessage('sampler value for the binary sampler must be an boolean, string given'); | ||
$this->subject->createSampler(BinarySampler::class, 'true'); | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
public function testCreate100PercentageSampler(): void | ||
{ | ||
$percentageSampler = $this->subject->createSampler(PercentageSampler::class, 1.0); | ||
self::assertTrue($percentageSampler->isSampled('dummyTraceId')); | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
public function testCreate0PercentageSampler(): void | ||
{ | ||
$percentageSampler = $this->subject->createSampler(PercentageSampler::class, 0.0); | ||
self::assertFalse($percentageSampler->isSampled('dummyTraceId')); | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
public function testCreateUnknownSampler(): void | ||
{ | ||
self::expectException(Exception::class); | ||
self::expectExceptionMessage('unknown sampler class unknown given'); | ||
|
||
$this->subject->createSampler('unknown', 0.0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters