Skip to content

Commit

Permalink
refactor redunant metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
demidovich committed Aug 26, 2023
1 parent 48cc67d commit ba86dbd
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 54 deletions.
54 changes: 27 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,38 +27,38 @@ http://localhost:8090/metrics
```
# HELP myapp_http_duration_seconds Histogram of HTTP request duration
# TYPE myapp_http_duration_seconds histogram
myapp_http_duration_seconds_bucket{node="10.0.0.1",status="200",le="0.01"} 1
myapp_http_duration_seconds_bucket{node="10.0.0.1",status="200",le="0.025"} 1
myapp_http_duration_seconds_bucket{node="10.0.0.1",status="200",le="0.05"} 1
myapp_http_duration_seconds_bucket{node="10.0.0.1",status="200",le="0.1"} 1
myapp_http_duration_seconds_bucket{node="10.0.0.1",status="200",le="0.25"} 1
myapp_http_duration_seconds_bucket{node="10.0.0.1",status="200",le="0.5"} 1
myapp_http_duration_seconds_bucket{node="10.0.0.1",status="200",le="1"} 1
myapp_http_duration_seconds_bucket{node="10.0.0.1",status="200",le="2.5"} 1
myapp_http_duration_seconds_bucket{node="10.0.0.1",status="200",le="5"} 1
myapp_http_duration_seconds_bucket{node="10.0.0.1",status="200",le="+Inf"} 1
myapp_http_duration_seconds_count{node="10.0.0.1",status="200"} 1
myapp_http_duration_seconds_sum{node="10.0.0.1",status="200"} 0.003229
myapp_http_duration_seconds_bucket{node="10.0.0.1",le="0.01"} 1
myapp_http_duration_seconds_bucket{node="10.0.0.1",le="0.025"} 1
myapp_http_duration_seconds_bucket{node="10.0.0.1",le="0.05"} 1
myapp_http_duration_seconds_bucket{node="10.0.0.1",le="0.1"} 1
myapp_http_duration_seconds_bucket{node="10.0.0.1",le="0.25"} 1
myapp_http_duration_seconds_bucket{node="10.0.0.1",le="0.5"} 1
myapp_http_duration_seconds_bucket{node="10.0.0.1",le="1"} 1
myapp_http_duration_seconds_bucket{node="10.0.0.1",le="2.5"} 1
myapp_http_duration_seconds_bucket{node="10.0.0.1",le="5"} 1
myapp_http_duration_seconds_bucket{node="10.0.0.1",le="+Inf"} 1
myapp_http_duration_seconds_count{node="10.0.0.1"} 1
myapp_http_duration_seconds_sum{node="10.0.0.1"} 0.003229
# HELP myapp_http_memory_usage_bytes Memory usage of bytes
# TYPE myapp_http_memory_usage_bytes gauge
myapp_http_memory_usage_bytes{node="10.0.0.1",route="api.books@read"} 5312296
# HELP myapp_http_memory_usage_bytes Histogram of HTTP memory usage in bytes
# TYPE myapp_http_memory_usage_bytes histogram
myapp_http_memory_usage_bytes_bucket{node="10.0.0.1",status="200",le="524288"} 0
myapp_http_memory_usage_bytes_bucket{node="10.0.0.1",status="200",le="1048576"} 0
myapp_http_memory_usage_bytes_bucket{node="10.0.0.1",status="200",le="1572864"} 0
myapp_http_memory_usage_bytes_bucket{node="10.0.0.1",status="200",le="2097152"} 0
myapp_http_memory_usage_bytes_bucket{node="10.0.0.1",status="200",le="2621440"} 0
myapp_http_memory_usage_bytes_bucket{node="10.0.0.1",status="200",le="3145728"} 0
myapp_http_memory_usage_bytes_bucket{node="10.0.0.1",status="200",le="3670016"} 0
myapp_http_memory_usage_bytes_bucket{node="10.0.0.1",status="200",le="4194304"} 0
myapp_http_memory_usage_bytes_bucket{node="10.0.0.1",status="200",le="4718592"} 0
myapp_http_memory_usage_bytes_bucket{node="10.0.0.1",status="200",le="5242880"} 0
myapp_http_memory_usage_bytes_bucket{node="10.0.0.1",status="200",le="10485760"} 1
myapp_http_memory_usage_bytes_bucket{node="10.0.0.1",status="200",le="15728640"} 1
myapp_http_memory_usage_bytes_bucket{node="10.0.0.1",status="200",le="+Inf"} 1
myapp_http_memory_usage_bytes_count{node="10.0.0.1",status="200"} 1
myapp_http_memory_usage_bytes_sum{node="10.0.0.1",status="200"} 5312296
myapp_http_memory_usage_bytes_bucket{node="10.0.0.1",le="524288"} 0
myapp_http_memory_usage_bytes_bucket{node="10.0.0.1",le="1048576"} 0
myapp_http_memory_usage_bytes_bucket{node="10.0.0.1",le="1572864"} 0
myapp_http_memory_usage_bytes_bucket{node="10.0.0.1",le="2097152"} 0
myapp_http_memory_usage_bytes_bucket{node="10.0.0.1",le="2621440"} 0
myapp_http_memory_usage_bytes_bucket{node="10.0.0.1",le="3145728"} 0
myapp_http_memory_usage_bytes_bucket{node="10.0.0.1",le="3670016"} 0
myapp_http_memory_usage_bytes_bucket{node="10.0.0.1",le="4194304"} 0
myapp_http_memory_usage_bytes_bucket{node="10.0.0.1",le="4718592"} 0
myapp_http_memory_usage_bytes_bucket{node="10.0.0.1",le="5242880"} 0
myapp_http_memory_usage_bytes_bucket{node="10.0.0.1",le="10485760"} 1
myapp_http_memory_usage_bytes_bucket{node="10.0.0.1",le="15728640"} 1
myapp_http_memory_usage_bytes_bucket{node="10.0.0.1",le="+Inf"} 1
myapp_http_memory_usage_bytes_count{node="10.0.0.1"} 1
myapp_http_memory_usage_bytes_sum{node="10.0.0.1"} 5312296
# HELP myapp_http_requests_count Count HTTP requests processed
# TYPE myapp_http_requests_count counter
myapp_http_requests_count{node="10.0.0.1",route="api.books@read",status="200"} 1
Expand Down
2 changes: 1 addition & 1 deletion src/Metrics/Counters.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class Counters
{
private $counters = [];
private array $counters = [];

public function increase(string $name, int $quantity = 1): void
{
Expand Down
2 changes: 1 addition & 1 deletion src/Metrics/Debug.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class Debug
{
private $logger;
private LoggerInterface $logger;

public function __construct(LoggerInterface $logger)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Metrics/Labels.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class Labels
{
private $labels = [];
private array $labels = [];

public function __construct(array $labels)
{
Expand Down
22 changes: 11 additions & 11 deletions src/Metrics/Metrics.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@

class Metrics
{
protected $namespace = "app";
protected string $namespace = "app";

private $httpRoute = "undefined";
private $httpMethod = "undefined";
private $httpStatus = 0;
private $labels;
private $runtime;
private $counters;
private $storage;
private string $httpRoute = "undefined";
private string $httpMethod = "undefined";
private int $httpStatus = 0;
private Labels $labels;
private Runtime $runtime;
private Counters $counters;
private Storage $storage;

// 512k 1M 1.5M 2M 2.5M 3M 3.5M 4M 4.5M 5M 10M 15M
protected $memoryUsageBuckets = [
protected array $memoryUsageBuckets = [
524288, 1048576, 1572864, 2097152, 2621440, 3145728,
3670016, 4194304, 4718592, 5242880, 10485760, 15728640
];

protected $requestDurationBuckets = [
protected array $requestDurationBuckets = [
0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5
];

Expand All @@ -39,8 +39,8 @@ public function __construct(
int $startTime,
array $labels = []
) {
$this->runtime = new Runtime($startTime);
$this->labels = new Labels($labels);
$this->runtime = new Runtime($startTime);
$this->counters = new Counters();
}

Expand Down
18 changes: 6 additions & 12 deletions src/Metrics/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@
/**
* Metrics:
*
* <prefix>_http_duration_seconds_bucket (status, le, <labels>)
* <prefix>_http_memory_usage_bytes_bucket (status, le, <labels>)
* <prefix>_http_duration_seconds_bucket (le, <labels>)
* <prefix>_http_memory_usage_bytes_bucket (le, <labels>)
* <prefix>_http_memory_usage_bytes (route, <labels>)
* <prefix>_http_requests_count (route, status, <labels>)
* <prefix>_http_runtime_seconds (route, timer, <labels>)
* <prefix>_<counter>_count (<labels>)
*/
class Storage
{
private $registry;
private CollectorRegistry $registry;

public function __construct(CollectorRegistry $registry)
{
Expand Down Expand Up @@ -105,8 +105,7 @@ private function persistRequestsCounter(Metrics $metrics): void

private function persistMemoryUsage(Metrics $metrics): void
{
$value = $metrics->memoryUsage();

$value = $metrics->memoryUsage();
$labels = $metrics->labels()->allWith([
"route" => $metrics->httpRoute()
]);
Expand All @@ -119,10 +118,7 @@ private function persistMemoryUsage(Metrics $metrics): void
);

$gauge->set($value, $labels);

$labels = $metrics->labels()->allWith([
"status" => $metrics->httpStatus()
]);
$labels = $metrics->labels()->all();

$histogram = $this->registry->getOrRegisterHistogram(
$metrics->namespace(),
Expand Down Expand Up @@ -159,9 +155,7 @@ private function persistTimers(Metrics $metrics): void

private function persistRequestDuration(Metrics $metrics): void
{
$labels = $metrics->labels()->allWith([
"status" => $metrics->httpStatus()
]);
$labels = $metrics->labels()->all();

$histogram = $this->registry->getOrRegisterHistogram(
$metrics->namespace(),
Expand Down
2 changes: 1 addition & 1 deletion tests/Stub/AppMetrics.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class AppMetrics extends Metrics
{
protected $namespace = "myapp";
protected string $namespace = "myapp";

public function startMongo(): void
{
Expand Down

0 comments on commit ba86dbd

Please sign in to comment.