Specifications

更新时间:
复制 MD 格式

A custom runtime is an HTTP server you implement inside a Function Compute execution environment. Function Compute communicates with it through a defined HTTP protocol: it sends requests with specific headers, expects specific response codes and headers, and collects logs in a specific format.

This page defines that contract.

A compliant custom runtime must:

  • Read the x-fc-control-path header to determine whether the request is an event invocation, an HTTP invocation, or an initialization call

  • Return StatusCode and x-fc-status in every HTTP response

  • Write FC Invoke Start RequestId: and FC Invoke End RequestId: to stdout for every invocation

Request headers

Function Compute automatically injects the following headers into every request it sends to your custom runtime. Both event functions and HTTP functions receive these headers.

The temporary AccessKey headers (x-fc-access-key-id, x-fc-access-key-secret, x-fc-security-token) are included so your runtime can call other Alibaba Cloud services. If you are migrating an existing application, you can ignore these headers.
HeaderDescription
x-fc-request-idThe request ID. Include this in your logs to correlate log entries with invocations.
x-fc-access-key-idTemporary AccessKey ID for calling other Alibaba Cloud services.
x-fc-access-key-secretTemporary AccessKey secret.
x-fc-security-tokenTemporary security token.
x-fc-function-handlerThe function handler. For custom runtimes and custom containers, where the runtime itself is the function, this value is not meaningful—set it to any string.
x-fc-function-memoryThe maximum memory the function can use.
x-fc-function-initializerThe Initializer function handler. For custom runtimes and custom containers, this value is not meaningful—set it to any string.
x-fc-initialization-timeoutThe timeout period for the Initializer function.
x-fc-instance-lifecycle-pre-stop-handlerThe PreStop lifecycle hook handler. For custom runtimes and custom containers, this value is not meaningful—set it to any string.
x-fc-instance-lifecycle-pre-freeze-handlerThe PreFreeze lifecycle hook handler. For custom runtimes and custom containers, this value is not meaningful—set it to any string.
x-fc-regionThe region where the function runs.
x-fc-account-idThe UID of the function owner.
x-fc-qualifierThe service version or alias used when invoking the function. For more information, see Use versions and aliases to implement phased releases.
x-fc-version-idThe service version used when invoking the function.
x-fc-function-nameThe function name.
x-fc-service-nameThe name of the service the function belongs to.
x-fc-service-logprojectThe Simple Log Service log project configured for the service.
x-fc-service-logstoreThe Logstore configured for the service.
x-fc-control-pathThe request type. Use this header to determine how to handle the incoming request.

x-fc-control-path values

ValueRequest typeDescription
/invokeEvent function invocationAn Invoke call. Handle the event payload and return a result.
/http-invokeHTTP function invocationAn HTTP Invoke call. Function Compute forwards the original request—path, body, and headers—along with the common headers. Return the response headers and body directly to the client.
/initializeInitializationFunction Compute sends this request once when the execution environment is first created, similar to a class constructor. Only one successful initialization call occurs per container lifecycle.

Response codes and response headers

Every function invocation is an HTTP request to your runtime. Each response must include both a StatusCode and an x-fc-status header.

Note

Set both StatusCode and x-fc-status in every HTTP response.

FieldRequiredValuesDescription
StatusCodeYes200 (success), 404 (failure)The standard HTTP response code.
x-fc-statusYes200 (success), 404 (failure)Reports execution status to Function Compute.

Why `x-fc-status` matters

If x-fc-status is not set, Function Compute assumes the invocation succeeded, even if your function threw an exception. This does not affect your business logic, but it prevents Function Compute from recording the failure in monitoring and observability data.

image8hanshujisuanruntime

When x-fc-status is set, Function Compute correctly records failures. Print the error stack to stdout alongside the response.

image9runtimefc

Log format

Enable logging when creating a service. All output written to stdout in a custom runtime is automatically collected in the Simple Log Service you specify. For more information, see Configure logs.

Important

If you invoke a function anonymously through an HTTP trigger, including x-fc-log-type: Tail in the request header may expose sensitive information. Enable authentication to prevent this. For more information, see Authentication.

For runtime environments other than custom runtime and custom container: when the request header includes x-fc-log-type: Tail, the x-fc-log-result response header contains up to 4 KB of execution logs, viewable in the Function Compute console.

For custom runtimes, write the start and end log entries below to stdout. Function Compute uses these entries to display logs in the console execution results.

Required log entries

Log contentRequiredLog string
Runtime startNo — flags a cold startFunctionCompute ${runtime} runtime inited. Use a custom name for ${runtime}; avoid official Function Compute language names such as Node.js, Python, or PHP.
Invoke startYesFC Invoke Start RequestId: ${RequestId}
Invoke endYesFC Invoke End RequestId: ${RequestId}
Initialize startNo — required if using the Initializer functionFC Initialize Start RequestId: ${RequestId}
Initialize endNo — required if using the Initializer functionFC Initialize End RequestId: ${RequestId}

Recommended log format

Include the request ID in all log entries to make issue diagnosis straightforward. The recommended format is:

$utcdatetime(yyyy-MM-ddTHH:mm:ss.fff) $requestId [$Level] $message

Log level interfaces vary by language. Set the log level based on your runtime environment. For more information, see Basic information.

What's next