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.
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 | ||
| 383 | 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 | 384 | The runtime logs its own failures to stderr, as Monolog does with a `php://stderr` handler. |
| 385 | 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 | 387 | An entry looks like this, with the `resource_id` label holding the function's UUID: |
| 388 | 388 | |
| 389 | 389 | ```json |
| @@ -397,18 +397,19 @@ From the command line, query Loki's HTTP API with a token that can only read log | ||
| 397 | 397 | ```sh |
| 398 | 398 | LOKI_URL=$(scw cockpit data-source list origin=scaleway types.0=logs -o json | jq -r '.[0].url') |
| 399 | 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. | |
| 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") | |
| 403 | 403 | |
| 404 | 404 | curl -sS -G "$LOKI_URL/loki/api/v1/query_range" -H "Authorization: Bearer $LOKI_TOKEN" \ |
| 405 | 405 | --data-urlencode "query={resource_id=\"$FUNCTION_ID\"} | json | stream=\"stderr\" | line_format \"{{.message}}\"" \ |
| 406 | 406 | --data-urlencode since=1h --data-urlencode limit=1000 \ |
| 407 | 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.json | |
| 409 | +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 | 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 | 415 | ## 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 | ```json | 389 | ```json |
| @@ -397,18 +397,19 @@ From the command line, query Loki's HTTP API with a token that can only read log | |||
| 397 | ```sh | 397 | ```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.json | 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.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.json | 409 | +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 Emulator | 415 | ## 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 | ||
| 57 | 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 | 58 | 2. Run the example deployment with `host` set, to check absolute URLs. |
| 59 | 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. | |
| @@ -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 | ||
| 99 | 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 | 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 | 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 | 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 | 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 | 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 | 114 | - Each line is a separate entry: a stack trace written in one `fwrite()` gave 29 entries, one per line, with consecutive timestamps. |
| 114 | 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 | 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 | 119 | ## 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 | ## Terraform | 119 | ## Terraform |
modified
probe/README.md +1 -1 | @@ -60,7 +60,7 @@ done > 06-body-sizes.txt | ||
| 60 | 60 | # 7. Output and failures: where does stdout go, what does an exception produce? |
| 61 | 61 | curl -sS -i "$PROBE_URL/?mode=stdout" > 07-stdout.txt |
| 62 | 62 | 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 | 64 | curl -sS -i "$PROBE_URL/?mode=stderr&bytes=65536" > 07-stderr.txt |
| 65 | 65 | |
| 66 | 66 | # 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.txt | 61 | curl -sS -i "$PROBE_URL/?mode=stdout" > 07-stdout.txt |
| 62 | curl -sS -i "$PROBE_URL/?mode=exception" > 07-exception.txt | 62 | 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.txt | 64 | 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) | ||
| 73 | 73 | // Written like the runtime logs its failures: does a multi-line exception stay one log entry? |
| 74 | 74 | $stderr = fopen('php://stderr', 'w'); |
| 75 | 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? | |
| 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); | |
| 78 | 78 | |
| 79 | 79 | return ['statusCode' => 200, 'headers' => ['Content-Type' => 'text/plain'], 'body' => "Check the stderr lines in the logs.\n"]; |
| 80 | 80 | |
| @@ -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 | ||