# Content

# Setup

Install package

composer require contributte/newrelic
1

Register extension

extensions:
	newrelic: Contributte\NewRelic\DI\NewRelicExtension
1
2

# Configuration

Basic configuration

newrelic:
	enabled: true # true is default
	# use false on dev when newrelic extension is not present
	appName: YourApplicationName # optional, defaults to "PHP Application"
1
2
3
4

Full configuration with default values

newrelic:
	enabled: true
	appName: PHP Application
	license: ''
	logLevel:
		- critical
		- exception
		- error
	rum:
		enabled: auto
	transactionTracer:
		enabled: true
		detail: 1
		recordSql: obfuscated
		slowSql: true
		threshold: apdex_f
		stackTraceThreshold: 500
		explainThreshold: 500
	errorCollector:
		enabled: true
		recordDatabaseErrors: true
	parameters:
		capture: false
		ignored: []
	custom:
		parameters: []
		tracers: []
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

# Realtime User Monitoring

If config option rum/enabled is set to auto (default), NewRelic extension is handling adding of monitoring JS on its own. You can disable that behavior setting this option to true or false. In both cases, auto instrumentation is set off. If set to false, Agent class is returning empty string when calling getBrowserTimingHeader() and getBrowserTimingFooter() functions.

To specify where these JS should be added, you can either add RUMControlTrait to your BasePresenter or create components your own way if you want to avoid adding <script> tags. If rum/enabled is se to false, these controls returns empty string.

<?php

declare(strict_types=1);

use Contributte\NewRelic\RUM\HeaderControl;
use Contributte\NewRelic\RUM\FooterControl;
use Contributte\NewRelic\RUM\RUMControlFactory;

abstract class BasePresenter extends \Nette\Application\UI\Presenter
{

	/**
	 * @var RUMControlFactory
	 * @inject
	 */
	protected $rumControlFactory;

	protected function createComponentNewRelicHeader(): HeaderControl
	{
		// Adding true avoid adding <script> tags
		return $this->rumControlFactory->createHeader(true);
	}

	protected function createComponentNewRelicFooter(): FooterControl
	{
		// Adding true avoid adding <script> tags
		return $this->rumControlFactory->createFooter(true);
	}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

To your @layout template add newRelicHeader component before </head> tag.

{control newRelicHeader}
1

To your @layout template add newRelicFooter compenent before </body> tag.

{control newRelicFooter}
1

# Console

This step is not necessary, but recommended as it will give you a nice formated data even for console commands.

You will need to add contributte/console (opens new window) and contributte/event-dispatcher (opens new window) packages.

composer require contributte/console contributte/event-dispatcher
1

And register them.

extensions:
	events: Contributte\EventDispatcher\DI\EventDispatcherExtension
	console: Contributte\Console\DI\ConsoleExtension(%consoleMode%)
	newrelic.console: Contributte\NewRelic\DI\NewRelicConsoleExtension
1
2
3
4

# Agent

If you want to communicate with NewRelic extension, you can use autowired Agent class, which wraps all NewRelic extension functions. If config options enabled is set to false, NewRelic native functions are not called, so it's great for development environments where NewRelic extension may not be installed.

For example, if you want to add your logged-in user id as custom parameter:

<?php

declare(strict_types=1);

use Contributte\NewRelic\Agent\Agent;

abstract class BasePresenter extends \Nette\Application\UI\Presenter
{

	/**
	 * @var Agent
	 * @inject
	 */
	private $newRelicAgent;

	protected function startup(): void
	{
		parent::startup();

		if ($this->getUser()->isLoggedIn()) {
			$this->newRelicAgent->addCustomParameter('userId', $this->getUser()->getId());
		}
	}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25