Skip to content

Commit

Permalink
prepare release
Browse files Browse the repository at this point in the history
  • Loading branch information
spiralbot committed Jan 7, 2025
1 parent c57903f commit fcabac2
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 127 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
],
"require": {
"php": ">=8.1",
"spiral/boot": "^3.15",
"spiral/core": "^3.15"
"spiral/boot": "^3.14.9",
"spiral/core": "^3.14.9"
},
"require-dev": {
"phpunit/phpunit": "^10.1",
Expand Down
42 changes: 7 additions & 35 deletions src/AbstractTracer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

namespace Spiral\Telemetry;

use Spiral\Core\BinderInterface;
use Spiral\Core\Attribute\Proxy;
use Spiral\Core\Container;
use Spiral\Core\ContainerScope;
use Spiral\Core\InvokerInterface;
use Spiral\Core\ScopeInterface;

Expand All @@ -18,7 +17,7 @@
abstract class AbstractTracer implements TracerInterface
{
public function __construct(
private readonly ?ScopeInterface $scope = new Container(),
#[Proxy] private readonly ?ScopeInterface $scope = new Container(),
) {
}

Expand All @@ -27,37 +26,10 @@ public function __construct(
*/
final protected function runScope(Span $span, callable $callback): mixed
{
$container = ContainerScope::getContainer();
if ($container === null) {
return $this->scope->runScope([
SpanInterface::class => $span,
TracerInterface::class => $this,
], static fn (InvokerInterface $invoker): mixed => $invoker->invoke($callback));
}

if ($container instanceof Container) {
$invoker = $container;
$binder = $container;
} else {
/** @var InvokerInterface $invoker */
$invoker = $container->get(InvokerInterface::class);
/** @var BinderInterface $binder */
$binder = $container->get(BinderInterface::class);
}

try {
$prevSpan = $container->get(SpanInterface::class);
} catch (\Throwable) {
$prevSpan = null;
}

$binder->bindSingleton(SpanInterface::class, $span);
try {
return $invoker->invoke($callback);
} finally {
$prevSpan === null
? $binder->removeBinding(SpanInterface::class)
: $binder->bindSingleton(SpanInterface::class, $prevSpan);
}
// TODO: Can we remove this scope?
return $this->scope->runScope([
SpanInterface::class => $span,
TracerInterface::class => $this,
], static fn (InvokerInterface $invoker): mixed => $invoker->invoke($callback));
}
}
14 changes: 8 additions & 6 deletions src/Bootloader/TelemetryBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
use Spiral\Telemetry\Config\TelemetryConfig;
use Spiral\Telemetry\ConfigTracerFactoryProvider;
use Spiral\Telemetry\Exception\TracerException;
use Spiral\Telemetry\LogTracer;
use Spiral\Telemetry\LogTracerFactory;
use Spiral\Telemetry\NullTracer;
use Spiral\Telemetry\NullTracerFactory;
use Spiral\Telemetry\TracerFactoryInterface;
use Spiral\Telemetry\TracerFactoryProviderInterface;
use Spiral\Telemetry\TracerInterface;
use Spiral\Telemetry\TracerFactoryProviderInterface;

final class TelemetryBootloader extends Bootloader
{
Expand All @@ -33,7 +35,7 @@ final class TelemetryBootloader extends Bootloader
];

public function __construct(
private readonly ConfiguratorInterface $config,
private readonly ConfiguratorInterface $config
) {
}

Expand All @@ -49,15 +51,15 @@ public function registerTracer(string $name, string|TracerFactoryInterface|Autow
{
$this->config->modify(
TelemetryConfig::CONFIG,
new Append('drivers', $name, $driver),
new Append('drivers', $name, $driver)
);
}

/**
* @throws TracerException
*/
public function initFactory(
TracerFactoryProviderInterface $tracerProvider,
TracerFactoryProviderInterface $tracerProvider
): TracerFactoryInterface {
return $tracerProvider->getTracerFactory();
}
Expand All @@ -66,7 +68,7 @@ public function initFactory(
* @throws TracerException
*/
public function getTracer(
TracerFactoryInterface $tracerFactory,
TracerFactoryInterface $tracerFactory
): TracerInterface {
return $tracerFactory->make();
}
Expand All @@ -81,7 +83,7 @@ private function initConfig(EnvironmentInterface $env): void
'null' => NullTracerFactory::class,
'log' => LogTracerFactory::class,
],
],
]
);
}
}
2 changes: 1 addition & 1 deletion src/LogTracerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class LogTracerFactory implements TracerFactoryInterface
private readonly LoggerInterface $logger;

public function __construct(
private readonly ScopeInterface $scope,
#[Proxy] private readonly ScopeInterface $scope,
private readonly ClockInterface $clock,
LogsInterface $logs,
string $channel = self::LOG_CHANNEL
Expand Down
2 changes: 1 addition & 1 deletion src/NullTracerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
final class NullTracerFactory implements TracerFactoryInterface
{
public function __construct(
private readonly ?ScopeInterface $scope = new Container(),
#[Proxy] private readonly ?ScopeInterface $scope = new Container(),
) {
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Clock/SystemClockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public function testNow(): void
{
$clock = new SystemClock();

self::assertIsInt($clock->now());
$this->assertIsInt($clock->now());
}
}
8 changes: 4 additions & 4 deletions tests/Config/TelemetryConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function testGetsDefaultDriver(): void
{
$config = new TelemetryConfig(['default' => 'foo']);

self::assertSame('foo', $config->getDefaultDriver());
$this->assertSame('foo', $config->getDefaultDriver());
}

public function testGetsDriverConfigAsString(): void
Expand All @@ -25,7 +25,7 @@ public function testGetsDriverConfigAsString(): void
'foo' => 'bar'
]]);

self::assertSame('bar', $config->getDriverConfig('foo'));
$this->assertSame('bar', $config->getDriverConfig('foo'));
}

public function testGetsDriverConfigAsAutowire(): void
Expand All @@ -34,7 +34,7 @@ public function testGetsDriverConfigAsAutowire(): void
'foo' => $driver = new Autowire('bar')
]]);

self::assertSame($driver, $config->getDriverConfig('foo'));
$this->assertSame($driver, $config->getDriverConfig('foo'));
}

public function testGetsDriverConfigAsObject(): void
Expand All @@ -43,6 +43,6 @@ public function testGetsDriverConfigAsObject(): void
'foo' => $driver = m::mock(TracerFactoryInterface::class)
]]);

self::assertSame($driver, $config->getDriverConfig('foo'));
$this->assertSame($driver, $config->getDriverConfig('foo'));
}
}
4 changes: 2 additions & 2 deletions tests/ConfigTracerFactoryProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function testGetsTraceeFactory(): void
->with('bar')
->andReturn($f = \Mockery::mock(TracerFactoryInterface::class));

self::assertSame($f, $provider->getTracerFactory('foo'));
$this->assertSame($f, $provider->getTracerFactory('foo'));
}

public function testGetsTraceeFactoryWithDefaultName(): void
Expand All @@ -46,7 +46,7 @@ public function testGetsTraceeFactoryWithDefaultName(): void
->with('bar')
->andReturn($f = \Mockery::mock(TracerFactoryInterface::class));

self::assertSame($f, $provider->getTracerFactory());
$this->assertSame($f, $provider->getTracerFactory());
}

public function testGetsTraceeFactoryWithNonExistName(): void
Expand Down
24 changes: 13 additions & 11 deletions tests/LogTracerFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Spiral\Tests\Telemetry;

use PHPUnit\Framework\Attributes\RunInSeparateProcess;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Spiral\Core\ScopeInterface;
Expand All @@ -15,28 +15,30 @@

final class LogTracerFactoryTest extends TestCase
{
use m\Adapter\Phpunit\MockeryPHPUnitIntegration;

public function testMake(): void
{
$logs = $this->createMock(LogsInterface::class);
$logs = m::mock(LogsInterface::class);

$logs->expects($this->once())
->method('getLogger')
$logs->shouldReceive('getLogger')->once()
->with('some-channel')
->willReturn($logger = $this->createMock(LoggerInterface::class));
->andReturn($logger = m::mock(LoggerInterface::class));

$factory = new LogTracerFactory(
$scope = $this->createMock(ScopeInterface::class),
$clock = $this->createMock(ClockInterface::class),
$scope = m::mock(ScopeInterface::class),
$clock = m::mock(ClockInterface::class),
$logs,
'some-channel'
);

$clock->method('now');
$logger->expects($this->once())->method('debug');
$clock->shouldReceive('now');
$scope->shouldReceive('runScope')->once();
$logger->shouldReceive('debug')->once();

self::assertInstanceOf(LogTracer::class, $tracer = $factory->make());
$this->assertInstanceOf(LogTracer::class, $tracer = $factory->make());

$tracer->trace('foo', static fn(): string => 'hello');
$tracer->trace('foo', fn() => 'hello');
}
}

14 changes: 10 additions & 4 deletions tests/LogTracerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function testTrace(): void

$uuid->shouldReceive('uuid4')->once()->andReturn($uuid = Uuid::uuid4());

$callable = static fn(): string => 'hello';
$callable = fn() => 'hello';

$invoker->shouldReceive('invoke')
->once()
Expand All @@ -43,13 +43,19 @@ public function testTrace(): void
$logger->shouldReceive('debug')->once();

$scope->shouldReceive('runScope')
->withArgs(static fn(array $scope): bool =>
->withArgs(fn(array $scope) =>
$scope[SpanInterface::class] instanceof Span
&& $scope[SpanInterface::class]->getName() === 'foo'
)
->andReturnUsing(fn(array $scope, callable $callable) => $callable($invoker));

self::assertSame('hello', $tracer->trace('foo', $callable, ['foo' => 'bar']));
self::assertSame(['telemetry' => $uuid->toString()], $tracer->getContext());
$this->assertSame(
'hello',
$tracer->trace('foo', $callable, ['foo' => 'bar'])
);
$this->assertSame(
['telemetry' => $uuid->toString()],
$tracer->getContext()
);
}
}
12 changes: 8 additions & 4 deletions tests/NullTracerFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Spiral\Tests\Telemetry;

use Mockery as m;
use PHPUnit\Framework\Attributes\RunInSeparateProcess;
use PHPUnit\Framework\TestCase;
use Spiral\Core\ScopeInterface;
use Spiral\Telemetry\NullTracer;
Expand All @@ -15,11 +14,16 @@ final class NullTracerFactoryTest extends TestCase
{
use m\Adapter\Phpunit\MockeryPHPUnitIntegration;

#[RunInSeparateProcess]
public function testMake(): void
{
$factory = new NullTracerFactory(m::mock(ScopeInterface::class));
$factory = new NullTracerFactory(
$scope = m::mock(ScopeInterface::class)
);

self::assertInstanceOf(NullTracer::class, $factory->make());
$scope->shouldReceive('runScope')->once();

$this->assertInstanceOf(NullTracer::class, $tracer = $factory->make());

$tracer->trace('foo', fn() => 'hello');
}
}
Loading

0 comments on commit fcabac2

Please sign in to comment.