Skip to content

Commit 7a2c736

Browse files
committed
feat(metrics): add metrics for symfony
1 parent fcb4fea commit 7a2c736

File tree

11 files changed

+209
-5
lines changed

11 files changed

+209
-5
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: 6 additions & 4 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;
@@ -36,8 +37,8 @@ class ConsoleListener
3637
/**
3738
* Constructor.
3839
*
39-
* @param HubInterface $hub The current hub
40-
* @param bool $captureErrors Whether to capture console errors
40+
* @param HubInterface $hub The current hub
41+
* @param bool $captureErrors Whether to capture console errors
4142
*/
4243
public function __construct(HubInterface $hub, bool $captureErrors = true)
4344
{
@@ -61,7 +62,7 @@ public function handleConsoleCommandEvent(ConsoleCommandEvent $event): void
6162
}
6263

6364
if ($input instanceof ArgvInput) {
64-
$scope->setExtra('Full command', (string) $input);
65+
$scope->setExtra('Full command', (string)$input);
6566
}
6667
}
6768

@@ -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

@@ -84,7 +86,7 @@ public function handleConsoleTerminateEvent(ConsoleTerminateEvent $event): void
8486
public function handleConsoleErrorEvent(ConsoleErrorEvent $event): void
8587
{
8688
$this->hub->configureScope(function (Scope $scope) use ($event): void {
87-
$scope->setTag('console.command.exit_code', (string) $event->getExitCode());
89+
$scope->setTag('console.command.exit_code', (string)$event->getExitCode());
8890

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

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

0 commit comments

Comments
 (0)