|
| 1 | +--- |
| 2 | +title: Querying Container Metrics with GraphQL |
| 3 | +pcx_content_type: example |
| 4 | +products: |
| 5 | + - Containers |
| 6 | +--- |
| 7 | + |
| 8 | +In this example, we are going to use the GraphQL Analytics API to query for Container Metrics over a specified time period. We can query up to one month of data for dates up to three months ago. |
| 9 | + |
| 10 | +In the following examples, be sure to replace `<CLOUDFLARE_ACCOUNT_TAG>` and `<API_TOKEN>`[^1] with your API credentials, and adjust the `datetimeStart`, and `datetimeEnd` variables as needed. |
| 11 | +The results returned will be in JSON (as requested), so piping the output to [`jq`](https://jqlang.org/) will make them easier to read. |
| 12 | + |
| 13 | +## Container Application Level Metrics |
| 14 | + |
| 15 | +This API call will request aggregate memory, CPU load, disk usage, and network bandwidth (egress and ingress) over a one hour period for all of the instances of a Container application. |
| 16 | + |
| 17 | +Set `applicationId` to the ID of the Container application you want to query by replacing `<CONTAINER_APPLICATION_ID>`. This can be copied from the top of the Container application page in your dashboard. |
| 18 | + |
| 19 | +```bash |
| 20 | +echo '{ |
| 21 | + "query": "query GetCloudchamberMetrics($accountTag: string!, $datetimeStart: Time, $datetimeEnd: Time, $applicationId: string) { |
| 22 | + viewer { |
| 23 | + accounts(filter: { accountTag: $accountTag }) { |
| 24 | + cloudchamberMetricsAdaptiveGroups( |
| 25 | + limit: 100 |
| 26 | + filter: { |
| 27 | + applicationId: $applicationId |
| 28 | + datetimeMinute_geq: $datetimeStart |
| 29 | + datetimeMinute_leq: $datetimeEnd |
| 30 | + procType: \"user\" |
| 31 | + } |
| 32 | + ) { |
| 33 | + avg { |
| 34 | + memory |
| 35 | + cpuLoad |
| 36 | + rxBandwidth |
| 37 | + txBandwidth |
| 38 | + } |
| 39 | + max { |
| 40 | + diskUsage |
| 41 | + } |
| 42 | + quantiles { |
| 43 | + memoryP50 |
| 44 | + memoryP90 |
| 45 | + memoryP99 |
| 46 | + cpuLoadP50 |
| 47 | + cpuLoadP90 |
| 48 | + cpuLoadP99 |
| 49 | + diskUsageP50 |
| 50 | + diskUsageP90 |
| 51 | + diskUsageP99 |
| 52 | + rxBandwidthBpsP50 |
| 53 | + rxBandwidthBpsP90 |
| 54 | + rxBandwidthBpsP99 |
| 55 | + txBandwidthBpsP50 |
| 56 | + txBandwidthBpsP90 |
| 57 | + txBandwidthBpsP99 |
| 58 | + } |
| 59 | + dimensions { |
| 60 | + datetimeFiveMinutes |
| 61 | + durableObjectId |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + }", |
| 67 | + "variables": { |
| 68 | + "accountTag": "<CLOUDFLARE_ACCOUNT_TAG>", |
| 69 | + "datetimeStart": "2025-07-23T00:00:00.000Z", |
| 70 | + "datetimeEnd": "2025-07-23T01:00:00.000Z", |
| 71 | + "applicationId": "<CONTAINER_APPLICATION_ID>" |
| 72 | + } |
| 73 | +}' | curl --silent \ |
| 74 | + https://api.cloudflare.com/client/v4/graphql \ |
| 75 | + --header "Authorization: Bearer <API_TOKEN>" \ |
| 76 | + --header "Accept: application/json" \ |
| 77 | + --header "Content-Type: application/json" \ |
| 78 | + --data @- |
| 79 | +``` |
| 80 | + |
| 81 | +To get metrics for each specific instance of the Container application, you can add `'deploymentId'` to the `dimensions` field in the query: |
| 82 | + |
| 83 | +```bash |
| 84 | +dimensions { |
| 85 | + datetimeFiveMinutes |
| 86 | + deploymentId |
| 87 | + durableObjectId |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +[^1]: Refer to [Configure an Analytics API token](/analytics/graphql-api/getting-started/authentication/api-token-auth/) for more information on configuration and permissions. |
0 commit comments