Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(backend): Add span based metrics #195

Merged
merged 3 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
15 changes: 15 additions & 0 deletions src/Middleware/SentryUserMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Sentry\Metrics\MetricsUnit;
use Sentry\SentrySdk;
use Sentry\State\Scope;
use function Sentry\metrics;

/**
* SentryUser middleware
Expand All @@ -32,6 +34,19 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
'id' => $user->id,
'username' => $user->slack_name,
]);

metrics()->set(
key: 'gibpotato.users.web_ui',
value: $user->id,
unit: MetricsUnit::custom('user_id'),
);

$span = $scope->getSpan();
if ($span !== null) {
$span->setData([
'gibpotato.users.web_ui' => $user->id,
]);
}
});
}

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;
}
}
Loading