diff --git a/composer.json b/composer.json index cf08955..ddfc153 100644 --- a/composer.json +++ b/composer.json @@ -20,11 +20,12 @@ "ramsey/uuid": "^3.8", "ralouphie/getallheaders": "^3.0", "ircmaxell/random-lib": "^1.2", - "guzzlehttp/guzzle": "^6.3", - "subzerobo/sabalim-action-wrapper": "^1.0" + "guzzlehttp/guzzle": "^6.5", + "subzerobo/sabalim-action-wrapper": "^1.0", + "psr/http-server-handler": "^1.0" }, "require-dev": { - "phpunit/phpunit": "6.*" + "phpunit/phpunit": "11.3.*" }, "suggest": { "ext-xdebug": "Required for processing of request headers", diff --git a/src/Middlewares/GuzzleAPMMiddleware.php b/src/Middlewares/GuzzleAPMMiddleware.php index 4fb5fcf..a42768d 100644 --- a/src/Middlewares/GuzzleAPMMiddleware.php +++ b/src/Middlewares/GuzzleAPMMiddleware.php @@ -8,7 +8,10 @@ namespace Subzerobo\ElasticApmPhpAgent\Middlewares; -use Psr\Http\Message\RequestInterface; +use Psr\Http\Message\ResponseInterface as Response; +use Psr\Http\Message\ServerRequestInterface as Request; +use Psr\Http\Server\RequestHandlerInterface as RequestHandler; + use Subzerobo\ElasticApmPhpAgent\ActionWrappers\APMHandlerAbstract; class GuzzleAPMMiddleware @@ -23,35 +26,17 @@ public function __construct(APMHandlerAbstract $actionWrapper) $this->actionWrapper = $actionWrapper; } - public function __invoke(callable $handler) { - - $apmActionWrapper = $this->actionWrapper; - - $before = function($request, $options) use ($apmActionWrapper) { - $type = $options['type'] ?? "General"; - $apmActionWrapper->handleBefore($request,$type,[]); - //var_dump( "BTap " . microtime(true) ); - }; - - $after = function($request,$option,$response) use ($apmActionWrapper) { - $type = $options['type'] ?? "General"; - $response->then(function (\Psr\Http\Message\ResponseInterface $response) use($apmActionWrapper,$type) { - $apmActionWrapper->handleAfter($response,$type,[]); - }); - }; - - // Tap Function of Guzzle - - return function ($request, array $options) use ($handler, $before, $after) { - if ($before) { - $before($request, $options); - } - $response = $handler($request, $options); - if ($after) { - $after($request, $options, $response); - } - return $response; - }; + public function __invoke(Request $request, RequestHandler $requestHandler): Response + { + $type = $options['type'] ?? "General"; + + $this->actionWrapper->handleBefore($request,$type,[]); + + $response = $requestHandler->handle($request); + + $this->actionWrapper->handleAfter($response,$type,[]); + + return $response; } } \ No newline at end of file diff --git a/src/Middlewares/PSR7Middleware.php b/src/Middlewares/PSR7Middleware.php index 766cb47..c17a6c5 100644 --- a/src/Middlewares/PSR7Middleware.php +++ b/src/Middlewares/PSR7Middleware.php @@ -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 * @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; } } \ No newline at end of file diff --git a/src/Wrappers/SpanEvent.php b/src/Wrappers/SpanEvent.php index 37010bb..3dc87eb 100644 --- a/src/Wrappers/SpanEvent.php +++ b/src/Wrappers/SpanEvent.php @@ -131,8 +131,6 @@ public function getSpanID() */ public function setSpanName(string $name) { - $name = strlen($name) > 1024 ? substr($name, 0, 1021).'...' : $name; - $this->span->setName($name); } @@ -555,4 +553,4 @@ public function getSpanObject() return $this->span; } -} +} \ No newline at end of file diff --git a/src/Wrappers/Traits/EventTrait.php b/src/Wrappers/Traits/EventTrait.php index 4a9210f..a4b6102 100644 --- a/src/Wrappers/Traits/EventTrait.php +++ b/src/Wrappers/Traits/EventTrait.php @@ -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 - * @since 2020-12-09 14:43 - */ - final protected function clean_non_chars($string) { - return preg_replace('/[^A-Za-z0-9\-\;\(\)\,\ \/\.]/', '', $string); // Removes special chars. - } -} +} \ No newline at end of file diff --git a/src/Wrappers/TransactionEvent.php b/src/Wrappers/TransactionEvent.php index c6d87b2..bf4ee26 100644 --- a/src/Wrappers/TransactionEvent.php +++ b/src/Wrappers/TransactionEvent.php @@ -75,7 +75,7 @@ public function start() * @see https://github.com/elastic/apm-server/blob/v6.7.1/docs/spec/timestamp_epoch.json */ $this->setTimestamp(); - $this->timestamp = intval(microtime(true) * 1000000); + $this->timestamp = microtime(true) * 1000000; $this->transaction->setTimestamp($this->getTimestamp()); $this->timer->start(); } @@ -274,4 +274,4 @@ public function getProtoBufTransaction() { } -} +} \ No newline at end of file