Skip to content

Add error types option #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,12 @@ sentry:
skip_capture:
- "Symfony\\Component\\HttpKernel\\Exception\\HttpExceptionInterface"
```

### error types

Define which error types should be reported.

```yaml
sentry:
error_types: E_ALL & ~E_DEPRECATED & ~E_NOTICE
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it not be simpler to just pass array's RavenClient expect instead of transforming strings to constants, back to Sentry levels again?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, you're right - 'errro_types' was a typo. It's corrected now.

Of course it would be easier, but this is the established syntax to define error levels in php.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a way (legacy one, I might add) on how you define error reporting levels for PHP interpreter - nothing more. This configuration however is meant for Sentry, and Sentry has it's own error levels defined already - so it only makes sense, and is in a way expected behavior to follow Sentry convention.

As far as I know, otherwise, it's inconsistent - Symfony client uses one set of levels, Sentry uses another. What gives?

Copy link
Contributor Author

@d3f3kt d3f3kt Nov 4, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really know which Sentry error levels do you mean. The Raven_ErrorHandler exactly needs the error levels which i defined it in the configuration.

    /**
     * Register a handler which will intercept standard PHP errors and report them to the
     * associated Sentry client.
     *
     * @param bool $call_existing Call any existing errors handlers after processing
     *                            this instance.
     * @return array
     */
    public function registerErrorHandler($call_existing = true, $error_types = null)
    {
        if ($error_types !== null) {
            $this->error_types = $error_types;
        }
        $this->old_error_handler = set_error_handler(array($this, 'handleError'), E_ALL);
        $this->call_existing_error_handler = $call_existing;
        return $this;
    }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, terribly sorry, I was under impression RavenClient uses "error", "warning" kind of strings.

Apart from that - can we avoid at least part of the conversions? For example, instead of having the php.ini-like single variable string E_ALL && ~E_WARNING, perhaps we can have array of levels we want and then just get the values via constant? Without all the preg_replace etc.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, consider that this code will be run every time there is an issue - so we probably want to keep the logic there to the absolute minimum. There aren't that many error types, and perhaps it should be inverse: e.g. list the types you don't want to receive. That would be consistent with exception filtering.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That code is not rocket science. It's a simply preg_replace to secure the php eval function.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course. However, consider that this code is used in projects of all sizes, and this code is executed on construct - which is both good (executed just once) and bad thing (executed always). It's not a big issue if you get few hundred thousand executions per day. It becomes an issue when you have this running couple of billion times a day (like for us) - especially if there is a better and much less costly way to do it. Converting a list of "exclude" rules to constants and passing them on to Raven is such a way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't make a mountain out of a mole hill. It's a simply string replace operation.

And furthermore the code gets only executed if you have defined error types in your configuration.

Copy link
Contributor Author

@d3f3kt d3f3kt Nov 4, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Short update - I've built a small test case for you:

class SentrySymfonyClient extends \Raven_Client
{
    public function __construct($dsn=null, $options=array())
    {
        $stopwatch = new Stopwatch();
        $stopwatch->start('initialize');

        if (true || array_key_exists('error_types', $options)) {
            $stopwatch->start('parser');
            $exParser = new ErrorTypesParser('E_ALL & ~E_DEPRECATED & ~E_NOTICE');
            $options['error_types'] = $exParser->parse();
            $parserEvent = $stopwatch->stop('parser');
        }

        $options['sdk'] = array(
            'name' => 'sentry-symfony',
            'version' => SentryBundle::VERSION,
        );
        parent::__construct($dsn, $options);
        $initEvent = $stopwatch->stop('initialize');

        echo $initEvent."\n";
        echo $parserEvent."\n";
    }
}

With following results. The complete init takes 2ms and the parser takes 0ms. That should be enough to prove a string replace function is not expensive.

```
3 changes: 3 additions & 0 deletions src/Sentry/SentryBundle/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public function getConfigTreeBuilder()
->scalarNode('dsn')
->defaultNull()
->end()
->scalarNode('error_types')
->defaultNull()
->end()
->scalarNode('exception_listener')
->defaultValue('Sentry\SentryBundle\EventListener\ExceptionListener')
->end()
Expand Down
73 changes: 73 additions & 0 deletions src/Sentry/SentryBundle/ErrorTypesParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Sentry\SentryBundle;

/**
* Evaluate an error types expression.
*/
class ErrorTypesParser
{
private $expression = null;

/**
* Initialize ErrorParser
*
* @param string $expression Error Types e.g. E_ALL & ~E_DEPRECATED & ~E_NOTICE
*/
public function __construct($expression)
{
$this->expression = $expression;
}

/**
* Parse and compute the error types expression
*
* @return int the parsed expression
*/
public function parse()
{
// convert constants to ints
$this->expression = $this->convertErrorConstants($this->expression);
$this->expression = str_replace(
array(",", " "),
array(".", ""),
$this->expression
);
// remove anything which could be a security issue
$this->expression = preg_replace("/[^\d.+*%^|&~<>\/()-]/", "", $this->expression);

return $this->compute($this->expression);
}


/**
* Converts error constants from string to int.
*
* @param string $expression e.g. E_ALL & ~E_DEPRECATED & ~E_NOTICE
* @return string convertes expression e.g. 32767 & ~8192 & ~8
*/
private function convertErrorConstants($expression)
{
$output = preg_replace_callback("/(E_[a-zA-Z_]+)/", function ($errorConstant) {
if (defined($errorConstant[1])) {
return constant($errorConstant[1]);
}
return $errorConstant[0];
}, $expression);

return $output;
}

/**
* Let PHP compute the prepared expression for us.
*
* @param string $expression prepared expression e.g. 32767&~8192&~8
* @return int computed expression e.g. 24567
*/
private function compute($expression)
{
$compute = create_function("", "return " . $expression . ";");

return 0 + $compute();
}
}
2 changes: 1 addition & 1 deletion src/Sentry/SentryBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
services:
sentry.client:
class: '%sentry.client%'
arguments: ['%sentry.dsn%']
arguments: ['%sentry.dsn%', {'error_types': '%sentry.error_types%'}]
calls:
- [setRelease, ['%sentry.release%']]
- [setEnvironment, ['%sentry.environment%']]
Expand Down
5 changes: 5 additions & 0 deletions src/Sentry/SentryBundle/SentrySymfonyClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ class SentrySymfonyClient extends \Raven_Client
{
public function __construct($dsn=null, $options=array())
{
if (array_key_exists('error_types', $options)) {
$exParser = new ErrorTypesParser($options['error_types']);
$options['error_types'] = $exParser->parse();
}

$options['sdk'] = array(
'name' => 'sentry-symfony',
'version' => SentryBundle::VERSION,
Expand Down
14 changes: 14 additions & 0 deletions test/Sentry/SentryBundle/Test/ErrorTypesParserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test cases seems lacking. For configuration extension, SentrySymfonyClient, no container test...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah they def are


namespace Sentry\SentryBundle\Test;

use Sentry\SentryBundle\ErrorTypesParser;

class ErrorTypesParserTest extends \PHPUnit_Framework_TestCase
{
public function test_error_types_parser()
{
$ex = new ErrorTypesParser('E_ALL & ~E_DEPRECATED & ~E_NOTICE');
$this->assertEquals($ex->parse(), E_ALL & ~E_DEPRECATED & ~E_NOTICE);
}
}