Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
gaelreyrol committed Feb 1, 2024
1 parent 20ab30c commit e757a25
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ composer.lock
phpunit.xml
.phpunit.result.cache
.phpunit.cache
coverage
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@
},
"scripts": {
"check-reqs": "@php vendor/bin/composer-require-checker check",
"coverage": [
"@putenv XDEBUG_MODE=coverage",
"@phpunit --coverage-html=coverage"
],
"format": [
"@php-cs-fixer:fix",
"@composer normalize"
Expand Down
81 changes: 81 additions & 0 deletions tests/Unit/OpenTelemetry/Trace/SamplerFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\Tests\Unit\OpenTelemetry\Trace;

use FriendsOfOpenTelemetry\OpenTelemetryBundle\OpenTelemetry\Trace\SamplerFactory;
use OpenTelemetry\SDK\Trace\Sampler\AlwaysOffSampler;
use OpenTelemetry\SDK\Trace\Sampler\AlwaysOnSampler;
use OpenTelemetry\SDK\Trace\Sampler\ParentBased;
use OpenTelemetry\SDK\Trace\Sampler\TraceIdRatioBasedSampler;
use OpenTelemetry\SDK\Trace\SamplerInterface;
use PHPUnit\Framework\TestCase;

/**
* @coversDefaultClass \FriendsOfOpenTelemetry\OpenTelemetryBundle\OpenTelemetry\Trace\SamplerFactory
*/
class SamplerFactoryTest extends TestCase
{
/**
* @dataProvider samplerProvider
*/
public function testCreateSampler(string $name, string $expectedClass, string $description, ?float $probability): void
{
$sampler = SamplerFactory::create($name, $probability);

self::assertInstanceOf($expectedClass, $sampler);
self::assertSame($description, $sampler->getDescription());
}

/**
* @return \Generator<array{
* 0: string,
* 1: class-string<SamplerInterface>,
* 2: string,
* 3: float|null,
* }>
*/
public function samplerProvider(): \Generator
{
yield [
'always_off',
AlwaysOffSampler::class,
'AlwaysOffSampler',
null,
];

yield [
'always_on',
AlwaysOnSampler::class,
'AlwaysOnSampler',
null,
];

yield [
'parent_based_always_off',
ParentBased::class,
'ParentBased+AlwaysOffSampler',
null,
];

yield [
'parent_based_always_on',
ParentBased::class,
'ParentBased+AlwaysOnSampler',
null,
];

yield [
'parent_based_trace_id_ratio',
ParentBased::class,
'ParentBased+TraceIdRatioBasedSampler{0.600000}',
0.6,
];

yield [
'trace_id_ratio',
TraceIdRatioBasedSampler::class,
'TraceIdRatioBasedSampler{0.200000}',
0.2,
];
}
}
49 changes: 46 additions & 3 deletions tests/Unit/OpenTelemetry/Transport/TransportFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use FriendsOfOpenTelemetry\OpenTelemetryBundle\OpenTelemetry\Transport\PsrHttpTransportFactory;
use FriendsOfOpenTelemetry\OpenTelemetryBundle\OpenTelemetry\Transport\StreamTransportFactory;
use FriendsOfOpenTelemetry\OpenTelemetryBundle\OpenTelemetry\Transport\TransportFactory;
use OpenTelemetry\Contrib\Grpc\GrpcTransport;
use OpenTelemetry\SDK\Common\Export\Http\PsrTransport;
use OpenTelemetry\SDK\Common\Export\Stream\StreamTransport;
use OpenTelemetry\SDK\Common\Export\TransportInterface;
Expand All @@ -36,7 +37,7 @@ public function testCreateTransport(
ExporterDsn $dsn,
ExporterOptionsInterface $options,
array $supportedEndpoints,
string $transportClass
?string $expectedTransportClass
): void {
$factory = new TransportFactory([
new GrpcTransportFactory(),
Expand All @@ -50,7 +51,7 @@ public function testCreateTransport(
$endpoint = $endpointClass::fromDsn($dsn);
self::assertTrue($factory->supports($endpoint, $options));
$transport = $factory->createTransport($endpoint, $options);
self::assertInstanceOf($transportClass, $transport);
self::assertInstanceOf($expectedTransportClass, $transport);
} else {
self::expectExceptionMessageMatches('#Unsupported .*#');
$endpoint = $endpointClass::fromDsn($dsn);
Expand All @@ -64,7 +65,7 @@ public function testCreateTransport(
* 0: ExporterDsn,
* 1: ExporterOptionsInterface,
* 2: array<class-string<ExporterEndpointInterface>, bool>,
* 3: class-string<TransportInterface<string>>,
* 3: ?class-string<TransportInterface<string>>,
* }>
*/
public function transportProvider(): \Generator
Expand Down Expand Up @@ -96,5 +97,47 @@ public function transportProvider(): \Generator
],
PsrTransport::class,
];

yield [
ExporterDsn::fromString('http+otlp://default'),
new EmptyExporterOptions(),
[
LogExporterEndpoint::class => true,
MetricExporterEndpoint::class => true,
TraceExporterEndpoint::class => true,
ZipkinExporterEndpoint::class => false,
ConsoleExporterEndpoint::class => false,
OtlpExporterEndpoint::class => true,
],
PsrTransport::class,
];

yield [
ExporterDsn::fromString('grpc+otlp://default'),
new EmptyExporterOptions(),
[
LogExporterEndpoint::class => true,
MetricExporterEndpoint::class => true,
TraceExporterEndpoint::class => true,
ZipkinExporterEndpoint::class => false,
ConsoleExporterEndpoint::class => false,
OtlpExporterEndpoint::class => true,
],
GrpcTransport::class,
];

yield [
ExporterDsn::fromString('noop://default'),
new EmptyExporterOptions(),
[
LogExporterEndpoint::class => false,
MetricExporterEndpoint::class => false,
TraceExporterEndpoint::class => false,
ZipkinExporterEndpoint::class => false,
ConsoleExporterEndpoint::class => false,
OtlpExporterEndpoint::class => false,
],
null,
];
}
}

0 comments on commit e757a25

Please sign in to comment.