-
Notifications
You must be signed in to change notification settings - Fork 187
Fix repeated web server error log failures #4254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cgcote
wants to merge
4
commits into
master
Choose a base branch
from
fix/consume-webserver-error-log
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5df4f42
Fix repeated web server error log failures
cgcote 7c8e961
Merge branch 'master' into fix/consume-webserver-error-log
cgcote f87daee
Detect web server error log replacement via inode, not size alone
cgcote a0a383c
Merge branch 'master' into fix/consume-webserver-error-log
cgcote File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| <?php | ||
|
|
||
| namespace DDTrace\Tests\Unit; | ||
|
|
||
| use DDTrace\Tests\Common\BaseTestCase; | ||
| use DDTrace\Tests\Sapi\Sapi; | ||
| use DDTrace\Tests\WebServer; | ||
|
|
||
| final class WebServerTest extends BaseTestCase | ||
| { | ||
| private $temporaryDirectory; | ||
|
|
||
| protected function ddTearDown() | ||
| { | ||
| if ($this->temporaryDirectory) { | ||
| @unlink($this->temporaryDirectory . '/index.php'); | ||
| @unlink($this->temporaryDirectory . '/' . WebServer::ERROR_LOG_NAME); | ||
| @rmdir($this->temporaryDirectory); | ||
| } | ||
|
|
||
| parent::ddTearDown(); | ||
| } | ||
|
|
||
| public function testCheckErrorsConsumesLogEntriesOnce() | ||
| { | ||
| $server = $this->createWebServer(); | ||
| $errorLog = $this->temporaryDirectory . '/' . WebServer::ERROR_LOG_NAME; | ||
|
|
||
| file_put_contents($errorLog, "[ddtrace] [error] first error\n"); | ||
|
|
||
| $this->assertSame('[ddtrace] [error] first error', $server->checkErrors()); | ||
| $this->assertNull($server->checkErrors()); | ||
|
|
||
| file_put_contents($errorLog, "[ddtrace] [warn] second error\n", FILE_APPEND); | ||
|
|
||
| $this->assertSame('[ddtrace] [warn] second error', $server->checkErrors()); | ||
| $this->assertNull($server->checkErrors()); | ||
| } | ||
|
|
||
| public function testCheckErrorsHandlesTruncatedLog() | ||
| { | ||
| $server = $this->createWebServer(); | ||
| $errorLog = $this->temporaryDirectory . '/' . WebServer::ERROR_LOG_NAME; | ||
|
|
||
| file_put_contents($errorLog, "[ddtrace] [error] a deliberately long error\n"); | ||
| $server->checkErrors(); | ||
|
|
||
| file_put_contents($errorLog, "[ddtrace] [error] new\n"); | ||
|
|
||
| $this->assertSame('[ddtrace] [error] new', $server->checkErrors()); | ||
| $this->assertNull($server->checkErrors()); | ||
| } | ||
|
|
||
| public function testCheckErrorsHandlesReplacedLogGrownPastPreviousOffset() | ||
| { | ||
| $server = $this->createWebServer(); | ||
| $errorLog = $this->temporaryDirectory . '/' . WebServer::ERROR_LOG_NAME; | ||
|
|
||
| // Long enough that a naive size-only comparison would not detect the | ||
| // replacement once the new file grows to at least this offset. | ||
| $padding = str_repeat('x', 4096); | ||
| file_put_contents($errorLog, "[ddtrace] [error] first error $padding\n"); | ||
| $server->checkErrors(); | ||
|
|
||
| // Simulate rotation: create a new file elsewhere and rename it over the | ||
| // old path, guaranteeing a new inode, already containing more bytes than | ||
| // the previous offset. | ||
| $replacement = $errorLog . '.1'; | ||
| file_put_contents($replacement, "[ddtrace] [error] early marker in replacement\n$padding\n"); | ||
| rename($replacement, $errorLog); | ||
|
|
||
| $this->assertSame('[ddtrace] [error] early marker in replacement', $server->checkErrors()); | ||
| $this->assertNull($server->checkErrors()); | ||
| } | ||
|
|
||
| private function createWebServer() | ||
| { | ||
| $temporaryFile = tempnam(sys_get_temp_dir(), 'ddtrace-webserver-test-'); | ||
| @unlink($temporaryFile); | ||
| mkdir($temporaryFile); | ||
| $this->temporaryDirectory = $temporaryFile; | ||
|
|
||
| $indexFile = $this->temporaryDirectory . '/index.php'; | ||
| touch($indexFile); | ||
|
|
||
| $server = new WebServer($indexFile); | ||
| $sapi = new WebServerTestSapi(); | ||
| $property = new \ReflectionProperty($server, 'sapi'); | ||
| $property->setAccessible(true); | ||
| $property->setValue($server, $sapi); | ||
|
|
||
| return $server; | ||
| } | ||
| } | ||
|
|
||
| final class WebServerTestSapi implements Sapi | ||
| { | ||
| public function start() | ||
| { | ||
| } | ||
|
|
||
| public function stop() | ||
| { | ||
| } | ||
|
|
||
| public function isFastCgi() | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| public function checkErrors() | ||
| { | ||
| return null; | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.