diff --git a/composer.lock b/composer.lock index f8eb585a..6261fa11 100644 --- a/composer.lock +++ b/composer.lock @@ -1843,12 +1843,12 @@ "source": { "type": "git", "url": "https://github.com/utopia-php/telemetry.git", - "reference": "654896edcefa4cc4ce61af0944bf732febd62f95" + "reference": "a51fd18289921fc81f69d98e51f73ebb381eeeb9" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/telemetry/zipball/654896edcefa4cc4ce61af0944bf732febd62f95", - "reference": "654896edcefa4cc4ce61af0944bf732febd62f95", + "url": "https://api.github.com/repos/utopia-php/telemetry/zipball/a51fd18289921fc81f69d98e51f73ebb381eeeb9", + "reference": "a51fd18289921fc81f69d98e51f73ebb381eeeb9", "shasum": "" }, "require": { @@ -1901,7 +1901,7 @@ "source": "https://github.com/utopia-php/telemetry/tree/initial-commit", "issues": "https://github.com/utopia-php/telemetry/issues" }, - "time": "2024-11-11T15:15:39+00:00" + "time": "2024-11-11T17:36:02+00:00" } ], "packages-dev": [ @@ -2634,16 +2634,16 @@ }, { "name": "phpstan/phpstan", - "version": "1.12.9", + "version": "1.12.10", "source": { "type": "git", "url": "https://github.com/phpstan/phpstan.git", - "reference": "ceb937fb39a92deabc02d20709cf14b2c452502c" + "reference": "fc463b5d0fe906dcf19689be692c65c50406a071" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/ceb937fb39a92deabc02d20709cf14b2c452502c", - "reference": "ceb937fb39a92deabc02d20709cf14b2c452502c", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/fc463b5d0fe906dcf19689be692c65c50406a071", + "reference": "fc463b5d0fe906dcf19689be692c65c50406a071", "shasum": "" }, "require": { @@ -2688,7 +2688,7 @@ "type": "github" } ], - "time": "2024-11-10T17:10:04+00:00" + "time": "2024-11-11T15:37:09+00:00" }, { "name": "phpunit/php-code-coverage", diff --git a/src/App.php b/src/App.php index addac0fb..06d88365 100755 --- a/src/App.php +++ b/src/App.php @@ -3,7 +3,9 @@ namespace Utopia; use Utopia\Telemetry\Adapter as Telemetry; -use Utopia\Telemetry\Adapter\Noop as NoopTelemetry; +use Utopia\Telemetry\Adapter\None as NoTelemetry; +use Utopia\Telemetry\Histogram; +use Utopia\Telemetry\UpDownCounter; class App { @@ -114,7 +116,10 @@ class App protected int $compressionMinSize = App::COMPRESSION_MIN_SIZE_DEFAULT; protected mixed $compressionSupported = []; - protected Metrics $metrics; + private Histogram $requestDuration; + private UpDownCounter $activeRequests; + private Histogram $requestBodySize; + private Histogram $responseBodySize; /** * App @@ -124,7 +129,7 @@ class App public function __construct(string $timezone) { \date_default_timezone_set($timezone); - $this->metrics = new Metrics(new NoopTelemetry()); + $this->setTelemetry(new NoTelemetry()); } /** @@ -159,7 +164,20 @@ public function setCompressionSupported(mixed $compressionSupported) */ public function setTelemetry(Telemetry $telemetry) { - $this->metrics = new Metrics($telemetry); + // https://opentelemetry.io/docs/specs/semconv/http/http-metrics/#metric-httpserverrequestduration + $this->requestDuration = $telemetry->createHistogram( + 'http.server.request.duration', + 's', + null, + ['ExplicitBucketBoundaries' => [0.005, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1, 2.5, 5, 7.5, 10]] + ); + + // https://opentelemetry.io/docs/specs/semconv/http/http-metrics/#metric-httpserveractive_requests + $this->activeRequests = $telemetry->createUpDownCounter('http.server.active_requests', '{request}'); + // https://opentelemetry.io/docs/specs/semconv/http/http-metrics/#metric-httpserverrequestbodysize + $this->requestBodySize = $telemetry->createHistogram('http.server.request.body.size', 'By'); + // https://opentelemetry.io/docs/specs/semconv/http/http-metrics/#metric-httpserverresponsebodysize + $this->responseBodySize = $telemetry->createHistogram('http.server.response.body.size', 'By'); } /** @@ -686,14 +704,28 @@ protected function getArguments(Hook $hook, array $values, array $requestParams) */ public function run(Request $request, Response $response): static { - $this->metrics->increaseActiveRequest($request); + $this->activeRequests->add(1, [ + 'http.request.method' => $request->getMethod(), + 'url.scheme' => $request->getProtocol(), + ]); $start = microtime(true); $result = $this->runInternal($request, $response); $requestDuration = microtime(true) - $start; - $this->metrics->recordMetrics($request, $response, $this->getRoute(), $requestDuration); - $this->metrics->decreaseActiveRequest($request); + $attributes = [ + 'url.scheme' => $request->getProtocol(), + 'http.request.method' => $request->getMethod(), + 'http.route' => $this->getRoute()?->getPath(), + 'http.response.status_code' => $response->getStatusCode(), + ]; + $this->requestDuration->record($requestDuration, $attributes); + $this->requestBodySize->record($request->getSize(), $attributes); + $this->responseBodySize->record($response->getSize(), $attributes); + $this->activeRequests->add(-1, [ + 'http.request.method' => $request->getMethod(), + 'url.scheme' => $request->getProtocol(), + ]); return $result; } diff --git a/src/Metrics.php b/src/Metrics.php deleted file mode 100644 index 12a27def..00000000 --- a/src/Metrics.php +++ /dev/null @@ -1,62 +0,0 @@ -requestDuration = $telemetry->createHistogram( - 'http.server.request.duration', - 's', - null, - ['ExplicitBucketBoundaries' => [0.005, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1, 2.5, 5, 7.5, 10]] - ); - - // https://opentelemetry.io/docs/specs/semconv/http/http-metrics/#metric-httpserveractive_requests - $this->activeRequests = $telemetry->createUpDownCounter('http.server.active_requests', '{request}'); - // https://opentelemetry.io/docs/specs/semconv/http/http-metrics/#metric-httpserverrequestbodysize - $this->requestBodySize = $telemetry->createHistogram('http.server.request.body.size', 'By'); - // https://opentelemetry.io/docs/specs/semconv/http/http-metrics/#metric-httpserverresponsebodysize - $this->responseBodySize = $telemetry->createHistogram('http.server.response.body.size', 'By'); - } - - public function recordMetrics(Request $request, Response $response, ?Route $route, float $requestDuration): void - { - $attributes = [ - 'url.scheme' => $request->getProtocol(), - 'http.request.method' => $request->getMethod(), - 'http.route' => $route?->getPath(), - 'http.response.status_code' => $response->getStatusCode(), - ]; - $this->requestDuration->record($requestDuration, $attributes); - $this->requestBodySize->record($request->getSize(), $attributes); - $this->responseBodySize->record($response->getSize(), $attributes); - } - - public function increaseActiveRequest(Request $request): void - { - $this->activeRequests->add(1, [ - 'http.request.method' => $request->getMethod(), - 'url.scheme' => $request->getProtocol(), - ]); - } - - public function decreaseActiveRequest(Request $request): void - { - $this->activeRequests->add(-1, [ - 'http.request.method' => $request->getMethod(), - 'url.scheme' => $request->getProtocol(), - ]); - } -}