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
|
10 |
|
public function __construct($instance, TransactionConfig $config) |
18
|
|
|
{ |
19
|
10 |
|
if (!is_object($instance)) { |
20
|
1 |
|
throw new InvalidCallerInstanceException(); |
21
|
|
|
} |
22
|
|
|
|
23
|
10 |
|
$this->instance = $instance; |
24
|
10 |
|
$this->config = $config; |
25
|
10 |
|
$this->formatter = new ArgumentsFormatter(); |
26
|
|
|
|
27
|
10 |
|
if (!extension_loaded('newrelic')) { |
28
|
|
|
throw new NotLoadedNewRelicExtensionException(); |
29
|
|
|
} |
30
|
10 |
|
} |
31
|
|
|
|
32
|
|
|
public function setFormatter(FormatterInterface $formatter) |
33
|
|
|
{ |
34
|
|
|
$this->formatter = $formatter; |
35
|
|
|
} |
36
|
|
|
|
37
|
9 |
|
public function __call($name, $arguments) |
38
|
|
|
{ |
39
|
9 |
|
newrelic_set_appname($this->config->applicationName); |
40
|
9 |
|
newrelic_start_transaction($this->config->applicationName); |
41
|
9 |
|
newrelic_name_transaction($this->config->transactionName); |
42
|
9 |
|
$customParameters = $this->formatter->format($arguments); |
43
|
9 |
|
$this->addNewRelicParameter($customParameters); |
44
|
|
|
|
45
|
|
|
try { |
46
|
9 |
|
return call_user_func_array([$this->instance, $name], $arguments); |
47
|
3 |
|
} catch (\Exception $genericException) { |
48
|
3 |
|
newrelic_notice_error($genericException->getMessage(), $genericException); |
49
|
3 |
|
throw $genericException; |
50
|
|
|
} finally { |
51
|
9 |
|
newrelic_end_transaction(); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
9 |
|
private function addNewRelicParameter($customParameters) |
56
|
|
|
{ |
57
|
9 |
|
foreach ($customParameters as $key => $value) { |
58
|
2 |
|
if (null === $value || is_scalar($value)) { |
59
|
2 |
|
newrelic_add_custom_parameter($key, $value); |
60
|
2 |
|
} else { |
61
|
|
|
newrelic_add_custom_parameter($key, @json_encode($value)); |
62
|
|
|
} |
63
|
9 |
|
} |
64
|
9 |
|
} |
65
|
|
|
} |
66
|
|
|
|