Created
July 3, 2018 14:21
-
-
Save stof/c57666855c540e9f85ec04bdad76fa66 to your computer and use it in GitHub Desktop.
Newrelic with CSP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8" /> | |
<{# lots of stuff #} | |
</head> | |
<body> | |
{% block body %}{% endblock %} | |
{% if has_newrelic() %} | |
<script nonce="{{ csp_nonce('script') }}">{{ get_newrelic_js() }}</script> | |
{% endif %} | |
{% block javascripts '' %} | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Incenteev\WebBundle\Twig; | |
use Twig\Extension\AbstractExtension; | |
use Twig\TwigFunction; | |
class NewrelicExtension extends AbstractExtension | |
{ | |
public function getFunctions() | |
{ | |
$checkerFunction = new TwigFunction('has_newrelic', 'extension_loaded'); | |
$checkerFunction->setArguments(array('newrelic')); | |
return array( | |
$checkerFunction, | |
new TwigFunction('get_newrelic_js', array($this, 'getNewrelicJs'), array('is_safe' => array('html'))), | |
); | |
} | |
public function getNewrelicJs() | |
{ | |
if (!\extension_loaded('newrelic')) { | |
return ''; | |
} | |
newrelic_disable_autorum(); | |
return newrelic_get_browser_timing_header(false).newrelic_get_browser_timing_footer(false); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment