Skip to content

Commit

Permalink
chore: review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
basert committed Nov 13, 2024
1 parent 9d4b8f2 commit 3f46ad8
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 78 deletions.
6 changes: 6 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,11 @@
"laravel/pint": "^1.2",
"phpstan/phpstan": "^1.10",
"phpbench/phpbench": "^1.2"
},
"config": {
"allow-plugins": {
"php-http/discovery": false,
"tbachert/spi": false
}
}
}
18 changes: 9 additions & 9 deletions composer.lock

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

47 changes: 40 additions & 7 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
Expand All @@ -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());
}

/**
Expand Down Expand Up @@ -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');
}

/**
Expand Down Expand Up @@ -686,14 +704,29 @@ 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(),
// use ->match(fresh: true) to get the matched internal route, not any wildcard route
'http.route' => $this->match($request, fresh: true)?->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;
}
Expand Down
62 changes: 0 additions & 62 deletions src/Metrics.php

This file was deleted.

0 comments on commit 3f46ad8

Please sign in to comment.