

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 获取每次查询的查询使用统计数据
<a name="AMP-stats"></a>

查询[定价](https://aws.amazon.com/prometheus/pricing/)基于一个月内从执行的查询中处理的查询样本总数。您可以获得每次查询的统计数据，以跟踪已处理的样本。通过在请求中包括查询参数 `stats=all`，`query` 或 `queryRange` API 的查询响应可包括已处理查询样本的统计数据。在 `stats` 对象中创建 `samples` 对象，并在响应中返回 `stats` 数据。

`samples` 对象包含以下属性：


| 属性 | 说明 | 
| --- | --- | 
| totalQueryableSamples | 已处理的查询样本总数。这是用于计费的信息。 | 
| totalQueryableSamplesPerStep | 每个步骤处理的查询样本数。其结构为一组数组，时间戳以纪元为单位，并包含在特定步骤上加载的样本数量。 | 

包含 `stats` 信息的示例请求和响应如下所示：

`query` 的示例：

**GET**

```
{{endpoint}}/api/v1/query?query=up&time=1652382537&stats=all
```

**响应**

```
{
    "status": "success",
    "data": {
        "resultType": "vector",
        "result": [
            {
                "metric": {
                    "__name__": "up",
                    "instance": "localhost:9090",
                    "job": "prometheus"
                },
                "value": [
                    1652382537,
                    "1"
                ]
            }
        ],
        "stats": {
            "timings": {
                "evalTotalTime": 0.00453349,
                "resultSortTime": 0,
                "queryPreparationTime": 0.000019363,
                "innerEvalTime": 0.004508405,
                "execQueueTime": 0.000008786,
                "execTotalTime": 0.004554219
            },
            "samples": {
                "totalQueryableSamples": 1,
                "totalQueryableSamplesPerStep": [
                    [
                        1652382537,
                        1
                    ]
                ]
            }
        }
    }
}
```

 `queryRange` 的示例：

**GET**

```
{{endpoint}}/api/v1/query_range?query=sum+%28rate+%28go_gc_duration_seconds_count%5B1m%5D%29%29&start=1652382537&end=1652384705&step=1000&stats=all
```

**响应**

```
{
    "status": "success",
    "data": {
        "resultType": "matrix",
        "result": [
            {
                "metric": {},
                "values": [
                    [
                        1652383000,
                        "0"
                    ],
                    [
                        1652384000,
                        "0"
                    ]
                ]
            }
        ],
        "stats": {
            "samples": {
                "totalQueryableSamples": 8,
                "totalQueryableSamplesPerStep": [
                    [
                        1652382000,
                        0
                    ],
                    [
                        1652383000,
                        4
                    ],
                    [
                        1652384000,
                        4
                    ]
                ]
            }
        }
    }
}
```