Skip to content

Commit

Permalink
add sentry
Browse files Browse the repository at this point in the history
  • Loading branch information
4rthem committed Sep 19, 2023
1 parent 442f0cc commit f11203a
Show file tree
Hide file tree
Showing 20 changed files with 81 additions and 48 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -263,4 +263,4 @@ NEWRELIC_LICENSE_KEY=

SENTRY_DSN=
SENTRY_ENVIRONMENT=prod
SENTRY_RELEASE=
SENTRY_RELEASE=unknown
7 changes: 4 additions & 3 deletions databox/api/config/packages/alchemy_core.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
alchemy_core:
app_url: '%env(DATABOX_API_URL)%'
healthcheck: ~
sentry: ~
app_name: databox
app_url: '%env(DATABOX_API_URL)%'
healthcheck: ~
sentry: ~
8 changes: 4 additions & 4 deletions databox/api/config/routes/alchemy_core.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
alchemy_core_healthcheck:
controller: Alchemy\CoreBundle\Controller\HealthCheckAction
path: /
resource: Alchemy\CoreBundle\Controller\HealthCheckAction
type: attribute
alchemy_core_sentry_test:
controller: Alchemy\CoreBundle\Controller\SentryTestController
path: /
resource: Alchemy\CoreBundle\Controller\SentryTestController
type: attribute
7 changes: 4 additions & 3 deletions expose/api/config/packages/alchemy_core.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
alchemy_core:
app_url: '%env(EXPOSE_API_URL)%'
healthcheck: ~
sentry: ~
app_name: expose
app_url: '%env(EXPOSE_API_URL)%'
healthcheck: ~
sentry: ~
2 changes: 0 additions & 2 deletions expose/api/config/packages/alchemy_report.yml

This file was deleted.

8 changes: 4 additions & 4 deletions expose/api/config/routes/alchemy_core.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
alchemy_core_healthcheck:
controller: Alchemy\CoreBundle\Controller\HealthCheckAction
path: /
resource: Alchemy\CoreBundle\Controller\HealthCheckAction
type: attribute
alchemy_core_sentry_test:
controller: Alchemy\CoreBundle\Controller\SentryTestController
path: /
resource: Alchemy\CoreBundle\Controller\SentryTestController
type: attribute
4 changes: 4 additions & 0 deletions lib/php/core-bundle/Controller/SentryTestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
namespace Alchemy\CoreBundle\Controller;

use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Component\Routing\Annotation\Route;

#[Route(path: '/_health/sentry-test')]
#[AsController]
#[Autoconfigure]
readonly class SentryTestController
{
public function __construct(private LoggerInterface $logger)
Expand Down
25 changes: 21 additions & 4 deletions lib/php/core-bundle/DependencyInjection/AlchemyCoreExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public function load(array $configs, ContainerBuilder $container): void
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

$container->setParameter('alchemy_core.app_id', $config['app_id']);
$container->setParameter('alchemy_core.app_name', $config['app_name']);

$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));

$loader->load('services.yaml');
Expand Down Expand Up @@ -69,8 +72,8 @@ private function loadHealthCheckers(ContainerBuilder $container): void

private function loadSentry(ContainerBuilder $container): void
{
$env = $container->getParameter('kernel.environment');
if ('prod' === $env) {
$bundles = $container->getParameter('kernel.bundles');
if (isset($bundles['SentryBundle'])) {
$def = new Definition(PsrLogMessageProcessor::class);
$def->addTag('monolog.processor', [
'handler' =>'sentry',
Expand Down Expand Up @@ -111,9 +114,8 @@ public function prepend(ContainerBuilder $container): void
]
]);
}
if (isset($bundles['SentryBundle']) && 'prod' === $env) {
if (isset($bundles['SentryBundle'])) {
$container->prependExtensionConfig('sentry', [
'dsn' => '%env(SENTRY_DSN)%',
'register_error_listener' => false, // Disables the ErrorListener to avoid duplicated log in sentry
'tracing' => [
'dbal' => [
Expand All @@ -124,11 +126,26 @@ public function prepend(ContainerBuilder $container): void
'environment' => '%env(SENTRY_ENVIRONMENT)%',
'release' => '%env(SENTRY_RELEASE)%',
'send_default_pii' => true,
'tags' => [
'app.name' => '%alchemy_core.app_name%',
'app.id' => '%alchemy_core.app_id%',
],
'ignore_exceptions' => [
TooManyRequestsHttpException::class,
],
]
]);

if (isset($bundles['MonologBundle'])) {
$container->prependExtensionConfig('monolog', [
'handlers' => [
'sentry' => [
'type' => 'service',
'id' => \Sentry\Monolog\Handler::class,
],
],
]);
}
}
}
}
2 changes: 2 additions & 0 deletions lib/php/core-bundle/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public function getConfigTreeBuilder(): TreeBuilder
$treeBuilder = new TreeBuilder('alchemy_core');
$treeBuilder->getRootNode()
->children()
->scalarNode('app_name')->isRequired()->cannotBeEmpty()->end()
->scalarNode('app_id')->defaultValue('%env(APP_ID)%')->cannotBeEmpty()->end()
->scalarNode('app_url')->defaultNull()->end()
->arrayNode('healthcheck')
->canBeEnabled()
Expand Down
7 changes: 4 additions & 3 deletions lib/php/core-bundle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ Add route:
```yaml
# config/routes/alchemy_core.yaml
alchemy_core_healthcheck:
controller: Alchemy\CoreBundle\Controller\HealthCheckAction
resource: Alchemy\CoreBundle\Controller\HealthCheckAction
type: attribute
alchemy_core_sentry_test:
controller: Alchemy\CoreBundle\Controller\SentryTestController

resource: Alchemy\CoreBundle\Controller\SentryTestController
type: attribute
```
Ensure the route is not protected:
Expand Down
12 changes: 12 additions & 0 deletions lib/php/core-bundle/Resources/config/sentry.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,15 @@ parameters:
env(SENTRY_DSN): ~
env(SENTRY_ENVIRONMENT): prod
env(SENTRY_RELEASE): 'undefined'

services:
_defaults:
autowire: true
autoconfigure: true

Alchemy\CoreBundle\Controller\SentryTestController: ~

Sentry\Monolog\Handler:
arguments:
$hub: '@Sentry\State\HubInterface'
$level: !php/const Monolog\Logger::ERROR
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ public function load(array $configs, ContainerBuilder $container): void
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

$container->setParameter('alchemy_report.app_name', $config['app_name']);
$container->setParameter('alchemy_report.app_id', $config['app_id']);

$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yaml');

Expand Down
2 changes: 0 additions & 2 deletions lib/php/report-bundle/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ public function getConfigTreeBuilder(): TreeBuilder
$treeBuilder = new TreeBuilder('alchemy_report');
$treeBuilder->getRootNode()
->children()
->scalarNode('app_id')->defaultValue('%env(APP_ID)%')->end()
->scalarNode('app_name')->isRequired()->end()
->end()
;

Expand Down
6 changes: 4 additions & 2 deletions lib/php/report-bundle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ If user is authenticated, user ID will be provided automatically.

## Configuration

Config is taken from alchemy_core extension:

```yaml
# config/packages/alchemy_report.yml
# config/packages/alchemy_core.yml

alchemy_report:
alchemy_core:
app_name: my-app-name
```
Expand Down
4 changes: 2 additions & 2 deletions lib/php/report-bundle/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ services:

Alchemy\ReportSDK\ReportClient:
arguments:
$appName: "%alchemy_report.app_name%"
$appId: "%alchemy_report.app_id%"
$appName: "%alchemy_core.app_name%"
$appId: "%alchemy_core.app_id%"
$client: "@alchemy_report.http_client"

Alchemy\ReportBundle\ReportUserService:
Expand Down
5 changes: 3 additions & 2 deletions notify/api/config/packages/alchemy_core.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
alchemy_core:
healthcheck: ~
sentry: ~
app_name: notify
healthcheck: ~
sentry: ~
8 changes: 4 additions & 4 deletions notify/api/config/routes/alchemy_core.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
alchemy_core_healthcheck:
controller: Alchemy\CoreBundle\Controller\HealthCheckAction
path: /
resource: Alchemy\CoreBundle\Controller\HealthCheckAction
type: attribute
alchemy_core_sentry_test:
controller: Alchemy\CoreBundle\Controller\SentryTestController
path: /
resource: Alchemy\CoreBundle\Controller\SentryTestController
type: attribute
7 changes: 4 additions & 3 deletions uploader/api/config/packages/alchemy_core.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
alchemy_core:
app_url: '%env(UPLOADER_API_URL)%'
healthcheck: ~
sentry: ~
app_name: uploader
app_url: '%env(UPLOADER_API_URL)%'
healthcheck: ~
sentry: ~
2 changes: 0 additions & 2 deletions uploader/api/config/packages/alchemy_report.yml

This file was deleted.

8 changes: 4 additions & 4 deletions uploader/api/config/routes/alchemy_core.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
alchemy_core_healthcheck:
controller: Alchemy\CoreBundle\Controller\HealthCheckAction
path: /
resource: Alchemy\CoreBundle\Controller\HealthCheckAction
type: attribute
alchemy_core_sentry_test:
controller: Alchemy\CoreBundle\Controller\SentryTestController
path: /
resource: Alchemy\CoreBundle\Controller\SentryTestController
type: attribute

0 comments on commit f11203a

Please sign in to comment.