1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
namespace ZERO2TEN\Observability\APM\Agent; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use Psr\Log\LoggerAwareInterface; |
7
|
|
|
use Psr\Log\LoggerAwareTrait; |
8
|
|
|
use Psr\Log\NullLogger; |
9
|
|
|
use ZERO2TEN\Observability\APM\AgentInterface; |
10
|
|
|
use ZERO2TEN\Observability\APM\FunctionProxy; |
11
|
|
|
use ZERO2TEN\Observability\APM\Transaction; |
12
|
|
|
use ZERO2TEN\Observability\APM\TransactionInterface; |
13
|
|
|
|
14
|
|
|
use function in_array; |
15
|
|
|
use function trim; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Agent |
19
|
|
|
* |
20
|
|
|
* @copyright Copyright (c) 2023 0TO10 B.V. <https://0to10.nl> |
21
|
|
|
* @package ZERO2TEN\Observability\APM\Agent |
22
|
|
|
*/ |
23
|
|
|
abstract class Agent implements AgentInterface, LoggerAwareInterface |
24
|
|
|
{ |
25
|
|
|
use FunctionProxy; |
26
|
|
|
use LoggerAwareTrait; |
27
|
|
|
|
28
|
|
|
/** @var bool */ |
29
|
|
|
private $initialised; |
30
|
|
|
/** @var string[] */ |
31
|
|
|
private $reservedWords = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @constructor |
35
|
|
|
*/ |
36
|
1 |
|
final public function __construct() |
37
|
|
|
{ |
38
|
1 |
|
$this->logger = new NullLogger(); |
39
|
|
|
|
40
|
1 |
|
$this->initialised = $this->initialise(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Initialises the Agent instance. |
45
|
|
|
* |
46
|
|
|
* Note that you *cannot* use the Logger in this method. When this method is |
47
|
|
|
* called, only a NullLogger instance will have been set - all calls to this |
48
|
|
|
* instance are discarded. Throw an exception (with caution) instead. |
49
|
|
|
* |
50
|
|
|
* @return bool |
51
|
|
|
*/ |
52
|
|
|
abstract protected function initialise(): bool; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* This method serves as a proxy for calling functions for Agents. |
56
|
|
|
* |
57
|
|
|
* Extending classes must ensure that the arguments passed to the function |
58
|
|
|
* are correct, as this method will just pass through all arguments. |
59
|
|
|
* |
60
|
|
|
* Returns the result of the called function (which may be `false`), or `false` |
61
|
|
|
* on failure. |
62
|
|
|
* |
63
|
|
|
* @param string $name |
64
|
|
|
* @param array $arguments |
65
|
|
|
* @return mixed|false |
66
|
|
|
*/ |
67
|
2 |
|
public function __call(string $name, array $arguments = []) |
68
|
|
|
{ |
69
|
2 |
|
if (!$this->initialised) { |
70
|
1 |
|
$this->logger->info('[APM] Client was not initialised.'); |
|
|
|
|
71
|
1 |
|
return false; |
72
|
|
|
} |
73
|
|
|
|
74
|
1 |
|
return $this->proxyFunctionCall($name, $arguments); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param AgentInterface $agent |
79
|
|
|
* @return TransactionInterface |
80
|
|
|
*/ |
81
|
1 |
|
final public function createTransaction(AgentInterface $agent): TransactionInterface |
82
|
|
|
{ |
83
|
1 |
|
return new Transaction($agent); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @inheritDoc |
88
|
|
|
*/ |
89
|
7 |
|
public function isReservedWord(string $word): bool |
90
|
|
|
{ |
91
|
7 |
|
return in_array(trim($word), $this->reservedWords, true); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @inheritDoc |
96
|
|
|
*/ |
97
|
1 |
|
final public function isSupported(): bool |
98
|
|
|
{ |
99
|
1 |
|
return $this->initialised; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string ...$word |
104
|
|
|
* @return void |
105
|
|
|
*/ |
106
|
7 |
|
final public function reserveWords(string ...$word): void |
107
|
|
|
{ |
108
|
7 |
|
$this->reservedWords = $word; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param string $word |
113
|
|
|
* @throws InvalidArgumentException |
114
|
|
|
* @return void |
115
|
|
|
*/ |
116
|
12 |
|
final protected function guardIsNotReservedWord(string $word): void |
117
|
|
|
{ |
118
|
12 |
|
if ($this->isReservedWord($word)) { |
119
|
10 |
|
throw new InvalidArgumentException('Cannot use reserved word "' . $word . '" as metric name.'); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.