Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"php": "^7.2||^8.0",
"guzzlehttp/psr7": "^2.1.1",
"jean85/pretty-package-versions": "^1.5||^2.0",
"sentry/sentry": "^4.18",
"sentry/sentry": "^4.19",
"symfony/cache-contracts": "^1.1||^2.4||^3.0",
"symfony/config": "^4.4.20||^5.0.11||^6.0||^7.0||^8.0",
"symfony/console": "^4.4.20||^5.0.11||^6.0||^7.0||^8.0",
Expand Down
2 changes: 2 additions & 0 deletions src/EventListener/ConsoleListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Sentry\EventHint;
use Sentry\ExceptionMechanism;
use Sentry\Logs\Logs;
use Sentry\Metrics\TraceMetrics;
use Sentry\State\HubInterface;
use Sentry\State\Scope;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
Expand Down Expand Up @@ -73,6 +74,7 @@ public function handleConsoleCommandEvent(ConsoleCommandEvent $event): void
public function handleConsoleTerminateEvent(ConsoleTerminateEvent $event): void
{
Logs::getInstance()->flush();
TraceMetrics::getInstance()->flush();
$this->hub->popScope();
}

Expand Down
26 changes: 26 additions & 0 deletions src/EventListener/MetricsRequestListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Sentry\SentryBundle\EventListener;

use Sentry\Metrics\TraceMetrics;
use Symfony\Component\HttpKernel\Event\TerminateEvent;

/**
* RequestListener for sentry metrics.
*/
class MetricsRequestListener
{
/**
* Flushes all metrics on kernel termination.
*
* @param TerminateEvent $event
*
* @return void
*/
public function handleKernelTerminateEvent(TerminateEvent $event)
{
TraceMetrics::getInstance()->flush();
}
}
4 changes: 4 additions & 0 deletions src/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ services:
tags:
- { name: kernel.event_listener, event: kernel.terminate, method: handleKernelTerminateEvent, priority: 10 }

Sentry\SentryBundle\EventListener\MetricsRequestListener:
tags:
- { name: kernel.event_listener, event: kernel.terminate, method: handleKernelTerminateEvent, priority: 10 }

# Command
Sentry\SentryBundle\Command\SentryTestCommand:
arguments: ['@Sentry\State\HubInterface']
Expand Down
23 changes: 23 additions & 0 deletions tests/End2End/App/Command/MetricsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Sentry\SentryBundle\Tests\End2End\App\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

use function Sentry\trace_metrics;

class MetricsCommand extends Command
{
protected function execute(InputInterface $input, OutputInterface $output): int
{
trace_metrics()->count('test-counter', 10);
trace_metrics()->gauge('test-gauge', 20.51);
trace_metrics()->distribution('test-distribution', 100.81);

return 0;
}
}
21 changes: 21 additions & 0 deletions tests/End2End/App/Controller/MetricsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Sentry\SentryBundle\Tests\End2End\App\Controller;

use Symfony\Component\HttpFoundation\Response;

use function Sentry\trace_metrics;

class MetricsController
{
public function metrics(): Response
{
trace_metrics()->count('test-counter', 10);
trace_metrics()->gauge('test-gauge', 20.51);
trace_metrics()->distribution('test-distribution', 100.81);

return new Response();
}
}
17 changes: 17 additions & 0 deletions tests/End2End/App/KernelWithMetrics.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Sentry\SentryBundle\Tests\End2End\App;

use Symfony\Component\Config\Loader\LoaderInterface;

class KernelWithMetrics extends Kernel
{
public function registerContainerConfiguration(LoaderInterface $loader): void
{
parent::registerContainerConfiguration($loader);

$loader->load(__DIR__ . '/metrics.yml');
}
}
9 changes: 9 additions & 0 deletions tests/End2End/App/metrics.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
services:
Sentry\SentryBundle\Tests\End2End\App\Controller\MetricsController:
autowire: true
tags:
- { name: controller.service_arguments }

Sentry\SentryBundle\Tests\End2End\App\Command\MetricsCommand:
autowire: true
tags: [ { name: 'console.command', command: 'metrics:test' } ]
4 changes: 4 additions & 0 deletions tests/End2End/App/routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,7 @@ logging_before_send_logs:
buffer_flush:
path: /buffer-flush
defaults: { _controller: 'Sentry\SentryBundle\Tests\End2End\App\Controller\BufferFlushController::testBufferFlush' }

metrics:
path: /metrics
defaults: { _controller: 'Sentry\SentryBundle\Tests\End2End\App\Controller\MetricsController::metrics'}
55 changes: 55 additions & 0 deletions tests/End2End/MetricsCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace Sentry\SentryBundle\Tests\End2End;

use Sentry\SentryBundle\Tests\End2End\App\KernelWithMetrics;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\NullOutput;

/**
* @runTestsInSeparateProcesses
*/
class MetricsCommandTest extends WebTestCase
{
/**
* @var Application
*/
private $application;

protected static function getKernelClass(): string
{
return KernelWithMetrics::class;
}

protected function setUp(): void
{
StubTransport::$events = [];
$this->application = new Application(self::bootKernel());
}

public function testMetricsInCommand(): void
{
$this->application->doRun(new ArgvInput(['bin/console', 'metrics:test']), new NullOutput());

$this->assertCount(1, StubTransport::$events);
$metrics = StubTransport::$events[0]->getMetrics();

$this->assertCount(3, $metrics);

$count = $metrics[0];
$this->assertSame('test-counter', $count->getName());
$this->assertSame(10.0, $count->getValue());

$gauge = $metrics[1];
$this->assertSame('test-gauge', $gauge->getName());
$this->assertSame(20.51, $gauge->getValue());

$distribution = $metrics[2];
$this->assertSame('test-distribution', $distribution->getName());
$this->assertSame(100.81, $distribution->getValue());
}
}
48 changes: 48 additions & 0 deletions tests/End2End/MetricsEnd2EndTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace Sentry\SentryBundle\Tests\End2End;

use Sentry\SentryBundle\Tests\End2End\App\KernelWithMetrics;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

/**
* @runTestsInSeparateProcesses
*/
class MetricsEnd2EndTest extends WebTestCase
{
protected function setUp(): void
{
StubTransport::$events = [];
}

protected static function getKernelClass(): string
{
return KernelWithMetrics::class;
}

public function testMetricsAreFlushedAfterRequest(): void
{
$client = static::createClient(['debug' => true]);

$client->request('GET', '/metrics');
$this->assertSame(200, $client->getResponse()->getStatusCode());

$this->assertCount(1, StubTransport::$events);
$metrics = StubTransport::$events[0]->getMetrics();
$this->assertCount(3, $metrics);

$count = $metrics[0];
$this->assertSame('test-counter', $count->getName());
$this->assertSame(10.0, $count->getValue());

$gauge = $metrics[1];
$this->assertSame('test-gauge', $gauge->getName());
$this->assertSame(20.51, $gauge->getValue());

$distribution = $metrics[2];
$this->assertSame('test-distribution', $distribution->getName());
$this->assertSame(100.81, $distribution->getValue());
}
}
Loading