Skip to content

Commit

Permalink
feat(backend): Add span based metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
cleptric committed Jul 5, 2024
1 parent 94b02a1 commit 76b5ac2
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/Controller/EventsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Cake\Controller\Controller;
use Cake\Http\Response;
use Sentry\Metrics\MetricsUnit;
use Sentry\SentrySdk;
use function Sentry\metrics;

class EventsController extends Controller
Expand Down Expand Up @@ -50,6 +51,14 @@ public function index(): Response
],
);

$span = SentrySdk::getCurrentHub()->getSpan();
if ($span !== null) {
$span->setData([
'gibpotato.potatoes.event_processing_time' => microtime(true) - $startTimestamp,
'gibpotato.potatoes.event_size' => mb_strlen(serialize($this->request->getData()), '8bit'),
]);
}

return $this->response
->withType('json')
->withStatus(200);
Expand Down
6 changes: 5 additions & 1 deletion src/Middleware/SentryMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface

SentrySdk::getCurrentHub()->setSpan($transaction);

$transaction->setHttpStatus($response->getStatusCode());
$transaction
->setHttpStatus($response->getStatusCode())
->setData([
'gibpotato.gcp.mem_peak_usage' => memory_get_peak_usage(false),
]);

metrics()->distribution(
key: 'gibpotato.gcp.mem_peak_usage',
Expand Down
8 changes: 8 additions & 0 deletions src/Service/AwardService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Model\Entity\User;
use Cake\ORM\Locator\LocatorAwareTrait;
use Sentry\Metrics\MetricsUnit;
use Sentry\SentrySdk;
use function Sentry\metrics;

class AwardService
Expand Down Expand Up @@ -67,5 +68,12 @@ private function gibToUser(
value: $event->amount,
unit: MetricsUnit::custom('potato'),
);

$span = SentrySdk::getCurrentHub()->getSpan();
if ($span !== null) {
$span->setData([
'gibpotato.potatoes.given_out' => $event->amount,
]);
}
}
}
8 changes: 8 additions & 0 deletions src/Service/QuickWinService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Model\Entity\User;
use Cake\ORM\Locator\LocatorAwareTrait;
use Sentry\Metrics\MetricsUnit;
use Sentry\SentrySdk;
use function Sentry\metrics;

class QuickWinService
Expand Down Expand Up @@ -48,6 +49,13 @@ public function store(
unit: MetricsUnit::custom('tags'),
);

$span = SentrySdk::getCurrentHub()->getSpan();
if ($span !== null) {
$span->setData([
'gibpotato.message.quick_win' => 1,
]);
}

return true;
}
}

0 comments on commit 76b5ac2

Please sign in to comment.