Skip to content
This repository was archived by the owner on Nov 29, 2021. It is now read-only.

Add possibility to setup custom request #74

Merged
merged 1 commit into from
May 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Agent.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public function stopTransaction(string $name, array $meta = [])
*
* @param string $name
*
* @return void
* @return Transaction
*/
public function getTransaction(string $name)
{
Expand Down
88 changes: 55 additions & 33 deletions src/Events/EventBean.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class EventBean
* @var array
*/
private $contexts = [
'request' => [],
'user' => [],
'custom' => [],
'env' => [],
Expand Down Expand Up @@ -145,6 +146,58 @@ final public function setTags(array $tags)
$this->contexts['tags'] = array_merge($this->contexts['tags'], $tags);
}

/**
* Set Transaction Request
*
* @param array $request
*/
final public function setRequest(array $request)
{
$this->contexts['request'] = array_merge($this->contexts['request'], $request);
}

/**
* Generate request data
*
* @return array
*/
final public function generateRequest(): array
{
$headers = getallheaders();
$http_or_https = isset($_SERVER['HTTPS']) ? 'https' : 'http';

// Build Context Stub
$SERVER_PROTOCOL = $_SERVER['SERVER_PROTOCOL'] ?? '';
$remote_address = $_SERVER['REMOTE_ADDR'] ?? '';
if (array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER) === true) {
$remote_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
}

return [
'http_version' => substr($SERVER_PROTOCOL, strpos($SERVER_PROTOCOL, '/')),
'method' => $_SERVER['REQUEST_METHOD'] ?? 'cli',
'socket' => [
'remote_address' => $remote_address,
'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' => isset($_SERVER['HTTP_HOST']) ? $http_or_https . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] : '',
],
'headers' => [
'user-agent' => $headers['User-Agent'] ?? '',
'cookie' => $this->getCookieHeader($headers['Cookie'] ?? ''),
],
'env' => (object)$this->getEnv(),
'cookies' => (object)$this->getCookies(),
];
}

/**
* Get Type defined in Meta
*
Expand Down Expand Up @@ -224,39 +277,8 @@ final protected function getCookieHeader(string $cookieHeader) : string
*/
final protected function getContext() : array
{
$headers = getallheaders();
$http_or_https = isset($_SERVER['HTTPS']) ? 'https' : 'http';

// Build Context Stub
$SERVER_PROTOCOL = $_SERVER['SERVER_PROTOCOL'] ?? '';
$remote_address = $_SERVER['REMOTE_ADDR'] ?? '';
if (array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER) === true) {
$remote_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
$context = [
'request' => [
'http_version' => substr($SERVER_PROTOCOL, strpos($SERVER_PROTOCOL, '/')),
'method' => $_SERVER['REQUEST_METHOD'] ?? 'cli',
'socket' => [
'remote_address' => $remote_address,
'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' => isset($_SERVER['HTTP_HOST']) ? $http_or_https . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] : '',
],
'headers' => [
'user-agent' => $headers['User-Agent'] ?? '',
'cookie' => $this->getCookieHeader($headers['Cookie'] ?? ''),
],
'env' => (object)$this->getEnv(),
'cookies' => (object)$this->getCookies(),
]
$context = [
'request' => empty($this->contexts['request']) ? $this->generateRequest() : $this->contexts['request']
];

// Add User Context
Expand Down