-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from heismehrab/v2
refactor: Update middlewares and dependencies to be compatible with PHP 8.3
- Loading branch information
Showing
6 changed files
with
43 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,15 +9,14 @@ | |
namespace Subzerobo\ElasticApmPhpAgent\Middlewares; | ||
|
||
|
||
use Psr\Container\ContainerInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Message\ResponseInterface as Response; | ||
use Psr\Http\Message\ServerRequestInterface as Request; | ||
use Psr\Http\Server\RequestHandlerInterface as RequestHandler; | ||
|
||
use Subzerobo\ElasticApmPhpAgent\ApmAgent; | ||
use Subzerobo\ElasticApmPhpAgent\Factories\DefaultTransactionNameFactoryAbstract; | ||
use Subzerobo\ElasticApmPhpAgent\Factories\TransactionNameGeneratorInterface; | ||
|
||
/** | ||
* Add this middleware as the first middleware to not loose any span | ||
* Add this middleware as the first middleware to not lose any span | ||
* | ||
* Class PSR7Middleware | ||
* @package Subzerobo\ElasticApmPhpAgent\Middlewares | ||
|
@@ -38,27 +37,24 @@ public function __construct($container, ApmAgent $apmAgent) | |
} | ||
|
||
/** | ||
* @param ServerRequestInterface $request | ||
* @param ResponseInterface $response | ||
* @param callable|null $next | ||
* @param \Psr\Http\Message\ServerRequestInterface $request | ||
* @param \Psr\Http\Server\RequestHandlerInterface $requestHandler | ||
* | ||
* @return ResponseInterface | ||
* @throws \GuzzleHttp\Exception\GuzzleException | ||
* @return \Psr\Http\Message\ResponseInterface | ||
* @throws \Subzerobo\ElasticApmPhpAgent\Exceptions\DuplicateTransactionNameException | ||
* @throws \Subzerobo\ElasticApmPhpAgent\Exceptions\TimerNotStartedException | ||
* @throws \Subzerobo\ElasticApmPhpAgent\Exceptions\TimerNotStoppedException | ||
* @throws \Subzerobo\ElasticApmPhpAgent\Exceptions\UnknownTransactionException | ||
* @throws \Subzerobo\ElasticApmPhpAgent\Exceptions\UnknownTransactionException|\GuzzleHttp\Exception\GuzzleException | ||
* @author alikaviani <[email protected]> | ||
* @since 2019-06-15 09:55 | ||
*/ | ||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next=null) | ||
public function __invoke(Request $request, RequestHandler $requestHandler): Response | ||
{ | ||
// Start Transaction | ||
$transactionEvent = $this->apmAgent->startTransaction(); | ||
|
||
// TODO: Implement __invoke() method. | ||
$response = $next($request, $response); | ||
|
||
$response = $requestHandler->handle($request); | ||
|
||
// Set and Stop Transaction | ||
$transactionEvent->setResult("HTTP " . $response->getStatusCode()); | ||
|
@@ -69,9 +65,12 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res | |
'headers_sent' => true, | ||
'status_code' => $response->getStatusCode(),] | ||
); | ||
|
||
$transactionEvent->stop(); | ||
|
||
$this->apmAgent->renameTransaction(); | ||
$this->apmAgent->send(); | ||
|
||
return $response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,7 +69,7 @@ private function initContext(EventSharedData $sharedData) : Context | |
{ | ||
// Generate Random UUIDs | ||
$this->id = UID::Generate(16); //Uuid::uuid4()->toString(); | ||
$this->trace_id = $this->trace_id = $_SERVER['HTTP_ELASTIC_APM_TRACEPARENT'] ? : UID::Generate(16); //Uuid::uuid4()->toString(); | ||
$this->trace_id = UID::Generate(16); //Uuid::uuid4()->toString(); | ||
|
||
// Set Shared Context Variable for further use | ||
$this->sharedData = $sharedData; | ||
|
@@ -278,19 +278,17 @@ final public function setRequest() { | |
], | ||
'headers' => [ | ||
'content_type' => $headers['Content-Type'] ?? '', | ||
'user_agent' => $this->clean_non_chars($headers['User-Agent']) ?? '', | ||
'user_agent' => $headers['User-Agent'] ?? '', | ||
'cookie' => $this->getCookieHeader($headers['Cookie'] ?? ''), | ||
], | ||
'env' => $this->getEnv(), | ||
'cookies' => $this->getCookies(), | ||
]; | ||
|
||
$ctxRequest = new Context\Request(); | ||
try { | ||
$ctxRequest->mergeFromJsonString(json_encode($contextRequestArr)); | ||
$this->context->setRequest($ctxRequest); | ||
}catch(\Exception $ex){ | ||
} | ||
$ctxRequest->mergeFromJsonString(json_encode($contextRequestArr)); | ||
|
||
$this->context->setRequest($ctxRequest); | ||
} | ||
|
||
/** | ||
|
@@ -319,11 +317,6 @@ final protected function getEnv() | |
? $_SERVER | ||
: array_intersect_key($_SERVER, array_flip($envMask)); | ||
|
||
array_walk($env, function(&$value) { | ||
if (is_array($value)) { | ||
$value = json_encode($value); | ||
} | ||
}); | ||
return (object) $env; | ||
} | ||
|
||
|
@@ -359,19 +352,6 @@ final protected function getCookieHeader(string $cookieHeader) : string | |
// Returns an empty string if cookies are masked. | ||
return empty($cookieMask) ? $cookieHeader : ''; | ||
} | ||
|
||
/** | ||
* Clean Bad Characters | ||
* | ||
* @param string $string | ||
* | ||
* @return string | ||
* @author alikaviani <[email protected]> | ||
* @since 2020-12-09 14:43 | ||
*/ | ||
final protected function clean_non_chars($string) { | ||
return preg_replace('/[^A-Za-z0-9\-\;\(\)\,\ \/\.]/', '', $string); // Removes special chars. | ||
} | ||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters