Skip to content

First steps toward 3.0 and 2.0 SDK #184

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 51 commits into from
Feb 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
b6e13cb
Require Sentry client 2.0
Jean85 Jan 8, 2019
59d98a8
Draft and migrate the services config
Jean85 Jan 21, 2019
6239a63
Rework and simplify the listener
Jean85 Jan 21, 2019
152814c
Split the listeners in two
Jean85 Jan 21, 2019
a6c8a9a
First working version
Jean85 Jan 22, 2019
2bf512e
Fix CS; delete unextendable classes from fixtures
Jean85 Jan 22, 2019
495353e
Add SDK identifier and version
Jean85 Jan 22, 2019
285defe
WIP: add first options to config
Jean85 Jan 22, 2019
783a1ab
Add, fix and improve DI tests
Jean85 Jan 22, 2019
70fde7d
Remove test; refactor extension
Jean85 Jan 23, 2019
385676b
Improve test of Configuration
Jean85 Jan 23, 2019
da12362
Map the first two options from the config
Jean85 Jan 23, 2019
eb80431
Change CS-Fixer commandline
Jean85 Jan 23, 2019
90e07c3
Refactor to single private method
Jean85 Jan 23, 2019
5fca96d
Map an other options; refactor to a constant map
Jean85 Jan 23, 2019
dea5617
Complete the test; remove serialize_all_object
Jean85 Jan 23, 2019
93f8aa6
Refactor to generated get/set names
Jean85 Jan 23, 2019
7def6f0
Require PHPStan-Prophecy extension
Jean85 Jan 23, 2019
30a14a9
WIP: start RequestListener test
Jean85 Jan 23, 2019
102f7ca
Skip old test
Jean85 Jan 23, 2019
132892b
Test RequestListener
Jean85 Jan 23, 2019
27208bb
Add test for ConsoleListener
Jean85 Jan 23, 2019
3c2da29
Fix PHPStan errors
Jean85 Jan 23, 2019
068eb11
Try to fix prefer-lowest build and add PHP 7.3
Jean85 Jan 24, 2019
3bd77c0
Do not cache composer.lock
Jean85 Jan 24, 2019
5f3452a
Fix composer install command in build
Jean85 Jan 24, 2019
b66f262
Fix BC with older SF versions
Jean85 Jan 24, 2019
118d586
Test (and fix) bundle listener registration
Jean85 Jan 24, 2019
5e19029
Prepare for master merge adding documentation
Jean85 Jan 24, 2019
e20139e
Merge branch 'master' into 3.0 to solve conflicts
Jean85 Jan 24, 2019
4eed8b3
Fix CS
Jean85 Jan 24, 2019
6a4e11e
Revert unwanted changes
Jean85 Jan 24, 2019
7bbdade
Cleanup the readme
Jean85 Jan 24, 2019
2c40ae8
Fix installation instructions
Jean85 Jan 24, 2019
c1b60a1
Describe better the CI job
Jean85 Jan 24, 2019
5b993cd
Fix typos
Jean85 Jan 24, 2019
5d0ff2e
Update PHPStan
Jean85 Jan 25, 2019
4ccf5cd
Merge branch 'master' into 3.0 to resolve conflicts
Jean85 Jan 28, 2019
5259132
Map the remaining scalar options
Jean85 Jan 28, 2019
632bdb0
Fix PHPStan in CI
Jean85 Jan 28, 2019
e4e333f
Require SDK beta 2
Jean85 Feb 15, 2019
ce42bb8
Fix tests after updating to beta 2
Jean85 Feb 15, 2019
19c3410
Fix test; remove duplicated test
Jean85 Feb 21, 2019
a593ac8
Refactor and move some listener tests
Jean85 Feb 21, 2019
ae197d0
Remove old test; improve parser test
Jean85 Feb 21, 2019
cabd27b
Fix CS, remove unneeded ignore in PHPStan config
Jean85 Feb 21, 2019
57333d9
Add cache and vendor dirs to default in app exclusions
Jean85 Feb 21, 2019
aa39a68
Switch from Hub::getScope to configureScope
Jean85 Feb 22, 2019
aadcb7e
Update README.md
HazAT Feb 22, 2019
cc2f480
Fix prefer-lowest build
Jean85 Feb 22, 2019
1712f84
Fix CS
Jean85 Feb 22, 2019
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: 7 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ dist: trusty
php:
- 7.1
- 7.2
- 7.3

cache:
directories:
Expand All @@ -16,7 +17,7 @@ before_install:
- composer global require hirak/prestissimo

install:
- travis_retry travis_wait composer update --no-interaction --prefer-dist --prefer-stable $COMPOSER_OPTIONS
- travis_retry travis_wait composer update --no-interaction --prefer-dist --prefer-stable

script:
- vendor/bin/phpunit -v
Expand All @@ -25,7 +26,11 @@ jobs:
include:
- stage: Test
php: 7.1
env: COMPOSER_OPTIONS="--prefer-lowest"
env:
- DEPS=--prefer-lowest
install:
- travis_retry travis_wait composer install --no-interaction --prefer-dist
- travis_retry travis_wait composer update --no-interaction --prefer-dist --prefer-stable --prefer-lowest
- stage: Code style and static analysis
script:
- composer phpstan
Expand Down
284 changes: 35 additions & 249 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ Symfony integration for [Sentry](https://getsentry.com/).
[![Scrutinizer][Master scrutinizer image]][Master scrutinizer link]
[![Coverage Status][Master coverage image]][Master scrutinizer link]

## Notice 3.0
> The current master branch contains the 3.0 version of this bundle, which is currently under development. This version
> will support the newest 2.0 version of the underlying Sentry SDK version.
>
> A beta version will be tagged as soon as possible, in the meantime you can continue to use the previous versions.
>
> To know more about the progress of this version see [the relative
milestone](https://github.com/getsentry/sentry-symfony/milestone/3)

## Benefits

Use sentry-symfony for:
Expand All @@ -26,17 +35,22 @@ Use sentry-symfony for:
## Installation

### Step 1: Download the Bundle
You can install this bundle using Composer. Since the Sentry SDK uses HTTPlug to remain transport-agnostic, you need to
manually require two additional packages that provides [`php-http/async-client-implementation`](https://packagist.org/providers/php-http/async-client-implementation)
and [`http-message-implementation`](https://packagist.org/providers/psr/http-message-implementation).

For example, if you want to install/upgrade using Curl as transport and the PSR-7 implementation by Guzzle, you can use:

Open a command console, enter your project directory and execute the
following command to download the latest stable version of this bundle:
```bash
composer require sentry/sentry-symfony:^3.0 php-http/curl-client guzzlehttp/psr7
```

Or, if you want to use only Guzzle 6, you can use:
```bash
$ composer require sentry/sentry-symfony
composer require sentry/sentry-symfony:^3.0 php-http/guzzle6-adapter guzzlehttp/psr7
```

This command requires you to have Composer installed globally, as explained
in the [installation chapter](https://getcomposer.org/doc/00-intro.md)
of the Composer documentation.
> TODO: Flex recipe
Copy link
Member

Choose a reason for hiding this comment

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

Delete or fill ^^

Copy link
Contributor Author

Choose a reason for hiding this comment

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


### Step 2: Enable the Bundle

Expand All @@ -52,20 +66,18 @@ class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
$bundles = [
// ...
);
new Sentry\SentryBundle\SentryBundle(),
];

if (in_array($this->getEnvironment(), ['staging', 'prod'], true)) {
$bundles[] = new Sentry\SentryBundle\SentryBundle();
}
// ...
}

// ...
}
```
Note that, with this snippet of code, the bundle will be enabled only for the `staging` and `prod` environment; adjust it to your needs. It's discouraged to enable this bundle in the `test` environment, because the Sentry client will change the error handler, which is already used by other packages like Symfony's deprecation handler (see [#46](https://github.com/getsentry/sentry-symfony/issues/46) and [#95](https://github.com/getsentry/sentry-symfony/issues/95)).
Note that, unlike before version 3, the bundle will be enabled in all environments.

### Step 3: Configure the SDK

Expand All @@ -78,250 +90,24 @@ sentry:
```

## Maintained versions
* 2.x is actively maintained on the master branch, but it requires Symfony 3+ and PHP 7.1+;
* 1.x is still supported to allow Symfony 2 and PHP 5.6/7.0; it may receive backports of features from the master branch, but it's not guaranteed
* 0.8.x is no longer maintained, with the 0.8.8 release containing the latest new features; it may only receive security fixes in the future.
* 3.x is actively maintained and developed on the master branch, and uses Sentry SDK 2.0;
* 2.x is supported only for fixes; from this version onwards it requires Symfony 3+ and PHP 7.1+;
* 1.x is no longer maintained; you can use it for Symfony < 2.8 and PHP 5.6/7.0;
* 0.8.x is no longer maintained.

## Configuration

The following options can be configured via ``app/config/config.yml``.

### Skip some exceptions

```yaml
sentry:
skip_capture:
- 'Symfony\Component\HttpKernel\Exception\HttpExceptionInterface'
```

### Listeners' priority

You can change the priority of the 3 default listeners of this bundle with the `listener_priorities` key of your config.
The default value is `0`, and here are the 3 possible sub-keys:

```yaml
listener_priorities:
request: 0
kernel_exception: 0
console_exception: 0
```

... respectively for the `onKernelRequest`, `onKernelException` and `onConsoleException` events.

### Options

In the following section you will find some of the available options you can configure, listed alphabetically. All available options and a more detailed description of each can be found [here](https://docs.sentry.io/clients/php/config/), in the Sentry documentation.

#### app_path

The base path to your application. Used to trim prefixes and mark frames of the stack trace as part of your application.

```yaml
sentry:
options:
app_path: "/path/to/myapp"
```

#### environment

The environment your code is running in (e.g. production).

```yaml
sentry:
options:
environment: "%kernel.environment%"
```

#### error types

Define which error types should be reported.

```yaml
sentry:
options:
error_types: E_ALL & ~E_DEPRECATED & ~E_NOTICE
```

#### exception_listener

This is used to replace the default exception listener that this bundle uses. The value must be a FQCN of a class implementing the SentryExceptionListenerInterface interface. See [Create a Custom ExceptionListener](#create-a-custom-exceptionlistener) for more details.

```yaml
sentry:
exception_listener: AppBundle\EventListener\MySentryExceptionListener
```

#### prefixes

A list of prefixes to strip from filenames. Often these would be vendor/include paths.

```yaml
sentry:
options:
prefixes:
- /usr/lib/include
```

#### release

The version of your application. Often this is the Git SHA hash of the commit.

```yaml
sentry:
options:
release: "beeee2a06521a60e646bbb8fe38702e61e4929bf"
```

#### tags

Define tags for the logged errors.

```yaml
sentry:
options:
tags:
tag1: tagvalue
tag2: tagvalue
```

### Deprecated configuration options

In previous releases of this bundle, up to 0.8.2, some of the previous options where set outside of the options level of the configuration file. Those still work but are deprecated, and they will be dropped in the stable 1.x release, so **you are advised to abandon them**; to provide forward compatibility, they can still be used alongside the standard syntax, but values must match. This is a list of those options:

```yaml
sentry:
app_path: ~
environment: ~
error_types: ~
excluded_app_paths: ~
prefixes: ~
release: ~
```
TODO
Copy link
Member

Choose a reason for hiding this comment

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

I would really just write basic docs here and move the majority to
https://github.com/getsentry/sentry-docs/tree/master/src/collections/_documentation/platforms/php symfony.md


## Customization

It is possible to customize the configuration of the user context, as well as modify the client immediately before an exception is captured by wiring up an event subscriber to the events that are emitted by the default configured `ExceptionListener` (alternatively, you can also just define your own custom exception listener).

### Create a custom ExceptionListener

You can always replace the default `ExceptionListener` with your own custom listener. To do this, assign a different class to the `exception_listener` property in your Sentry configuration, e.g.:

```yaml
sentry:
exception_listener: AppBundle\EventListener\MySentryExceptionListener
```

... and then define the custom `ExceptionListener` that implements the `SentryExceptionListenerInterface`, e.g.:

```php
// src/AppBundle/EventSubscriber/MySentryEventListener.php
namespace AppBundle\EventSubscriber;

use Sentry\SentryBundle\EventListener\SentryExceptionListenerInterface;
use Symfony\Component\Console\Event\ConsoleExceptionEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;

class MySentryExceptionListener implements SentryExceptionListenerInterface
{
// ...

public function __construct(TokenStorageInterface $tokenStorage = null, AuthorizationCheckerInterface $authorizationChecker = null, \Raven_Client $client = null, array $skipCapture, EventDispatcherInterface $dispatcher = null)
{
// ...
}

public function onKernelRequest(GetResponseEvent $event)
{
// ...
}

public function onKernelException(GetResponseForExceptionEvent $event)
{
// ...
}

public function onConsoleException(ConsoleExceptionEvent $event)
{
// ...
}
}
```

As a side note, while the above demonstrates a custom exception listener that
does not extend anything you could choose to extend the default
`ExceptionListener` and only override the functionality that you want to.

### Add an EventSubscriber for Sentry Events

Create a new class, e.g. `MySentryEventSubscriber`:

```php
// src/AppBundle/EventSubscriber/MySentryEventListener.php
namespace AppBundle\EventSubscriber;

use Sentry\SentryBundle\Event\SentryUserContextEvent;
use Sentry\SentryBundle\SentrySymfonyEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class MySentryEventSubscriber implements EventSubscriberInterface
{
/** @var \Raven_Client */
protected $client;

public function __construct(\Raven_Client $client)
{
$this->client = $client;
}

public static function getSubscribedEvents()
{
// return the subscribed events, their methods and priorities
return array(
SentrySymfonyEvents::PRE_CAPTURE => 'onPreCapture',
SentrySymfonyEvents::SET_USER_CONTEXT => 'onSetUserContext'
);
}
The Sentry 2.0 SDK uses the Unified API, hence it uses the concept of `Scope`s to hold information about the current
state of the app, and attach it to any event that is reported. This bundle has two listeners (`RequestListener` and
`ConsoleListener`) that adds some easy default information. Those listeners normally are executed with a priority of `1`
to allow easier customization with custom listener, that by default run with a lower priority of `0`.

public function onSetUserContext(SentryUserContextEvent $event)
{
// ...
}

public function onPreCapture(Event $event)
{
if ($event instanceof GetResponseForExceptionEvent) {
// ...
}
elseif ($event instanceof ConsoleExceptionEvent) {
// ...
}
}
}
```

In the example above, if you subscribe to the `PRE_CAPTURE` event you may
get an event object that caters more toward a response to a web request (e.g.
`GetResponseForExceptionEvent`) or one for actions taken at the command line
(e.g. `ConsoleExceptionEvent`). Depending on what and how the code was
invoked, and whether or not you need to distinguish between these events
during pre-capture, it might be best to test for the type of the event (as is
demonstrated above) before you do any relevant processing of the object.

To configure the above add the following configuration to your services
definitions:

```yaml
app.my_sentry_event_subscriber:
class: AppBundle\EventSubscriber\MySentryEventSubscriber
arguments:
- '@sentry.client'
tags:
- { name: kernel.event_subscriber }
```
Those listeners are `final` so not extendable, but you can look at those to know how to add more information to the
current `Scope` and enrich you Sentry events.

[Last stable image]: https://poser.pugx.org/sentry/sentry-symfony/version.svg
[Last unstable image]: https://poser.pugx.org/sentry/sentry-symfony/v/unstable.svg
Expand Down
Loading