Skip to content

Commit

Permalink
Add support for the in_app_include and in_app_exclude options
Browse files Browse the repository at this point in the history
  • Loading branch information
DieterHolvoet committed Feb 17, 2021
1 parent 8bb050d commit 10312ae
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

### Added
- Add support for the `in_app_include` and `in_app_exclude` options

### Fixed
- Replace recommended guzzlehttp/psr7 with php-http/guzzle6-adapter

Expand Down
8 changes: 8 additions & 0 deletions config/install/wmsentry.settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,11 @@ log_levels:
include_stacktrace_func_args: false
excluded_exceptions: { }
excluded_tags: { }
in_app_include: { }
in_app_exclude:
- ../vendor
- core
- modules/contrib
- themes/contrib
- profiles/contrib
- libraries
12 changes: 12 additions & 0 deletions config/schema/wmsentry.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,15 @@ wmsentry.settings:
8:
type: integer
label: 'Debug'
in_app_include:
type: sequence
label: 'A list of string prefixes of module names that belong to the app. This option takes precedence over in_app_exclude.'
sequence:
type: string
label: 'Path prefix'
in_app_exclude:
type: sequence
label: 'A list of string prefixes of module names that do not belong to the app, but rather to third-party packages. Modules considered not part of the app will be hidden from stack traces by default. This option can be overridden using in_app_include.'
sequence:
type: string
label: 'Path prefix'
27 changes: 23 additions & 4 deletions src/Form/SettingsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function buildForm(array $form, FormStateInterface $form_state)
'#type' => 'textarea',
'#title' => 'Excluded exceptions',
'#description' => 'Sometimes you may want to skip capturing certain exceptions. This option sets the FQCN of the classes of the exceptions that you don’t want to capture. The check is done using the instanceof operator against each item of the array and if at least one of them passes the event will be discarded.',
'#default_value' => $this->transformExcludedExceptions($config->get('excluded_exceptions')),
'#default_value' => $this->transformStringList($config->get('excluded_exceptions')),
];

$form['excluded_tags'] = [
Expand All @@ -66,6 +66,20 @@ public function buildForm(array $form, FormStateInterface $form_state)
'#default_value' => $this->transformExcludedTags($config->get('excluded_tags')),
];

$form['in_app_include'] = [
'#type' => 'textarea',
'#title' => 'Included file paths',
'#description' => 'A list of path prefixes that belong to the app. Paths are relative to the Drupal root. This option takes precedence over in_app_exclude.',
'#default_value' => $this->transformStringList($config->get('in_app_include')),
];

$form['in_app_exclude'] = [
'#type' => 'textarea',
'#title' => 'Excluded file paths',
'#description' => 'A list of path prefixes that do not belong to the app, but rather to third-party packages. Paths are relative to the Drupal root. Modules considered not part of the app will be hidden from stack traces by default. This option can be overridden using in_app_include.',
'#default_value' => $this->transformStringList($config->get('in_app_exclude')),
];

return parent::buildForm($form, $form_state);
}

Expand All @@ -77,8 +91,10 @@ public function submitForm(array &$form, FormStateInterface $form_state)
->set('environment', $form_state->getValue('environment'))
->set('log_levels', $form_state->getValue('log_levels'))
->set('include_stacktrace_func_args', $form_state->getValue('include_stacktrace_func_args'))
->set('excluded_exceptions', $this->transformExcludedExceptions($form_state->getValue('excluded_exceptions')))
->set('excluded_exceptions', $this->transformStringList($form_state->getValue('excluded_exceptions')))
->set('excluded_tags', $this->transformExcludedTags($form_state->getValue('excluded_tags')))
->set('in_app_include', $this->transformStringList($form_state->getValue('in_app_include')))
->set('in_app_exclude', $this->transformStringList($form_state->getValue('in_app_exclude')))
->save();

parent::submitForm($form, $form_state);
Expand All @@ -100,10 +116,13 @@ protected function getLogLevelOptions(): array
return $options;
}

protected function transformExcludedExceptions($value)
protected function transformStringList($value)
{
if (is_string($value)) {
return array_map('trim', explode(PHP_EOL, $value));
$values = explode(PHP_EOL, $value);
$values = array_map('trim', $values);

return array_filter($values);
}

if (is_array($value)) {
Expand Down
12 changes: 12 additions & 0 deletions src/Logger/Sentry.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ protected function getClient(): ?ClientInterface
'attach_stacktrace' => true,
'before_send' => [$this, 'onBeforeSend'],
'before_breadcrumb' => [$this, 'onBeforeBreadcrumb'],
'in_app_exclude' => $this->normalizePaths($this->config->get('in_app_exclude')),
'in_app_include' => $this->normalizePaths($this->config->get('in_app_include')),
]);

if ($value = $this->config->get('release')) {
Expand Down Expand Up @@ -270,4 +272,14 @@ protected function isExceptionIncluded(string $type): bool
{
return !in_array($type, $this->config->get('excluded_exceptions'), true);
}

protected function normalizePaths(array $paths): array
{
return array_map(
static function (string $path) {
return DRUPAL_ROOT . DIRECTORY_SEPARATOR . ltrim($path, DIRECTORY_SEPARATOR);
},
$paths
);
}
}
24 changes: 24 additions & 0 deletions wmsentry.install
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/**
* Set default values for the in_app_exclude option
*/
function wmsentry_update_8001(): void
{
$config = \Drupal::configFactory()
->getEditable('wmsentry.settings');

if ($config->get('in_app_exclude')) {
return;
}

$config->set('in_app_exclude', [
'../vendor',
'core',
'modules/contrib',
'themes/contrib',
'profiles/contrib',
'libraries',
]);
$config->save();
}

0 comments on commit 10312ae

Please sign in to comment.