Hello,
I am trying to fetch CPU Utilization percentage for a specific database instance. But, That gives me an exception to unable to use multiple metrics at a time. Your documentation suggests you can use multiple metrics in single request.
The below is java code :
public Double readCloudSqlCpuLevel() throws IOException {
final MetricServiceClient metricServiceClient = MetricServiceClient.create();
// Trying to read last 3 minutes data only.
long startMillis = System.currentTimeMillis() - ((60 * 3) * 1000);
final TimeInterval interval = TimeInterval.newBuilder()
.setStartTime(Timestamps.fromMillis(startMillis))
.setEndTime(Timestamps.fromMillis(System.currentTimeMillis()))
.build();
final String filter = "metric.type=\"cloudsql.googleapis.com/database/cpu/utilization\" AND metric.labels.instance_name = \"my-instance-id\" ";
final ListTimeSeriesRequest.Builder requestBuilder = ListTimeSeriesRequest.newBuilder()
.setName(projectName.toString())
.setFilter(filter)
.setInterval(interval)
.setView(ListTimeSeriesRequest.TimeSeriesView.FULL);
final ListTimeSeriesRequest request = requestBuilder.build();
final MetricServiceClient.ListTimeSeriesPagedResponse response = metricServiceClient.listTimeSeries(request);
Double cpuLevel = 0.0D;
for (TimeSeries ts : response.iterateAll()) {
for (Point pt : ts.getPointsList()) {
cpuLevel = pt.getValue().getDoubleValue() * 100;
}
}
log.info("cpuLevel : {} ", cpuLevel);
metricServiceClient.shutdownNow();
return cpuLevel;
}
This code gives me :
c.i.b.t.g.s.c.CloudSqlCpuCacheService : Unable to fetch Cloud SQL CPU level from GCloud StackDriver Monitoring API : error : io.grpc.StatusRuntimeException: NOT_FOUND: The metric referenced by the provided filter is unknown. Check the metric name and labels.
g.s.c.CloudSqlCpuCacheMaintenanceService : Unable to get Cloud SQL CPU level from GCloud API : Error : com.google.api.gax.rpc.NotFoundException: io.grpc.StatusRuntimeException: NOT_FOUND: The metric referenced by the provided filter is unknown. Check the metric name and labels.
Please suggest any solution, please.