Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DDEV-1966 update metrics in termination #15

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}
}
2 changes: 1 addition & 1 deletion src/HttpMiddleware/HttpMetricsMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function handle($request, Closure $next)
$endTime = microtime(true);

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

return $response;
}
Expand Down
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);
});
Loading