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 95
/
Copy pathEventBean.php
245 lines (219 loc) · 6.05 KB
/
EventBean.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
<?php
namespace PhilKra\Events;
use Ramsey\Uuid\Uuid;
/**
*
* EventBean for occuring events such as Excpetions or Transactions
*
*/
class EventBean
{
/**
* UUID
*
* @var string
*/
private $id;
/**
* Error occurred on Timestamp
*
* @var string
*/
private $timestamp;
/**
* Event Metadata
*
* @var array
*/
private $meta = [
'result' => 200,
'type' => 'generic'
];
/**
* Extended Contexts such as Custom and/or User
*
* @var array
*/
private $contexts = [
'user' => [],
'custom' => [],
'env' => [],
'tags' => [],
'response' => [
'finished' => true,
'headers_sent' => true,
'status_code' => 200,
],
];
/**
* Init the Event with the Timestamp and UUID
*
* @link https://github.com/philkra/elastic-apm-php-agent/issues/3
*
* @param array $contexts
*/
public function __construct(array $contexts)
{
// Generate Random UUID
$this->id = Uuid::uuid4()->toString();
// Merge Initial Context
$this->contexts = array_merge($this->contexts, $contexts);
// Get UTC timestamp of Now
$timestamp = \DateTime::createFromFormat('U.u', sprintf('%.6F', microtime(true)));
$timestamp->setTimeZone(new \DateTimeZone('UTC'));
$this->timestamp = $timestamp->format('Y-m-d\TH:i:s.u\Z');
}
/**
* Get the Event Id
*
* @return string
*/
public function getId() : string
{
return $this->id;
}
/**
* Get the Event's Timestamp
*
* @return string
*/
public function getTimestamp() : string
{
return $this->timestamp;
}
/**
* Set the Transaction Meta data
*
* @param array $meta
*
* @return void
*/
final public function setMeta(array $meta)
{
$this->meta = array_merge($this->meta, $meta);
}
/**
* Set Meta data of User Context
*
* @param array $userContext
*/
final public function setUserContext(array $userContext)
{
$this->contexts['user'] = array_merge($this->contexts['user'], $userContext);
}
/**
* Set custom Meta data for the Transaction in Context
*
* @param array $customContext
*/
final public function setCustomContext(array $customContext)
{
$this->contexts['custom'] = array_merge($this->contexts['custom'], $customContext);
}
/**
* Set Transaction Response
*
* @param array $response
*/
final public function setResponse(array $response)
{
$this->contexts['response'] = array_merge($this->contexts['response'], $response);
}
/**
* Set Tags for this Transaction
*
* @param array $tags
*/
final public function setTags(array $tags)
{
$this->contexts['tags'] = array_merge($this->contexts['tags'], $tags);
}
/**
* Get Type defined in Meta
*
* @return string
*/
final protected function getMetaType() : string
{
return $this->meta['type'];
}
/**
* Get the Result of the Event from the Meta store
*
* @return string
*/
final protected function getMetaResult() : string
{
return (string)$this->meta['result'];
}
/**
* Get the Environment Variables
*
* @link http://php.net/manual/en/reserved.variables.server.php
* @link https://github.com/philkra/elastic-apm-php-agent/issues/27
*
* @return array
*/
final protected function getEnv() : array
{
$env = $this->contexts['env'];
return ( empty( $env ) === true )
? $_SERVER
: array_intersect_key($_SERVER, array_flip($env));
}
/**
* Get the Events Context
*
* @link https://www.elastic.co/guide/en/apm/server/current/transaction-api.html#transaction-context-schema
*
* @return array
*/
final protected function getContext() : array
{
$headers = getallheaders();
$http_or_https = isset($_SERVER['HTTPS']) ? 'https' : 'http';
// Build Context Stub
$SERVER_PROTOCOL = $_SERVER['SERVER_PROTOCOL'] ?? '';
$context = [
'request' => [
'http_version' => substr($SERVER_PROTOCOL, strpos($SERVER_PROTOCOL, '/')),
'method' => $_SERVER['REQUEST_METHOD'] ?? 'cli',
'socket' => [
'remote_address' => $_SERVER['REMOTE_ADDR'] ?? '',
'encrypted' => isset($_SERVER['HTTPS'])
],
'response' => $this->contexts['response'],
'url' => [
'protocol' => $http_or_https,
'hostname' => $_SERVER['SERVER_NAME'] ?? '',
'port' => $_SERVER['SERVER_PORT'] ?? '',
'pathname' => $_SERVER['SCRIPT_NAME'] ?? '',
'search' => '?' . (($_SERVER['QUERY_STRING'] ?? '') ?? ''),
'full' => $http_or_https . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']
],
'headers' => [
'user-agent' => $headers['User-Agent'] ?? '',
'cookie' => $headers['Cookie'] ?? ''
],
'env' => $this->getEnv(),
]
];
// Add Cookies Map
if (empty($_COOKIE) === false) {
$context['request']['cookies'] = $_COOKIE;
}
// Add User Context
if (empty($this->contexts['user']) === false) {
$context['user'] = $this->contexts['user'];
}
// Add Custom Context
if (empty($this->contexts['custom']) === false) {
$context['custom'] = $this->contexts['custom'];
}
// Add Tags Context
if (empty($this->contexts['tags']) === false) {
$context['tags'] = $this->contexts['tags'];
}
return $context;
}
}