Skip to content

Commit

Permalink
prepare release
Browse files Browse the repository at this point in the history
  • Loading branch information
spiralbot committed Dec 11, 2024
1 parent 428eed7 commit d8b5ff1
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 105 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.8",
"spiral/core": "^3.14.8"
},
"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 src/Span.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function getAttribute(string $name): mixed
return $this->attributes[$name] ?? null;
}

public function setStatus(string|int $code, string $description = null): self
public function setStatus(string|int $code, ?string $description = null): self
{
$this->status = new Status($code, $description);

Expand Down
2 changes: 1 addition & 1 deletion src/SpanInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function updateName(string $name): self;
* @param non-empty-string|int $code
* @param non-empty-string|null $description
*/
public function setStatus(string|int $code, string $description = null): self;
public function setStatus(string|int $code, ?string $description = null): self;

/**
* Get the current span status.
Expand Down
20 changes: 11 additions & 9 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,24 +15,26 @@

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->expects($this->any())->method('now');
$logger->expects($this->once())->method('debug');
$clock->shouldReceive('now');
$scope->shouldReceive('runScope')->once();
$logger->shouldReceive('debug')->once();

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

Expand Down
2 changes: 0 additions & 2 deletions tests/Monolog/TelemetryProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@
namespace Spiral\Tests\Telemetry\Monolog;

use Mockery as m;
use PHPUnit\Framework\Attributes\RunClassInSeparateProcess;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Spiral\Telemetry\Monolog\TelemetryProcessor;
use Spiral\Telemetry\TracerInterface;

#[RunClassInSeparateProcess]
final class TelemetryProcessorTest extends TestCase
{
use m\Adapter\Phpunit\MockeryPHPUnitIntegration;
Expand Down
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)
);

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

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

$tracer->trace('foo', fn() => 'hello');
}
}
44 changes: 1 addition & 43 deletions tests/NullTracerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@
namespace Spiral\Tests\Telemetry;

use Mockery as m;
use PHPUnit\Framework\Attributes\RunInSeparateProcess;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Spiral\Core\BinderInterface;
use Spiral\Core\ContainerScope;
use Spiral\Core\InvokerInterface;
use Spiral\Core\ScopeInterface;
use Spiral\Telemetry\NullTracer;
Expand All @@ -20,8 +16,7 @@ final class NullTracerTest extends TestCase
{
use m\Adapter\Phpunit\MockeryPHPUnitIntegration;

#[RunInSeparateProcess]
public function testFallbackRunScope(): void
public function testTrace(): void
{
$tracer = new NullTracer(
$scope = m::mock(ScopeInterface::class)
Expand All @@ -48,41 +43,4 @@ public function testFallbackRunScope(): void
$tracer->trace('foo', $callable, ['foo' => 'bar'])
);
}

#[RunInSeparateProcess]
public function testWithScopedContainer(): void
{
$tracer = new NullTracer(
$scope = m::mock(ScopeInterface::class)
);

$invoker = m::mock(InvokerInterface::class);
$binder = m::mock(BinderInterface::class);
$container = m::mock(ContainerInterface::class);
$container->expects('get')
->with(InvokerInterface::class)
->andReturn($invoker);
$container->expects('get')
->with(BinderInterface::class)
->andReturn($binder);

$callable = fn() => 'hello';

$invoker->shouldReceive('invoke')
->once()
->with($callable)
->andReturn('hello');
$binder->shouldReceive('bindSingleton')
->once();
$binder->shouldReceive('removeBinding')
->with(SpanInterface::class);
$scope->shouldNotReceive('runScope');

ContainerScope::runScope($container, function () use ($tracer, $callable) {
$this->assertSame(
'hello',
$tracer->trace('foo', $callable, ['foo' => 'bar'])
);
});
}
}

0 comments on commit d8b5ff1

Please sign in to comment.