This repository was archived by the owner on Nov 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathAgent.php
250 lines (220 loc) · 6.59 KB
/
Agent.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?php
namespace PhilKra;
use PhilKra\Events\DefaultEventFactory;
use PhilKra\Events\EventFactoryInterface;
use PhilKra\Stores\ErrorsStore;
use PhilKra\Stores\TransactionsStore;
use PhilKra\Events\Transaction;
use PhilKra\Events\Error;
use PhilKra\Helper\Timer;
use PhilKra\Helper\Config;
use PhilKra\Middleware\Connector;
use PhilKra\Exception\Transaction\DuplicateTransactionNameException;
use PhilKra\Exception\Transaction\UnknownTransactionException;
/**
*
* APM Agent
*
* @link https://www.elastic.co/guide/en/apm/server/master/transaction-api.html
*
*/
class Agent
{
/**
* Agent Version
*
* @var string
*/
const VERSION = '6.5.4';
/**
* Agent Name
*
* @var string
*/
const NAME = 'elastic-php';
/**
* Config Store
*
* @var \PhilKra\Helper\Config
*/
private $config;
/**
* Transactions Store
*
* @var \PhilKra\Stores\TransactionsStore
*/
private $transactionsStore;
/**
* Error Events Store
*
* @var \PhilKra\Stores\ErrorsStore
*/
private $errorsStore;
/**
* Apm Timer
*
* @var \PhilKra\Helper\Timer
*/
private $timer;
/**
* Common/Shared Contexts for Errors and Transactions
*
* @var array
*/
private $sharedContext = [
'user' => [],
'custom' => [],
'tags' => []
];
/**
* @var EventFactoryInterface
*/
private $eventFactory;
/**
* Setup the APM Agent
*
* @param array $config
* @param array $sharedContext Set shared contexts such as user and tags
* @param EventFactoryInterface $eventFactory Alternative factory to use when creating event objects
*
* @return void
*/
public function __construct(array $config, array $sharedContext = [], EventFactoryInterface $eventFactory = null, TransactionsStore $transactionsStore = null, ErrorsStore $errorsStore = null)
{
// Init Agent Config
$this->config = new Config($config);
// Use the custom event factory or create a default one
$this->eventFactory = $eventFactory ?? new DefaultEventFactory();
// Init the Shared Context
$this->sharedContext['user'] = $sharedContext['user'] ?? [];
$this->sharedContext['custom'] = $sharedContext['custom'] ?? [];
$this->sharedContext['tags'] = $sharedContext['tags'] ?? [];
// Let's misuse the context to pass the environment variable and cookies
// config to the EventBeans and the getContext method
// @see https://github.com/philkra/elastic-apm-php-agent/issues/27
// @see https://github.com/philkra/elastic-apm-php-agent/issues/30
$this->sharedContext['env'] = $this->config->get('env', []);
$this->sharedContext['cookies'] = $this->config->get('cookies', []);
// Initialize Event Stores
$this->transactionsStore = $transactionsStore ?? new TransactionsStore();
$this->errorsStore = $errorsStore ?? new ErrorsStore();
// Start Global Agent Timer
$this->timer = new Timer();
$this->timer->start();
}
/**
* Start the Transaction capturing
*
* @throws \PhilKra\Exception\Transaction\DuplicateTransactionNameException
*
* @param string $name
* @param array $context
*
* @return Transaction
*/
public function startTransaction(string $name, array $context = [], float $start = null): Transaction
{
// Create and Store Transaction
$this->transactionsStore->register(
$this->eventFactory->createTransaction($name, array_replace_recursive($this->sharedContext, $context), $start)
);
// Start the Transaction
$transaction = $this->transactionsStore->fetch($name);
if (null === $start) {
$transaction->start();
}
return $transaction;
}
/**
* Stop the Transaction
*
* @throws \PhilKra\Exception\Transaction\UnknownTransactionException
*
* @param string $name
* @param array $meta, Def: []
*
* @return void
*/
public function stopTransaction(string $name, array $meta = [])
{
$this->getTransaction($name)->setBacktraceLimit($this->config->get('backtraceLimit', 0));
$this->getTransaction($name)->stop();
$this->getTransaction($name)->setMeta($meta);
}
/**
* Get a Transaction
*
* @throws \PhilKra\Exception\Transaction\UnknownTransactionException
*
* @param string $name
*
* @return void
*/
public function getTransaction(string $name)
{
$transaction = $this->transactionsStore->fetch($name);
if ($transaction === null) {
throw new UnknownTransactionException($name);
}
return $transaction;
}
/**
* Register a Thrown Exception, Error, etc.
*
* @link http://php.net/manual/en/class.throwable.php
*
* @param \Throwable $thrown
* @param array $context
*
* @return void
*/
public function captureThrowable(\Throwable $thrown, array $context = [])
{
$this->errorsStore->register(
$this->eventFactory->createError($thrown, array_replace_recursive($this->sharedContext, $context))
);
}
/**
* Get the Agent Config
*
* @return \PhilKra\Helper\Config
*/
public function getConfig() : \PhilKra\Helper\Config
{
return $this->config;
}
/**
* Send Data to APM Service
*
* @link https://github.com/philkra/elastic-apm-laravel/issues/22
* @link https://github.com/philkra/elastic-apm-laravel/issues/26
*
* @return bool
*/
public function send() : bool
{
// Is the Agent enabled ?
if ($this->config->get('active') === false) {
$this->errorsStore->reset();
$this->transactionsStore->reset();
return true;
}
$connector = new Connector($this->config);
$status = true;
// Commit the Errors
if ($this->errorsStore->isEmpty() === false) {
$status = $status && $connector->sendErrors($this->errorsStore);
if ($status === true) {
$this->errorsStore->reset();
}
}
// Commit the Transactions
if ($this->transactionsStore->isEmpty() === false) {
$status = $status && $connector->sendTransactions($this->transactionsStore);
if ($status === true) {
$this->transactionsStore->reset();
}
}
return $status;
}
}