Skip to content

Commit 42b6e95

Browse files
committed
Initialize Sentry Laravel Serverless package with core functionality, middleware customization, and basic package setup.
1 parent c9fd79f commit 42b6e95

File tree

5 files changed

+164
-1
lines changed

5 files changed

+164
-1
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/vendor/
2+
.phpunit.result.cache
3+
composer.lock
4+
.idea/
5+
.vscode/

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,21 @@
1-
# sentry-laravel-serverless
1+
# Sentry Laravel Serverless
2+
3+
Laravel package for Sentry integration in serverless environments.
4+
5+
## Installation
6+
7+
You can install the package via composer:
8+
9+
## Usage
10+
11+
After installing the package, publish the configuration file:
12+
13+
## Configuration
14+
15+
Configure the package in your `.env` file:
16+
17+
## Testing
18+
19+
## License
20+
21+
The MIT License (MIT).

composer.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "datpmwork/sentry-laravel-serverless",
3+
"description": "Sentry integration for Laravel in serverless (bref) environments",
4+
"type": "library",
5+
"license": "MIT",
6+
"autoload": {
7+
"psr-4": {
8+
"DatPM\\Sentry\\Serverless\\": "src/"
9+
}
10+
},
11+
"require": {
12+
"php": "^7.4|^8.0",
13+
"laravel/framework": "^8.0|^9.0|^10.0",
14+
"sentry/sentry-laravel": "^3.0"
15+
},
16+
"extra": {
17+
"laravel": {
18+
"providers": [
19+
"DatPM\\Sentry\\Serverless\\ServiceProvider"
20+
]
21+
}
22+
},
23+
"minimum-stability": "stable",
24+
"prefer-stable": true,
25+
"config": {
26+
"allow-plugins": {
27+
"php-http/discovery": true
28+
}
29+
}
30+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
namespace DatPM\Sentry\Serverless\Middlewares;
4+
5+
use Closure;
6+
use Sentry\State\Scope;
7+
use Illuminate\Http\Request;
8+
use Sentry\Laravel\Integration;
9+
use Sentry\Laravel\Tracing\Middleware;
10+
use Illuminate\Database\Eloquent\Model;
11+
use Illuminate\Contracts\Auth\Authenticatable;
12+
use Illuminate\Contracts\Container\BindingResolutionException;
13+
14+
class ServerlessSentryMiddleware extends Middleware
15+
{
16+
/**
17+
* Handle an incoming request.
18+
*
19+
* @param \Illuminate\Http\Request $request
20+
* @param \Closure $next
21+
*
22+
* @return mixed
23+
*/
24+
public function handle(Request $request, Closure $next)
25+
{
26+
$response = parent::handle($request, $next);
27+
28+
$this->setUserData(auth()->user());
29+
30+
return $response;
31+
}
32+
33+
protected function setUserData($authUser)
34+
{
35+
if (!$authUser instanceof Model) {
36+
return;
37+
}
38+
39+
// If the user is a Laravel Eloquent model we try to extract some common fields from it
40+
$userData = [
41+
'id' => $authUser instanceof Authenticatable
42+
? $authUser->getAuthIdentifier()
43+
: $authUser->getKey(),
44+
'email' => $authUser->getAttribute('email') ?? $authUser->getAttribute('mail'),
45+
];
46+
47+
try {
48+
/** @var \Illuminate\Http\Request $request */
49+
$request = app()->make('request');
50+
51+
if ($request instanceof Request) {
52+
$ipAddress = $request->ip();
53+
54+
if ($ipAddress !== null) {
55+
$userData['ip_address'] = $ipAddress;
56+
}
57+
}
58+
} catch (BindingResolutionException $e) {
59+
// If there is no request bound we cannot get the IP address from it
60+
}
61+
62+
Integration::configureScope(static function (Scope $scope) use ($userData): void {
63+
$scope->setUser(array_filter($userData));
64+
});
65+
}
66+
67+
public function terminate(Request $request, $response): void
68+
{
69+
# Do nothing when the application is terminating
70+
return;
71+
}
72+
73+
/**
74+
* @param Request $request
75+
* @param $response
76+
* @return void
77+
*/
78+
public function parentTerminate(Request $request, $response)
79+
{
80+
parent::terminate($request, $response);
81+
}
82+
}
83+

src/ServiceProvider.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace DatPM\Sentry\Serverless;
4+
5+
use Sentry\Laravel\Tracing\Middleware;
6+
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
7+
use DatPM\Sentry\Serverless\Middlewares\ServerlessSentryMiddleware;
8+
9+
class ServiceProvider extends BaseServiceProvider
10+
{
11+
/**
12+
* Register any application services.
13+
*
14+
* @return void
15+
*/
16+
public function register()
17+
{
18+
# Overwrite default Middleware class of Sentry
19+
# Default sentry will send APM in terminate step, after the FPM response was sent
20+
# Our customization will send the APM when RequestHandled is triggered
21+
# When this event is dispatched, we will call `parentTerminate` method of SentryTracingMiddleware to send
22+
# The default `terminate` will be rewritten as empty
23+
$this->app->bind(Middleware::class, ServerlessSentryMiddleware::class, true);
24+
}
25+
}

0 commit comments

Comments
 (0)