Skip to content

Commit 5ff44c2

Browse files
Merge pull request #26 from open-runtimes/feat-improve-logs
Feat: Improve logs
2 parents 68b7110 + 70c8f42 commit 5ff44c2

1 file changed

Lines changed: 40 additions & 20 deletions

File tree

app/http.php

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@
5353
$register = new Registry();
5454

5555
/**
56-
* Create logger for cloud logging
57-
*/
56+
* Create logger for cloud logging
57+
*/
5858
$register->set('logger', function () {
5959
$providerName = App::getEnv('OPR_EXECUTOR_LOGGING_PROVIDER', '');
6060
$providerConfig = App::getEnv('OPR_EXECUTOR_LOGGING_CONFIG', '');
@@ -93,8 +93,8 @@
9393
});
9494

9595
/**
96-
* Create a Swoole table to store runtime information
97-
*/
96+
* Create a Swoole table to store runtime information
97+
*/
9898
$register->set('activeRuntimes', function () {
9999
$table = new Table(1024);
100100

@@ -111,8 +111,8 @@
111111
});
112112

113113
/**
114-
* Create a Swoole table of usage stats (separate for host and containers)
115-
*/
114+
* Create a Swoole table of usage stats (separate for host and containers)
115+
*/
116116
$register->set('statsContainers', function () {
117117
$table = new Table(1024);
118118

@@ -141,17 +141,21 @@
141141
App::setResource('orchestrationConnection', fn (Pool $orchestrationPool) => $orchestrationPool->pop(), ['orchestrationPool']);
142142
App::setResource('orchestration', fn (Connection $orchestrationConnection) => $orchestrationConnection->getResource(), ['orchestrationConnection']);
143143

144-
function logError(Throwable $error, string $action, Logger $logger = null, Utopia\Route $route = null): void
144+
App::setResource('log', fn () => new Log());
145+
146+
function logError(Log $log, Throwable $error, string $action, Logger $logger = null, Utopia\Route $route = null): void
145147
{
146148
Console::error('[Error] Type: ' . get_class($error));
147149
Console::error('[Error] Message: ' . $error->getMessage());
148150
Console::error('[Error] File: ' . $error->getFile());
149151
Console::error('[Error] Line: ' . $error->getLine());
150152

151-
if ($logger) {
152-
$version = (string) App::getEnv('OPR_EXECUTOR_VERSION', 'UNKNOWN');
153+
if ($logger && ($error->getCode() === 500 || $error->getCode() === 0)) {
154+
$version = (string) App::getEnv('OPR_EXECUTOR_VERSION', '');
155+
if (empty($version)) {
156+
$version = 'UNKNOWN';
157+
}
153158

154-
$log = new Log();
155159
$log->setNamespace("executor");
156160
$log->setServer(\gethostname() !== false ? \gethostname() : null);
157161
$log->setVersion($version);
@@ -163,7 +167,7 @@ function logError(Throwable $error, string $action, Logger $logger = null, Utopi
163167
$log->addTag('url', $route->getPath());
164168
}
165169

166-
$log->addTag('code', $error->getCode());
170+
$log->addTag('code', \strval($error->getCode()));
167171
$log->addTag('verboseType', get_class($error));
168172

169173
$log->addExtra('file', $error->getFile());
@@ -451,7 +455,7 @@ function removeAllRuntimes(Table $activeRuntimes, Pool $orchestrationPool): void
451455
// Silently try to kill container
452456
try {
453457
$orchestration->remove($containerId, true);
454-
} catch(Throwable $th) {
458+
} catch (Throwable $th) {
455459
}
456460

457461
$activeRuntimes->del($activeRuntimeId);
@@ -498,9 +502,12 @@ function removeAllRuntimes(Table $activeRuntimes, Pool $orchestrationPool): void
498502
->param('runtimeId', '', new Text(64), 'Runtime unique ID.')
499503
->inject('activeRuntimes')
500504
->inject('response')
501-
->action(function (string $runtimeId, Table $activeRuntimes, Response $response) {
505+
->inject('log')
506+
->action(function (string $runtimeId, Table $activeRuntimes, Response $response, Log $log) {
502507
$activeRuntimeId = $runtimeId; // Used with Swoole table (key)
503508

509+
$log->addExtra('runtimeId', $activeRuntimeId);
510+
504511
if (!$activeRuntimes->exists($activeRuntimeId)) {
505512
throw new Exception('Runtime not found', 404);
506513
}
@@ -518,10 +525,13 @@ function removeAllRuntimes(Table $activeRuntimes, Pool $orchestrationPool): void
518525
->inject('orchestration')
519526
->inject('activeRuntimes')
520527
->inject('response')
521-
->action(function (string $runtimeId, Orchestration $orchestration, Table $activeRuntimes, Response $response) {
528+
->inject('log')
529+
->action(function (string $runtimeId, Orchestration $orchestration, Table $activeRuntimes, Response $response, Log $log) {
522530
$activeRuntimeId = $runtimeId; // Used with Swoole table (key)
523531
$runtimeId = System::getHostname() . '-' . $runtimeId; // Used in Docker (name)
524532

533+
$log->addExtra('runtimeId', $activeRuntimeId);
534+
525535
if (!$activeRuntimes->exists($activeRuntimeId)) {
526536
throw new Exception('Runtime not found', 404);
527537
}
@@ -549,11 +559,14 @@ function removeAllRuntimes(Table $activeRuntimes, Pool $orchestrationPool): void
549559
->param('memory', 512, new Integer(), 'Comtainer RAM memory.', true)
550560
->inject('activeRuntimes')
551561
->inject('response')
562+
->inject('log')
552563
->action(
553-
function (string $runtimeId, string $payload, array $variables, int $timeout, string $image, string $source, string $entrypoint, int $cpus, int $memory, Table $activeRuntimes, Response $response) {
564+
function (string $runtimeId, string $payload, array $variables, int $timeout, string $image, string $source, string $entrypoint, int $cpus, int $memory, Table $activeRuntimes, Response $response, Log $log) {
554565
$activeRuntimeId = $runtimeId; // Used with Swoole table (key)
555566
$runtimeId = System::getHostname() . '-' . $runtimeId; // Used in Docker (name)
556567

568+
$log->addExtra('runtimeId', $activeRuntimeId);
569+
557570
$variables = \array_merge($variables, [
558571
'INERNAL_EXECUTOR_HOSTNAME' => System::getHostname()
559572
]);
@@ -616,7 +629,7 @@ function (string $runtimeId, string $payload, array $variables, int $timeout, st
616629

617630
// Prepare runtime
618631
for ($i = 0; $i < 5; $i++) {
619-
[ 'errNo' => $errNo, 'error' => $error, 'statusCode' => $statusCode, 'executorResponse' => $executorResponse ] = \call_user_func($sendCreateRuntimeRequest);
632+
['errNo' => $errNo, 'error' => $error, 'statusCode' => $statusCode, 'executorResponse' => $executorResponse] = \call_user_func($sendCreateRuntimeRequest);
620633

621634
if ($errNo === 0 && $statusCode < 500) {
622635
$body = \json_decode($executorResponse, true);
@@ -713,7 +726,7 @@ function (string $runtimeId, string $payload, array $variables, int $timeout, st
713726

714727
// Execute function
715728
for ($i = 0; $i < 5; $i++) {
716-
[ 'errNo' => $errNo, 'error' => $error, 'statusCode' => $statusCode, 'executorResponse' => $executorResponse ] = \call_user_func($sendExecuteRequest);
729+
['errNo' => $errNo, 'error' => $error, 'statusCode' => $statusCode, 'executorResponse' => $executorResponse] = \call_user_func($sendExecuteRequest);
717730

718731
// No error
719732
if ($errNo === 0) {
@@ -815,9 +828,10 @@ function (string $runtimeId, string $payload, array $variables, int $timeout, st
815828
->inject('logger')
816829
->inject('request')
817830
->inject('response')
818-
->action(function (App $utopia, Throwable $error, ?Logger $logger, Request $request, Response $response) {
831+
->inject('log')
832+
->action(function (App $utopia, Throwable $error, ?Logger $logger, Request $request, Response $response, Log $log) {
819833
$route = $utopia->match($request);
820-
logError($error, "httpError", $logger, $route);
834+
logError($log, $error, "httpError", $logger, $route);
821835

822836
switch ($error->getCode()) {
823837
case 400: // Error allowed publicly
@@ -1024,11 +1038,17 @@ function getStats(Table $statsHost, Table $statsContainers, Orchestration $orche
10241038
} catch (\Throwable $th) {
10251039
$code = 500;
10261040

1041+
10271042
/**
10281043
* @var Logger $logger
10291044
*/
10301045
$logger = $app->getResource('logger');
1031-
logError($th, "serverError", $logger);
1046+
1047+
/**
1048+
* @var Log $log
1049+
*/
1050+
$log = $app->getResource('log');
1051+
logError($log, $th, "serverError", $logger);
10321052
$swooleResponse->setStatusCode($code);
10331053
$output = [
10341054
'message' => 'Error: ' . $th->getMessage(),

0 commit comments

Comments
 (0)