diff --git a/composer.json b/composer.json index b092c8ca..50dc4781 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/EventListener/ConsoleListener.php b/src/EventListener/ConsoleListener.php index 2ef88afa..4ec7a523 100644 --- a/src/EventListener/ConsoleListener.php +++ b/src/EventListener/ConsoleListener.php @@ -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; @@ -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(); } diff --git a/src/EventListener/MetricsRequestListener.php b/src/EventListener/MetricsRequestListener.php new file mode 100644 index 00000000..3d8124fe --- /dev/null +++ b/src/EventListener/MetricsRequestListener.php @@ -0,0 +1,26 @@ +flush(); + } +} diff --git a/src/Resources/config/services.yaml b/src/Resources/config/services.yaml index c4aba752..d06cf86c 100644 --- a/src/Resources/config/services.yaml +++ b/src/Resources/config/services.yaml @@ -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'] diff --git a/tests/End2End/App/Command/MetricsCommand.php b/tests/End2End/App/Command/MetricsCommand.php new file mode 100644 index 00000000..4f35ff12 --- /dev/null +++ b/tests/End2End/App/Command/MetricsCommand.php @@ -0,0 +1,23 @@ +count('test-counter', 10); + trace_metrics()->gauge('test-gauge', 20.51); + trace_metrics()->distribution('test-distribution', 100.81); + + return 0; + } +} diff --git a/tests/End2End/App/Controller/MetricsController.php b/tests/End2End/App/Controller/MetricsController.php new file mode 100644 index 00000000..2fa01561 --- /dev/null +++ b/tests/End2End/App/Controller/MetricsController.php @@ -0,0 +1,21 @@ +count('test-counter', 10); + trace_metrics()->gauge('test-gauge', 20.51); + trace_metrics()->distribution('test-distribution', 100.81); + + return new Response(); + } +} diff --git a/tests/End2End/App/KernelWithMetrics.php b/tests/End2End/App/KernelWithMetrics.php new file mode 100644 index 00000000..84a3d5f1 --- /dev/null +++ b/tests/End2End/App/KernelWithMetrics.php @@ -0,0 +1,17 @@ +load(__DIR__ . '/metrics.yml'); + } +} diff --git a/tests/End2End/App/metrics.yml b/tests/End2End/App/metrics.yml new file mode 100644 index 00000000..f80092b5 --- /dev/null +++ b/tests/End2End/App/metrics.yml @@ -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' } ] diff --git a/tests/End2End/App/routing.yml b/tests/End2End/App/routing.yml index b7b1f6d6..caaa9f5d 100644 --- a/tests/End2End/App/routing.yml +++ b/tests/End2End/App/routing.yml @@ -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'} diff --git a/tests/End2End/MetricsCommandTest.php b/tests/End2End/MetricsCommandTest.php new file mode 100644 index 00000000..a86a0f13 --- /dev/null +++ b/tests/End2End/MetricsCommandTest.php @@ -0,0 +1,55 @@ +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()); + } +} diff --git a/tests/End2End/MetricsEnd2EndTest.php b/tests/End2End/MetricsEnd2EndTest.php new file mode 100644 index 00000000..d206bf1c --- /dev/null +++ b/tests/End2End/MetricsEnd2EndTest.php @@ -0,0 +1,48 @@ + 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()); + } +}