Skip to content

Commit

Permalink
DDEV-1966 update metrics in termination
Browse files Browse the repository at this point in the history
  • Loading branch information
Vadim Davydenko committed Nov 5, 2024
1 parent d3bede8 commit d57f8ea
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/Command/CommandMetrics.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public static function write(CommandFinished $event): void

$labels = CommandLabels::extractFromTask($event);

Prometheus::update('command_runs_total', 1, $labels);
Prometheus::update('command_run_seconds_total', Helper::duration(), $labels);
app()->terminating(fn() => Prometheus::update('command_runs_total', 1, $labels));
app()->terminating(fn() => Prometheus::update('command_run_seconds_total', Helper::duration(), $labels));
}

protected static function needToIgnoreCommand(?string $command): bool
Expand Down
4 changes: 2 additions & 2 deletions src/Guzzle/GuzzleMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public static function handleResponse(string $type, $start, string $host): void

$labels = [$host];

Prometheus::update('http_client_seconds_total', $end - $start, $labels);
app()->terminating(fn() => Prometheus::update('http_client_seconds_total', $end - $start, $labels));

Prometheus::update('http_client_requests_total', 1, $labels);
app()->terminating(fn() => Prometheus::update('http_client_requests_total', 1, $labels));
}
}
10 changes: 8 additions & 2 deletions src/HttpMiddleware/HttpMetricsMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

class HttpMetricsMiddleware
{
protected int|float $duration;

public function __construct(
private readonly LatencyProfiler $latencyProfiler
) {
Expand All @@ -27,9 +29,13 @@ public function handle($request, Closure $next)
$response = $next($request);
$endTime = microtime(true);

$duration = $endTime - $startTime;
$this->latencyProfiler->writeMetrics(Prometheus::bag(), $response->getStatusCode(), $duration);
$this->duration = $endTime - $startTime;

return $response;
}

public function terminate(Request $request, Response $response): void
{
$this->latencyProfiler->writeMetrics(Prometheus::bag(), $response->getStatusCode(), $this->duration);
}
}
4 changes: 2 additions & 2 deletions src/Job/JobMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function handle($job, $next)
$next($job);
$duration = microtime(true) - $start;

Prometheus::update('queue_job_runs_total', 1, $labels);
Prometheus::update('queue_job_run_seconds_total', $duration, $labels);
app()->terminating(fn() => Prometheus::update('queue_job_runs_total', 1, $labels));
app()->terminating(fn() => Prometheus::update('queue_job_run_seconds_total', $duration, $labels));
}
}
4 changes: 2 additions & 2 deletions src/Kafka/KafkaMetricsMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ private function writeMetrics(Message $message, float $startKafka, KafkaResponse
$duration = microtime(true) - $startKafka;
$labels = KafkaLabels::extractFromMessage($message, $status->value);

Prometheus::update('kafka_runs_total', 1, $labels);
Prometheus::update('kafka_run_seconds_total', $duration, $labels);
app()->terminating(fn() => Prometheus::update('kafka_runs_total', 1, $labels));
app()->terminating(fn() => Prometheus::update('kafka_run_seconds_total', $duration, $labels));
}
}
10 changes: 5 additions & 5 deletions src/MetricsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,24 +103,24 @@ private function registerEventListeners(): void
});

Event::listen(MessageLogged::class, function (MessageLogged $event) {
Prometheus::update('log_messages_count', 1, [$event->level]);
app()->terminating(fn() => Prometheus::update('log_messages_count', 1, [$event->level]));
});

Event::listen(JobFailed::class, function (JobFailed $event) {
Prometheus::update('queue_job_failed_total', 1, JobLabels::extractFromJob($event->job));
app()->terminating(fn() => Prometheus::update('queue_job_failed_total', 1, JobLabels::extractFromJob($event->job)));
});

Event::listen(JobQueued::class, function (JobQueued $event) {
Prometheus::update('queue_job_dispatched_total', 1, JobLabels::extractFromJob($event->job));
app()->terminating(fn() => Prometheus::update('queue_job_dispatched_total', 1, JobLabels::extractFromJob($event->job)));
});

Bus::pipeThrough([
JobMiddleware::class,
]);

Event::listen(ScheduledTaskFinished::class, function (ScheduledTaskFinished $event) {
Prometheus::update('task_runs_total', 1, TaskLabels::extractFromTask($event->task));
Prometheus::update('task_run_seconds_total', $event->runtime, TaskLabels::extractFromTask($event->task));
app()->terminating(fn() => Prometheus::update('task_runs_total', 1, TaskLabels::extractFromTask($event->task)));
app()->terminating(fn() => Prometheus::update('task_run_seconds_total', $event->runtime, TaskLabels::extractFromTask($event->task)));
});

Event::listen(CommandFinished::class, function (CommandFinished $event) {
Expand Down
4 changes: 2 additions & 2 deletions src/Workers/WorkerUsage.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public function register(MetricsBag $metricsBag): void
public function update(MetricsBag $metricsBag): void
{
if ($this->hasSwoole()) {
Prometheus::update('workers_total', $this->getTotal());
Prometheus::update('workers_idle', $this->getIdle());
app()->terminating(fn() => Prometheus::update('workers_total', $this->getTotal()));
app()->terminating(fn() => Prometheus::update('workers_idle', $this->getIdle()));
}
}

Expand Down
8 changes: 8 additions & 0 deletions tests/EventListenersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
->withArgs(['log_messages_count', 1, ['info']]);

logger()->info("hello");

app()->terminate();
});

test('test listen message job queued', function () {
Expand Down Expand Up @@ -70,6 +72,8 @@
};
}
Event::dispatch(new JobQueued(...$arr));

app()->terminate();
});

test('test listen message job processed', function () {
Expand All @@ -93,6 +97,8 @@
});

Bus::dispatch($job);

app()->terminate();
});

test('test listen message command finished', function () {
Expand Down Expand Up @@ -120,6 +126,8 @@
});

Event::dispatch(new CommandFinished($command, $input, $output, $exitCode));

app()->terminate();
});

test('test listen message command finished skip', function () {
Expand Down
2 changes: 2 additions & 0 deletions tests/HttpMetricsMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,7 @@
return $expectedResponse;
});

$middleware->terminate($expectedRequest, $expectedResponse);

assertSame($expectedResponse, $response);
});

0 comments on commit d57f8ea

Please sign in to comment.