Skip to content
CakePHP plugin integration for Sentry
Branch: master
Clone or download
o0h Merge pull request #6 from Connehito/update-readme-abount-send-callback
Update README about send_callback fixed #3
Latest commit 6f1e30e Apr 20, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
config 💄 cosmetic change bootstrap.php Feb 27, 2018
src Declare type with imported namespaces Feb 27, 2018
tests Fix test Feb 27, 2018
.gitignore #2 set composer.json and .gitignore Dec 11, 2017
.travis.yml add php version for build Mar 26, 2018
LICENSE Add license Apr 1, 2018
README.md Update README about send_callback Apr 20, 2018
composer.json fix composer.json Feb 27, 2018
phpcs.xml Rename phpcs.xml.dist to phpcs.xml Feb 27, 2018
phpunit.xml Fix phpunit.xml Feb 27, 2018

README.md

CakePHP Sentry Plugin

CakePHP integration for Sentry.

Build Status codecov MIT License

Requirements

  • PHP 7.0+
  • CakePHP 3.5+
  • and Sentry account

Installation

With composer install.

composer require connehito/cake-sentry

Usage

Set config files.

in config/bootstrap.php

Plugin::load('Connehito/CakeSentry', ['bootstrap' => true]);
// in `config/app.php`
  'Sentry' => [
    'dsn' => YOUR_SENTRY_DSN_HERE
  ]
];

or use cake command.

bin/cake plugin load Connehito/CakeSentry --bootstrap

That's all! 🎉

Advanced Usage

Ignore noisy exceptions

You can filter out exceptions that make a fuss and harder to determine the issues to address(like PageNotFoundException) Set exceptions not to log in Error.skipLog.

ex)

// in `config/app.php`
'Error' => [
    'skipLog' => [
        NotFoundException::class,
        MissingRouteException::class,
        MissingControllerException::class,
    ],
]

ref: CakePHP Cookbook
https://book.cakephp.org/3.0/en/development/errors.html#error-exception-configuration

Send more context

Client dispatch CakeSentry.Client.beforeCapture event before sending error to sentry.
You can set context with EventListener.Calling Raven_Client's API or returning values, error context will be sent. The Returned values will be passed to Raven_Client::captureMessage() 3rd arguments(Additional attributes to pass with this event).

Now, cake-sentry supports to get Request instance in implemented event via $event->getSubject()->getRequest().

ex)

use Cake\Event\Event;
use Cake\Event\EventListenerInterface;

class SentryErrorContext implements EventListenerInterface
{
    public function implementedEvents()
    {
        return [
            'CakeSentry.Client.beforeCapture' => 'setContext',
        ];
    }

    public function setContext(Event $event)
    {
        $request = $event->getSubject()->getRequest();
        $request->trustProxy = true;
        $raven = $event->getSubject()->getRaven();
        $raven->user_context([
                'ip_address' => $request->clientIp()
            ]);
        $raven->tags_context([
            'app_version' => $request->getHeaderLine('App-Version') ?: 1.0,
        ]);

        return [
            'extra' => [
                'foo' => 'bar',
            ]
        ];
    }
}

And in config/bootstrap.php

EventManager::instance()->on(new SentryErrorContext());

ref: Sentry official PHP SDK document.
https://docs.sentry.io/clients/php/

Register send callback

The plugin allows you to inject send_callback option to Raven client.
It will be called in after client send data to Sentry.
See also offcial doc.

ex)

// In app.php, setup callback closure for receiving event id from Raven.
// This sample enables you to get "Event ID" via `$Session` in your controller.
// cf) https://docs.sentry.io/learn/user-feedback/
'Sentry' => [
    'dsn' => env('SENTRY_DSN'),
    'options' => [
        'send_callback' => function ($data) {
            $request = \Cake\Http\ServerRequestFactory::fromGlobals();
            $session = $request->getSession();
            $session->write('last_event_id', $data['event_id']);
        }
    ],
],

Contributing

Pull requests and feedback are very welcome :)
on GitHub at https://github.com/connehito/cake-sentry .

License

The plugin is available as open source under the terms of the MIT License.

You can’t perform that action at this time.