jdecool/scaleway-runtimepublic Fork
⑂ 61ed3cb
Commits
⬇ Clone ▾
git clone https://git.rickub.com/jdecool/scaleway-runtime.git
git clone ssh://git@rickub.com/jdecool/scaleway-runtime.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

Download ZIP

fix(probe): bound the stderr line size and correct the logs docsUnverified

The public probe's bytes parameter went straight to str_repeat(), so a
huge value exhausted the function's memory: it is now capped at 1 MiB.

Re-measured on Scaleway: continuation entries of a split line have the
unknown level, not debug, and latency reached 2.4 seconds. A real
Invocation failed line from the published runtime confirms its format.
The README no longer writes the token secret to a file in the current
directory, and warns that a leftover token name makes creation fail.
Jérémy DECOOL committed 2026-11-14T14:23:45+01:00 Browse files
61ed3cb parent: fd5aef9
modified README.md +6 -5
@@ -383,7 +383,7 @@ Replacing a function, for instance to change its `runtime`, deletes its CRON tri
383383 Scaleway sends what the function writes to stdout and stderr to [Cockpit](https://www.scaleway.com/en/docs/cockpit/), in the project's `Scaleway Logs` data source, about 2 seconds after it is written.
384384 The runtime logs its own failures to stderr, as Monolog does with a `php://stderr` handler.
385385 Each line is one log entry: a stack trace spans one entry per line, and a line over 16 KiB is split into 16 KiB entries and cut after 64 KiB.
386-Every entry has the `debug` level, stderr ones included: filter on the `stream` field instead.
386+Every entry has the `debug` level, stderr ones included, except the continuations of a split line, whose level is `unknown`: filter on the `stream` field instead.
387387 An entry looks like this, with the `resource_id` label holding the function's UUID:
388388
389389 ```json
@@ -397,18 +397,19 @@ From the command line, query Loki's HTTP API with a token that can only read log
397397 ```sh
398398 LOKI_URL=$(scw cockpit data-source list origin=scaleway types.0=logs -o json | jq -r '.[0].url')
399399 FUNCTION_ID=$(scw function function list name=<function name> -o json | jq -r '.[0].id')
400-# The secret key is only shown at creation.
401-scw cockpit token create name=read-logs token-scopes.0=read_only_logs -o json > token.json
402-LOKI_TOKEN=$(jq -r .secret_key token.json)
400+# The secret key is only shown at creation: keep it in memory rather than in a file that could get committed.
401+TOKEN=$(scw cockpit token create name=read-logs token-scopes.0=read_only_logs -o json)
402+LOKI_TOKEN=$(jq -r .secret_key <<< "$TOKEN")
403403
404404 curl -sS -G "$LOKI_URL/loki/api/v1/query_range" -H "Authorization: Bearer $LOKI_TOKEN" \
405405 --data-urlencode "query={resource_id=\"$FUNCTION_ID\"} | json | stream=\"stderr\" | line_format \"{{.message}}\"" \
406406 --data-urlencode since=1h --data-urlencode limit=1000 \
407407 | jq -r '[.data.result[].values[]] | sort_by(.[0]) | .[][1]'
408408
409-scw cockpit token delete token-id=$(jq -r .id token.json) && rm token.json
409+scw cockpit token delete token-id=$(jq -r .id <<< "$TOKEN")
410410 ```
411411
412+Token names are unique within a project: if a `read-logs` token was left behind, `token create` fails with `resource already exists`, so delete it first.
412413 Without the `stream="stderr"` filter, the query also returns stdout and the platform's own lines (`Function Triggered: /`, `Function is now ready to serve requests!`).
413414
414415 ## Local development with the Emulator
@@ -383,7 +383,7 @@ Replacing a function, for instance to change its `runtime`, deletes its CRON tri
383 Scaleway sends what the function writes to stdout and stderr to [Cockpit](https://www.scaleway.com/en/docs/cockpit/), in the project's `Scaleway Logs` data source, about 2 seconds after it is written.383 Scaleway sends what the function writes to stdout and stderr to [Cockpit](https://www.scaleway.com/en/docs/cockpit/), in the project's `Scaleway Logs` data source, about 2 seconds after it is written.
384 The runtime logs its own failures to stderr, as Monolog does with a `php://stderr` handler.384 The runtime logs its own failures to stderr, as Monolog does with a `php://stderr` handler.
385 Each line is one log entry: a stack trace spans one entry per line, and a line over 16 KiB is split into 16 KiB entries and cut after 64 KiB.385 Each line is one log entry: a stack trace spans one entry per line, and a line over 16 KiB is split into 16 KiB entries and cut after 64 KiB.
386-Every entry has the `debug` level, stderr ones included: filter on the `stream` field instead.386+Every entry has the `debug` level, stderr ones included, except the continuations of a split line, whose level is `unknown`: filter on the `stream` field instead.
387 An entry looks like this, with the `resource_id` label holding the function's UUID:387 An entry looks like this, with the `resource_id` label holding the function's UUID:
388 388
389 ```json389 ```json
@@ -397,18 +397,19 @@ From the command line, query Loki's HTTP API with a token that can only read log
397 ```sh397 ```sh
398 LOKI_URL=$(scw cockpit data-source list origin=scaleway types.0=logs -o json | jq -r '.[0].url')398 LOKI_URL=$(scw cockpit data-source list origin=scaleway types.0=logs -o json | jq -r '.[0].url')
399 FUNCTION_ID=$(scw function function list name=<function name> -o json | jq -r '.[0].id')399 FUNCTION_ID=$(scw function function list name=<function name> -o json | jq -r '.[0].id')
400-# The secret key is only shown at creation.400+# The secret key is only shown at creation: keep it in memory rather than in a file that could get committed.
401-scw cockpit token create name=read-logs token-scopes.0=read_only_logs -o json > token.json401+TOKEN=$(scw cockpit token create name=read-logs token-scopes.0=read_only_logs -o json)
402-LOKI_TOKEN=$(jq -r .secret_key token.json)402+LOKI_TOKEN=$(jq -r .secret_key <<< "$TOKEN")
403 403
404 curl -sS -G "$LOKI_URL/loki/api/v1/query_range" -H "Authorization: Bearer $LOKI_TOKEN" \404 curl -sS -G "$LOKI_URL/loki/api/v1/query_range" -H "Authorization: Bearer $LOKI_TOKEN" \
405 --data-urlencode "query={resource_id=\"$FUNCTION_ID\"} | json | stream=\"stderr\" | line_format \"{{.message}}\"" \405 --data-urlencode "query={resource_id=\"$FUNCTION_ID\"} | json | stream=\"stderr\" | line_format \"{{.message}}\"" \
406 --data-urlencode since=1h --data-urlencode limit=1000 \406 --data-urlencode since=1h --data-urlencode limit=1000 \
407 | jq -r '[.data.result[].values[]] | sort_by(.[0]) | .[][1]'407 | jq -r '[.data.result[].values[]] | sort_by(.[0]) | .[][1]'
408 408
409-scw cockpit token delete token-id=$(jq -r .id token.json) && rm token.json409+scw cockpit token delete token-id=$(jq -r .id <<< "$TOKEN")
410 ```410 ```
411 411
412+Token names are unique within a project: if a `read-logs` token was left behind, `token create` fails with `resource already exists`, so delete it first.
412 Without the `stream="stderr"` filter, the query also returns stdout and the platform's own lines (`Function Triggered: /`, `Function is now ready to serve requests!`).413 Without the `stream="stderr"` filter, the query also returns stdout and the platform's own lines (`Function Triggered: /`, `Function is now ready to serve requests!`).
413 414
414 ## Local development with the Emulator415 ## Local development with the Emulator
modified docs/plan.md +1 -1
@@ -57,4 +57,4 @@ Done on 2026-11-14: the repository is on GitHub with CI green, the package is on
5757 1. Verify a Command Invocation end to end on Scaleway: the e2e scenarios trigger CRON Invocations only through the Emulator, and the deployed one could only be observed in the logs.
5858 2. Run the example deployment with `host` set, to check absolute URLs.
5959 3. Measure what the probe left open: what a deployment with a failing Composer script serves.
60- Where logs go was measured on 2026-11-14 ([probe results](probe-results.md#logs)): the runtime's stderr lines reach Cockpit unchanged, one entry per line.
60+ Where logs go was measured on 2026-11-14 ([probe results](probe-results.md#logs)): the runtime's stderr lines reach Cockpit one entry per line, with a `DEBUG - ` prefix and a ` source=user stream=stderr` suffix.
@@ -57,4 +57,4 @@ Done on 2026-11-14: the repository is on GitHub with CI green, the package is on
57 1. Verify a Command Invocation end to end on Scaleway: the e2e scenarios trigger CRON Invocations only through the Emulator, and the deployed one could only be observed in the logs.57 1. Verify a Command Invocation end to end on Scaleway: the e2e scenarios trigger CRON Invocations only through the Emulator, and the deployed one could only be observed in the logs.
58 2. Run the example deployment with `host` set, to check absolute URLs.58 2. Run the example deployment with `host` set, to check absolute URLs.
59 3. Measure what the probe left open: what a deployment with a failing Composer script serves.59 3. Measure what the probe left open: what a deployment with a failing Composer script serves.
60- Where logs go was measured on 2026-11-14 ([probe results](probe-results.md#logs)): the runtime's stderr lines reach Cockpit unchanged, one entry per line.60+ Where logs go was measured on 2026-11-14 ([probe results](probe-results.md#logs)): the runtime's stderr lines reach Cockpit one entry per line, with a `DEBUG - ` prefix and a ` source=user stream=stderr` suffix.
modified docs/probe-results.md +3 -1
@@ -99,7 +99,7 @@ An `array`: `{"memoryLimitInMb": 256, "functionName": "probe-php85", "functionVe
9999 Measured with the Loki HTTP API of the project's `Scaleway Logs` data source (`scw cockpit data-source list origin=scaleway types.0=logs`), with a `read_only_logs` Cockpit token sent as `Authorization: Bearer`; requests without it get `401`.
100100
101101 - stdout and stderr written by the handler both reach Cockpit, `echo` and `error_log()` alike, as does a `php://stderr` stream opened with `fopen()` like the runtime's.
102-- An entry is visible through the API 1.5 to 1.9 seconds after the Invocation (4 samples, polling every 0.5 to 1 second).
102+- An entry is visible through the API 1.5 to 2.4 seconds after the Invocation (7 samples, polling every 0.3 to 1 second).
103103 - Labels: `resource_type="serverless_function"`, `service_name="serverless_function"`, `resource_id` (the function's UUID), `resource_name` (`<namespace name and suffix>-<function name>`, e.g. `scalewayruntimeprobekzb5ehst-probe-php85`), `project_id`, `project_name`, `region`, and `detected_level`.
104104 - Each entry is a JSON line; `stream` tells stdout from stderr, `resource_instance` the instance:
105105
@@ -109,9 +109,11 @@ Measured with the Loki HTTP API of the project's `Scaleway Logs` data source (`s
109109 ```
110110
111111 - Everything the function writes is at the `DEBUG` level (`detected_level="debug"`), stderr included.
112+ The continuations of a split line (below) lack the `DEBUG - ` prefix and get `detected_level="unknown"`.
112113 - The platform logs its own lines with `source=core`: `Function is not ready to serve requests yet...` every second while an instance starts, `Function is now ready to serve requests!`, `Function Triggered: /` before each Invocation, and `INFO - Forked function has terminated` when an instance stops.
113114 - Each line is a separate entry: a stack trace written in one `fwrite()` gave 29 entries, one per line, with consecutive timestamps.
114115 - A line over 16 KiB is split into entries of 16384 characters (counting the `DEBUG - ` prefix), and cut after 64 KiB: lines of 65536 and 300000 bytes both ended in 4 entries, the last one ending with `... (truncated, line too long)`; a 20000 byte line arrived complete, in 2 entries.
116+- The runtime's own failure line, checked with a published `jdecool/scaleway-runtime` v0.1.0 application whose Front Controller throws, arrives the same way, one entry per line: `DEBUG - Invocation failed: RuntimeException: <message> in /home/app/function/public/index.php:6 source=user stream=stderr`, then `DEBUG - Stack trace: ...` and one entry per frame.
115117 - An uncaught exception logs its message only, on stderr (`probe: exception escaping the handler` above), without its class, file, line or trace; the Warm Process survives it (same pid, the counter went on).
116118
117119 ## Terraform
@@ -99,7 +99,7 @@ An `array`: `{"memoryLimitInMb": 256, "functionName": "probe-php85", "functionVe
99 Measured with the Loki HTTP API of the project's `Scaleway Logs` data source (`scw cockpit data-source list origin=scaleway types.0=logs`), with a `read_only_logs` Cockpit token sent as `Authorization: Bearer`; requests without it get `401`.99 Measured with the Loki HTTP API of the project's `Scaleway Logs` data source (`scw cockpit data-source list origin=scaleway types.0=logs`), with a `read_only_logs` Cockpit token sent as `Authorization: Bearer`; requests without it get `401`.
100 100
101 - stdout and stderr written by the handler both reach Cockpit, `echo` and `error_log()` alike, as does a `php://stderr` stream opened with `fopen()` like the runtime's.101 - stdout and stderr written by the handler both reach Cockpit, `echo` and `error_log()` alike, as does a `php://stderr` stream opened with `fopen()` like the runtime's.
102-- An entry is visible through the API 1.5 to 1.9 seconds after the Invocation (4 samples, polling every 0.5 to 1 second).102+- An entry is visible through the API 1.5 to 2.4 seconds after the Invocation (7 samples, polling every 0.3 to 1 second).
103 - Labels: `resource_type="serverless_function"`, `service_name="serverless_function"`, `resource_id` (the function's UUID), `resource_name` (`<namespace name and suffix>-<function name>`, e.g. `scalewayruntimeprobekzb5ehst-probe-php85`), `project_id`, `project_name`, `region`, and `detected_level`.103 - Labels: `resource_type="serverless_function"`, `service_name="serverless_function"`, `resource_id` (the function's UUID), `resource_name` (`<namespace name and suffix>-<function name>`, e.g. `scalewayruntimeprobekzb5ehst-probe-php85`), `project_id`, `project_name`, `region`, and `detected_level`.
104 - Each entry is a JSON line; `stream` tells stdout from stderr, `resource_instance` the instance:104 - Each entry is a JSON line; `stream` tells stdout from stderr, `resource_instance` the instance:
105 105
@@ -109,9 +109,11 @@ Measured with the Loki HTTP API of the project's `Scaleway Logs` data source (`s
109 ```109 ```
110 110
111 - Everything the function writes is at the `DEBUG` level (`detected_level="debug"`), stderr included.111 - Everything the function writes is at the `DEBUG` level (`detected_level="debug"`), stderr included.
112+ The continuations of a split line (below) lack the `DEBUG - ` prefix and get `detected_level="unknown"`.
112 - The platform logs its own lines with `source=core`: `Function is not ready to serve requests yet...` every second while an instance starts, `Function is now ready to serve requests!`, `Function Triggered: /` before each Invocation, and `INFO - Forked function has terminated` when an instance stops.113 - The platform logs its own lines with `source=core`: `Function is not ready to serve requests yet...` every second while an instance starts, `Function is now ready to serve requests!`, `Function Triggered: /` before each Invocation, and `INFO - Forked function has terminated` when an instance stops.
113 - Each line is a separate entry: a stack trace written in one `fwrite()` gave 29 entries, one per line, with consecutive timestamps.114 - Each line is a separate entry: a stack trace written in one `fwrite()` gave 29 entries, one per line, with consecutive timestamps.
114 - A line over 16 KiB is split into entries of 16384 characters (counting the `DEBUG - ` prefix), and cut after 64 KiB: lines of 65536 and 300000 bytes both ended in 4 entries, the last one ending with `... (truncated, line too long)`; a 20000 byte line arrived complete, in 2 entries.115 - A line over 16 KiB is split into entries of 16384 characters (counting the `DEBUG - ` prefix), and cut after 64 KiB: lines of 65536 and 300000 bytes both ended in 4 entries, the last one ending with `... (truncated, line too long)`; a 20000 byte line arrived complete, in 2 entries.
116+- The runtime's own failure line, checked with a published `jdecool/scaleway-runtime` v0.1.0 application whose Front Controller throws, arrives the same way, one entry per line: `DEBUG - Invocation failed: RuntimeException: <message> in /home/app/function/public/index.php:6 source=user stream=stderr`, then `DEBUG - Stack trace: ...` and one entry per frame.
115 - An uncaught exception logs its message only, on stderr (`probe: exception escaping the handler` above), without its class, file, line or trace; the Warm Process survives it (same pid, the counter went on).117 - An uncaught exception logs its message only, on stderr (`probe: exception escaping the handler` above), without its class, file, line or trace; the Warm Process survives it (same pid, the counter went on).
116 118
117 ## Terraform119 ## Terraform
modified probe/README.md +1 -1
@@ -60,7 +60,7 @@ done > 06-body-sizes.txt
6060 # 7. Output and failures: where does stdout go, what does an exception produce?
6161 curl -sS -i "$PROBE_URL/?mode=stdout" > 07-stdout.txt
6262 curl -sS -i "$PROBE_URL/?mode=exception" > 07-exception.txt
63-# A multi-line exception and a long line (bytes, 65536 by default) written to stderr: how are they split in the logs?
63+# A multi-line exception and a long line (bytes, 65536 by default, at most 1 MiB) written to stderr: how are they split in the logs?
6464 curl -sS -i "$PROBE_URL/?mode=stderr&bytes=65536" > 07-stderr.txt
6565
6666 # 8. Other methods and HEAD.
@@ -60,7 +60,7 @@ done > 06-body-sizes.txt
60 # 7. Output and failures: where does stdout go, what does an exception produce?60 # 7. Output and failures: where does stdout go, what does an exception produce?
61 curl -sS -i "$PROBE_URL/?mode=stdout" > 07-stdout.txt61 curl -sS -i "$PROBE_URL/?mode=stdout" > 07-stdout.txt
62 curl -sS -i "$PROBE_URL/?mode=exception" > 07-exception.txt62 curl -sS -i "$PROBE_URL/?mode=exception" > 07-exception.txt
63-# A multi-line exception and a long line (bytes, 65536 by default) written to stderr: how are they split in the logs?63+# A multi-line exception and a long line (bytes, 65536 by default, at most 1 MiB) written to stderr: how are they split in the logs?
64 curl -sS -i "$PROBE_URL/?mode=stderr&bytes=65536" > 07-stderr.txt64 curl -sS -i "$PROBE_URL/?mode=stderr&bytes=65536" > 07-stderr.txt
65 65
66 # 8. Other methods and HEAD.66 # 8. Other methods and HEAD.
modified probe/function/handler.php +2 -2
@@ -73,8 +73,8 @@ function handle($event, $context)
7373 // Written like the runtime logs its failures: does a multi-line exception stay one log entry?
7474 $stderr = fopen('php://stderr', 'w');
7575 fwrite($stderr, 'probe: written to stderr: '.new RuntimeException('probe: multi-line').PHP_EOL);
76- // Is a long line, like a whole stack trace on one line, kept or cut?
77- fwrite($stderr, 'probe: long line '.str_repeat('a', (int) ($query['bytes'] ?? 65536)).' end of the long line'.PHP_EOL);
76+ // Is a long line, like a whole stack trace on one line, kept or cut? Capped so the public probe cannot exhaust its memory.
77+ fwrite($stderr, 'probe: long line '.str_repeat('a', min(max((int) ($query['bytes'] ?? 65536), 0), 1 << 20)).' end of the long line'.PHP_EOL);
7878
7979 return ['statusCode' => 200, 'headers' => ['Content-Type' => 'text/plain'], 'body' => "Check the stderr lines in the logs.\n"];
8080
@@ -73,8 +73,8 @@ function handle($event, $context)
73 // Written like the runtime logs its failures: does a multi-line exception stay one log entry?73 // Written like the runtime logs its failures: does a multi-line exception stay one log entry?
74 $stderr = fopen('php://stderr', 'w');74 $stderr = fopen('php://stderr', 'w');
75 fwrite($stderr, 'probe: written to stderr: '.new RuntimeException('probe: multi-line').PHP_EOL);75 fwrite($stderr, 'probe: written to stderr: '.new RuntimeException('probe: multi-line').PHP_EOL);
76- // Is a long line, like a whole stack trace on one line, kept or cut?76+ // Is a long line, like a whole stack trace on one line, kept or cut? Capped so the public probe cannot exhaust its memory.
77- fwrite($stderr, 'probe: long line '.str_repeat('a', (int) ($query['bytes'] ?? 65536)).' end of the long line'.PHP_EOL);77+ fwrite($stderr, 'probe: long line '.str_repeat('a', min(max((int) ($query['bytes'] ?? 65536), 0), 1 << 20)).' end of the long line'.PHP_EOL);
78 78
79 return ['statusCode' => 200, 'headers' => ['Content-Type' => 'text/plain'], 'body' => "Check the stderr lines in the logs.\n"];79 return ['statusCode' => 200, 'headers' => ['Content-Type' => 'text/plain'], 'body' => "Check the stderr lines in the logs.\n"];
80 80