forked from bazilio91/yii2-newrelic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNewrelic.php
84 lines (72 loc) · 1.99 KB
/
Newrelic.php
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
namespace byinti\yii\newrelic;
use byinti\yii\newrelic\handlers\BaseHandler;
use byinti\yii\newrelic\handlers\ConsoleHandler;
use byinti\yii\newrelic\handlers\WebHandler;
use byinti\NewRelic\Agent;
use yii\base\BootstrapInterface;
use yii\base\Component;
/**
* Class Newrelic
* @package byinti\yii\newrelic
*/
class Newrelic extends Component implements BootstrapInterface
{
/**
* @var bool Enable agent
*/
public $enabled = true;
/**
* @var Agent
*/
public $agent;
/**
* @var string App name
*/
public $name;
/**
* @var string Licence key
*/
public $licence = null; // use extension key by default
/**
* @var string handlers\Handler
*/
public $handler;
/**
* @var bool Enable view instrumentation with newrelic scripts
*/
public $enableEndUser = true;
/**
* @inheritdoc
*/
public function bootstrap($app)
{
if (!$this->enabled) {
return;
}
if ($this->handler) {
$handler = new $this->handler(['newrelic' => $this]);
} elseif ($app instanceof \yii\web\Application) {
$handler = new WebHandler(['newrelic' => $this]);
} elseif ($app instanceof \yii\console\Application) {
$handler = new ConsoleHandler(['newrelic' => $this]);
} else {
$handler = new BaseHandler(['newrelic' => $this]);
}
$handler->bootstrap($app);
}
public function init()
{
parent::init();
if ($this->enabled) {
if (extension_loaded('newrelic')) {
$this->name = $this->name ? $this->name : \Yii::$app->name;
$this->agent = new Agent();
$this->agent->setAppname($this->name, $this->licence);
} else {
$this->enabled = false;
\Yii::$app->getLog()->getLogger()->log('Newrelic extension is not loaded', \yii\log\Logger::LEVEL_ERROR);
}
}
}
}