This topic describes how to retrieve monitoring data for Function Compute using Cloud Monitor API operations. To retrieve the data, you must specify parameters such as Project, StartTime, EndTime, Dimensions, Period, and Metric.
For more information about API operations, see API overview.
Project
All monitoring metrics for Function Compute use the same project name: acs_fc.
The following code shows an example of how to set the project using the Java software development kit (SDK):
QueryMetricRequest request = new QueryMetricRequest();
request.setProject("acs_fc");StartTime and EndTime
The time range for Cloud Monitor time parameters is a left-open and right-closed interval, which is represented as (StartTime, EndTime]. This means that data at the StartTime boundary is not retrieved, but data at the EndTime boundary is retrieved.
The retention period for Cloud Monitor data is 31 days. The time interval between StartTime and EndTime cannot exceed 31 days. Data older than 31 days cannot be retrieved.
For more information about other time parameters, see API overview.
The following code shows an example of Java SDK configuration:
request.setStartTime("2024-07-19 08:00:00");
request.setEndTime("2024-07-19 09:00:00"); Dimensions
Based on the resource structure and scenarios of Function Compute, monitoring metrics are categorized into region, service, and function dimensions. The `Dimensions` parameter is set differently for each dimension.
Set the `Dimensions` parameter for region-level data as follows:
{"region": "${your_region}"}Set the `Dimensions` parameter for function-level data as follows:
{"region": "${your_region}", "functionName": "${your_functionName}"}
Dimensions is a JSON string that contains one or more key-value pairs to represent different monitoring dimensions. The following code shows an example of how to set the dimensions using the Java SDK:
request.setDimensions("{\"region\":\"your_region\"}");Period
The aggregation granularity for all Function Compute monitoring metrics is 60 seconds.
The following code example shows how to configure the Java SDK:
request.setPeriod("60");Metric
The following code example shows how to use the Java SDK:
request.setMetric("your_metric");The following table describes the `Metric` names that correspond to the monitoring metrics for Function Compute.
Metric dimension | Metric | Metric name |
Region | RegionTotalInvocations | Function invocations |
RegionServerErrors | Server-side errors | |
RegionClientErrors | Client-side errors | |
RegionFunctionErrors | Function errors | |
RegionThrottles | Throttling errors due to concurrent instance limit exceeded | |
RegionResourceThrottles | Throttling errors due to total instance limit exceeded | |
RegionConcurrencyLimit | On-demand instance quota | |
RegionConcurrentCount | Number of on-demand instances | |
RegionProvisionedCurrentInstance | Number of provisioned instances | |
Function | FunctionTotalInvocations | Total function invocations |
FunctionProvisionInvocations | Invocations in provisioned mode | |
FunctionHTTPStatus2xx | Number of requests with HTTP status code 2xx | |
FunctionHTTPStatus3xx | Number of requests with HTTP status code 3xx | |
FunctionHTTPStatus4xx | Number of requests with HTTP status code 4xx | |
FunctionHTTPStatus5xx | Number of requests with HTTP status code 5xx | |
FunctionServerErrors | Server-side errors | |
FunctionClientErrors | Client-side errors | |
FunctionFunctionErrors | Function errors | |
FunctionConcurrencyThrottles | Throttling errors due to concurrent instance limit exceeded | |
FunctionResourceThrottles | Throttling errors due to total instance limit exceeded | |
FunctionAvgDuration | Average function running time | |
FunctionP90Duration | P90 function running time | |
FunctionP99Duration | P99 function running time | |
FunctionMaxDuration | Maximum function running time | |
FunctionLatencyAvg | Average end-to-end latency | |
FunctionMemoryLimitMB | Memory quota | |
FunctionMaxMemoryUsage | Used memory | |
FunctionOndemandInstanceQuota | Function on-demand instance quota | |
FunctionOndemandActiveInstance | Number of active on-demand instances for the function | |
FunctionProvisionedCurrentInstance | Number of provisioned instances for the function | |
FunctionEnqueueCount | Asynchronous requests enqueued | |
FunctionDequeueCount | Asynchronous requests processed | |
FunctionAsyncMessageLatencyAvg | Average latency of asynchronous message processing | |
FunctionAsyncMessageLatencyMax | Maximum latency of asynchronous message processing | |
FunctionAsyncEventExpiredDropped | Asynchronous invocation events dropped due to timeout | |
FunctionDestinationErrors | Destination trigger failed for asynchronous invocation events | |
FunctionDestinationSucceeded | Destination trigger succeeded for asynchronous invocation events | |
FunctionAsyncMessagesBacklogV2 | Number of backlogged asynchronous requests | |
FunctionAsyncMessagesInProcess | Number of in-process asynchronous requests | |
FunctionMaxConcurrentRequests | Maximum concurrent requests per instance (instance-level metric) | |
FunctionAvgConcurrentRequests | Average concurrent requests per instance (instance-level metric) | |
FunctionvCPUQuotaCores | vCPU quota (instance-level metric) | |
FunctionMaxvCPUCores | Maximum vCPUs (instance-level metric) | |
FunctionAvgvCPUCores | Average vCPUs (instance-level metric) | |
FunctionMaxvCPUUtilization | Maximum vCPU utilization (instance-level metric) | |
FunctionAvgvCPUUtilization | Average vCPU utilization (instance-level metric) | |
FunctionRXBytesPerSec | Inbound traffic (instance-level metric) | |
FunctionTXBytesPerSec | Outbound traffic (instance-level metric) | |
FunctionMemoryLimitMB | Memory quota (instance-level metric) | |
FunctionMaxMemoryUsageMB | Maximum memory usage (instance-level metric) | |
FunctionAvgMemoryUsageMB | Average memory usage (instance-level metric) | |
FunctionMaxMemoryUtilization | Maximum memory utilization (instance-level metric) | |
FunctionAvgMemoryUtilization | Average memory utilization (instance-level metric) | |
FunctionGPUMemoryLimitMB | GPU memory quota (instance-level metric) | |
FunctionGPUMaxMemoryUsage | Used GPU memory (instance-level metric) | |
FunctionGPUMemoryUsagePercent | GPU memory utilization (instance-level metric) | |
FunctionGPUSMPercent | GPU SM utilization (instance-level metric) | |
FunctionGPUEncoderPercent | GPU hardware encoder utilization (instance-level metric) | |
FunctionGPUDecoderPercent | GPU hardware decoder utilization (instance-level metric) |
Usage example
The following code shows an example of pom.xml:
...
<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>
...
The following code shows an example:
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) {
/*
An AccessKey for an Alibaba Cloud account grants full access to all API operations. Use a Resource Access Management (RAM) user for API access or daily O&M.
Do not store your AccessKey ID and AccessKey secret in your project code. This can lead to an AccessKey leak and compromise the security of all resources in your account.
This example shows how to use environment variables to store your AccessKey and AccessKey secret for identity verification.
Before you run this example, set the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables in your local environment.
In the Function Compute runtime environment, the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are automatically set 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"); // Select a metric.
dim.put("region", "<your_region>"); // For 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"); // Select a metric.
dim.put("region", "<your_region>");
// For a function created in FC 2.0, the name is {serviceName}${functionName}. To get metrics, set dim.put("serviceName",{serviceName}). For a function created in FC 3.0, the name is {functionName}. To get metrics, set dim.put("serviceName","").
dim.put("serviceName", "");
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();
}
}
}