Skip to content

Cafnio/cake-sentry

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

45 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

in src/Application.php

public function bootstrap()
{
    parent::bootstrap();
    
    // Import other Plugins here.

    // Load the contact manager plugin by class name
    $this->addPlugin('Connehito/CakeSentry', ['bootstrap' => true]);
}

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.

About

CakePHP plugin integration for Sentry

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%