This topic describes the basic concepts related to code development in Function Compute, including handlers, lifecycle hooks for function instances, logging, and error handling.
Handlers
You must specify a handler when creating a function. The Function Compute runtime loads and calls this handler to process requests. Handlers are classified into the following types:
Event handler
An event handler is used to process requests from various event sources other than HTTP triggers. Such event sources include Object Storage Service (OSS), Simple Log Service, and ApsaraMQ for RocketMQ triggers.
HTTP handler
An HTTP handler is used to process HTTP requests. For more information, see Configure and use an HTTP trigger.
To configure the handler for your function, specify the Handler parameter within the Function Compute console. For more information, see Create a function.
Lifecycle hooks for function instances
In on-demand mode, instances are automatically created as needed. When they are not processing requests, on-demand instances are frozen. They will be destroyed if they remain in this frozen state for a certain period of time. You can configure lifecycle hooks for instances to perform callback operations when the instance status changes. The following lifecycle hooks can be configured in Function Compute: Initializer, PreFreeze, and PreStop. For more information, see Function instance lifecycle.
Initializer hook
An Initializer hook is invoked after an instance is initialized, but before the handler is executed. Function Compute ensures that the Initializer hook is executed only once throughout an instance's lifecycle. If the execution fails, the current instance is destroyed, and a new one is created to retry the execution.
You can incorporate time-consuming service logic into the Initializer hook. Doing so eliminates the need to run such service logic every time the function is called, thereby decreasing the function's latency. Examples of time-consuming service logic include creating connection pools, loading function dependencies, and performing other database-related tasks.
PreFreeze hook
A PreFreeze hook is executed before a function instance is frozen. You can use this hook to perform necessary operations before the instance is frozen, such as waiting for metrics to be successfully sent.
PreStop hook
A PreStop hook is executed before a function instance is destroyed. You can utilize this hook to carry out specific tasks prior to an instance's destruction. For example, you can configure a PreStop hook to ensure that database connections are closed and the status is reported and updated before the instance is destroyed.
Log management
You must configure a service-level Logstore to store the function logs of Function Compute. For more information, see Configure logging.
If you enable the logging feature when you create a service in the Function Compute console, a Logstore is automatically set up for you.
Function Compute is integrated with Simple Log Service to store function invocation logs and logs that are printed in the function code to the Logstore. You can use the logging statements provided by Function Compute to record function logs, which facilitates debugging and troubleshooting. The following table describes the built-in log printing statements of different programming languages, along with the logging statements provided by Function Compute.
Programming language | Built-in log printing statement of the programming language | Logging statement provided by Function Compute | References |
Node.js | console.log() | context.logger.info() | |
Python | print() | logging.getLogger().info() | |
Java | System.out.println() | context.getLogger().info() | |
PHP | echo "" . PHP_EOL | $GLOBALS['fcLogger']->info() | |
C# | Console.WriteLine("") | context.Logger.LogInformation() |
Both the logs printed by using the programming languages' built-in log printing statements and the logging statements provided by Function Compute are collected and stored in the Logstore. The latter logs are tagged with request IDs for easier filtering.
# Log printed by using the built-in log printing statement of the programming language
# print('hello world')
message: hello world
# Log printed by using the logging statements provided by Function Compute
# logger.info('hello world')
message: 2020-03-13T04:06:49.099Z f84a9f4f-2dfb-41b0-9d6c-1682a2f3a650 [INFO] hello world
Log elements
A function execution log contains the service name, function name, current version, alias, and code logs.
The following example shows the data structure of a function execution log:
__source__:
__tag__:__receive_time__: 1584072413
__topic__: myService
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
FC Invoke Start RequestId: f84a9f4f-2dfb-41b0-9d6c-1682a2f3a650
when the function execution starts.The system prints
FC Invoke End RequestId: f84a9f4f-2dfb-41b0-9d6c-1682a2f3a650
when the function execution ends.
Error handling
There are two types of errors in Function Compute: HandledInvocationError
and UnhandledInvocationError
.
HandledInvocationError
Only the errors returned by the
callback
parameter in Node.js are of theHandledInvocationError
type. Error information is contained in the response.'use strict'; module.exports.handler = function(event, context, callback) { console.log('hello world'); callback('this is error', 'hello world'); }
The following is an example of the error message:
{"errorMessage":"this is error"}
UnhandledInvocationError
All errors, except those classified as
HandledInvocationError
errors, fall under theUnhandledInvocationError
category.The
stackTrace
property of anUnhandledInvocationError
error is printed in logs. You can view the logs to find the correspondingstackTrace
based on the context.
Operating system environment
Function Compute uses Debian 9 LTS for execution, and only the x86_64 architecture is supported.
You can modify the operating system environment by configuring layers or environment variables. For example, you can configure environment variables to change the time zone. By default, function instances run in UTC. If you set the value of the environment variable TZ to Asia/Shanghai
, Function Compute changes its time zone to UTC+8. For more information, see Manage layers and Environment variables.