diff --git a/src/Events/Request/Request.php b/src/Events/Request/Request.php index b047156d..c6235d8f 100644 --- a/src/Events/Request/Request.php +++ b/src/Events/Request/Request.php @@ -10,6 +10,7 @@ use Scoutapm\Events\Span\Span; use Scoutapm\Events\Tag\TagRequest; use Scoutapm\Helper\Backtrace; +use Scoutapm\Helper\MemoryUsage; use Scoutapm\Helper\Timer; /** @internal */ @@ -29,12 +30,19 @@ class Request implements CommandWithChildren /** @var RequestId */ private $id; + /** @var MemoryUsage */ + private $startMemory; + + /** @var MemoryUsage|null */ + private $stopMemory; + /** @throws Exception */ public function __construct() { $this->id = RequestId::new(); - $this->timer = new Timer(); + $this->timer = new Timer(); + $this->startMemory = MemoryUsage::record(); $this->currentCommand = $this; } @@ -42,6 +50,7 @@ public function __construct() public function stop(?float $overrideTimestamp = null) : void { $this->timer->stop($overrideTimestamp); + $this->stopMemory = MemoryUsage::record(); } /** @throws Exception */ @@ -105,6 +114,7 @@ public function jsonSerialize() : array 'StartRequest' => [ 'request_id' => $this->id->toString(), 'timestamp' => $this->timer->getStart(), + 'memory_usage' => $this->startMemory, ], ]; @@ -118,6 +128,7 @@ public function jsonSerialize() : array 'FinishRequest' => [ 'request_id' => $this->id->toString(), 'timestamp' => $this->timer->getStop(), + 'memory_usage' => $this->stopMemory, ], ]; diff --git a/src/Events/Span/Span.php b/src/Events/Span/Span.php index d3560de2..8cf8b6b3 100644 --- a/src/Events/Span/Span.php +++ b/src/Events/Span/Span.php @@ -10,6 +10,7 @@ use Scoutapm\Connector\CommandWithParent; use Scoutapm\Events\Request\RequestId; use Scoutapm\Events\Tag\TagSpan; +use Scoutapm\Helper\MemoryUsage; use Scoutapm\Helper\Timer; use function array_filter; @@ -34,6 +35,12 @@ class Span implements CommandWithParent, CommandWithChildren /** @var Timer */ private $timer; + /** @var MemoryUsage */ + private $startMemory; + + /** @var MemoryUsage|null */ + private $stopMemory; + /** @throws Exception */ public function __construct(CommandWithChildren $parent, string $name, RequestId $requestId, ?float $override = null) { @@ -44,7 +51,8 @@ public function __construct(CommandWithChildren $parent, string $name, RequestId $this->name = $name; $this->requestId = $requestId; - $this->timer = new Timer($override); + $this->timer = new Timer($override); + $this->startMemory = MemoryUsage::record(); } public function id() : SpanId @@ -64,6 +72,7 @@ public function parent() : CommandWithChildren */ public function stop(?float $override = null) : void { + $this->stopMemory = MemoryUsage::record(); $this->timer->stop($override); } @@ -136,6 +145,7 @@ public function jsonSerialize() : array 'parent_id' => $this->parent instanceof self ? $this->parent->id->toString() : null, 'operation' => $this->name, 'timestamp' => $this->getStartTime(), + 'memory_usage' => $this->startMemory, ], ]; @@ -150,6 +160,7 @@ public function jsonSerialize() : array 'request_id' => $this->requestId->toString(), 'span_id' => $this->id->toString(), 'timestamp' => $this->getStopTime(), + 'memory_usage' => $this->stopMemory, ], ];