Skip to content

Switch on trace logs and read them

When something goes wrong in production you want to see what a function decided, without paying to log everything all the time. Trace logs are opt-in and time-boxed: the function writes to named contexts, and nothing is stored unless an operator switches a context on, for a limited time. The rest of the time a trace call is a cheap no-op. The reasoning is in Audit, traces and logs.

A context is a name that starts with a lower-case letter, then lower-case letters, digits or -, at most 40 characters, at most 20 per function. Declare it in code with an optional description (at most 200 bytes of UTF-8) that operators see next to the switch. http is built in and needs no declaration; a function cannot write to it.

import { fn } from "@flowcatalyst/fn";
fn.traceContexts({ name: "orders", description: "What the order mapping decided, line by line." });
fn.webhook("POST /events/order-created", (req) => {
const order = req.json() as { tripNumber: string; lines: unknown[] };
req.setReference(order.tripNumber); // the business key; stamps every later record of this call
req.trace("orders", "mapping order", { lines: order.lines.length });
if (req.tracing("orders")) {
// only build an expensive payload when someone is looking
req.trace("orders", "full order", { order }, { level: "debug" });
}
return fn.status(202);
});
Go Rust Java
Declare fn.TraceContexts(fn.TraceContext{Name: "orders", Description: "..."}) app.trace_context("orders", "...") traceContexts in function.json
Record fn.Trace(ctx, "orders", "mapping order", map[string]any{"lines": n}, fn.WithLevel(fn.TraceWarn), fn.WithReference(trip)) ctx.trace("orders", "mapping order", Some(&data), TraceOpts::new().level(TraceLevel::Warn).reference(trip)) ctx.trace("orders", "mapping order", Map.of("lines", 3))
Ask fn.Tracing(ctx, "orders") ctx.tracing("orders") ctx.tracing("orders")
Business key fn.SetReference(ctx, trip) ctx.set_reference(trip) ctx.setReference(trip)
  • level is debug, info (the default), warn or error. data is an optional JSON object.
  • A reference is your business key (a trip or order number, at most 200 characters). setReference tells the runner, which stamps it on every later record of the call, the built-in http and log records included. A reference passed to a single trace call is that record’s only.
  • Tracing never fails a call. Trace and tracing calls never throw or return an error. If the context is off, was not sampled in, is not declared (one warning per context is written to the function log), or the runner is older and does not know trace logs, the call does nothing and tracing answers false.
  • Ask tracing before building anything expensive. Its answer depends on the invocation, so do not remember it.

In the console, open the function, then its Diagnostics section for the switches and its View trace logs page for the records.

From the command line:

Terminal window
fcdev fn trace on billing.invoice-pdf orders --for 10m --sample 2 # live until 12:20:45 (10m)
fcdev fn trace off billing.invoice-pdf orders
fcdev fn trace status billing.invoice-pdf # contexts, live or off, time left, sample
fcdev fn trace log billing.invoice-pdf --reference T-1042 --since 30m
fcdev fn trace log billing.invoice-pdf --context http --bodies -f
fcdev fn trace run billing.invoice-pdf orders # on, follow, and back as it was on Ctrl-C

An operator picks how long and may sample, recording one call in N. The switch takes effect within about a second and turns itself off when the time is up.

Context Longest window
your own contexts 60 minutes
http 15 minutes (its bodies can be sensitive); never “until switched off”
your own contexts, “until switched off” at most 7 days (FC_FUNCTIONS_TRACE_INDEFINITE_MAX_DAYS); needs a confirmation in the dialog or --no-limit

While http is on, every call also records its request, response, timing and outcome, together with everything the call printed or logged.

Permission Allows
platform:function:trace:manage switch contexts on and off
platform:function:trace:view read records: time, context, kind, level, invocation, correlation id, reference and version; of an http record only its method, path (no query string), status, duration, outcome and body sizes
platform:function:trace:view-bodies the rest: headers, query string, bodies, printed and logged lines, and the data of your trace records. Switching http on needs it too
platform:function:trace:indefinite leaving one of your own contexts on until switched off

The read-only platform:viewer role does not read trace records, and a CI credential with only platform:function-deployer may not use trace logs. Switching a context on or off is recorded in the audit log.

fcdev fn trace takes the same platform, profile and credential flags as the other fn commands.

  • --for takes a duration (5m, 1h), rounded up to whole minutes. --sample N records one call in N.
  • log prints the newest --limit records (default 100), oldest first, filtered by --context, --kind, --level, --reference, --correlation, --invocation, --version, --since and --until (a duration back from now, an RFC 3339 time or unix milliseconds).
  • An http record is one line, POST /orders -> 200 (12ms, ok). --bodies adds the request and response bodies (JSON pretty-printed; base64 and truncation are noted; hidden without view-bodies). -f polls every 2 seconds and prints only new records. --json prints one raw JSON record per line.
  • run shows only records from now on and stops when the window ends. If the context is already on for at least as long as --for, run only follows it and leaves it on; otherwise it switches it on and, on exit, puts it back as it found it unless you pass --keep. A window someone else changes while run is following is left as they set it.
Limit Value
a message 4096 bytes
a record’s data 32768 bytes (a record over the limit is dropped, never an error)
rate 200 records a second per function per platform process; the excess is counted as dropped
one call at most 500 log records and 500 trace records
HTTP bodies cut at 64 KiB

The value of any request or response header whose name contains auth, key, token, secret, password, signature, cookie or session is replaced by [redacted]. That covers authorization, x-api-key, set-cookie and the delivery signature x-flowcatalyst-signature. The same goes for a query parameter with one of those names, or named access_token, code, token, sig, signature, X-Amz-*, key, apikey, api_key, password or secret.

Each record has the time, function version, invocation id, correlation id (the request’s X-Correlation-Id), reference, context, kind (trace, http, log, or outbound for a JVM function), level, message and data. Records are shipped by the runner in the background, so they survive whatever happens to the call, and are kept for 30 days after they reach the platform and then deleted.

A JVM function declares traceContexts in its describe and writes with ctx.trace; everything above applies. Its built-in http context also records the lines the function logged through ctx.logger() (every level) and an outbound record for each call made with ctx.http(). The host replaces your declared secrets, the values you give ctx.conceal(value) and any credential an outbound answer hands out (SessionToken, access_token and similar in a JSON answer), and every query value except paging and time-window names (limit, offset, page, since, until) is [redacted]. Call ctx.conceal for a credential the function obtains at run time, in init too.

For tracing an ingestion rather than a function, see Troubleshoot ingestion.