1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DJStarCOM\NewRelic; |
4
|
|
|
|
5
|
|
|
use DJStarCOM\NewRelic\Config\TransactionConfig; |
6
|
|
|
use DJStarCOM\NewRelic\Exception\InvalidCallerInstanceException; |
7
|
|
|
use DJStarCOM\NewRelic\Exception\NotLoadedNewRelicExtensionException; |
8
|
|
|
use DJStarCOM\NewRelic\Formatter\ArgumentsFormatter; |
9
|
|
|
use DJStarCOM\NewRelic\Formatter\FormatterInterface; |
10
|
|
|
|
11
|
|
|
class Transaction |
12
|
|
|
{ |
13
|
|
|
private $instance; |
14
|
|
|
private $config; |
15
|
|
|
private $formatter; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Transaction constructor. |
19
|
|
|
* @param $instance |
20
|
|
|
* @param TransactionConfig $config |
21
|
|
|
*/ |
22
|
13 |
|
public function __construct($instance, TransactionConfig $config) |
23
|
|
|
{ |
24
|
13 |
|
if (!\is_object($instance)) { |
25
|
1 |
|
throw new InvalidCallerInstanceException(); |
26
|
|
|
} |
27
|
|
|
|
28
|
13 |
|
$this->instance = $instance; |
29
|
13 |
|
$this->config = $config; |
30
|
13 |
|
$this->formatter = new ArgumentsFormatter(); |
31
|
|
|
|
32
|
13 |
|
if (!extension_loaded('newrelic')) { |
33
|
1 |
|
throw new NotLoadedNewRelicExtensionException(); |
34
|
|
|
} |
35
|
13 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param FormatterInterface $formatter |
39
|
|
|
*/ |
40
|
|
|
public function setFormatter(FormatterInterface $formatter) |
41
|
|
|
{ |
42
|
|
|
$this->formatter = $formatter; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @inheritdoc |
47
|
|
|
* @throws \Exception |
48
|
|
|
*/ |
49
|
11 |
|
public function __call($name, $arguments) |
50
|
|
|
{ |
51
|
11 |
|
$this->transactionStart($name, $arguments); |
52
|
|
|
|
53
|
|
|
try { |
54
|
11 |
|
return \call_user_func_array([$this->instance, $name], $arguments); |
55
|
3 |
|
} catch (\Exception $genericException) { |
56
|
3 |
|
$this->transactionFail($name, $genericException); |
57
|
3 |
|
throw $genericException; |
58
|
|
|
} finally { |
59
|
11 |
|
$this->transactionEnd($name); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param $customParameters |
65
|
|
|
*/ |
66
|
11 |
|
private function addNewRelicParameter($customParameters) |
67
|
|
|
{ |
68
|
11 |
|
foreach ($customParameters as $key => $value) { |
69
|
2 |
|
if (null === $value || is_scalar($value)) { |
70
|
2 |
|
newrelic_add_custom_parameter($key, $value); |
71
|
|
|
} else { |
72
|
2 |
|
newrelic_add_custom_parameter($key, @json_encode($value)); |
73
|
|
|
} |
74
|
|
|
} |
75
|
11 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $name |
79
|
|
|
* @param mixed $arguments |
80
|
|
|
*/ |
81
|
11 |
|
private function transactionStart($name, $arguments) |
82
|
|
|
{ |
83
|
11 |
|
if (!$this->shouldBeMonitored($name)) { |
84
|
1 |
|
return; |
85
|
|
|
} |
86
|
|
|
|
87
|
11 |
|
if (PHP_SAPI === 'cli') { |
88
|
11 |
|
newrelic_background_job(true); |
89
|
|
|
} |
90
|
|
|
|
91
|
11 |
|
newrelic_set_appname($this->config->applicationName); |
92
|
11 |
|
newrelic_start_transaction($this->config->applicationName); |
93
|
11 |
|
newrelic_name_transaction($this->config->transactionName); |
94
|
11 |
|
$customParameters = $this->formatter->format($arguments); |
95
|
11 |
|
$this->addNewRelicParameter($customParameters); |
96
|
11 |
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param $name |
100
|
|
|
* @return bool |
101
|
|
|
*/ |
102
|
11 |
|
private function shouldBeMonitored($name) |
103
|
|
|
{ |
104
|
11 |
|
return !$this->config->monitoredMethodName || $name == $this->config->monitoredMethodName; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param $name |
109
|
|
|
*/ |
110
|
11 |
|
private function transactionEnd($name) |
111
|
|
|
{ |
112
|
11 |
|
if (!$this->shouldBeMonitored($name)) { |
113
|
1 |
|
return; |
114
|
|
|
} |
115
|
|
|
|
116
|
11 |
|
newrelic_end_transaction(); |
117
|
11 |
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param $name |
121
|
|
|
* @param \Exception $genericException |
122
|
|
|
*/ |
123
|
3 |
|
private function transactionFail($name, \Exception $genericException) |
124
|
|
|
{ |
125
|
3 |
|
if (!$this->shouldBeMonitored($name)) { |
126
|
|
|
return; |
127
|
|
|
} |
128
|
|
|
|
129
|
3 |
|
newrelic_notice_error($genericException->getMessage(), $genericException); |
130
|
3 |
|
} |
131
|
|
|
} |
132
|
|
|
|