Skip to content

Commit

Permalink
add storefront implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
rommelfreddy committed Aug 26, 2024
1 parent fe58a4c commit da46b34
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 3 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,22 @@ sentry:
# Trace 10% of requests
traces_sample_rate: 0.1

# Optional: Report scheduled tasks status to Sentry. See https://docs.sentry.io/product/crons/ for more information and check # before enabling this feature.
frosh_sentry:
# Optional: Report scheduled tasks status to Sentry. See https://docs.sentry.io/product/crons/ for more information and check # before enabling this feature.
report_scheduled_tasks: false
storefront:
# optional: if you want track occurs within the browser (javascript/cors/csp)
enabled: true
# optional: if you want record the user sessions. Please aware the GDPR.
replay_recording:
enabled: true
sample_rate: 0.1
# optional: if you want measure the performance within the browser
tracing:
enabled: true
sample_rate: 0.1
# you should always specify a sdk version. If you do not provide any version, a hard-coded version got used. We try to keep the version up to date with the latest version, but cause the fast release-line of the javascript SDK it is recommend to update the version number regularly.
javascript_sdk_version: "8.26.0"
```
### Pictures
Expand Down
21 changes: 20 additions & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,29 @@ public function getConfigTreeBuilder(): TreeBuilder
$treeBuilder = new TreeBuilder('frosh_sentry');
$rootNode = $treeBuilder->getRootNode();

$rootNode
// @formatter:off
$rootNode // @phpstan-ignore-line
->children()
->booleanNode('report_scheduled_tasks')->defaultFalse()->end()
->arrayNode('storefront')
->children()
->booleanNode('enabled')->defaultFalse()->end()
->arrayNode('replay_recording')
->children()
->booleanNode('enabled')->defaultFalse()->end()
->floatNode('sample_rate')->defaultValue(0.1)->end()
->end()
->end()
->arrayNode('tracing')
->children()
->booleanNode('enabled')->defaultFalse()->end()
->floatNode('sample_rate')->defaultValue(0.1)->end()
->end()
->end()
->scalarNode('javascript_sdk_version')->defaultValue('8.26.0')->end()
->end()
->end();
// @formatter:on

return $treeBuilder;
}
Expand Down
12 changes: 12 additions & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="Frosh\SentryBundle\Subscriber\StorefrontPageSubscriber">
<argument key="$container" type="service" id="service_container" />
<argument key="$sentryOptions" type="service" id="sentry.client.options" />
<tag name="kernel.event_subscriber" />
</service>
</services>
</container>
7 changes: 7 additions & 0 deletions src/Resources/views/storefront/layout/meta.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% sw_extends '@Storefront/storefront/layout/meta.html.twig' %}

{% block layout_head_javascript_feature %}
{% sw_include '@ShopwareSentryBundle/storefront/sentry.html.twig' %}

{{ parent() }}
{% endblock %}
37 changes: 37 additions & 0 deletions src/Resources/views/storefront/sentry.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{% if sentry %}
{% block sentry %}
<script src="{{ sentry.javascript_src }}"></script>
<script>
Sentry.init({
dsn: '{{ sentry.dsn }}',
release: '{{ sentry.release }}',
environment: '{{ sentry.environment }}',
integrations: [
{% if sentry.tracing.enabled -%}
Sentry.browserTracingIntegration({
enableInp: true,
{% block sentry_additional_configuration_tracing %}{% endblock %}
...{ }
}),
{% endif -%}
{% if sentry.replay_recording.enabled -%}
Sentry.replayIntegration({
blockAllMedia: false,
maskAllText: false,
{% block sentry_additional_configuration_replay %}{% endblock %}
...{ }
}),
{% endif -%}
],
{% if sentry.tracing.enabled -%}
tracesSampleRate: {{ sentry.tracing.sample_rate }},
{% endif -%}
{% if sentry.replay_recording.enabled -%}
replaysSessionSampleRate: {{ sentry.replay_recording.sample_rate }},
{% endif -%}
{% block sentry_additional_configuration %}{% endblock %}
...{ }
});
</script>
{% endblock %}
{% endif %}
4 changes: 3 additions & 1 deletion src/ShopwareSentryBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@
use Frosh\SentryBundle\Listener\FixRequestUrlListener;
use Frosh\SentryBundle\Listener\SalesChannelContextListener;
use Frosh\SentryBundle\Subscriber\ScheduledTaskSubscriber;
use Shopware\Core\Framework\Bundle;
use Shopware\Core\System\SalesChannel\Event\SalesChannelContextCreatedEvent;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Sentry\State\HubInterface;

class ShopwareSentryBundle extends Bundle
{
public function build(ContainerBuilder $container): void
{
parent::build($container);

$container
->register(SalesChannelContextListener::class)
->addTag('kernel.event_listener', ['event' => SalesChannelContextCreatedEvent::class, 'method' => '__invoke'])
Expand Down
73 changes: 73 additions & 0 deletions src/Subscriber/StorefrontPageSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

declare(strict_types=1);

namespace Frosh\SentryBundle\Subscriber;

use Sentry\Options;
use Shopware\Storefront\Event\StorefrontRenderEvent;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class StorefrontPageSubscriber implements EventSubscriberInterface
{
public function __construct(
private readonly ContainerInterface $container,
private readonly Options $sentryOptions,
) {}

public static function getSubscribedEvents(): array
{
return [
StorefrontRenderEvent::class => 'onRender',
];
}

public function onRender(StorefrontRenderEvent $event): void
{
if ($this->sentryOptions->getDsn() == null
|| !$this->container->getParameter('frosh_sentry.storefront.enabled')
) {
return;
}

$jsSdkVersion = $this->container->getParameter('frosh_sentry.storefront.javascript_sdk_version');
if (!is_string($jsSdkVersion)) {
return;
}

$isReplayRecordingEnabled = $this->container->getParameter('frosh_sentry.storefront.replay_recording.enabled');
$isPerformanceTracingEnabled = $this->container->getParameter('frosh_sentry.storefront.tracing.enabled');

if ($isReplayRecordingEnabled && $isPerformanceTracingEnabled) {
$jsFile = 'bundle.tracing.replay.min.js';
} elseif ($isReplayRecordingEnabled && !$isPerformanceTracingEnabled) {
$jsFile = 'bundle.replay.min.js';
} elseif (!$isReplayRecordingEnabled && $isPerformanceTracingEnabled) {
$jsFile = 'bundle.tracing.min.js';
} elseif (!$isReplayRecordingEnabled && !$isPerformanceTracingEnabled) {
$jsFile = 'bundle.min.js';
} else {
// this case should never happen
return;
}

$replaySample = $this->container->getParameter('frosh_sentry.storefront.replay_recording.sample_rate');
$tracingSample = $this->container->getParameter('frosh_sentry.storefront.tracing.sample_rate');

$event->setParameter('sentry', [
'dsn' => $this->sentryOptions->getDsn(),
'javascript_src' => sprintf("https://browser.sentry-cdn.com/%s/%s", $jsSdkVersion, $jsFile),
'replay_recording' => [
'enabled' => $isReplayRecordingEnabled,
'sample_rate' => is_float($replaySample) ? $replaySample : 0.1,
],
'tracing' => [
'enabled' => $isPerformanceTracingEnabled,
'sample_rate' => is_float($tracingSample) ? $tracingSample : 0.1,
],
'release' => $this->sentryOptions->getRelease(),
'environment' => $this->sentryOptions->getEnvironment(),
]);
}
}

0 comments on commit da46b34

Please # to comment.