Skip to content

Commit

Permalink
Merge branch 'master' into lumen
Browse files Browse the repository at this point in the history
# Conflicts:
#	README.md
  • Loading branch information
stayallive committed May 8, 2023
2 parents 4a7241f + b8ca66c commit 4ab7a34
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 36 deletions.
26 changes: 19 additions & 7 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,40 @@ Thank you for contributing!

## Development environment

### Requirements

Make sure that you have PHP 7.2+ installed. Version 7.4 or higher is required to run style checkers. On macOS, we recommend using brew to install PHP. For Windows, we recommend an official PHP.net release.

You may use [make](https://www.gnu.org/software/make) to take advantage of the provided [Makefile](Makefile).

### Clone the repository

```bash
git clone git@github.com:getsentry/sentry-laravel.git
```

Make sure that you have PHP 7.2+ installed. Version 7.4 or higher is required to run style checkers. On macOS, we recommend using brew to install PHP. For Windows, we recommend an official PHP.net release.

### Install the dependencies

Dependencies are managed through [Composer](https://getcomposer.org/):
Dependencies are managed through [Composer](https://getcomposer.org).

```bash
composer install
```

### Running tests

Tests can then be run via [PHPUnit](https://phpunit.de/):
Tests can be run via [PHPUnit](https://phpunit.de).

```bash
composer tests
```

### Code style

The code is automatically formatted through [php-cs-fixer](https://cs.symfony.com).

```bash
vendor/bin/phpunit
composer phpcs
```

## Releasing a new version
Expand All @@ -57,7 +69,7 @@ vendor/bin/phpunit
Prerequisites:

- All changes that should be released must be in the `master` branch.
- Every commit should follow the [Commit Message Format](https://develop.sentry.dev/commit-messages/#commit-message-format) convention.
- Every commit should follow the [Commit Message Format](https://develop.sentry.dev/commit-messages#commit-message-format) convention.

Manual Process:

Expand All @@ -81,7 +93,7 @@ There is a sequence diagram visualizing all this in the [README.md](https://gith

### Versioning Policy

This project follows [semver](https://semver.org/), with three additions:
This project follows [semver](https://semver.org), with three additions:

- Semver says that major version `0` can include breaking changes at any time. Still, it is common practice to assume that only `0.x` releases (minor versions) can contain breaking changes while `0.x.y` releases (patch versions) are used for backwards-compatible changes (bugfixes and features). This project also follows that practice.

Expand Down
30 changes: 17 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
.PHONY: test
.PHONY: develop
develop: vendor update-submodules setup-git

develop: update-submodules
composer install --dev
make setup-git
vendor: composer.lock
composer install

composer.lock: composer.json
composer update

.PHONY: update-submodules
update-submodules:
git submodule init
git submodule update

cs:
vendor/bin/php-cs-fixer fix --config=.php_cs --verbose --diff

cs-dry-run:
vendor/bin/php-cs-fixer fix --config=.php_cs --verbose --diff --dry-run

test:
vendor/bin/phpunit

.PHONY: setup-git
setup-git:
git config branch.autosetuprebase always

.PHONY: phpcs
phpcs:
composer phpcs

.PHONY: tests
tests:
composer tests
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<p align="center">
<a href="https://sentry.io/?utm_source=github&utm_medium=logo" target="_blank">
<a href="https://sentry.io?utm_source=github&utm_medium=logo" target="_blank">
<img src="https://sentry-brand.storage.googleapis.com/sentry-wordmark-dark-280x84.png" alt="Sentry" width="280" height="84">
</a>
</p>
Expand All @@ -15,11 +15,11 @@ _Bad software is everywhere, and we're tired of it. Sentry is on a mission to he
[![Monthly Downloads](https://poser.pugx.org/sentry/sentry-laravel/d/monthly)](https://packagist.org/packages/sentry/sentry-laravel)
[![Discord](https://img.shields.io/discord/621778831602221064)](https://discord.gg/cWnMQeA)

This is the official Laravel SDK for [Sentry](https://sentry.io/)
This is the official Laravel SDK for [Sentry](https://sentry.io).

## Getting Started

The installation steps below work on the latest versions of the Laravel framework (8.x, 9.x and 10.x).
The installation steps below work on versions 8.x, 9.x and 10.x of the Laravel framework.

For older Laravel versions and Lumen see:

Expand Down Expand Up @@ -72,7 +72,7 @@ SENTRY_LARAVEL_DSN=___PUBLIC_DSN___
use function Sentry\captureException;

try {
$this->functionFailsForSure();
$this->functionThatMayFail();
} catch (\Throwable $exception) {
captureException($exception);
}
Expand Down Expand Up @@ -102,7 +102,7 @@ The Laravel versions listed below were supported in previous versions of the Sen

Please refer to [CONTRIBUTING.md](CONTRIBUTING.md).

## Getting help/support
## Getting Help/Support

If you need help setting up or configuring the Laravel SDK (or anything else in the Sentry universe) please head over to the [Sentry Community on Discord](https://discord.com/invite/Ww9hbqr). There is a ton of great people in our Discord community ready to help you!

Expand All @@ -115,4 +115,4 @@ If you need help setting up or configuring the Laravel SDK (or anything else in

## License

Licensed under the MIT license, see [`LICENSE`](LICENSE)
Licensed under the MIT license, see [`LICENSE`](LICENSE).
12 changes: 3 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,11 @@
}
},
"scripts": {
"tests": [
"vendor/bin/phpunit --verbose"
],
"tests-travis": [
"XDEBUG_MODE=coverage vendor/bin/phpunit --verbose --configuration phpunit.xml --coverage-clover test/clover.xml"
],
"tests-report": [
"XDEBUG_MODE=coverage vendor/bin/phpunit --verbose --configuration phpunit.xml --coverage-html test/html-report"
],
"phpcs": [
"vendor/bin/php-cs-fixer fix --verbose --diff --dry-run"
],
"tests": [
"vendor/bin/phpunit --verbose"
]
},
"extra": {
Expand Down
3 changes: 2 additions & 1 deletion config/sentry.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

return [

// @see https://docs.sentry.io/product/sentry-basics/dsn-explainer/
'dsn' => env('SENTRY_LARAVEL_DSN', env('SENTRY_DSN')),

// The release version of your application
Expand Down Expand Up @@ -77,7 +78,7 @@

// @see: https://docs.sentry.io/platforms/php/guides/laravel/configuration/options/#traces-sample-rate
'traces_sample_rate' => env('SENTRY_TRACES_SAMPLE_RATE') === null ? null : (float)env('SENTRY_TRACES_SAMPLE_RATE'),

'profiles_sample_rate' => env('SENTRY_PROFILES_SAMPLE_RATE') === null ? null : (float)env('SENTRY_PROFILES_SAMPLE_RATE'),

];
6 changes: 6 additions & 0 deletions src/Sentry/Laravel/Features/Concerns/ResolvesEventOrigin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@

namespace Sentry\Laravel\Features\Concerns;

use Illuminate\Contracts\Container\Container;
use Sentry\Laravel\Tracing\BacktraceHelper;

trait ResolvesEventOrigin
{
protected function container(): Container
{
return app();
}

protected function resolveEventOrigin(): ?string
{
$backtraceHelper = $this->makeBacktraceHelper();
Expand Down
52 changes: 52 additions & 0 deletions src/Sentry/Laravel/Integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

namespace Sentry\Laravel;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\LazyLoadingViolationException;
use Illuminate\Routing\Route;
use Sentry\EventHint;
use Sentry\EventId;
use Sentry\ExceptionMechanism;
use Sentry\Laravel\Features\Concerns\ResolvesEventOrigin;
use Sentry\SentrySdk;
use Sentry\Severity;
use Sentry\Tracing\TransactionSource;
use Throwable;
use function Sentry\addBreadcrumb;
Expand Down Expand Up @@ -220,6 +224,54 @@ public static function captureUnhandledException(Throwable $throwable): ?EventId
return SentrySdk::getCurrentHub()->captureException($throwable, $hint);
}

/**
* Returns a callback that can be passed to `Model::handleLazyLoadingViolationUsing` to report lazy loading violations to Sentry.
*
* @param callable|null $callback Optional callback to be called after the violation is reported to Sentry.
*
* @return callable
*/
public static function lazyLoadingViolationReporter(?callable $callback = null): callable
{
return new class($callback) {
use ResolvesEventOrigin;

/** @var callable|null $callback */
private $callback;

public function __construct(?callable $callback)
{
$this->callback = $callback;
}

public function __invoke(Model $model, string $relation): void
{
SentrySdk::getCurrentHub()->withScope(function (Scope $scope) use ($model, $relation) {
$scope->setContext('violation', [
'model' => get_class($model),
'relation' => $relation,
'origin' => $this->resolveEventOrigin(),
]);

SentrySdk::getCurrentHub()->captureEvent(
tap(Event::createEvent(), static function (Event $event) {
$event->setLevel(Severity::warning());
}),
EventHint::fromArray([
'exception' => new LazyLoadingViolationException($model, $relation),
'mechanism' => new ExceptionMechanism(ExceptionMechanism::TYPE_GENERIC, true),
])
);
});

// Forward the violation to the next handler if there is one
if ($this->callback !== null) {
call_user_func($this->callback, $model, $relation);
}
}
};
}

/**
* Try to make an educated guess if the call came from the Laravel `report` helper.
*
Expand Down

0 comments on commit 4ab7a34

Please sign in to comment.