Skip to content

Commit e5e9887

Browse files
authored
Merge pull request #12056 from nanaya/sentry-metric
Add sentry tracking for slow requests
2 parents 55cb344 + 70f776b commit e5e9887

File tree

7 files changed

+211
-469
lines changed

7 files changed

+211
-469
lines changed

.env.example

+3
Original file line numberDiff line numberDiff line change
@@ -326,3 +326,6 @@ CLIENT_CHECK_VERSION=false
326326

327327
# TAGS_CACHE_DURATION=60
328328
# BEATMAP_TAGS_CACHE_DURATION=60
329+
330+
# OSU_SENTRY_MIN_LOG_DURATION_MS=500
331+
# SENTRY_TRACES_SAMPLE_RATE=

app/Http/Kernel.php

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class Kernel extends HttpKernel
1616
*/
1717
protected $middleware = [
1818
Middleware\DatadogMetrics::class,
19+
Middleware\SentryMetrics::class,
1920
];
2021

2122
protected $middlewareGroups = [

app/Http/Middleware/DatadogMetrics.php

+16-11
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,7 @@
1212

1313
class DatadogMetrics extends LaravelDatadogMiddleware
1414
{
15-
/**
16-
* Logs the duration of a specific request through the application
17-
*
18-
* @param Request $request
19-
* @param Response $response
20-
* @param float $startTime
21-
*/
22-
protected static function logDuration(Request $request, Response $response, $startTime)
15+
public static function makeLogTags(Request $request, Response $response): array
2316
{
2417
static $hostname;
2518
if (!isset($hostname)) {
@@ -29,8 +22,7 @@ protected static function logDuration(Request $request, Response $response, $sta
2922
}
3023
}
3124

32-
$duration = microtime(true) - $startTime;
33-
$tags = [
25+
return [
3426
'action' => 'error_page',
3527
'api' => is_api_request() ? 'true' : 'false',
3628
'controller' => 'error',
@@ -39,9 +31,22 @@ protected static function logDuration(Request $request, Response $response, $sta
3931
'section' => 'error',
4032
'status_code' => $response->getStatusCode(),
4133
'status_code_extra' => $request->attributes->get('status_code_extra'),
34+
...app('route-section')->getOriginal(),
4235
];
36+
}
4337

44-
$tags = array_merge($tags, app('route-section')->getOriginal());
38+
/**
39+
* Logs the duration of a specific request through the application
40+
*
41+
* @param Request $request
42+
* @param Response $response
43+
* @param float $startTime
44+
*/
45+
protected static function logDuration(Request $request, Response $response, $startTime)
46+
{
47+
$tags = static::makeLogTags($request, $response);
48+
49+
$duration = microtime(true) - $startTime;
4550

4651
Datadog::timing($GLOBALS['cfg']['datadog-helper']['prefix_web'].'.request_time', $duration, 1, $tags);
4752
}

app/Http/Middleware/SentryMetrics.php

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
4+
// See the LICENCE file in the repository root for full licence text.
5+
6+
namespace App\Http\Middleware;
7+
8+
use Symfony\Component\HttpFoundation\Request;
9+
use Symfony\Component\HttpFoundation\Response;
10+
11+
class SentryMetrics
12+
{
13+
public function handle(Request $request, \Closure $next): Response
14+
{
15+
$start = microtime(true);
16+
17+
$response = $next($request);
18+
19+
$end = microtime(true);
20+
$duration = $end - $start;
21+
22+
if ($duration > $GLOBALS['cfg']['osu']['sentry']['min_log_duration']) {
23+
$data = DatadogMetrics::makeLogTags($request, $response);
24+
$name = "{$data['namespace']}/{$data['controller']}:{$data['action']}";
25+
26+
$transactionContext = \Sentry\Tracing\TransactionContext::make()
27+
->setName($name)
28+
->setData($data)
29+
->setStartTimestamp($start)
30+
->setParentSampled(false)
31+
->setSampled(true);
32+
33+
\Sentry\startTransaction($transactionContext)->finish($end);
34+
}
35+
36+
return $response;
37+
}
38+
}

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"paypal/paypal-checkout-sdk": "*",
4444
"reedware/laravel-relation-joins": "^6.0",
4545
"romanzipp/laravel-turnstile": "^1.3",
46-
"sentry/sentry-laravel": "*",
46+
"sentry/sentry-laravel": ">=4.13.0",
4747
"shopify/shopify-api": "^5.6",
4848
"symfony/yaml": "*",
4949
"tightenco/ziggy": "^1.8",

0 commit comments

Comments
 (0)