Skip to content

Commit

Permalink
Register shutdown function to set header (#15)
Browse files Browse the repository at this point in the history
Fix header is set on `Allowed memory size of X bytes exhausted` errors
  • Loading branch information
convenient authored Oct 24, 2022
1 parent 3060006 commit 49d2e2b
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 2 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ cache:

before_install:
- if [ ! "$TRAVIS_PULL_REQUEST" = "false" ]; then git branch; git branch -D "$TRAVIS_BRANCH" || true; git checkout -b "$TRAVIS_BRANCH"; fi
- phpenv config-rm xdebug.ini || true
- composer self-update --2
install:
- export COMPOSER_MEMORY_LIMIT=-1
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="dev/phpunit-unit-bootstrap.php" backupGlobals="false"
backupStaticAttributes="false" colors="true" verbose="true" convertErrorsToExceptions="true"
convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false"
convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="true"
stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<testsuites>
<testsuite name="Ampersand Module Unit Test Suite">
Expand Down
32 changes: 32 additions & 0 deletions src/Service/CorrelationIdentifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
declare(strict_types=1);
namespace Ampersand\LogCorrelationId\Service;

use Ampersand\LogCorrelationId\HttpResponse\HeaderProvider\LogCorrelationIdHeader as Header;
use Ampersand\LogCorrelationId\Service\CorrelationIdentifier\Storage;
use Magento\Framework\App\Request\Http as HttpRequest;

Expand All @@ -23,6 +24,8 @@ public function __construct(string $identifierKey, string $headerInput)
{
Storage::setKey($identifierKey);
$this->headerInput = $headerInput;
// phpcs:ignore Magento2.Functions.DiscouragedFunction.Discouraged
register_shutdown_function([$this, 'shutDownFunction']);
}

/**
Expand Down Expand Up @@ -70,4 +73,33 @@ public function getIdentifierKey(): string
{
return Storage::getKey();
}

/**
* A shutdown function to ensure the correlation ID header is added for every type of erroring request
*
* This was added to catch "Allowed memory size of X bytes exhausted" type errors
*
* @see \Magento\Framework\Webapi\ErrorProcessor::registerShutdownFunction
*
* @return void
*/
public function shutDownFunction()
{
// phpcs:ignore Magento2.Functions.DiscouragedFunction.Discouraged
if (headers_sent()) {
return;
}

// phpcs:ignore Magento2.Functions.DiscouragedFunction.Discouraged
$headerAlreadyDefined = array_filter(headers_list(), function ($header) {
return (stripos($header, Header::X_LOG_CORRELATION_ID) !== false);
});

if (!empty($headerAlreadyDefined)) {
return;
}

// phpcs:ignore Magento2.Functions.DiscouragedFunction.Discouraged
header(Header::X_LOG_CORRELATION_ID . ': ' . $this->getIdentifierValue());
}
}
27 changes: 27 additions & 0 deletions src/Test/Unit/Service/CorrelationIdentifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
declare(strict_types=1);
namespace Ampersand\LogCorrelationId\Test\Unit\Service;

use Ampersand\LogCorrelationId\HttpResponse\HeaderProvider\LogCorrelationIdHeader as Header;
use Ampersand\LogCorrelationId\Service\CorrelationIdentifier;
use Magento\Framework\App\Request\Http as HttpRequest;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -53,6 +54,32 @@ public function testGetIdentifier()
);
}

/**
*/
public function testIdentifierHeaderIsSetOnShutdown()
{
$httpRequest = $this->createMock(HttpRequest::class);
$httpRequest->expects($this->any())
->method('getHeader')
->willReturn(false);

$service = $this->createService();
$service->init($httpRequest, true);

// https://stackoverflow.com/a/39892373/4354325
$headerIsNotSet = empty(array_filter(xdebug_get_headers(), function ($header) {
return (stripos($header, Header::X_LOG_CORRELATION_ID) !== false);
}));
$this->assertTrue($headerIsNotSet, 'The correlation header should not yet be set');

$service->shutDownFunction();

$headerIsSet = !empty(array_filter(xdebug_get_headers(), function ($header) {
return (stripos($header, Header::X_LOG_CORRELATION_ID) !== false);
}));
$this->assertTrue($headerIsSet, 'The correlation header should be set');
}

public function testGetIdentifierFromHeader()
{
$httpRequest = $this->createMock(HttpRequest::class);
Expand Down

0 comments on commit 49d2e2b

Please sign in to comment.