This topic covers handlers, instance lifecycle hooks, logging, and operating systems in Function Compute.
Handlers
Every function requires a handler. The Function Compute runtime loads and calls this handler to process incoming requests.
Built-in runtimes
Handlers in built-in runtimes follow a standard format. They process events from trigger sources such as Object Storage Service (OSS), Simple Log Service, ApsaraMQ for RocketMQ, and EventBridge triggers. You can set up an HTTP trigger for a function, which provides a Uniform Resource Identifier (URI) endpoint for direct HTTP invocation.
Custom and Custom Container runtimes
Handlers in custom runtimes or Custom Container runtimes differ from built-in runtimes. For details, see Web functions.
Configure the handler
Set the Handler parameter in the Function Compute console. For more information, see Create a function.
Instance lifecycle hooks
In on-demand mode, Function Compute automatically creates instances to handle incoming requests. On-demand instances are frozen when not processing requests. Instances that remain frozen for a certain period are destroyed.
Configure lifecycle hooks to run custom logic when an instance's status changes. All runtime types support both the Initializer and PreStop hooks. For more information, see Configure instance lifecycles.
Initializer hook
The Initializer hook runs after an instance starts but before the handler runs. Function Compute guarantees the Initializer hook runs only once per instance and must complete successfully. If it fails, the behavior depends on the invocation type:
Synchronous invocations -- the system returns the error.
Asynchronous invocations -- the system retries based on the configured retry policy.
Place time-consuming initialization logic in the Initializer hook to avoid repeating it on every invocation. Common examples:
Creating connection pools
Loading function dependencies
Database-related setup tasks
PreStop hook
The PreStop hook runs before a function instance is destroyed. Use it for cleanup tasks such as:
Closing database connections
Reporting and updating the instance's status
Logs
Function Compute integrates with Simple Log Service to store function invocation logs and logs printed in function code.
Configure a service-level Logstore to store function logs. For more information, see Configure the logging feature.
If logging is enabled when creating a function, the Function Compute console automatically sets up a default Logstore.
Logging statements
Each supported language has a built-in log statement and a Function Compute log statement. Function Compute log statements tag each entry with a request ID, which makes filtering easier.
Language | Built-in statement | Function Compute statement | Reference |
Node.js |
|
| |
Python |
|
| |
Java |
|
| |
PHP |
|
| |
C# |
|
| |
Golang |
|
|
Both types of statements send logs to the Logstore. The difference is that Function Compute statements tag each log entry with a request ID:
# Built-in statement: print('hello world')
message: hello world
# Function Compute statement: logger.info('hello world')
message: 2020-03-13T04:06:49.099Z f84a9f4f-2dfb-41b0-9d6c-1682a2f3a650 [INFO] hello worldLog structure
A function execution log contains the service name, function name, current version, alias, and code logs. The following example shows the data structure:
__source__:
__tag__:__receive_time__: 1584072413
__topic__: FCLogs:myFunction
functionName: myFunction
message: 2020-03-13T04:06:49.099Z f84a9f4f-2dfb-41b0-9d6c-1682a2f3a650 [INFO] hello world
qualifier: LATEST
serviceName: myService
versionId:The system prints the following markers at the start and end of each function execution:
Start:
FC Invoke Start RequestId: f84a9f4f-2dfb-41b0-9d6c-1682a2f3a650End:
FC Invoke End RequestId: f84a9f4f-2dfb-41b0-9d6c-1682a2f3a650
Operating systems
Built-in runtimes run on Debian 9 LTS, Debian 10 LTS, Debian 11 LTS, or Debian 12 LTS. Only the x86_64 architecture is supported.
Customize the operating system through layers or environment variables. For example, Function Compute uses UTC by default. Set the environment variable TZ to Asia/Shanghai to switch to UTC+8. For more information, see Configure layers and Configure environment variables.