Skip to content

Commit

Permalink
Added new explicitly_collection_exceptions for dealing with multiple …
Browse files Browse the repository at this point in the history
…exception reporting in commands
  • Loading branch information
iainmckay committed Jul 7, 2021
1 parent fc73220 commit 639ec25
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 1.0.0-rc2
- Added `explicitly_collect_exceptions` to `commands` configuration to work around multiple collection of the same exception.

## 1.0.0-rc1
- Added basic memory tracking, disabled by default
- Refactored how custom labels and custom are attached
Expand Down
6 changes: 6 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ public function getConfigTreeBuilder(): TreeBuilder
->end()
->arrayNode('commands')
->canBeDisabled()
->children()
->booleanNode('explicitly_collect_exceptions')
->info('Should exceptions be explicitly collected? This can conflict with the built-in collection in PHP APM')
->defaultTrue()
->end()
->end()
->end()
->end()
;
Expand Down
1 change: 1 addition & 0 deletions DependencyInjection/ElasticApmExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function load(array $configs, ContainerBuilder $container): void
'$customContext' => $config['custom_context'],
'$shouldCollectMemoryUsage' => $config['track_memory_usage'],
'$memoryUsageLabelName' => $config['memory_usage_label'],
'$shouldExplicitlyCollectCommandExceptions' => $config['commands']['explicitly_collect_exceptions'],
]
);

Expand Down
9 changes: 8 additions & 1 deletion Interactor/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ class Config
private $customContext;
private $shouldCollectMemoryUsage;
private $memoryUsageLabelName;
private $shouldExplicitlyCollectCommandExceptions;

public function __construct(array $customLabels, array $customContext, bool $shouldCollectMemoryUsage, string $memoryUsageLabelName)
public function __construct(array $customLabels, array $customContext, bool $shouldCollectMemoryUsage, string $memoryUsageLabelName, bool $shouldExplicitlyCollectCommandExceptions)
{
if (0 === strlen($memoryUsageLabelName)) {
throw new ConfigurationException('$memoryUsageLabelName cannot be blank');
Expand All @@ -35,6 +36,7 @@ public function __construct(array $customLabels, array $customContext, bool $sho
$this->customContext = $customContext;
$this->shouldCollectMemoryUsage = $shouldCollectMemoryUsage;
$this->memoryUsageLabelName = $memoryUsageLabelName;
$this->shouldExplicitlyCollectCommandExceptions = $shouldExplicitlyCollectCommandExceptions;
}

public function setCustomLabels(array $customLabels): void
Expand Down Expand Up @@ -82,4 +84,9 @@ public function getMemoryUsageLabelName(): string
{
return $this->memoryUsageLabelName;
}

public function shouldExplicitlyCollectCommandExceptions(): bool
{
return $this->shouldExplicitlyCollectCommandExceptions;
}
}
11 changes: 10 additions & 1 deletion Listener/CommandListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace ElasticApmBundle\Listener;

use ElasticApmBundle\Interactor\Config;
use ElasticApmBundle\Interactor\ElasticApmInteractorInterface;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
Expand All @@ -22,10 +23,12 @@
class CommandListener implements EventSubscriberInterface
{
private $interactor;
private $config;

public function __construct(ElasticApmInteractorInterface $interactor)
public function __construct(ElasticApmInteractorInterface $interactor, Config $config)
{
$this->interactor = $interactor;
$this->config = $config;
}

public static function getSubscribedEvents(): array
Expand Down Expand Up @@ -69,9 +72,15 @@ public function onConsoleCommand(ConsoleCommandEvent $event): void

public function onConsoleError(ConsoleErrorEvent $event): void
{
if (! $this->config->shouldExplicitlyCollectCommandExceptions()) {
return;
}

$this->interactor->addContextFromConfig();
$this->interactor->noticeThrowable($event->getError());

if (null !== $event->getError()->getPrevious()) {
$this->interactor->addContextFromConfig();
$this->interactor->noticeThrowable($event->getError()->getPrevious());
}
}
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ elastic_apm:
transaction_naming_service: ~ # Transaction naming service (see below)
commands:
enabled: true # If true, enhances CLI commands with options and arguments (default: true)
explicitly_collect_exceptions: true # Turn this off if you are experiencing multiple reports of exceptions.
```
## Enhanced RUM instrumentation
Expand Down Expand Up @@ -130,6 +131,15 @@ monolog:
id: 'Monolog\Handler\ElasticsearchHandler'
```

## Troubleshooting

### Exceptions from commands are being recorded multiple times

PHP APM will automatically collect unhandled exceptions. The bundle will also install a listener for command exceptions. Our listener and the default behaviour can conflict which causes this behaviour.

To fix this you can turn off `explicitly_collect_exceptions` under the `command` configuration node.


## Credits

This bundle is based largely on the work done by https://github.com/ekino/EkinoNewRelicBundle.

0 comments on commit 639ec25

Please sign in to comment.