Skip to content

Commit

Permalink
Add frontend and backend integrations
Browse files Browse the repository at this point in the history
  • Loading branch information
tonning committed Mar 25, 2024
1 parent a201b69 commit ef94c14
Show file tree
Hide file tree
Showing 14 changed files with 295 additions and 37 deletions.
18 changes: 0 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,6 @@
[![GitHub Code Style Action Status](https://img.shields.io/github/workflow/status/astrogoat/sentry/Check%20&%20fix%20styling?label=code%20style)](https://github.com/astrogoat/sentry/actions?query=workflow%3A"Check+%26+fix+styling"+branch%3Amain)
[![Total Downloads](https://img.shields.io/packagist/dt/astrogoat/sentry.svg?style=flat-square)](https://packagist.org/packages/astrogoat/sentry)

---
This repo can be used to scaffold a Strata CMS App package. Follow these steps to get started:

1. Press the "Use template" button at the top of this repo to create a new repo with the contents of this sentry
2. Run "php ./configure.php" to run a script that will replace all placeholders throughout all the files
3. Remove this block of text.
4. Have fun creating your package.
---

This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.

## Installation

You can install the package via composer:
Expand All @@ -24,13 +13,6 @@ You can install the package via composer:
composer require astrogoat/sentry
```

## Usage

```php
$sentry = new Astrogoat\Sentry();
echo $sentry->echoPhrase('Hello, Astrogoat!');
```

## Testing

```bash
Expand Down
64 changes: 53 additions & 11 deletions config/sentry.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,57 @@
<?php
// config for Astrogoat/Sentry

return [
/*
* If you want to override the automatic injection of views
* into some areas of your application so you to include
* them yourself then you disable each in this array.
*/
'include-frontend-views' => [
'head' => true,
'body' => true,
'end' => true,
]

'dsn' => env('SENTRY_LARAVEL_DSN', env('SENTRY_DSN')),

// capture release as git sha
'release' => trim(exec('git --git-dir ' . base_path('.git') . ' log --pretty="%h" -n1 HEAD')),

// When left empty or `null` the Laravel environment will be used
'environment' => env('SENTRY_ENVIRONMENT'),

'breadcrumbs' => [
// Capture Laravel logs in breadcrumbs
'logs' => true,

// Capture SQL queries in breadcrumbs
'sql_queries' => true,

// Capture bindings on SQL queries logged in breadcrumbs
'sql_bindings' => true,

// Capture queue job information in breadcrumbs
'queue_info' => true,

// Capture command information in breadcrumbs
'command_info' => true,
],

'tracing' => [
// Trace queue jobs as their own transactions
'queue_job_transactions' => env('SENTRY_TRACE_QUEUE_ENABLED', false),

// Capture queue jobs as spans when executed on the sync driver
'queue_jobs' => true,

// Capture SQL queries as spans
'sql_queries' => true,

// Try to find out where the SQL query originated from and add it to the query spans
'sql_origin' => true,

// Capture views as spans
'views' => true,

// Indicates if the tracing integrations supplied by Sentry should be loaded
'default_integrations' => true,
],

// @see: https://docs.sentry.io/platforms/php/configuration/options/#send-default-pii
'send_default_pii' => env('SENTRY_SEND_DEFAULT_PII', false),

'traces_sample_rate' => (float) (env('SENTRY_TRACES_SAMPLE_RATE', 0.0)),

'controllers_base_namespace' => env('SENTRY_CONTROLLERS_BASE_NAMESPACE', 'App\\Http\\Controllers'),

];
3 changes: 1 addition & 2 deletions mix-manifest.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{
"/public/js/sentry.js": "/public/js/sentry.js",
"/public/css/sentry.css": "/public/css/sentry.css"
"/public/js/sentry.js": "/public/js/sentry.js"
}
105 changes: 105 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,8 @@
"devDependencies": {
"laravel-mix": "^6.0.49",
"tailwindcss": "^3.4.1"
},
"dependencies": {
"@sentry/browser": "^7.108.0"
}
}
1 change: 0 additions & 1 deletion public/css/sentry.css

This file was deleted.

2 changes: 1 addition & 1 deletion public/js/sentry.js

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion resources/js/sentry.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
// Javascript file for sentry
import * as Sentry from "@sentry/browser";

window.Sentry = Sentry;
42 changes: 42 additions & 0 deletions resources/views/backend.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
@if(app()->bound('sentry') && Astrogoat\Sentry\Settings\SentrySettings::isEnabled())
@php $user = auth()->user(); @endphp
<script src="{{ asset('vendor/sentry/js/sentry.js') }}" defer></script>
<script>
document.addEventListener('DOMContentLoaded', () => {
Sentry.setUser({
email: "{{ $user->email }}",
fullName: "{{ $user->name }}",
});
Sentry.init({
dsn: "{{ config('sentry.dsn') }}",
environment: "{{ app()->environment() }}",
beforeSend(event, hint) {
// Check if it is an exception, and if so, show the report dialog
if (event.exception && event.event_id) {
Sentry.showReportDialog({ eventId: event.event_id });
}
return event;
},
integrations: [
Sentry.feedbackIntegration({
// Additional SDK configuration goes in here, for example:
colorScheme: "system",
showName: false,
showEmail: false,
useSentryUser: {
email: "email",
name: "fullName",
},
}),
Sentry.browserTracingIntegration(),
],
tracesSampleRate: {{ config('sentry.traces_sample_rate', 0.0) }},
});
});
</script>
@endif
17 changes: 17 additions & 0 deletions resources/views/frontend.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@if(app()->bound('sentry') && Astrogoat\Sentry\Settings\SentrySettings::isEnabled())
<script src="{{ asset('vendor/sentry/js/sentry.js') }}" defer></script>
<script>
document.addEventListener('DOMContentLoaded', () => {
Sentry.init({
dsn: "{{ config('sentry.dsn') }}",
environment: "{{ app()->environment() }}",
integrations: [
Sentry.browserTracingIntegration(),
],
tracesSampleRate: {{ config('sentry.traces_sample_rate', 0.0) }},
});
});
</script>
@endif
43 changes: 43 additions & 0 deletions src/Middleware/AddAdditionalSentryInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Astrogoat\Sentry\Middleware;

use Closure;
use Illuminate\Http\Request;
use Astrogoat\Sentry\Settings\SentrySettings;
use Symfony\Component\HttpFoundation\Response;

class AddAdditionalSentryInfo
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
*/
public function handle(Request $request, Closure $next): Response
{
if (app()->bound('sentry') && SentrySettings::isEnabled()) {
\Sentry\configureScope(function (\Sentry\State\Scope $scope): void {
$scope->setTag('tenant.name', tenant()->name ?? 'Central');
$scope->setTag('tenant.id', tenant()->id ?? 'central');

if (auth()->check()) {
$user = auth()->user();

$scope->setUser([
'id' => $user->id,
'username' => $user->name,
'email' => $user->email,
'strata_user_id' => strata_user_id(),
]);
} else {
$scope->setUser([
'id' => strata_user_id(),
]);
}
});
}

return $next($request);
}
}
23 changes: 23 additions & 0 deletions src/SentryServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@

namespace Astrogoat\Sentry;

use Exception;
use Helix\Lego\Apps\App;
use Helix\Lego\LegoManager;
use Illuminate\Routing\Router;
use Spatie\LaravelPackageTools\Package;
use Helix\Lego\Providers\RouteServiceProvider;
use Helix\Lego\Apps\Services\IncludeBackendViews;
use Helix\Lego\Apps\Services\IncludeFrontendViews;
use Spatie\LaravelPackageTools\PackageServiceProvider;
use Astrogoat\Sentry\Settings\SentrySettings;
use Astrogoat\Sentry\Middleware\AddAdditionalSentryInfo;

class SentryServiceProvider extends PackageServiceProvider
{
Expand All @@ -19,6 +25,12 @@ public function registerApp(App $app)
__DIR__ . '/../database/migrations',
__DIR__ . '/../database/migrations/settings',
])
->includeFrontendViews(function (IncludeFrontendViews $frontendViews) {
return $frontendViews->addToHead('sentry::frontend');
})
->includeBackendViews(function (IncludeBackendViews $backendViews) {
return $backendViews->addToHead('sentry::backend');
})
->backendRoutes(__DIR__.'/../routes/backend.php')
->frontendRoutes(__DIR__.'/../routes/frontend.php');
}
Expand All @@ -30,6 +42,17 @@ public function registeringPackage()
});
}

public function bootingPackage()
{
resolve(Router::class)->pushMiddlewareToGroup(RouteServiceProvider::MIDDLEWARE_GROUP_FRONTEND, AddAdditionalSentryInfo::class);

if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__.'/../public' => public_path('vendor/sentry/'),
], 'sentry-assets');
}
}

public function configurePackage(Package $package): void
{
$package->name('sentry')->hasConfigFile()->hasViews();
Expand Down

0 comments on commit ef94c14

Please sign in to comment.