Skip to content

Commit

Permalink
Record and serialize memory usage before/after requests
Browse files Browse the repository at this point in the history
  • Loading branch information
asgrim committed Oct 10, 2019
1 parent 32a7e90 commit 744e532
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/Events/Request/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -29,19 +30,27 @@ 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;
}

public function stop(?float $overrideTimestamp = null) : void
{
$this->timer->stop($overrideTimestamp);
$this->stopMemory = MemoryUsage::record();
}

/** @throws Exception */
Expand Down Expand Up @@ -105,6 +114,7 @@ public function jsonSerialize() : array
'StartRequest' => [
'request_id' => $this->id->toString(),
'timestamp' => $this->timer->getStart(),
'memory_usage' => $this->startMemory,
],
];

Expand All @@ -118,6 +128,7 @@ public function jsonSerialize() : array
'FinishRequest' => [
'request_id' => $this->id->toString(),
'timestamp' => $this->timer->getStop(),
'memory_usage' => $this->stopMemory,
],
];

Expand Down
13 changes: 12 additions & 1 deletion src/Events/Span/Span.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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)
{
Expand All @@ -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
Expand All @@ -64,6 +72,7 @@ public function parent() : CommandWithChildren
*/
public function stop(?float $override = null) : void
{
$this->stopMemory = MemoryUsage::record();
$this->timer->stop($override);
}

Expand Down Expand Up @@ -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,
],
];

Expand All @@ -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,
],
];

Expand Down

0 comments on commit 744e532

Please sign in to comment.