From 3eb2a91940fadb4313f6911f44aada0cebb5280e Mon Sep 17 00:00:00 2001 From: Andrey Savchenko Date: Mon, 17 Dec 2018 18:16:35 +0200 Subject: [PATCH] Added backtraces to HTTP calls. Fixes #16 --- CHANGELOG.md | 2 +- src/Record/Http_Record_Collector.php | 24 ++++++++++++++++++++++++ tests/Record/HttpTest.php | 4 ++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cf11eb4..18a635e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## Unreleased ### Added -- call backtraces to SQL records +- call backtraces to SQL records and HTTP calls - new format of hook events - sidebar tracking diff --git a/src/Record/Http_Record_Collector.php b/src/Record/Http_Record_Collector.php index 6176013..b7e0399 100644 --- a/src/Record/Http_Record_Collector.php +++ b/src/Record/Http_Record_Collector.php @@ -3,13 +3,21 @@ declare( strict_types=1 ); namespace Rarst\Laps\Record; +use Rarst\Laps\Formatter\Backtrace_Formatter; use Symfony\Component\Stopwatch\Stopwatch; +use Symfony\Component\Stopwatch\StopwatchEvent; /** * Captures time of network requests made with HTTP API. */ class Http_Record_Collector extends Stopwatch_Record_Collector { + /** @var Backtrace_Formatter */ + private $formatter; + + /** @var array */ + private $callers = []; + /** * @param Stopwatch $stopwatch Stopwatch instance. */ @@ -17,6 +25,8 @@ class Http_Record_Collector extends Stopwatch_Record_Collector { parent::__construct( $stopwatch ); + $this->formatter = new Backtrace_Formatter(); + add_action( 'pre_http_request', [ $this, 'pre_http_request' ], 10, 3 ); add_action( 'http_api_debug', [ $this, 'http_api_debug' ], 10, 5 ); } @@ -33,6 +43,7 @@ class Http_Record_Collector extends Stopwatch_Record_Collector { public function pre_http_request( bool $false, array $args, string $url ): bool { $this->start( $url, 'http' ); + $this->callers[ $url ] = wp_debug_backtrace_summary( __CLASS__, 5, true ); return $false; } @@ -56,4 +67,17 @@ class Http_Record_Collector extends Stopwatch_Record_Collector { return $response; } + + /** + * @param string $name Event name. + * @param StopwatchEvent $event Stopwatch event instance. + * + * @return Stopwatch_Record + */ + public function transform( string $name, StopwatchEvent $event ): Stopwatch_Record { + + $backtrace = $this->formatter->format( $this->callers[ $name ] ); + + return new Stopwatch_Record( $name, $event, implode( ' → ', $backtrace ) ); + } } diff --git a/tests/Record/HttpTest.php b/tests/Record/HttpTest.php index 38e104a..ad615ce 100644 --- a/tests/Record/HttpTest.php +++ b/tests/Record/HttpTest.php @@ -2,6 +2,7 @@ namespace Rarst\Laps\Tests\Record; +use Brain\Monkey\Functions; use Rarst\Laps\Record\Http_Record_Collector; use Rarst\Laps\Tests\LapsTestCase; use Symfony\Component\Stopwatch\Stopwatch; @@ -10,6 +11,9 @@ class HttpTest extends LapsTestCase { public function testCollector() { + Functions\expect( 'wp_normalize_path' )->zeroOrMoreTimes()->andReturnFirstArg(); + Functions\expect( 'wp_debug_backtrace_summary' )->zeroOrMoreTimes()->andReturn( [] ); + $stopwatch = new Stopwatch(); $url = 'https://example.com/'; $collector = new Http_Record_Collector( $stopwatch );