This topic describes how to query Function Compute metrics by using the Cloud Monitor API. You can call relevant API operations and use different request parameters to obtain the data that you need. These parameters include Project, StartTime, EndTime, Dimensions, Period, and Metric.
For more information about the Cloud Monitor API, 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 the Java SDK:
QueryMetricRequest request = new QueryMetricRequest();
request.setProject("acs_fc");
Time range
In Cloud Monitor, a time range is defined by the StartTime and EndTime parameters. The time range is exclusive with respect to StartTime and inclusive with respect to EndTime.
For more information about time parameters, see List of operations by function.
The following sample code shows how to set a time range by using the Java SDK:
request.setStartTime("2017-04-26 08:00:00");
request.setEndTime("2017-04-26 09:00:00");
Dimensions
The monitoring service of Function Compute divides Function Compute metrics from 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 service metrics is in the following format:
{"region": "${your_region}", "serviceName": "${your_serviceName}"}
- The Dimensions parameter for function metrics is in the following format:
{"region": "${your_region}", "serviceName": "${your_serviceName}", "functionName": "${your_functionName}"}
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 set an aggregation period by using the Java SDK:
request.setPeriod("60");
Metric
The following sample code shows how to specify a metric by using the Java SDK:
request.setMetric("your_metric");
The following table lists the metrics that are described in the Function Compute documentation.
Dimension | Metric | Description |
---|---|---|
Region | RegionTotalInvocations | The total number of function invocation requests in the region. |
RegionBillableInvocations | The number of billable successful function invocation requests in the region. | |
RegionThrottles | The number of throttled function invocation requests in the region. | |
RegionClientErrors | The number of the region-specific function invocation requests for which client errors were returned. | |
RegionServerErrors | The number of the region-specific function invocation requests for which server errors were returned. | |
RegionBillableInvocationsRate | The percentage of billable successful function invocation requests in the region. | |
RegionThrottlesRate | The percentage of throttled function invocation requests in the region. | |
RegionClientErrorsRate | The percentage of the region-specific function invocation requests for which client errors were returned. | |
RegionServerErrorsRate | The percentage of the region-specific function invocation requests for which server errors are returned. | |
Service | ServiceTotalInvocations | The total number of function invocation requests in the service. |
ServiceBillableInvocations | The number of billable successful function invocation requests in the service. | |
ServiceThrottles | The number of throttled function invocation requests in the service. | |
ServiceClientErrors | The number of the service-specific function invocation requests for which client errors were returned. | |
ServiceServerErrors | The number of the service-specific function invocation requests for which server errors were returned. | |
ServiceBillableInvocationsRate | The percentage of billable successful function invocation requests in the service. | |
ServiceThrottlesRate | The percentage of throttled function invocation requests in the service. | |
ServiceClientErrorsRate | The percentage of the service-specific function invocation requests for which client errors were returned. | |
ServiceServerErrorsRate | The percentage of the service-specific function invocation requests for which server errors were returned. | |
Function | FunctionTotalInvocations | The total number of invocation requests for the function. |
FunctionBillableInvocations | The number of billable successful invocation requests for the function. | |
FunctionThrottles | The number of throttled invocation requests for the function. | |
FunctionFunctionErrors | The number of errors that occurred during the execution of the function. | |
FunctionClientErrors | The number of the function invocation requests for which client errors were returned. | |
FunctionServerErrors | The number of the function invocation requests for which server errors were returned. | |
FunctionBillableInvocationsRate | The percentage of billable invocation requests for the function. | |
FunctionThrottlesRate | The percentage of throttled invocation requests for the function. | |
FunctionFunctionErrorsRate | The percentage of the function invocation requests during which errors occurred in function execution. | |
FunctionClientErrorsRate | The percentage of the function invocation requests for which client errors were returned. | |
FunctionServerErrorsRate | The percentage of the function invocation requests for which server errors were returned. | |
FunctionAvgDuration | The average duration for all requests in 60 seconds. | |
FunctionMaxMemoryUsage | The maximum memory used for function invocations in the aggregation period. |
Example
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>
...
The following code requests metrics from the Cloud Monitor API:
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) {
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<your_access_key_id>", "<your_access_key_secret>");
IAcsClient client = new DefaultAcsClient(profile);
QueryMetricListRequest request = new QueryMetricListRequest();
request.setProject("acs_fc");
request.setPeriod("60");
request.setStartTime("2017-04-26 16:20:00");
request.setEndTime("2017-04-26 16:30:00");
request.setAcceptFormat(FormatType.JSON);
try {
// Region JSONObject dim = new JSONObject();
request.setMetric("RegionTotalInvocations"); // Specifies 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());
// Service dim = new JSONObject();
request.setMetric("ServiceTotalInvocations"); // Specifies a metric.
dim.put("region", "<your_region>");
dim.put("serviceName", "<your_service_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());
// Function dim = new JSONObject();
request.setMetric("FunctionTotalInvocations"); // Specifies a metric.
dim.put("region", "<your_region>");
dim.put("serviceName", "<your_service_name>");
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();
}
}
}