All Products
Search
Document Center

Function Compute (2.0):Configure logging

Last Updated:Mar 12, 2024

Simple Log Service can be integrated with Function Compute to provide the logging feature for functions. Function Compute automatically collects logs of functions and ships the logs to a specified Logstore in Simple Log Service after you configure the logging feature. You can view logs of a request, instance, or function in the Function Compute console. You can also use the log analysis feature in the Simple Log Service console to retrieve logs based on your business requirements.

Background

Simple Log Service is a one-stop log management service developed by Alibaba Cloud. Before you use Simple Log Service to store function logs, you must specify a project and Logstore in Simple Log Service for the corresponding service in Function Compute and grant the service permissions to access Simple Log Service. Function invocation logs are stored in the specified Logstore. All function invocation logs that are generated for a service in the same region are stored in the same Logstore.

Billing

Function Compute does not charge fees for using the logging feature. However, fees are generated by Simple Log Service when you use the logging feature. For more information, see Billing overview.

Configure the logging feature

Use the Function Compute console

Before you start

Procedure

  1. Log on to the Function Compute console. In the left-side navigation pane, click Services & Functions.

  2. In the top navigation bar, select a region. On the Services page, find the desired service and click Configure in the Actions column.

  3. In the Log Settings section of the Modify Service page, configure parameters and click Save.

    image

    Note
    • If you enable the logging feature when you create a service, the Function Compute console automatically creates a log project that is prefixed with aliyun-fc-cn-<region_id> and a default Logstore under the log project. Only one of such log project can be created in each region. If the system detects that such a log project has already been created in the region, the log project is directly used.

    • If you do not enable the logging feature when you create the service, you must manually specify Simple Log Service Project and Logstore when you update the service.

    • The system automatically enables Log Segmentation Rules, Request-level Metrics, and Instance-level Metrics, and creates all indexes that are required to query logs.

    Parameter

    Required

    Description

    Logging

    Yes

    Specify whether to enable the logging feature. Valid values:

    • Enable: If you enable the logging feature, Function Compute imports logs to the log project you specify. This way, you can query and retrieve logs in the Function Compute console or the Simple Log Service console and use the logs to troubleshoot issues.

      Note

      To enable the logging feature, you must use a role that is authorized to access Simple Log Service in the Role Settings section. For more information about service roles, see Grant Function Compute permissions to access other Alibaba Cloud services.

    • Disable: If you do not enable the logging feature, function logs are not persisted, which may make troubleshooting difficult.

    Configuration Mode

    Yes

    Valid values: Automatic Configuration and Custom Configuration. If you select Custom Configuration, you must specify Simple Log Service Project and Logstore.

    Simple Log Service Project

    Yes

    Specify the Simple Log Service project that stores function invocation logs.

    Logstore

    Yes

    Specify the Logstore that stores function invocation logs.

    Log Segmentation Rules

    No

    Specify whether to enable log segmentation. Valid values:

    • Enable: If you enable log segmentation, Function Compute splits logs into segments based on rules before writing the logs to Simple Log Service.

      The default log segmentation rule is ^.{0,2}\d{4}-\d{2}-\d{2}, which matches dates that conform to the xxxx-xx-xx format. x indicates digits. Function Compute splits logs based on the date condition. For example, a log that starts with 2023-10-10 is considered as the first line of a log entry and is written to Simple Log Service together with subsequent logs that do not contain a date. All these logs are considered as one log entry by Simple Log Service.

    • Disable: If you disable log segmentation, Function Compute splits logs by using \n by default.

    Request-level Metrics

    No

    Specify whether to import logs of request metrics to the Logstore. We recommend that you enable this feature.

    Valid values:

    • Enable: If you enable this mode, the metrics for each invocation of a function, such as the execution time, memory usage, execution errors, cold start status, and the duration of each cold start step, are delivered to the specified Logstore. Function Compute lists requests in invocation logs based on these metrics. You can also filter requests based on the metrics. For example, you can filter all requests for which errors are reported and requests in which cold starts occur.

    • Disabled: If you disable this mode, you cannot view the execution details of requests.

    Instance-level Metrics

    No

    Specify whether to enable collection of instance-level metrics. Valid values:

    • Enable: If you enable this mode, metric data of function instances is shipped to the specified Logstore. This way, you can view the performance metrics of function instances, such as CPU, memory, and network I/O usage. Function Compute provides instance-level observability based on these metrics. You can monitor function instances or troubleshoot issues in an end-to-end manner. For more information, see Instance-level metrics.

    • Disable: If you disable this mode, you cannot view details of instance metrics.

Use Serverless Devs

Before you start

Procedure

  1. Create a code directory. The following sample code shows the directory structure:

    .
    ├── code
    │   └── index.js
    └── s.yaml

    The following sample code shows an s.yaml file.

    In this example, the logConfig section is used to configure the logging feature. You must specify the logstore and project fields based on your log project and Logstore. You can also set logConfig to auto to allow Function Compute automatically creates a log project and a Logstore for you.

    edition: 1.0.0
    name: hello-world-app
    access: "default"
    
    vars: 
      region: "cn-hangzhou"
      service:
        name: "hello-world-service"
        description: 'hello world by serverless devs'
        logConfig: # Specify your project and Logstore. Alternatively, set this field to auto to allow the system to create a project and a Logstore for you.
          project: 'your-project'
          logstore: 'your-logstore'
          enableRequestMetrics: true
          enableInstanceMetrics: true
        role: 'acs:ram::<accountID>:role/aliyunfcdefaultrole'
    
    services:
      helloworld:
        component: fc 
        props:
          region: ${vars.region} 
          service: ${vars.service}
          function:
            name: "hello-world"
            description: 'hello world by serverless devs'
            runtime: nodejs14
            codeUri: ./code
            handler: index.handler
            memorySize: 128
            timeout: 60
  2. Run the following command to deploy the application:

    sudo s deploy -y

    After you run the command, you can log on to the Function Compute console to check whether the function is deployed and the logging feature is enabled.

View function invocation logs

To view invocation logs of a function, click the Logs tab on the details page of the function.

Note

You can change the version or alias of the function in the Version or Alias drop-down list in the upper part of the page to query the invocation logs of the specific version or alias.

You can view function invocation logs on the Request List tab or the Search by Keyword tab. The following items describe the differences of the information displayed on the two tabs:

  • Request List tab

    The function invocation time, request ID, invocation result, instance ID, and function version and alias are displayed on the Call Request List tab. The following figure shows an example.item1

    • Click the request ID of an invocation record to view the details of the request and logs.

    • Click the instance ID of an invocation record to view the resource usage of the instance, such as CPU utilization, memory usage, and network traffic.

    • Click View Logs in the Actions column that corresponds to an invocation record to view the log details. Click Advanced Logs in the Actions column to go to the Simple Log Service console to query and analyze logs.

  • Search by Keyword tab

    The function invocation time, log content, and function version and alias are displayed on the Search by Keyword tab. The following figure shows an example. You can click Log Context in the Actions column to search for context logs of the log entry. You can also click Logstore in the Actions column to go to the Simple Log Service console to query and analyze logs.item2

    You can enter a keyword in the search box to filter logs. For example, if you want to search for a log that contains a hello world, you can enter the keyword hello world in the search box to search all logs that contain the keyword. The following figure shows an example. For more information about the syntax of keywords, see Search syntax.item3

References

You can also call an SDK or API operation to configure logging for functions. For more information, see CreateService and LogConfig.