1 parent
eb573e143e
commit
3eb2a91940
3 files changed
+29
-1
No files matched your search
@@ -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 ) );
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user