I’m querying custom metrics via the OCI Monitoring API and consistently getting empty results, even though I can see the metrics in the console. Using the custom namespace ‘app_performance’ that I created for application-level monitoring.
My API call:
POST /20180401/metrics
{
"namespace": "app_performance",
"query": "cpu_usage[1m].mean()",
"startTime": "2025-02-07T00:00:00Z",
"endTime": "2025-02-08T00:00:00Z"
}
The response is {"data": []} but the console dashboard shows data for the same time range. This is breaking our alerting automation. Am I missing something in the query syntax or compartment specification?
Your issue has three components that need fixing:
Custom namespace usage: The namespace looks correct, but you’re using the wrong API endpoint. For custom metrics, use the SummarizeMetricsData operation, not the generic metrics endpoint. Your current endpoint is for Oracle-managed namespaces only.
Correct API call structure:
POST /20180401/metrics/actions/summarizeMetricsData
{
"namespace": "app_performance",
"query": "cpu_usage[1m]{resourceId=instance-123}.mean()",
"compartmentId": "ocid1.compartment.oc1..aaa..."
}
Compartment OCID validation: The compartmentId field is mandatory for custom metrics queries. The API doesn’t inherit compartment context from your authentication token for custom namespaces. You must explicitly pass it in the request body, not as a header or query parameter. Verify the OCID format matches ocid1.compartment.oc1..<unique_id> and that you have read permissions on the monitoring-family resources in that compartment.
Metric retention period: While standard retention is 90 days, custom metrics have a 5-minute ingestion delay minimum. Data points posted within the last 5 minutes won’t appear in API queries even if visible in console (console shows pending data). Additionally, your time range must align with the metric resolution - for 1-minute resolution metrics, query boundaries should align to minute boundaries. Use ISO 8601 format with seconds: 2025-02-07T00:00:00.000Z.
The query syntax also needs dimension filters. Custom metrics require at least one dimension specified in curly braces. Use ListMetrics first to discover available dimensions:
GET /20180401/metrics?compartmentId=<ocid>&namespace=app_performance
This returns all metrics with their dimensions. Then construct your query with proper dimension filters. The console auto-generates these, which is why it works there but not in your raw API call.
Also check your metric retention period. Custom metrics in OCI have a 90-day retention by default, but if you’re querying very recent data (last few hours), there might be ingestion delays. The console might show cached data while the API returns only fully ingested metrics. Try querying data from yesterday instead of today.
You need to specify the compartmentId in your request. The API requires explicit compartment scoping even if you’re authenticated to a specific compartment. Add it to your JSON payload and try again.
I added compartmentId but still empty results. The data I’m querying is from yesterday so retention shouldn’t be an issue. Could the namespace format be wrong? I created it without any prefix - should it be something like ‘oci_app_performance’?