Skip to content

Commit

Permalink
feat: opentelemetry metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
basert committed Nov 4, 2024
1 parent a7f5775 commit 913914d
Show file tree
Hide file tree
Showing 3 changed files with 278 additions and 54 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"bench": "vendor/bin/phpbench run --report=benchmark"
},
"require": {
"php": ">=8.0"
"php": ">=8.3",
"open-telemetry/api": "^1.1"
},
"require-dev": {
"phpunit/phpunit": "^9.5.25",
Expand Down
310 changes: 258 additions & 52 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 18 additions & 1 deletion src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Utopia;

use OpenTelemetry\API\Instrumentation\CachedInstrumentation;

class App
{
/**
Expand Down Expand Up @@ -501,10 +503,15 @@ public function match(Request $request, bool $fresh = false): ?Route
*/
public function execute(Route $route, Request $request, Response $response): static
{
$arguments = [];
static $instrumentation = new CachedInstrumentation('utopia/http');
static $requestDuration = $instrumentation->meter()->createHistogram("request_duration", "seconds");
static $responseSize = $instrumentation->meter()->createHistogram("response_size", "bytes");

$groups = $route->getGroups();
$pathValues = $route->getPathValues($request);

$startTime = microtime(true);

try {
if ($route->getHook()) {
foreach (self::$init as $hook) { // Global init hooks
Expand Down Expand Up @@ -575,6 +582,16 @@ public function execute(Route $route, Request $request, Response $response): sta
}
}
}
} finally {
$endTime = microtime(true);

$attributes = [
'http.request.method' => $route->getMethod(),
'http.request.path' => $route->getPath(),
'http.response.status' => $response->getStatusCode(),
];
$requestDuration->record($endTime - $startTime, $attributes);
$responseSize->record($response->getSize(), $attributes);
}

return $this;
Expand Down

0 comments on commit 913914d

Please sign in to comment.