All Products
Search
Document Center

Function Compute:Monitoring data

Last Updated:Feb 05, 2024

This topic describes how to query Function Compute metrics by using the CloudMonitor API. You can call relevant API operations and use different request parameters to obtain the monitoring data of Function Compute. These parameters include Project, StartTime, EndTime, Dimensions, Period, and Metric.

For more information about API operations, see List of operations by function.

Project

All Function Compute metrics are available for querying under the acs_fc project.

The following sample code shows how to configure the project by using SDK for Java:

QueryMetricRequest request = new QueryMetricRequest();
request.setProject("acs_fc");

StartTime and EndTime

In CloudMonitor, a time range is defined by the StartTime and EndTime parameters in the (StartTime, EndTime] form. The time range is exclusive with respect to StartTime and inclusive with respect to EndTime.

Note

CloudMonitor retains data for 31 days. For this reason, the time range between StartTime and EndTime cannot exceed 31 days, and data earlier than 31 days cannot be queried.

For more information about other time parameters, see List of operations by function.

The following sample code shows how to configure the project by using SDK for Java:

request.setStartTime("2023-08-26 08:00:00");
request.setEndTime("2023-08-26 09:00:00");        

Dimensions

The monitoring service of Function Compute divides Function Compute metrics in terms of the region, service, and function dimensions. Each dimension uses a different format for the value of the Dimensions parameter.

  • The Dimensions parameter for regional metrics is in the following format:

    {"region": "${your_region}"}    
  • The Dimensions parameter for function metrics is in the following format:

    {"region": "${your_region}", "functionName": "${your_functionName}"}
Note

The value of the Dimensions parameter for all Function Compute metrics is a JSON string that contains a single key-value pair. The following sample code shows how to configure the project by using SDK for Java:

request.setDimensions("{\"region\":\"your_region\"}");

Period

The granularity of the aggregation period for Function Compute metrics must be 60 seconds.

The following sample code shows how to configure the project by using SDK for Java:

request.setPeriod("60");

Metric

The following sample code shows how to configure the project by using SDK for Java:

request.setMetric("your_metric");

The following table lists the metrics that are described in Function Compute documentation.

Dimension

Metric

Description

Region

RegionTotalInvocations

TotalInvocations

RegionBillableInvocations

BillableInvocations

RegionThrottles

Throttles

RegionClientErrors

ClientErrors

RegionServerErrors

ServerErrors

RegionBillableInvocationsRate

The BillableInvocations ratio.

RegionThrottlesRate

The throttling ratio.

RegionClientErrorsRate

The client error ratio.

RegionServerErrorsRate

The server error ratio.

Service

ServiceTotalInvocations

TotalInvocations

ServiceBillableInvocations

BillableInvocations

ServiceThrottles

Throttles

ServiceClientErrors

ClientErrors

ServiceServerErrors

ServerErrors

ServiceBillableInvocationsRate

The billable invocation ratio.

ServiceThrottlesRate

The throttling ratio.

ServiceClientErrorsRate

The client error ratio.

ServiceServerErrorsRate

The server error ratio.

Function

FunctionTotalInvocations

TotalInvocations

FunctionBillableInvocations

BillableInvocations

FunctionThrottles

Throttles

FunctionErrors

FunctionErrors

FunctionClientErrors

ClientErrors

FunctionServerErrors

ServerErrors

FunctionBillableInvocationsRate

The billable invocation ratio.

FunctionThrottlesRate

The throttling ratio.

FunctionErrorsRate

The function error ratio.

FunctionClientErrorsRate

The client error ratio.

FunctionServerErrorsRate

The server error ratio.

FunctionAvgDuration

The average duration.

FunctionMaxMemoryUsage

The maximum memory usage.

Sample code

The following text shows a sample pom.xml file:

...
    <dependencies>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-cms</artifactId>
            <version>5.0.1</version>
        </dependency>
    </dependencies>
...
            

Sample code:

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.cms.model.v20170301.QueryMetricListRequest;
import com.aliyuncs.cms.model.v20170301.QueryMetricListResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

public class MonitorService {
    public static void main(String[] args) {
        /*
        The AccessKey pair of an Alibaba Cloud account can be used to access all API operations. Using these credentials to perform operations in Function Compute is a high-risk operation. We recommend that you use a Resource Access Management (RAM) user to call API operations or perform routine O&M. 
        We recommend that you do not save the AccessKey ID and AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources in your account may be compromised. 
        In this example, the AccessKey pair is saved to the environment variables for authentication. 
        Configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables in your local environment before you run sample code. 
        In Function Compute runtimes, the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are automatically configured after you configure the execution permissions. 
        */
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessSecretKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKey, accessSecretKey);
        IAcsClient client = new DefaultAcsClient(profile);


        QueryMetricListRequest request = new QueryMetricListRequest();
        request.setProject("acs_fc");
        request.setPeriod("60");
        request.setStartTime("2023-08-26 16:20:00");
        request.setEndTime("2023-08-26 16:30:00");
        request.setAcceptFormat(FormatType.JSON);

        try {
            // Region dimension. JSONObject dim = new JSONObject();
            request.setMetric("RegionTotalInvocations");  // Specify the metric. 
            dim.put("region", "<your_region>");  //Example: cn-shanghai
            request.setDimensions(dim.toJSONString());
            QueryMetricListResponse response = client.getAcsResponse(request);
            System.out.println(response.getCode());
            System.out.println(response.getMessage());
            System.out.println(response.getRequestId());
            System.out.println(response.getDatapoints());

            // Function dimension. dim = new JSONObject();
            request.setMetric("FunctionTotalInvocations");  // Specify the metric. 
            dim.put("region", "<your_region>");
            dim.put("functionName", "<your_function_name>");
            request.setDimensions(dim.toJSONString());
            response = client.getAcsResponse(request);
            System.out.println(response.getCode());
            System.out.println(response.getMessage());
            System.out.println(response.getRequestId());
            System.out.println(response.getDatapoints());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}