1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EasyTaxi\NewRelic; |
4
|
|
|
|
5
|
|
|
use EasyTaxi\NewRelic\Config\TransactionConfig; |
6
|
|
|
use EasyTaxi\NewRelic\Exception\InvalidCallerInstanceException; |
7
|
|
|
use EasyTaxi\NewRelic\Exception\NotLoadedNewRelicExtensionException; |
8
|
|
|
use EasyTaxi\NewRelic\Formatter\ArgumentsFormatter; |
9
|
|
|
use EasyTaxi\NewRelic\Formatter\FormatterInterface; |
10
|
|
|
|
11
|
|
|
class Transaction |
12
|
|
|
{ |
13
|
|
|
private $instance; |
14
|
|
|
private $config; |
15
|
|
|
private $formatter; |
16
|
|
|
|
17
|
12 |
|
public function __construct($instance, TransactionConfig $config) |
18
|
|
|
{ |
19
|
12 |
|
if (!is_object($instance)) { |
20
|
1 |
|
throw new InvalidCallerInstanceException(); |
21
|
|
|
} |
22
|
|
|
|
23
|
12 |
|
$this->instance = $instance; |
24
|
12 |
|
$this->config = $config; |
25
|
12 |
|
$this->formatter = new ArgumentsFormatter(); |
26
|
|
|
|
27
|
12 |
|
if (!extension_loaded('newrelic')) { |
28
|
1 |
|
throw new NotLoadedNewRelicExtensionException(); |
29
|
|
|
} |
30
|
12 |
|
} |
31
|
|
|
|
32
|
|
|
public function setFormatter(FormatterInterface $formatter) |
33
|
|
|
{ |
34
|
|
|
$this->formatter = $formatter; |
35
|
|
|
} |
36
|
|
|
|
37
|
10 |
|
public function __call($name, $arguments) |
38
|
|
|
{ |
39
|
10 |
|
$this->transactionStart($name, $arguments); |
40
|
|
|
|
41
|
|
|
try { |
42
|
10 |
|
return call_user_func_array([$this->instance, $name], $arguments); |
43
|
3 |
|
} catch (\Exception $genericException) { |
44
|
3 |
|
$this->transactionFail($name, $genericException); |
45
|
3 |
|
throw $genericException; |
46
|
|
|
} finally { |
47
|
10 |
|
$this->transactionEnd($name); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
10 |
|
private function addNewRelicParameter($customParameters) |
52
|
|
|
{ |
53
|
10 |
|
foreach ($customParameters as $key => $value) { |
54
|
2 |
|
if (null === $value || is_scalar($value)) { |
55
|
2 |
|
newrelic_add_custom_parameter($key, $value); |
56
|
|
|
} else { |
57
|
|
|
newrelic_add_custom_parameter($key, @json_encode($value)); |
58
|
|
|
} |
59
|
|
|
} |
60
|
10 |
|
} |
61
|
|
|
|
62
|
10 |
|
private function transactionStart($name, $arguments) |
63
|
|
|
{ |
64
|
10 |
|
if (!$this->shouldBeMonitored($name)) { |
65
|
1 |
|
return; |
66
|
|
|
} |
67
|
|
|
|
68
|
10 |
|
newrelic_set_appname($this->config->applicationName); |
69
|
10 |
|
newrelic_start_transaction($this->config->applicationName); |
70
|
10 |
|
newrelic_name_transaction($this->config->transactionName); |
71
|
10 |
|
$customParameters = $this->formatter->format($arguments); |
72
|
10 |
|
$this->addNewRelicParameter($customParameters); |
73
|
10 |
|
} |
74
|
|
|
|
75
|
10 |
|
private function shouldBeMonitored($name) |
76
|
|
|
{ |
77
|
10 |
|
return !$this->config->monitoredMethodName || $name == $this->config->monitoredMethodName; |
78
|
|
|
} |
79
|
|
|
|
80
|
10 |
|
private function transactionEnd($name) |
81
|
|
|
{ |
82
|
10 |
|
if (!$this->shouldBeMonitored($name)) { |
83
|
1 |
|
return; |
84
|
|
|
} |
85
|
|
|
|
86
|
10 |
|
newrelic_end_transaction(); |
87
|
10 |
|
} |
88
|
|
|
|
89
|
3 |
|
private function transactionFail($name, \Exception $genericException) |
90
|
|
|
{ |
91
|
3 |
|
if (!$this->shouldBeMonitored($name)) { |
92
|
|
|
return; |
93
|
|
|
} |
94
|
|
|
|
95
|
3 |
|
newrelic_notice_error($genericException->getMessage(), $genericException); |
96
|
3 |
|
} |
97
|
|
|
} |
98
|
|
|
|