Skip to content

Commit 43dd903

Browse files
authored
feat(metrics): add metrics for symfony (#977)
1 parent 9ca6c3a commit 43dd903

File tree

11 files changed

+210
-1
lines changed

11 files changed

+210
-1
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"php": "^7.2||^8.0",
1616
"guzzlehttp/psr7": "^2.1.1",
1717
"jean85/pretty-package-versions": "^1.5||^2.0",
18-
"sentry/sentry": "^4.18",
18+
"sentry/sentry": "^4.19",
1919
"symfony/cache-contracts": "^1.1||^2.4||^3.0",
2020
"symfony/config": "^4.4.20||^5.0.11||^6.0||^7.0||^8.0",
2121
"symfony/console": "^4.4.20||^5.0.11||^6.0||^7.0||^8.0",

src/EventListener/ConsoleListener.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Sentry\EventHint;
99
use Sentry\ExceptionMechanism;
1010
use Sentry\Logs\Logs;
11+
use Sentry\Metrics\TraceMetrics;
1112
use Sentry\State\HubInterface;
1213
use Sentry\State\Scope;
1314
use Symfony\Component\Console\Event\ConsoleCommandEvent;
@@ -73,6 +74,7 @@ public function handleConsoleCommandEvent(ConsoleCommandEvent $event): void
7374
public function handleConsoleTerminateEvent(ConsoleTerminateEvent $event): void
7475
{
7576
Logs::getInstance()->flush();
77+
TraceMetrics::getInstance()->flush();
7678
$this->hub->popScope();
7779
}
7880

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sentry\SentryBundle\EventListener;
6+
7+
use Sentry\Metrics\TraceMetrics;
8+
use Symfony\Component\HttpKernel\Event\TerminateEvent;
9+
10+
/**
11+
* RequestListener for sentry metrics.
12+
*/
13+
class MetricsRequestListener
14+
{
15+
/**
16+
* Flushes all metrics on kernel termination.
17+
*
18+
* @param TerminateEvent $event
19+
*
20+
* @return void
21+
*/
22+
public function handleKernelTerminateEvent(TerminateEvent $event)
23+
{
24+
TraceMetrics::getInstance()->flush();
25+
}
26+
}

src/Resources/config/services.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ services:
7878
tags:
7979
- { name: kernel.event_listener, event: kernel.terminate, method: handleKernelTerminateEvent, priority: 10 }
8080

81+
Sentry\SentryBundle\EventListener\MetricsRequestListener:
82+
tags:
83+
- { name: kernel.event_listener, event: kernel.terminate, method: handleKernelTerminateEvent, priority: 10 }
84+
8185
# Command
8286
Sentry\SentryBundle\Command\SentryTestCommand:
8387
arguments: ['@Sentry\State\HubInterface']
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sentry\SentryBundle\Tests\End2End\App\Command;
6+
7+
use Symfony\Component\Console\Command\Command;
8+
use Symfony\Component\Console\Input\InputInterface;
9+
use Symfony\Component\Console\Output\OutputInterface;
10+
11+
use function Sentry\trace_metrics;
12+
13+
class MetricsCommand extends Command
14+
{
15+
protected function execute(InputInterface $input, OutputInterface $output): int
16+
{
17+
trace_metrics()->count('test-counter', 10);
18+
trace_metrics()->gauge('test-gauge', 20.51);
19+
trace_metrics()->distribution('test-distribution', 100.81);
20+
21+
return 0;
22+
}
23+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sentry\SentryBundle\Tests\End2End\App\Controller;
6+
7+
use Symfony\Component\HttpFoundation\Response;
8+
9+
use function Sentry\trace_metrics;
10+
11+
class MetricsController
12+
{
13+
public function metrics(): Response
14+
{
15+
trace_metrics()->count('test-counter', 10);
16+
trace_metrics()->gauge('test-gauge', 20.51);
17+
trace_metrics()->distribution('test-distribution', 100.81);
18+
19+
return new Response();
20+
}
21+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sentry\SentryBundle\Tests\End2End\App;
6+
7+
use Symfony\Component\Config\Loader\LoaderInterface;
8+
9+
class KernelWithMetrics extends Kernel
10+
{
11+
public function registerContainerConfiguration(LoaderInterface $loader): void
12+
{
13+
parent::registerContainerConfiguration($loader);
14+
15+
$loader->load(__DIR__ . '/metrics.yml');
16+
}
17+
}

tests/End2End/App/metrics.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
services:
2+
Sentry\SentryBundle\Tests\End2End\App\Controller\MetricsController:
3+
autowire: true
4+
tags:
5+
- { name: controller.service_arguments }
6+
7+
Sentry\SentryBundle\Tests\End2End\App\Command\MetricsCommand:
8+
autowire: true
9+
tags: [ { name: 'console.command', command: 'metrics:test' } ]

tests/End2End/App/routing.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,7 @@ logging_before_send_logs:
8585
buffer_flush:
8686
path: /buffer-flush
8787
defaults: { _controller: 'Sentry\SentryBundle\Tests\End2End\App\Controller\BufferFlushController::testBufferFlush' }
88+
89+
metrics:
90+
path: /metrics
91+
defaults: { _controller: 'Sentry\SentryBundle\Tests\End2End\App\Controller\MetricsController::metrics'}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sentry\SentryBundle\Tests\End2End;
6+
7+
use Sentry\SentryBundle\Tests\End2End\App\KernelWithMetrics;
8+
use Symfony\Bundle\FrameworkBundle\Console\Application;
9+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
10+
use Symfony\Component\Console\Input\ArgvInput;
11+
use Symfony\Component\Console\Output\NullOutput;
12+
13+
/**
14+
* @runTestsInSeparateProcesses
15+
*/
16+
class MetricsCommandTest extends WebTestCase
17+
{
18+
/**
19+
* @var Application
20+
*/
21+
private $application;
22+
23+
protected static function getKernelClass(): string
24+
{
25+
return KernelWithMetrics::class;
26+
}
27+
28+
protected function setUp(): void
29+
{
30+
StubTransport::$events = [];
31+
$this->application = new Application(self::bootKernel());
32+
}
33+
34+
public function testMetricsInCommand(): void
35+
{
36+
$this->application->doRun(new ArgvInput(['bin/console', 'metrics:test']), new NullOutput());
37+
38+
$this->assertCount(1, StubTransport::$events);
39+
$metrics = StubTransport::$events[0]->getMetrics();
40+
41+
$this->assertCount(3, $metrics);
42+
43+
$count = $metrics[0];
44+
$this->assertSame('test-counter', $count->getName());
45+
$this->assertSame(10.0, $count->getValue());
46+
47+
$gauge = $metrics[1];
48+
$this->assertSame('test-gauge', $gauge->getName());
49+
$this->assertSame(20.51, $gauge->getValue());
50+
51+
$distribution = $metrics[2];
52+
$this->assertSame('test-distribution', $distribution->getName());
53+
$this->assertSame(100.81, $distribution->getValue());
54+
}
55+
}

0 commit comments

Comments
 (0)