|
| 1 | +--- |
| 2 | +pcx_content_type: concept |
| 3 | +title: Metrics and analytics |
| 4 | +sidebar: |
| 5 | + order: 10 |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +KV exposes analytics that allow you to inspect requests and storage across all namespaces in your account. |
| 10 | + |
| 11 | +The metrics displayed in the [Cloudflare dashboard](https://dash.cloudflare.com/) charts are queried from Cloudflare’s [GraphQL Analytics API](/analytics/graphql-api/). You can access the metrics [programmatically](#query-via-the-graphql-api) via GraphQL or HTTP client. |
| 12 | + |
| 13 | +## Metrics |
| 14 | + |
| 15 | +KV currently exposes the below metrics: |
| 16 | + |
| 17 | +| Dataset | GraphQL Dataset Name | Description | |
| 18 | +| ----------------------- | --------------------------- | ------------------------------------------------------------- | |
| 19 | +| Operations | `kvOperationsAdaptiveGroups`| This dataset consists of the operations made to your KV namespaces. | |
| 20 | +| Storage | `kvStorageAdaptiveGroups` | This dataset consists of the storage details of your KV namespaces. | |
| 21 | + |
| 22 | +Metrics can be queried (and are retained) for the past 31 days. |
| 23 | + |
| 24 | +## View metrics in the dashboard |
| 25 | + |
| 26 | +Per-namespace analytics for KV are available in the Cloudflare dashboard. To view current and historical metrics for a database: |
| 27 | + |
| 28 | +1. Log in to the [Cloudflare dashboard](https://dash.cloudflare.com) and select your account. |
| 29 | +2. Go to [**Workers & Pages** > **KV**](https://dash.cloudflare.com/?to=/:account/workers/kv/namespaces). |
| 30 | +3. Select an existing namespace. |
| 31 | +4. Select the **Metrics** tab. |
| 32 | + |
| 33 | +You can optionally select a time window to query. This defaults to the last 24 hours. |
| 34 | + |
| 35 | +## Query via the GraphQL API |
| 36 | + |
| 37 | +You can programmatically query analytics for your KV namespaces via the [GraphQL Analytics API](/analytics/graphql-api/). This API queries the same datasets as the Cloudflare dashboard, and supports GraphQL [introspection](/analytics/graphql-api/features/discovery/introspection/). |
| 38 | + |
| 39 | +To get started using the [GraphQL Analytics API](/analytics/graphql-api/), follow the documentation to setup [Authentication for the GraphQL Analytics API](/analytics/graphql-api/getting-started/authentication/). |
| 40 | + |
| 41 | +To use the GraphQL API to retrieve KV's datasets, you must provide the `accountTag` filter with your Cloudflare Account ID. The GraphQL datasets for KV include: |
| 42 | + |
| 43 | +- `kvOperationsAdaptiveGroups` |
| 44 | +- `kvStorageAdaptiveGroups` |
| 45 | + |
| 46 | +### Examples |
| 47 | + |
| 48 | +The following are common GraphQL queries that you can use to retrieve information about KV analytics. These queries make use of variables `$accountTag`, `$date_geq`, `$date_leq`, and `$namespaceId`, which should be set as GraphQL variables or replaced in line. These variables should look similar to these: |
| 49 | + |
| 50 | +```json |
| 51 | +{ |
| 52 | + "accountTag":"<YOUR_ACCOUNT_ID>", |
| 53 | + "namespaceId": "<YOUR_KV_NAMESPACE_ID>", |
| 54 | + "date_geq": "2024-07-15", |
| 55 | + "date_leq": "2024-07-30" |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +#### Operations |
| 60 | + |
| 61 | +To query the sum of read, write, delete, and list operations for a given `namespaceId` and for a given date range (`date_geq` and `date_leq`), grouped by `date` and `actionType`: |
| 62 | + |
| 63 | +```graphql |
| 64 | +query { |
| 65 | + viewer { |
| 66 | + accounts(filter: { accountTag: $accountTag }) { |
| 67 | + kvOperationsAdaptiveGroups( |
| 68 | + filter: { namespaceId: $namespaceId, date_geq: $date_geq, date_leq: $date_leq } |
| 69 | + limit: 10000 |
| 70 | + orderBy: [date_DESC] |
| 71 | + ) { |
| 72 | + sum { |
| 73 | + requests |
| 74 | + } |
| 75 | + dimensions { |
| 76 | + date |
| 77 | + actionType |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | + |
| 86 | +To query the distribution of the latency for read operations for a given `namespaceId` within a given date range (`date_geq`, `date_leq`): |
| 87 | + |
| 88 | +```graphql |
| 89 | +query { |
| 90 | + viewer { |
| 91 | + accounts(filter: { accountTag: $accountTag }) { |
| 92 | + kvOperationsAdaptiveGroups( |
| 93 | + filter: { namespaceId: $namespaceId, date_geq: $date_geq, date_leq: $date_leq, actionType: "read" } |
| 94 | + limit: 10000 |
| 95 | + ) { |
| 96 | + sum { |
| 97 | + requests |
| 98 | + } |
| 99 | + dimensions { |
| 100 | + actionType |
| 101 | + } |
| 102 | + quantiles { |
| 103 | + latencyMsP25 |
| 104 | + latencyMsP50 |
| 105 | + latencyMsP75 |
| 106 | + latencyMsP90 |
| 107 | + latencyMsP99 |
| 108 | + latencyMsP999 |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +To query your account-wide read, write, delete, and list operations across all KV namespaces: |
| 117 | + |
| 118 | +```graphql |
| 119 | +query { |
| 120 | + viewer { |
| 121 | + accounts(filter: { accountTag: $accountTag }) { |
| 122 | + kvOperationsAdaptiveGroups(filter: { date_geq: $date_geq, date_leq: $date_leq }, limit: 10000) { |
| 123 | + sum { |
| 124 | + requests |
| 125 | + } |
| 126 | + dimensions { |
| 127 | + actionType |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | +} |
| 133 | +``` |
| 134 | + |
| 135 | +#### Storage |
| 136 | + |
| 137 | +To query the storage details (`keyCount` and `byteCount`) of a KV namespace for every day of a given date range: |
| 138 | + |
| 139 | +```graphql |
| 140 | +query Viewer { |
| 141 | + viewer { |
| 142 | + accounts(filter: { accountTag: $accountTag }) { |
| 143 | + kvStorageAdaptiveGroups( |
| 144 | + filter: { date_geq: $date_geq, date_leq: $date_leq, namespaceId: $namespaceId } |
| 145 | + limit: 10000 |
| 146 | + orderBy: [date_DESC] |
| 147 | + ) { |
| 148 | + max { |
| 149 | + keyCount |
| 150 | + byteCount |
| 151 | + } |
| 152 | + dimensions { |
| 153 | + date |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | +} |
| 159 | +``` |
0 commit comments