Skip to content

Commit 0d07871

Browse files
committed
fix: custom tags not passing to js reporter
1 parent cf1537a commit 0d07871

File tree

3 files changed

+36
-82
lines changed

3 files changed

+36
-82
lines changed

README.md

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use FoF\Sentry\Extend\Sentry;
3737

3838
return [
3939
// Other extenders
40-
40+
4141
(new Sentry())
4242
->setRelease('my-app-v1.2.3')
4343
->setEnvironment('production')
@@ -73,26 +73,6 @@ Add a custom tag to all Sentry events (applied to both backend and frontend):
7373
->addTag('php_version', PHP_VERSION);
7474
```
7575

76-
##### `addBackendConfig(string $key, $value)`
77-
78-
Add a custom configuration option for the PHP SDK:
79-
80-
```php
81-
(new Sentry())
82-
->addBackendConfig('max_breadcrumbs', 50)
83-
->addBackendConfig('send_default_pii', true);
84-
```
85-
86-
##### `addFrontendConfig(string $key, $value)`
87-
88-
Add a custom configuration option for the JavaScript SDK:
89-
90-
```php
91-
(new Sentry())
92-
->addFrontendConfig('maxBreadcrumbs', 50)
93-
->addFrontendConfig('debug', true);
94-
```
95-
9676
#### Example: Setting Environment Variables
9777

9878
You can use environment variables to configure Sentry:

src/Content/SentryJavaScript.php

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace FoF\Sentry\Content;
1313

14+
use Flarum\Foundation\Application;
15+
use Flarum\Foundation\Config;
1416
use Flarum\Frontend\Document;
1517
use Flarum\Http\UrlGenerator;
1618
use Flarum\Settings\SettingsRepositoryInterface;
@@ -33,11 +35,17 @@ class SentryJavaScript
3335
*/
3436
private $container;
3537

36-
public function __construct(SettingsRepositoryInterface $settings, UrlGenerator $url, Container $container)
38+
/**
39+
* @var Config
40+
*/
41+
private $config;
42+
43+
public function __construct(SettingsRepositoryInterface $settings, UrlGenerator $url, Container $container, Config $config)
3744
{
3845
$this->settings = $settings;
3946
$this->url = $url;
4047
$this->container = $container;
48+
$this->config = $config;
4149
}
4250

4351
public function __invoke(Document $document)
@@ -84,12 +92,18 @@ public function __invoke(Document $document)
8492
'tracesSampleRate' => $tracesSampleRate,
8593
'replaysSessionSampleRate' => $replaysSessionSampleRate,
8694
'replaysOnErrorSampleRate' => $replaysErrorSampleRate,
95+
'tags' => [
96+
'flarum' => Application::VERSION,
97+
'offline' => $this->config['offline'] ? 'true' : 'false',
98+
'debug' => $this->config->inDebugMode() ? 'true' : 'false',
99+
],
87100
];
88101

89-
// Merge with custom config
90-
if ($this->container->bound('fof.sentry.frontend.config')) {
91-
$customConfig = $this->container->make('fof.sentry.frontend.config');
92-
$config = array_merge($config, $customConfig);
102+
// Retrieve custom tags if they exist
103+
if ($this->container->bound('fof.sentry.tags')) {
104+
$customTags = $this->container->make('fof.sentry.tags');
105+
// Merge custom tags with the default ones
106+
$config['tags'] = array_merge($config['tags'], $customTags);
93107
}
94108

95109
// Add the config to the document payload
@@ -99,17 +113,18 @@ public function __invoke(Document $document)
99113
$document->foot[] = "
100114
<script>
101115
if (window.Sentry) {
116+
const sentryConfig = app.data['fof-sentry'] || {};
102117
const client = Sentry.createClient({
103-
dsn: '$dsn',
104-
environment: '$environment',
105-
scrubEmails: ".($shouldScrubEmailsFromUserData ? 'true' : 'false').',
106-
showFeedback: '.($showFeedback ? 'true' : 'false').',
107-
108-
captureConsole: '.($captureConsole ? 'true' : 'false').",
109-
110-
tracesSampleRate: $tracesSampleRate,
111-
replaysSessionSampleRate: $replaysSessionSampleRate,
112-
replaysOnErrorSampleRate: $replaysErrorSampleRate,
118+
dsn: sentryConfig.dsn || '$dsn',
119+
environment: sentryConfig.environment || '$environment',
120+
release: sentryConfig.release || '$release',
121+
scrubEmails: sentryConfig.scrubEmails !== undefined ? sentryConfig.scrubEmails : ".($shouldScrubEmailsFromUserData ? 'true' : 'false').',
122+
showFeedback: sentryConfig.showFeedback !== undefined ? sentryConfig.showFeedback : '.($showFeedback ? 'true' : 'false').',
123+
captureConsole: sentryConfig.captureConsole !== undefined ? sentryConfig.captureConsole : '.($captureConsole ? 'true' : 'false').",
124+
tracesSampleRate: sentryConfig.tracesSampleRate !== undefined ? sentryConfig.tracesSampleRate : $tracesSampleRate,
125+
replaysSessionSampleRate: sentryConfig.replaysSessionSampleRate !== undefined ? sentryConfig.replaysSessionSampleRate : $replaysSessionSampleRate,
126+
replaysOnErrorSampleRate: sentryConfig.replaysOnErrorSampleRate !== undefined ? sentryConfig.replaysOnErrorSampleRate : $replaysErrorSampleRate,
127+
tags: sentryConfig.tags || {}
113128
});
114129
115130
Sentry.getCurrentHub().bindClient(client);

src/Extend/Sentry.php

Lines changed: 5 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ class Sentry implements ExtenderInterface
2121
{
2222
private $customRelease = null;
2323
private $customEnvironment = null;
24-
private $backendConfig = [];
25-
private $frontendConfig = [];
2624
private $tags = [];
2725

2826
/**
@@ -53,36 +51,6 @@ public function setEnvironment(string $environment): self
5351
return $this;
5452
}
5553

56-
/**
57-
* Add a custom configuration option for the PHP SDK.
58-
*
59-
* @param string $key Configuration key
60-
* @param mixed $value Configuration value
61-
*
62-
* @return self
63-
*/
64-
public function addBackendConfig(string $key, $value): self
65-
{
66-
$this->backendConfig[$key] = $value;
67-
68-
return $this;
69-
}
70-
71-
/**
72-
* Add a custom configuration option for the JavaScript SDK.
73-
*
74-
* @param string $key Configuration key
75-
* @param mixed $value Configuration value
76-
*
77-
* @return self
78-
*/
79-
public function addFrontendConfig(string $key, $value): self
80-
{
81-
$this->frontendConfig[$key] = $value;
82-
83-
return $this;
84-
}
85-
8654
/**
8755
* Add a tag that will be sent with all events.
8856
*
@@ -114,22 +82,13 @@ public function extend(Container $container, ?Extension $extension = null)
11482
});
11583
}
11684

117-
// Add backend config options
118-
if (!empty($this->backendConfig)) {
119-
$container->extend('fof.sentry.backend.config', function ($config) {
120-
return array_merge($config ?? [], $this->backendConfig);
121-
});
122-
}
123-
124-
// Add frontend config options
125-
if (!empty($this->frontendConfig)) {
126-
$container->extend('fof.sentry.frontend.config', function ($config) {
127-
return array_merge($config ?? [], $this->frontendConfig);
128-
});
129-
}
130-
13185
// Add tags to backend
13286
if (!empty($this->tags)) {
87+
// Register tags in the container so they can be accessed by SentryJavaScript
88+
$container->singleton('fof.sentry.tags', function () {
89+
return $this->tags;
90+
});
91+
13392
$container->extend(HubInterface::class, function (HubInterface $hub) {
13493
$hub->configureScope(function (Scope $scope) {
13594
foreach ($this->tags as $key => $value) {

0 commit comments

Comments
 (0)