From 1446d71b56d9a941c59497021473ffe9bb3f8c3b Mon Sep 17 00:00:00 2001 From: indy koning Date: Fri, 6 Jun 2025 14:52:57 +0200 Subject: [PATCH 1/7] Automatically pass all supported sentry config --- .gitignore | 1 + Helper/Data.php | 120 +++++++++++------- Plugin/GlobalExceptionCatcher.php | 4 +- README.md | 8 +- etc/config.xml | 2 +- .../config/deployment-config-info.phtml | 2 +- 6 files changed, 80 insertions(+), 57 deletions(-) diff --git a/.gitignore b/.gitignore index 24df6ff..00f8568 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ .idea composer.lock /vendor +vendor diff --git a/Helper/Data.php b/Helper/Data.php index c5edfd2..51e574c 100644 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -25,6 +25,32 @@ class Data extends AbstractHelper public const XML_PATH_SRS = 'sentry/general/'; public const XML_PATH_SRS_ISSUE_GROUPING = 'sentry/issue_grouping/'; + public const NATIVE_SENTRY_CONFIG_KEYS = [ + // https://docs.sentry.io/platforms/php/configuration/options/#core-options + 'dsn' => ['type' => 'string'], + 'max_breadcrumbs' => ['type' => 'int'], + 'attach_stacktrace' => ['type' => 'bool'], + 'send_default_pii' => ['type' => 'bool'], + 'server_name' => ['type' => 'string'], + 'in_app_include' => ['type' => 'array'], + 'in_app_exclude' => ['type' => 'array'], + 'max_request_body_size' => ['type' => 'string'], + 'max_value_length' => ['type' => 'int'], + // https://docs.sentry.io/platforms/php/configuration/options/#error-monitoring-options + 'sample_rate' => ['type' => 'float'], + 'ignore_exceptions' => ['type' => 'array'], + 'error_types' => ['type' => 'int'], + 'context_lines' => ['type' => 'int'], + // https://docs.sentry.io/platforms/php/configuration/options/#tracing-options + 'traces_sample_rate' => ['type' => 'float'], + 'ignore_transactions' => ['type' => 'array'], + 'trace_propagation_targets' => ['type' => 'array'], + // https://docs.sentry.io/platforms/php/configuration/options/#transport-options + 'http_proxy' => ['type' => 'string'], + 'http_connect_timeout' => ['type' => 'int'], + 'http_timeout' => ['type' => 'int'], + ]; + /** * @var ScopeConfigInterface */ @@ -39,19 +65,18 @@ class Data extends AbstractHelper * @var array */ protected $configKeys = [ - 'dsn', - 'logrocket_key', - 'log_level', - 'errorexception_reporting', - 'ignore_exceptions', - 'mage_mode_development', - 'environment', - 'js_sdk_version', - 'tracing_enabled', - 'tracing_sample_rate', - 'ignore_js_errors', - 'disable_default_integrations', - 'clean_stacktrace', + ...self::NATIVE_SENTRY_CONFIG_KEYS, + 'logrocket_key' => ['type' => 'array'], + 'log_level' => ['type' => 'int'], + 'errorexception_reporting' => ['type' => 'int'], // Deprecated by error_types https://docs.sentry.io/platforms/php/configuration/options/#error_types + 'mage_mode_development' => ['type' => 'bool'], + 'environment' => ['type' => 'string'], + 'js_sdk_version' => ['type' => 'string'], + 'tracing_enabled' => ['type' => 'bool'], + 'tracing_sample_rate' => ['type' => 'float'], + 'ignore_js_errors' => ['type' => 'array'], + 'disable_default_integrations' => ['type' => 'array'], + 'clean_stacktrace' => ['type' => 'bool'], ]; /** @@ -119,25 +144,7 @@ public function getDisabledDefaultIntegrations(): array */ public function getIgnoreJsErrors() { - $list = $this->collectModuleConfig()['ignore_js_errors']; - - if ($list === null) { - return null; - } - - try { - $config = $this->collectModuleConfig(); - $list = is_array($config['ignore_js_errors']) - ? $config['ignore_js_errors'] - : $this->serializer->unserialize($config['ignore_js_errors']); - } catch (InvalidArgumentException $e) { - throw new RuntimeException( - __('Sentry configuration error: `ignore_js_errors` has to be an array or `null`. Given type: %s', gettype($list)), // phpcs:ignore - $e - ); - } - - return $list; + return $this->collectModuleConfig()['ignore_js_errors']; } /** @@ -216,13 +223,16 @@ public function collectModuleConfig(): array $this->config[$storeId]['enabled'] = $this->scopeConfig->getValue('sentry/environment/enabled', ScopeInterface::SCOPE_STORE) ?? $this->deploymentConfig->get('sentry') !== null; } catch (TableNotFoundException|FileSystemException|RuntimeException $e) { - $this->config[$storeId]['enabled'] = null; + $this->config[$storeId]['enabled'] = $this->deploymentConfig->get('sentry') !== null; } - foreach ($this->configKeys as $value) { + foreach ($this->configKeys as $value => $config) { try { - $this->config[$storeId][$value] = $this->scopeConfig->getValue('sentry/environment/'.$value, ScopeInterface::SCOPE_STORE) - ?? $this->deploymentConfig->get('sentry/'.$value); + $this->config[$storeId][$value] = $this->processConfigValue( + $this->scopeConfig->getValue('sentry/environment/'.$value, ScopeInterface::SCOPE_STORE) + ?? $this->deploymentConfig->get('sentry/'.$value), + $config + ); } catch (TableNotFoundException|FileSystemException|RuntimeException $e) { $this->config[$storeId][$value] = null; } @@ -231,6 +241,27 @@ public function collectModuleConfig(): array return $this->config[$storeId]; } + /** + * Parse the config value to the type defined in the config + * + * @param mixed $value + * @param array{type: string} $config + */ + public function processConfigValue(mixed $value, array $config): mixed { + if (is_null($value)) { + return null; + } + + return match ($config['type']) { + 'array' => is_array($value) ? $value : $this->serializer->unserialize($value), + 'int' => (int) $value, + 'float' => (float) $value, + 'bool' => (bool) $value, + 'string' => (string) $value, + default => $value, + }; + } + /** * Whether Sentry is active. * @@ -477,9 +508,9 @@ public function stripStoreCode(): bool * * @return int */ - public function getErrorExceptionReporting(): int + public function getErrorTypes(): int { - return (int) ($this->collectModuleConfig()['errorexception_reporting'] ?? error_reporting()); + return (int) ($this->collectModuleConfig()['error_types'] ?? $this->collectModuleConfig()['errorexception_reporting'] ?? error_reporting()); } /** @@ -489,16 +520,7 @@ public function getErrorExceptionReporting(): int */ public function getIgnoreExceptions(): array { - $config = $this->collectModuleConfig(); - if (is_array($config['ignore_exceptions'])) { - return $config['ignore_exceptions']; - } - - try { - return $this->serializer->unserialize($config['ignore_exceptions']); - } catch (InvalidArgumentException $e) { - return []; - } + return $this->collectModuleConfig()['ignore_exceptions'] ?? []; } /** @@ -510,7 +532,7 @@ public function getIgnoreExceptions(): array */ public function shouldCaptureException(Throwable $ex): bool { - if ($ex instanceof ErrorException && !($ex->getSeverity() & $this->getErrorExceptionReporting())) { + if ($ex instanceof ErrorException && !($ex->getSeverity() & $this->getErrorTypes())) { return false; } diff --git a/Plugin/GlobalExceptionCatcher.php b/Plugin/GlobalExceptionCatcher.php index 39b7a98..fd51113 100755 --- a/Plugin/GlobalExceptionCatcher.php +++ b/Plugin/GlobalExceptionCatcher.php @@ -49,6 +49,7 @@ public function aroundLaunch(AppInterface $subject, callable $proceed) /** @var DataObject $config */ $config = $this->dataObjectFactory->create(); + $config->setData(array_filter(array_intersect_key($this->sentryHelper->collectModuleConfig(), SenteryHelper::NATIVE_SENTRY_CONFIG_KEYS))); $config->setDsn($this->sentryHelper->getDSN()); if ($release = $this->releaseIdentifier->getReleaseId()) { @@ -76,8 +77,7 @@ public function aroundLaunch(AppInterface $subject, callable $proceed) static fn (IntegrationInterface $integration) => !in_array(get_class($integration), $disabledDefaultIntegrations) )); - $config->setIgnoreExceptions($this->sentryHelper->getIgnoreExceptions()); - $config->setErrorTypes($this->sentryHelper->getErrorExceptionReporting()); + $config->setErrorTypes($this->sentryHelper->getErrorTypes()); $this->eventManager->dispatch('sentry_before_init', [ 'config' => $config, diff --git a/README.md b/README.md index 49baffa..5afc4b2 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ This module uses the [Magento Deployment Configuration](https://devdocs.magento. 'logrocket_key' => 'example/example', 'environment' => null, 'log_level' => \Monolog\Logger::WARNING, - 'errorexception_reporting' => E_ALL, + 'error_types' => E_ALL, 'ignore_exceptions' => [], 'mage_mode_development' => false, 'js_sdk_version' => \JustBetter\Sentry\Block\SentryScript::CURRENT_VERSION, @@ -61,7 +61,7 @@ Next to that there are some configuration options under Stores > Configuration > | `dsn` | — | The DSN you got from Sentry for your project. You can find the DSN in the project settings under "Client Key (DSN)" | | `environment` | — | Specify the environment under which the deployed version is running. Common values: production, staging, development. Helps differentiate errors between environments. | | `log_level` | `\Monolog\Logger::WARNING` | Specify from which logging level on Sentry should get the messages. | -| `errorexception_reporting` | `E_ALL` | If the Exception is an instance of [ErrorException](https://www.php.net/manual/en/class.errorexception.php), send the error to Sentry if it matches the error reporting. Uses the same syntax as [Error Reporting](https://www.php.net/manual/en/function.error-reporting.php), e.g., `E_ERROR` | E_WARNING`. | +| `error_types` | `E_ALL` | If the Exception is an instance of [ErrorException](https://www.php.net/manual/en/class.errorexception.php), send the error to Sentry if it matches the error reporting. Uses the same syntax as [Error Reporting](https://www.php.net/manual/en/function.error-reporting.php), e.g., `E_ERROR` | E_WARNING`. | | `ignore_exceptions` | `[]` | If the class being thrown matches any in this list, do not send it to Sentry, e.g., `[\Magento\Framework\Exception\NoSuchEntityException::class]` | | `clean_stacktrace` | `true` | Whether unnecessary files (like Interceptor.php, Proxy.php, and Factory.php) should be removed from the stacktrace. (They will not be removed if they threw the error.) | | `mage_mode_development` | `false` | If set to true, you will receive issues in Sentry even if Magento is running in develop mode. | @@ -82,9 +82,9 @@ using the "Variables" in Adobe Commerce using the following variables: | `CONFIG__SENTRY__ENVIRONMENT__LOGROCKET_KEY` | string | | `CONFIG__SENTRY__ENVIRONMENT__ENVIRONMENT` | string | | `CONFIG__SENTRY__ENVIRONMENT__LOG_LEVEL` | integer | -| `CONFIG__SENTRY__ENVIRONMENT__ERROREXCEPTION_REPORTING` | integer | +| `CONFIG__SENTRY__ENVIRONMENT__ERROR_TYPES` | integer | | `CONFIG__SENTRY__ENVIRONMENT__IGNORE_EXCEPTIONS` | JSON array of classes | -| `CONFIG__SENTRY__ENVIRONMENT__CLEAN_STACKTRACE` | boolean | +| `CONFIG__SENTRY__ENVIRONMENT__CLEAN_STACKTRACE` | boolean | | `CONFIG__SENTRY__ENVIRONMENT__MAGE_MODE_DEVELOPMENT` | string | | `CONFIG__SENTRY__ENVIRONMENT__JS_SDK_VERSION` | string | | `CONFIG__SENTRY__ENVIRONMENT__TRACING_ENABLED` | boolean | diff --git a/etc/config.xml b/etc/config.xml index d315ab3..b580ad0 100755 --- a/etc/config.xml +++ b/etc/config.xml @@ -18,7 +18,7 @@ - + diff --git a/view/adminhtml/templates/system/config/deployment-config-info.phtml b/view/adminhtml/templates/system/config/deployment-config-info.phtml index ddf53e8..411be22 100644 --- a/view/adminhtml/templates/system/config/deployment-config-info.phtml +++ b/view/adminhtml/templates/system/config/deployment-config-info.phtml @@ -21,7 +21,7 @@ 'logrocket_key' => 'example/example', 'environment' => null, 'log_level' => \Monolog\Logger::WARNING, - 'errorexception_reporting' => E_ALL, + 'error_types' => E_ALL, 'ignore_exceptions' => [], 'mage_mode_development' => false, ]"); ?> From d7bace5b20b9a784047cdba04e514394660a379f Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Fri, 6 Jun 2025 12:53:10 +0000 Subject: [PATCH 2/7] Apply fixes from StyleCI --- Helper/Data.php | 68 ++++++++++++++++++++++++------------------------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/Helper/Data.php b/Helper/Data.php index 51e574c..a59fab4 100644 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -3,7 +3,6 @@ namespace JustBetter\Sentry\Helper; use ErrorException; -use InvalidArgumentException; use JustBetter\Sentry\Block\SentryScript; use Magento\Framework\App\Area; use Magento\Framework\App\Config\ScopeConfigInterface; @@ -27,28 +26,28 @@ class Data extends AbstractHelper public const NATIVE_SENTRY_CONFIG_KEYS = [ // https://docs.sentry.io/platforms/php/configuration/options/#core-options - 'dsn' => ['type' => 'string'], - 'max_breadcrumbs' => ['type' => 'int'], - 'attach_stacktrace' => ['type' => 'bool'], - 'send_default_pii' => ['type' => 'bool'], - 'server_name' => ['type' => 'string'], - 'in_app_include' => ['type' => 'array'], - 'in_app_exclude' => ['type' => 'array'], + 'dsn' => ['type' => 'string'], + 'max_breadcrumbs' => ['type' => 'int'], + 'attach_stacktrace' => ['type' => 'bool'], + 'send_default_pii' => ['type' => 'bool'], + 'server_name' => ['type' => 'string'], + 'in_app_include' => ['type' => 'array'], + 'in_app_exclude' => ['type' => 'array'], 'max_request_body_size' => ['type' => 'string'], - 'max_value_length' => ['type' => 'int'], + 'max_value_length' => ['type' => 'int'], // https://docs.sentry.io/platforms/php/configuration/options/#error-monitoring-options - 'sample_rate' => ['type' => 'float'], + 'sample_rate' => ['type' => 'float'], 'ignore_exceptions' => ['type' => 'array'], - 'error_types' => ['type' => 'int'], - 'context_lines' => ['type' => 'int'], + 'error_types' => ['type' => 'int'], + 'context_lines' => ['type' => 'int'], // https://docs.sentry.io/platforms/php/configuration/options/#tracing-options - 'traces_sample_rate' => ['type' => 'float'], - 'ignore_transactions' => ['type' => 'array'], + 'traces_sample_rate' => ['type' => 'float'], + 'ignore_transactions' => ['type' => 'array'], 'trace_propagation_targets' => ['type' => 'array'], // https://docs.sentry.io/platforms/php/configuration/options/#transport-options - 'http_proxy' => ['type' => 'string'], + 'http_proxy' => ['type' => 'string'], 'http_connect_timeout' => ['type' => 'int'], - 'http_timeout' => ['type' => 'int'], + 'http_timeout' => ['type' => 'int'], ]; /** @@ -66,17 +65,17 @@ class Data extends AbstractHelper */ protected $configKeys = [ ...self::NATIVE_SENTRY_CONFIG_KEYS, - 'logrocket_key' => ['type' => 'array'], - 'log_level' => ['type' => 'int'], - 'errorexception_reporting' => ['type' => 'int'], // Deprecated by error_types https://docs.sentry.io/platforms/php/configuration/options/#error_types - 'mage_mode_development' => ['type' => 'bool'], - 'environment' => ['type' => 'string'], - 'js_sdk_version' => ['type' => 'string'], - 'tracing_enabled' => ['type' => 'bool'], - 'tracing_sample_rate' => ['type' => 'float'], - 'ignore_js_errors' => ['type' => 'array'], + 'logrocket_key' => ['type' => 'array'], + 'log_level' => ['type' => 'int'], + 'errorexception_reporting' => ['type' => 'int'], // Deprecated by error_types https://docs.sentry.io/platforms/php/configuration/options/#error_types + 'mage_mode_development' => ['type' => 'bool'], + 'environment' => ['type' => 'string'], + 'js_sdk_version' => ['type' => 'string'], + 'tracing_enabled' => ['type' => 'bool'], + 'tracing_sample_rate' => ['type' => 'float'], + 'ignore_js_errors' => ['type' => 'array'], 'disable_default_integrations' => ['type' => 'array'], - 'clean_stacktrace' => ['type' => 'bool'], + 'clean_stacktrace' => ['type' => 'bool'], ]; /** @@ -242,23 +241,24 @@ public function collectModuleConfig(): array } /** - * Parse the config value to the type defined in the config + * Parse the config value to the type defined in the config. * - * @param mixed $value + * @param mixed $value * @param array{type: string} $config */ - public function processConfigValue(mixed $value, array $config): mixed { + public function processConfigValue(mixed $value, array $config): mixed + { if (is_null($value)) { return null; } return match ($config['type']) { - 'array' => is_array($value) ? $value : $this->serializer->unserialize($value), - 'int' => (int) $value, - 'float' => (float) $value, - 'bool' => (bool) $value, + 'array' => is_array($value) ? $value : $this->serializer->unserialize($value), + 'int' => (int) $value, + 'float' => (float) $value, + 'bool' => (bool) $value, 'string' => (string) $value, - default => $value, + default => $value, }; } From 20a2155669d3f780ac4f4fdfa3577530628dd8a3 Mon Sep 17 00:00:00 2001 From: indy koning Date: Fri, 6 Jun 2025 14:55:54 +0200 Subject: [PATCH 3/7] Codestyle fixes --- Helper/Data.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Helper/Data.php b/Helper/Data.php index a59fab4..e6fb003 100644 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -243,12 +243,14 @@ public function collectModuleConfig(): array /** * Parse the config value to the type defined in the config. * - * @param mixed $value - * @param array{type: string} $config + * @param mixed $value + * @param array $config + * + * @return mixed */ public function processConfigValue(mixed $value, array $config): mixed { - if (is_null($value)) { + if ($value === null) { return null; } From bc5b38281185adfdaf4be50b1cb6886bcafd3057 Mon Sep 17 00:00:00 2001 From: indy koning Date: Fri, 6 Jun 2025 15:39:11 +0200 Subject: [PATCH 4/7] rename tracing_sample_rate to traces_sample_rate --- Helper/Data.php | 19 +++++-------------- Plugin/GlobalExceptionCatcher.php | 10 ++++------ README.md | 4 ++-- composer.json | 1 - etc/adminhtml/system.xml | 2 +- 5 files changed, 12 insertions(+), 24 deletions(-) diff --git a/Helper/Data.php b/Helper/Data.php index 6017e36..6506f6c 100644 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -44,7 +44,8 @@ class Data extends AbstractHelper 'traces_sample_rate' => ['type' => 'float'], 'ignore_transactions' => ['type' => 'array'], 'trace_propagation_targets' => ['type' => 'array'], - 'profiles_sample_rate', => ['type' => 'float'], + // https://docs.sentry.io/platforms/php/profiling/#enabling-profiling + 'profiles_sample_rate' => ['type' => 'float'], // https://docs.sentry.io/platforms/php/configuration/options/#transport-options 'http_proxy' => ['type' => 'string'], 'http_connect_timeout' => ['type' => 'int'], @@ -68,12 +69,12 @@ class Data extends AbstractHelper ...self::NATIVE_SENTRY_CONFIG_KEYS, 'logrocket_key' => ['type' => 'array'], 'log_level' => ['type' => 'int'], - 'errorexception_reporting' => ['type' => 'int'], // Deprecated by error_types https://docs.sentry.io/platforms/php/configuration/options/#error_types + 'errorexception_reporting' => ['type' => 'int'], /* @deprecated by @see: error_types https://docs.sentry.io/platforms/php/configuration/options/#error_types */ 'mage_mode_development' => ['type' => 'bool'], 'environment' => ['type' => 'string'], 'js_sdk_version' => ['type' => 'string'], 'tracing_enabled' => ['type' => 'bool'], - 'tracing_sample_rate' => ['type' => 'float'], + 'tracing_sample_rate' => ['type' => 'float'], /* @deprecated by @see: traces_sample_rate https://docs.sentry.io/platforms/php/configuration/options/#error_types */ 'performance_tracking_enabled' => ['type' => 'bool'], 'performance_tracking_excluded_areas' => ['type' => 'array'], 'ignore_js_errors' => ['type' => 'array'], @@ -115,16 +116,6 @@ public function getDSN() return $this->collectModuleConfig()['dsn']; } - /** - * Get sample rate for php profiling. - * - * @return float - */ - public function getPhpProfileSampleRate(): float - { - return (float) ($this->collectModuleConfig()['profiles_sample_rate'] ?? 0); - } - /** * Whether tracing is enabled. */ @@ -138,7 +129,7 @@ public function isTracingEnabled(): bool */ public function getTracingSampleRate(): float { - return (float) ($this->collectModuleConfig()['tracing_sample_rate'] ?? 0.2); + return (float) ($this->collectModuleConfig()['traces_sample_rate'] ?? $this->collectModuleConfig()['tracing_sample_rate'] ?? 0.2); } /** diff --git a/Plugin/GlobalExceptionCatcher.php b/Plugin/GlobalExceptionCatcher.php index 4dfbdbc..c741ae8 100755 --- a/Plugin/GlobalExceptionCatcher.php +++ b/Plugin/GlobalExceptionCatcher.php @@ -53,7 +53,7 @@ public function aroundLaunch(AppInterface $subject, callable $proceed) /** @var DataObject $config */ $config = $this->dataObjectFactory->create(); - $config->setData(array_filter(array_intersect_key($this->sentryHelper->collectModuleConfig(), SenteryHelper::NATIVE_SENTRY_CONFIG_KEYS))); + $config->setData(array_intersect_key($this->sentryHelper->collectModuleConfig(), SenteryHelper::NATIVE_SENTRY_CONFIG_KEYS)); $config->setDsn($this->sentryHelper->getDSN()); if ($release = $this->releaseIdentifier->getReleaseId()) { @@ -85,17 +85,15 @@ public function aroundLaunch(AppInterface $subject, callable $proceed) if ($this->sentryHelper->isPerformanceTrackingEnabled()) { $config->setTracesSampleRate($this->sentryHelper->getTracingSampleRate()); - } - - if ($rate = $this->sentryHelper->getPhpProfileSampleRate()) { - $config->setData('profiles_sample_rate', $rate); + } else { + $config->unsetTracesSampleRate(null); } $this->eventManager->dispatch('sentry_before_init', [ 'config' => $config, ]); - $this->sentryInteraction->initialize($config->getData()); + $this->sentryInteraction->initialize(array_filter($config->getData())); $this->sentryPerformance->startTransaction($subject); try { diff --git a/README.md b/README.md index ac636bd..3d9c692 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ This module uses the [Magento Deployment Configuration](https://devdocs.magento. 'mage_mode_development' => false, 'js_sdk_version' => \JustBetter\Sentry\Block\SentryScript::CURRENT_VERSION, 'tracing_enabled' => true, - 'tracing_sample_rate' => 0.5, + 'traces_sample_rate' => 0.5, 'disable_default_integrations' => [ \Sentry\Integration\ModulesIntegration::class, ] @@ -70,7 +70,7 @@ Next to that there are some configuration options under Stores > Configuration > | `mage_mode_development` | `false` | If set to true, you will receive issues in Sentry even if Magento is running in develop mode. | | `js_sdk_version` | `\JustBetter\Sentry\Block\SentryScript::CURRENT_VERSION` | If set, loads the explicit version of the JavaScript SDK of Sentry. | | `tracing_enabled` | `false` | If set to true, tracing is enabled (bundle file is loaded automatically). | -| `tracing_sample_rate` | `0.2` | If tracing is enabled, set the sample rate. | +| `traces_sample_rate` | `0.2` | If tracing is enabled, set the sample rate. | | `performance_tracking_enabled` | `false` | if performance tracking is enabled, a performance report got generated for the request. | | `performance_tracking_excluded_areas` | `['adminhtml', 'crontab']` | if `performance_tracking_enabled` is enabled, we recommend to exclude the `adminhtml` & `crontab` area. | | `profiles_sample_rate` | `0` (disabled) | if this option is larger than 0 (zero), the module will create a profile of the request. Please note that you have to install [Excimer](https://www.mediawiki.org/wiki/Excimer) on your server to use profiling. [Sentry documentation](https://docs.sentry.io/platforms/php/profiling/). You have to enable tracing too. | diff --git a/composer.json b/composer.json index b299e15..71633f3 100755 --- a/composer.json +++ b/composer.json @@ -21,7 +21,6 @@ "Performance" ], "type": "magento2-module", - "version": "4.1.0", "license": "MIT", "require": { "php": ">=8.0", diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 4bc574d..e2ec49d 100755 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -125,7 +125,7 @@ 1 - + validate-not-negative-number validate-number From 9fc9b6d18e2a02be8e5ead14b802d0ce5de6da2c Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Fri, 6 Jun 2025 13:39:26 +0000 Subject: [PATCH 5/7] Apply fixes from StyleCI --- Helper/Data.php | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/Helper/Data.php b/Helper/Data.php index 6506f6c..10730d2 100644 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -67,19 +67,19 @@ class Data extends AbstractHelper */ protected $configKeys = [ ...self::NATIVE_SENTRY_CONFIG_KEYS, - 'logrocket_key' => ['type' => 'array'], - 'log_level' => ['type' => 'int'], - 'errorexception_reporting' => ['type' => 'int'], /* @deprecated by @see: error_types https://docs.sentry.io/platforms/php/configuration/options/#error_types */ - 'mage_mode_development' => ['type' => 'bool'], - 'environment' => ['type' => 'string'], - 'js_sdk_version' => ['type' => 'string'], - 'tracing_enabled' => ['type' => 'bool'], - 'tracing_sample_rate' => ['type' => 'float'], /* @deprecated by @see: traces_sample_rate https://docs.sentry.io/platforms/php/configuration/options/#error_types */ - 'performance_tracking_enabled' => ['type' => 'bool'], + 'logrocket_key' => ['type' => 'array'], + 'log_level' => ['type' => 'int'], + 'errorexception_reporting' => ['type' => 'int'], /* @deprecated by @see: error_types https://docs.sentry.io/platforms/php/configuration/options/#error_types */ + 'mage_mode_development' => ['type' => 'bool'], + 'environment' => ['type' => 'string'], + 'js_sdk_version' => ['type' => 'string'], + 'tracing_enabled' => ['type' => 'bool'], + 'tracing_sample_rate' => ['type' => 'float'], /* @deprecated by @see: traces_sample_rate https://docs.sentry.io/platforms/php/configuration/options/#error_types */ + 'performance_tracking_enabled' => ['type' => 'bool'], 'performance_tracking_excluded_areas' => ['type' => 'array'], - 'ignore_js_errors' => ['type' => 'array'], - 'disable_default_integrations' => ['type' => 'array'], - 'clean_stacktrace' => ['type' => 'bool'], + 'ignore_js_errors' => ['type' => 'array'], + 'disable_default_integrations' => ['type' => 'array'], + 'clean_stacktrace' => ['type' => 'bool'], ]; /** From 0027181f90e13b307f0998eb8e93dc5600ac11df Mon Sep 17 00:00:00 2001 From: indy koning Date: Fri, 6 Jun 2025 15:41:31 +0200 Subject: [PATCH 6/7] Fixed typo --- Plugin/GlobalExceptionCatcher.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Plugin/GlobalExceptionCatcher.php b/Plugin/GlobalExceptionCatcher.php index c741ae8..ef15509 100755 --- a/Plugin/GlobalExceptionCatcher.php +++ b/Plugin/GlobalExceptionCatcher.php @@ -53,7 +53,7 @@ public function aroundLaunch(AppInterface $subject, callable $proceed) /** @var DataObject $config */ $config = $this->dataObjectFactory->create(); - $config->setData(array_intersect_key($this->sentryHelper->collectModuleConfig(), SenteryHelper::NATIVE_SENTRY_CONFIG_KEYS)); + $config->setData(array_intersect_key($this->sentryHelper->collectModuleConfig(), SentryHelper::NATIVE_SENTRY_CONFIG_KEYS)); $config->setDsn($this->sentryHelper->getDSN()); if ($release = $this->releaseIdentifier->getReleaseId()) { From e31bd209aed5312fff0df1b7ef1b1b5d95b7e926 Mon Sep 17 00:00:00 2001 From: indy koning Date: Fri, 6 Jun 2025 15:58:37 +0200 Subject: [PATCH 7/7] Added the before callbacks --- Helper/Data.php | 2 +- Plugin/GlobalExceptionCatcher.php | 66 +++++++++++++++++++++++++------ README.md | 6 +++ 3 files changed, 61 insertions(+), 13 deletions(-) diff --git a/Helper/Data.php b/Helper/Data.php index 10730d2..28252bb 100644 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -27,6 +27,7 @@ class Data extends AbstractHelper public const NATIVE_SENTRY_CONFIG_KEYS = [ // https://docs.sentry.io/platforms/php/configuration/options/#core-options 'dsn' => ['type' => 'string'], + 'environment' => ['type' => 'string'], 'max_breadcrumbs' => ['type' => 'int'], 'attach_stacktrace' => ['type' => 'bool'], 'send_default_pii' => ['type' => 'bool'], @@ -71,7 +72,6 @@ class Data extends AbstractHelper 'log_level' => ['type' => 'int'], 'errorexception_reporting' => ['type' => 'int'], /* @deprecated by @see: error_types https://docs.sentry.io/platforms/php/configuration/options/#error_types */ 'mage_mode_development' => ['type' => 'bool'], - 'environment' => ['type' => 'string'], 'js_sdk_version' => ['type' => 'string'], 'tracing_enabled' => ['type' => 'bool'], 'tracing_sample_rate' => ['type' => 'float'], /* @deprecated by @see: traces_sample_rate https://docs.sentry.io/platforms/php/configuration/options/#error_types */ diff --git a/Plugin/GlobalExceptionCatcher.php b/Plugin/GlobalExceptionCatcher.php index ef15509..6bc787d 100755 --- a/Plugin/GlobalExceptionCatcher.php +++ b/Plugin/GlobalExceptionCatcher.php @@ -51,6 +51,29 @@ public function aroundLaunch(AppInterface $subject, callable $proceed) return $proceed(); } + $config = $this->prepareConfig(); + + $this->sentryInteraction->initialize(array_filter($config->getData())); + $this->sentryPerformance->startTransaction($subject); + + try { + return $response = $proceed(); + } catch (Throwable $exception) { + $this->sentryInteraction->captureException($exception); + + throw $exception; + } finally { + $this->sentryPerformance->finishTransaction($response ?? 500); + } + } + + /** + * Prepare all the config passed to sentry. + * + * @return DataObject + */ + public function prepareConfig(): DataObject + { /** @var DataObject $config */ $config = $this->dataObjectFactory->create(); $config->setData(array_intersect_key($this->sentryHelper->collectModuleConfig(), SentryHelper::NATIVE_SENTRY_CONFIG_KEYS)); @@ -64,6 +87,36 @@ public function aroundLaunch(AppInterface $subject, callable $proceed) $config->setEnvironment($environment); } + $config->setBeforeBreadcrumb(function (\Sentry\Breadcrumb $breadcrumb): ?\Sentry\Breadcrumb { + $data = $this->dataObjectFactory->create(); + $data->setBreadcrumb($breadcrumb); + $this->eventManager->dispatch('sentry_before_breadcrumb', [ + 'sentry_breadcrumb' => $data, + ]); + + return $data->getBreadcrumb(); + }); + + $config->setBeforeSendTransaction(function (\Sentry\Event $transaction): ?\Sentry\Event { + $data = $this->dataObjectFactory->create(); + $data->setTransaction($transaction); + $this->eventManager->dispatch('sentry_before_send_transaction', [ + 'sentry_transaction' => $data, + ]); + + return $data->getTransaction(); + }); + + $config->setBeforeSendCheckIn(function (\Sentry\Event $checkIn): ?\Sentry\Event { + $data = $this->dataObjectFactory->create(); + $data->setCheckIn($checkIn); + $this->eventManager->dispatch('sentry_before_send_check_in', [ + 'sentry_check_in' => $data, + ]); + + return $data->getCheckIn(); + }); + $config->setBeforeSend(function (\Sentry\Event $event, ?\Sentry\EventHint $hint): ?\Sentry\Event { $data = $this->dataObjectFactory->create(); $data->setEvent($event); @@ -93,17 +146,6 @@ public function aroundLaunch(AppInterface $subject, callable $proceed) 'config' => $config, ]); - $this->sentryInteraction->initialize(array_filter($config->getData())); - $this->sentryPerformance->startTransaction($subject); - - try { - return $response = $proceed(); - } catch (Throwable $exception) { - $this->sentryInteraction->captureException($exception); - - throw $exception; - } finally { - $this->sentryPerformance->finishTransaction($response ?? 500); - } + return $config; } } diff --git a/README.md b/README.md index 3d9c692..f3f90f1 100644 --- a/README.md +++ b/README.md @@ -122,6 +122,12 @@ public function execute(\Magento\Framework\Event\Observer $observer) Example: https://github.com/justbetter/magento2-sentry-filter-events +This same thing is the case for +| sentry_before_send | https://docs.sentry.io/platforms/php/configuration/options/#before_send | +| sentry_before_send_transaction | https://docs.sentry.io/platforms/php/configuration/options/#before_send_transaction | +| sentry_before_send_check_in | https://docs.sentry.io/platforms/php/configuration/options/#before_send_check_in | +| sentry_before_breadcrumb | https://docs.sentry.io/platforms/php/configuration/options/#before_breadcrumb | + ## Compatibility The module is tested on Magento version 2.4.x with sentry sdk version 3.x. feel free to fork this project or make a pull request.