Skip to content

Commit af7bb64

Browse files
Merge pull request #195 from justbetter/feature/spotlight
Added support for spotlight
2 parents 88885d8 + cf1c272 commit af7bb64

File tree

6 files changed

+76
-26
lines changed

6 files changed

+76
-26
lines changed

Block/SentryScript.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,19 @@ public function __construct(
3939
*/
4040
public function canUseScriptTag($blockName)
4141
{
42-
if (!$this->dataHelper->isActive()) {
42+
if (!$this->dataHelper->isActive() || !$this->dataHelper->showScriptTagInThisBlock($blockName)) {
4343
return false;
4444
}
4545

46-
if ($this->dataHelper->useScriptTag() && $this->dataHelper->showScriptTagInThisBlock($blockName)) {
46+
if ($this->useScriptTag()) {
4747
return true;
4848
}
4949

50-
if ($this->dataHelper->useSessionReplay()) {
50+
if ($this->useSessionReplay()) {
51+
return true;
52+
}
53+
54+
if ($this->isSpotlightEnabled()) {
5155
return true;
5256
}
5357

@@ -61,7 +65,7 @@ public function canUseScriptTag($blockName)
6165
*/
6266
public function getDSN()
6367
{
64-
return $this->dataHelper->getDSN();
68+
return (string) $this->dataHelper->getDSN();
6569
}
6670

6771
/**
@@ -94,6 +98,14 @@ public function getEnvironment()
9498
return $this->dataHelper->getEnvironment();
9599
}
96100

101+
/**
102+
* Whether to enable sentry js tracking.
103+
*/
104+
public function useScriptTag(): bool
105+
{
106+
return $this->dataHelper->useScriptTag();
107+
}
108+
97109
/**
98110
* Whether to enable session replay.
99111
*/
@@ -202,6 +214,14 @@ public function isTracingEnabled(): bool
202214
return $this->dataHelper->isTracingEnabled();
203215
}
204216

217+
/**
218+
* Whether spotlight is enabled.
219+
*/
220+
public function isSpotlightEnabled(): bool
221+
{
222+
return $this->dataHelper->isSpotlightEnabled();
223+
}
224+
205225
/**
206226
* Get sample rate for tracing.
207227
*/

Helper/Data.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ class Data extends AbstractHelper
5353
'http_proxy' => ['type' => 'string'],
5454
'http_connect_timeout' => ['type' => 'int'],
5555
'http_timeout' => ['type' => 'int'],
56+
// https://spotlightjs.com/
57+
'spotlight' => ['type' => 'bool'],
58+
'spotlight_url' => ['type' => 'string'],
5659
];
5760

5861
/**
@@ -144,6 +147,16 @@ public function isTracingEnabled(): bool
144147
return $this->collectModuleConfig()['tracing_enabled'] ?? false;
145148
}
146149

150+
/**
151+
* Whether spotlight is enabled.
152+
*
153+
* Enabling spotlight will implicitly enable override production mode.
154+
*/
155+
public function isSpotlightEnabled(): bool
156+
{
157+
return ($this->collectModuleConfig()['spotlight'] ?? false) && !$this->isProductionMode();
158+
}
159+
147160
/**
148161
* Get sample rate for tracing.
149162
*/
@@ -320,7 +333,7 @@ public function isActiveWithReason(): array
320333
if (!$configEnabled) {
321334
$reasons[] = __('Module is not enabled in config.');
322335
}
323-
if (!$dsnNotEmpty) {
336+
if (!$dsnNotEmpty && !$this->isSpotlightEnabled()) {
324337
$reasons[] = __('DSN is empty.');
325338
}
326339
if (!$productionMode) {
@@ -359,7 +372,7 @@ public function isOverwriteProductionMode(): bool
359372
{
360373
$config = $this->collectModuleConfig();
361374

362-
return isset($config['mage_mode_development']) && $config['mage_mode_development'];
375+
return (isset($config['mage_mode_development']) && $config['mage_mode_development']) || $this->isSpotlightEnabled();
363376
}
364377

365378
/**

Model/Collector/SentryRelatedCspCollector.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ public function collect(array $defaultPolicies = []): array
3939
);
4040
}
4141

42+
if ($this->dataHelper->isSpotlightEnabled()) {
43+
$policies[] = new FetchPolicy(
44+
'script-src',
45+
false,
46+
['https://unpkg.com/@spotlightjs/']
47+
);
48+
}
49+
4250
if ($this->dataHelper->useLogrocket()) {
4351
$policies[] = new FetchPolicy(
4452
'script-src',

Plugin/GlobalExceptionCatcher.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ public function prepareConfig(): DataObject
130130
$config = $this->dataObjectFactory->create();
131131
$config->setData(array_intersect_key($this->sentryHelper->collectModuleConfig(), SentryHelper::NATIVE_SENTRY_CONFIG_KEYS));
132132

133+
$config->setSpotlight($this->sentryHelper->isSpotlightEnabled());
133134
$config->setDsn($this->sentryHelper->getDSN());
134135
if ($release = $this->releaseIdentifier->getReleaseId()) {
135136
$config->setRelease((string) $release);

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ Next to that there are some configuration options under Stores > Configuration >
8080
| `disable_default_integrations` | `[]` | Provide a list of FQCN of default integrations you do not want to use. [List of default integrations](https://github.com/getsentry/sentry-php/tree/master/src/Integration).|
8181
| `cron_monitoring_enabled` | `false` | Wether to enable [cron check ins](https://docs.sentry.io/platforms/php/crons/#upserting-cron-monitors) |
8282
| `track_crons` | `[]` | Cron handles of crons to track with cron monitoring, [Sentry only supports 6 check-ins per minute](https://docs.sentry.io/platforms/php/crons/#rate-limits) Magento does many more. |
83+
| `spotlight` | `false` | Enable [Spotlight](https://spotlightjs.com/) on the page |
84+
| `spotlight_url` | - | Override the [Sidecar url](https://spotlightjs.com/sidecar/) |
8385

8486
### Configuration for Adobe Cloud
8587
Since Adobe Cloud doesn't allow you to add manually add content to the `env.php` file, the configuration can be done

view/frontend/templates/script/sentry.phtml

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,33 @@
55
if (!$block->canUseScriptTag($block->getNameInLayout())) {
66
return;
77
}
8+
?>
89

9-
$bundleFile = 'bundle';
10-
11-
if ($block->isTracingEnabled()) {
12-
$bundleFile .= '.tracing';
13-
}
14-
15-
if ($block->useSessionReplay()) {
16-
$bundleFile .= '.replay';
17-
}
10+
<?php if ($this->useScriptTag()): ?>
11+
<?php
12+
$bundleFile = 'bundle';
1813

19-
$bundleFile .= '.min.js';
14+
if ($block->isTracingEnabled()) {
15+
$bundleFile .= '.tracing';
16+
}
2017

21-
$remoteFile = sprintf(
22-
'https://browser.sentry-cdn.com/%s/%s',
23-
$escaper->escapeHtmlAttr($block->getJsSdkVersion()),
24-
$bundleFile
25-
);
26-
?>
18+
if ($block->useSessionReplay()) {
19+
$bundleFile .= '.replay';
20+
}
2721

28-
<?= /* @noEscape */ $secureRenderer->renderTag('script', ['src' => $remoteFile, 'crossorigin' => 'anonymous']) ?>
29-
<?= /* @noEscape */ $secureRenderer->renderTag('script', [], $block->getLayout()->createBlock(\JustBetter\Sentry\Block\SentryScript::class)
30-
->setTemplate('JustBetter_Sentry::script/sentry_init.phtml')
31-
->toHtml(), false); ?>
22+
$bundleFile .= '.min.js';
3223

24+
$remoteFile = sprintf(
25+
'https://browser.sentry-cdn.com/%s/%s',
26+
$escaper->escapeHtmlAttr($block->getJsSdkVersion()),
27+
$bundleFile
28+
);
29+
?>
30+
<?= /* @noEscape */ $secureRenderer->renderTag('script', ['src' => $remoteFile, 'crossorigin' => 'anonymous']) ?>
31+
<?= /* @noEscape */ $secureRenderer->renderTag('script', [], $block->getLayout()->createBlock(\JustBetter\Sentry\Block\SentryScript::class)
32+
->setTemplate('JustBetter_Sentry::script/sentry_init.phtml')
33+
->toHtml(), false); ?>
34+
<?php endif; ?>
3335
<?php if ($block->useLogRocket()): ?>
3436
<?= /* @noEscape */ $secureRenderer->renderTag('script', ['src' => 'https://cdn.lr-ingest.io/LogRocket.min.js', 'crossorigin' => 'anonymous']) ?>
3537
<?= /* @noEscape */ $secureRenderer->renderTag(
@@ -52,3 +54,7 @@ $remoteFile = sprintf(
5254
->setTemplate('JustBetter_Sentry::script/logrocket_init.phtml')
5355
->toHtml(), false); ?>
5456
<?php endif; ?>
57+
58+
<?php if ($block->isSpotlightEnabled()): ?>
59+
<?= /* @noEscape */ $secureRenderer->renderTag('script', ['src' => 'https://unpkg.com/@spotlightjs/overlay@latest/dist/sentry-spotlight.iife.js', 'crossorigin' => 'anonymous']) ?>
60+
<?php endif; ?>

0 commit comments

Comments
 (0)