> ## Documentation Index
> Fetch the complete documentation index at: https://docs.flashduty.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 查询结构化数据

> 对已配置的数据源执行同步即席查询，并返回稳定的 `query_result.v1` 结果；结果会按自然语义呈现为 frames、records 或 samples。此公开接口要求 monit-edge v0.65.0 或更高版本。

## 调用限制

| 项         | 值                                     |
| --------- | ------------------------------------- |
| 速率限制      | 每账户 **100 次/分钟**、**5 次/秒**            |
| 权限        | 任意有效的 `app_key`（只读，不受特定权限分类限制）        |
| Edge 版本要求 | 受支持的部署要求 **monit-edge v0.65.0 或更高版本** |

## 使用说明

* 此公开接口以 **monit-edge v0.65.0** 为最低受支持版本。迁移期间，WebAPI 仍保留旧版 Edge 适配器：query.v2 结果仍可能保留 frames、records 或 samples，而 legacy rows 只能提供其已保留的信息。这些适配器不会改变最低支持版本；旧协议缺少 query.v3 的取消和错误生命周期语义，legacy rows 已丢失的数据也无法恢复。
* 公开响应格式固定为 `query_result.v1`，与 Edge 内部查询协议版本无关。必须根据 `result.kind`（`frames`、`records` 或 `samples`）分发结果，不要根据 `ds_type` 或 Edge 版本猜测结果形态。
* `frames` 结果可以包含多个表格或时序 Frame。字段值按列组织，同一 Frame 中所有字段的值数量一致。
* `records` 结果可以包含嵌套 JSON 和 null record。超出 JavaScript 安全整数范围的整数字面量会以十进制字符串返回。
* `samples` 结果包含标签集合和瞬时值。值可以是数字，也可以是字符串 `NaN`、`+Inf` 或 `-Inf`。
* 最终成功响应上限为 8 MiB，查询结果上限为 1,000 行。超限时请缩短时间范围、减少字段或在数据源侧聚合。
* 查询失败使用非 2xx HTTP 状态码和标准错误 envelope。不要透明回退到已弃用的 `/monit/query/rows` 接口。
* WebAPI 跨实例转发和 Edge 执行合计可能耗时 35 秒。客户端超时建议至少设为 40 秒，并在调用方放弃查询时向下传播取消信号。


## OpenAPI

````yaml /api-reference/monitors.openapi.zh.json post /monit/query/data
openapi: 3.1.0
info:
  title: Flashduty 开放 API
  description: >-
    Flashduty 事件管理平台的公开 HTTP API —— 覆盖故障、通知模板、协作空间、值班排班、监控、RUM、以及平台管理。每次调用都需在
    query 中携带 `app_key`，该 key 在 Flashduty 控制台 账户 → APP Key 中签发。所有响应使用统一结构：成功时为
    `{ request_id, data }`，失败时为 `{ request_id, error }`。
  version: 1.0.0
servers:
  - url: https://api.flashcat.cloud
    description: Flashduty Open API
security:
  - AppKeyAuth: []
tags:
  - name: Monitors/告警规则
    description: 创建、管理和导出监控告警规则，查询规则统计和审计历史。
  - name: Monitors/告警数据源
    description: 管理监控告警规则用于查询指标的数据源。
  - name: Monitors/规则集
    description: 管理 Monitors 规则仓库中的共享规则集，规则集可在账户内或公开共享。
  - name: Monitors/诊断分析
    description: Flashduty AI SRE 使用的诊断与查询接口——数据源即席查询、日志/指标诊断,以及监控对象侧的工具调用。
  - name: Monitors/通用工具
    description: 监控服务开通及数据预览工具。
  - name: Monitors/服务拓扑
    description: 查询基于网络观测生成的服务拓扑、依赖摘要，以及主机的服务拓扑采集状态。
paths:
  /monit/query/data:
    post:
      tags:
        - Monitors/诊断分析
      summary: 查询结构化数据
      description: >-
        对已配置的数据源执行同步即席查询，并返回稳定的 `query_result.v1` 结果；结果会按自然语义呈现为 frames、records
        或 samples。此公开接口要求 monit-edge v0.65.0 或更高版本。
      operationId: monit-read-query-data
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/QueryDataRequest'
            example:
              ds_type: prometheus
              ds_name: prod-prom
              expr: sum by (job) (rate(http_requests_total[5m]))
              delay_seconds: 0
              args: {}
      responses:
        '200':
          description: 成功
          content:
            application/json:
              schema:
                allOf:
                  - $ref: '#/components/schemas/SuccessEnvelope'
                  - type: object
                    properties:
                      data:
                        $ref: '#/components/schemas/QueryDataResponse'
              example:
                request_id: 01HK8XQE3Z7JM2NTFQ5YJ8P9R4
                data:
                  format: query_result.v1
                  result:
                    kind: samples
                    samples:
                      - labels:
                          job: api
                        value: 1.25
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '403':
          $ref: '#/components/responses/Forbidden'
        '413':
          description: 请求或最终响应超过大小限制。
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '429':
          $ref: '#/components/responses/TooManyRequests'
        '499':
          description: 客户端已取消查询。
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '500':
          $ref: '#/components/responses/ServerError'
        '503':
          $ref: '#/components/responses/ServiceUnavailable'
        '504':
          description: 查询超时。
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
components:
  schemas:
    QueryDataRequest:
      description: 稳定结构化查询接口的请求。字段与已弃用的 rows 接口一致。
      allOf:
        - $ref: '#/components/schemas/QueryRowsRequest'
    SuccessEnvelope:
      type: object
      description: >-
        成功响应结构。2xx 响应中 `request_id` 标识本次调用（同时出现在 `Flashcat-Request-Id`
        响应头中），`data` 为接口业务 payload。失败响应使用不同结构，参见 `ErrorResponse`。
      properties:
        request_id:
          type: string
          description: 本次请求的唯一 ID，也会在 Flashcat-Request-Id 响应头中返回。反馈问题时请一并附上。
          example: 01HK8XQE3Z7JM2NTFQ5YJ8P9R4
        data:
          description: 每个接口自己的业务 payload，详见各接口的 200 响应 schema。
      required:
        - request_id
        - data
    QueryDataResponse:
      type: object
      description: 与 Edge 版本无关的稳定结构化查询响应。
      required:
        - format
        - result
      properties:
        format:
          type: string
          enum:
            - query_result.v1
          description: >-
            公开结果契约版本，与 monit-edge 内部查询协议版本相互独立。固定为 `query_result.v1`，`result`
            字段的结构由该契约定义。
        result:
          $ref: '#/components/schemas/QueryResult'
    ErrorResponse:
      type: object
      description: 错误响应结构。`error` 必填，`data` 不存在。
      properties:
        request_id:
          type: string
          example: 01HK8XQE3Z7JM2NTFQ5YJ8P9R4
          description: 本次请求的唯一追踪 ID（trace ID），反馈问题时请提供该值以便检索日志。
        error:
          $ref: '#/components/schemas/DutyError'
      required:
        - request_id
        - error
    QueryRowsRequest:
      type: object
      required:
        - ds_type
        - ds_name
        - expr
      properties:
        account_id:
          type: integer
          format: int64
          description: 可选的一致性校验。若提供,必须等于已认证账户;不一致将被拒绝。业务执行始终使用已认证账户。
        ds_type:
          type: string
          description: >-
            数据源类型;必须匹配租户下已配置的数据源。示例:`prometheus`、`loki`、`victorialogs`、`sls`、`elasticsearch`、`mysql`、`postgres`、`oracle`、`clickhouse`。
        ds_name:
          type: string
          description: 数据源名称;必须匹配租户下已配置的数据源。
        expr:
          type: string
          description: >-
            查询表达式。语法取决于 `ds_type`,由对应的 monit-edge 客户端解释(Prometheus 用 PromQL,Loki
            用 LogQL,SQL 类数据源用 SQL,等等)。
        delay_seconds:
          type: integer
          description: >-
            应用于点查询(Prometheus、Loki stats、VictoriaLogs stats)的回看偏移,单位秒。明细 / raw
            查询忽略该字段。
          default: 0
        args:
          type: object
          description: >-
            透传给 monit-edge 的多态键值扩展参数。所有值必须是字符串，键一律按数据源加前缀（如
            `sls.project`、`loki.type`）。校验规则取决于 `ds_type`：SLS 必须提供 `sls.project`
            与 `sls.logstore`；Elasticsearch 的 `es.type` 只接受 `sql`
            或不传，其他值一律拒绝；Loki 与 VictoriaLogs 的 `<source>.type` 接受 `stats`、`raw`
            或不传，其中 `raw` 还必须给出时间范围——`<source>.start` + `<source>.end`，或
            `<source>.timespan.value` + `<source>.timespan.unit`（单位取
            `s`/`m`/`h`/`d`）。Prometheus 及其余 SQL 类数据源完全忽略 `args`。
          additionalProperties:
            type: string
    QueryResult:
      description: 由 `kind` 选择且严格互斥的自然结果形态。
      oneOf:
        - $ref: '#/components/schemas/QueryFramesResult'
        - $ref: '#/components/schemas/QueryRecordsResult'
        - $ref: '#/components/schemas/QuerySamplesResult'
      discriminator:
        propertyName: kind
        mapping:
          frames:
            $ref: '#/components/schemas/QueryFramesResult'
          records:
            $ref: '#/components/schemas/QueryRecordsResult'
          samples:
            $ref: '#/components/schemas/QuerySamplesResult'
    DutyError:
      type: object
      description: 响应结构中的错误 payload，仅在非 2xx 响应时出现。
      properties:
        code:
          $ref: '#/components/schemas/ErrorCode'
        message:
          type: string
          description: 用户可读的错误描述，语言会跟随调用方的 Accept-Language。可能包含字段名、ID 等请求上下文。
          example: The specified parameter template_id is not valid.
      required:
        - code
        - message
    QueryFramesResult:
      type: object
      required:
        - kind
        - frames
      properties:
        kind:
          type: string
          enum:
            - frames
          description: 结果类型判别字段，固定为 `frames`，表示 `frames` 载荷为类型化表格/时序 Frame。
        frames:
          type: array
          description: 类型化表格或时序 Frame。一次响应可以包含多个 Frame。
          items:
            $ref: '#/components/schemas/QueryFrame'
    QueryRecordsResult:
      type: object
      required:
        - kind
        - records
      properties:
        kind:
          type: string
          enum:
            - records
          description: 结果类型判别字段，固定为 `records`，表示 `records` 载荷为 schema 灵活的记录数组。
        records:
          type: array
          description: >-
            schema 灵活的 records。不同 record 可以拥有不同字段、包含嵌套 JSON 或为 null。超出
            JavaScript 安全范围的整数会编码为十进制字符串。
          items:
            oneOf:
              - type: object
                additionalProperties: true
              - type: 'null'
    QuerySamplesResult:
      type: object
      required:
        - kind
        - samples
      properties:
        kind:
          type: string
          enum:
            - samples
          description: 结果类型判别字段，固定为 `samples`，表示 `samples` 载荷为带标签的瞬时样本数组。
        samples:
          type: array
          description: 带完整标签集合的瞬时样本。
          items:
            $ref: '#/components/schemas/QuerySample'
    ErrorCode:
      type: string
      description: >-
        Flashduty 错误码枚举。每个失败响应的 `error.code` 都是下列稳定值之一，HTTP 状态码仅作参考。


        | 错误码 | HTTP | 含义 |

        |---|---|---|

        | `OK` | 200 | 保留值，正常错误响应不会返回。 |

        | `InvalidParameter` | 400 | 必填参数缺失或未通过校验。 |

        | `BadRequest` | 400 | 通用的 400 错误，通常是请求本身不合法。 |

        | `InvalidContentType` | 400 | 请求头 `Content-Type` 不是 `application/json`。
        |

        | `ResourceNotFound` | 400 | 目标资源不存在。注意 HTTP 状态码是 400 而非 404（历史设计）。 |

        | `NoLicense` | 400 | 功能需要有效授权，但未找到可用的 license。 |

        | `ReferenceExist` | 400 | 该资源仍被其他实体引用，无法删除。 |

        | `Unauthorized` | 401 | `app_key` 缺失、无效或已过期。 |

        | `BalanceNotEnough` | 402 | 账户余额不足，无法执行需要计费的操作。 |

        | `AccessDenied` | 403 | 身份认证通过，但 RBAC 权限不足以执行该操作。 |

        | `RouteNotFound` | 404 | 请求的 URL 路径不是已知路由。 |

        | `MethodNotAllowed` | 405 | 当前路径不接受所使用的 HTTP 方法。 |

        | `UndonedOrderExist` | 409 | 账户存在未完成的订单，请稍后重试。 |

        | `RequestLocked` | 423 | 因连续失败被临时锁定。 |

        | `EntityTooLarge` | 413 | 请求体超过允许的最大长度。 |

        | `RequestTooFrequently` | 429 | 命中限流（全局、账户级或集成级）。 |

        | `RequestVerifyRequired` | 428 | 操作需要二次验证码，但未提供。 |

        | `DangerousOperation` | 428 | 危险操作，需要进行 MFA 验证。 |

        | `InternalError` | 500 | 服务端未预期错误。反馈问题请附上 `request_id`。 |

        | `ServiceUnavailable` | 503 | 后端依赖不可用，请稍后重试。 |
      enum:
        - OK
        - InvalidParameter
        - BadRequest
        - InvalidContentType
        - ResourceNotFound
        - NoLicense
        - ReferenceExist
        - Unauthorized
        - BalanceNotEnough
        - AccessDenied
        - RouteNotFound
        - MethodNotAllowed
        - UndonedOrderExist
        - RequestLocked
        - EntityTooLarge
        - RequestTooFrequently
        - RequestVerifyRequired
        - DangerousOperation
        - InternalError
        - ServiceUnavailable
      x-enumDescriptions:
        OK: 保留值，正常错误响应不会返回。
        InvalidParameter: 必填参数缺失或未通过校验。
        BadRequest: 通用的 400 错误，通常是请求本身不合法。
        InvalidContentType: 请求头 `Content-Type` 不是 `application/json`。
        ResourceNotFound: 目标资源不存在。注意 HTTP 状态码是 400 而非 404（历史设计）。
        NoLicense: 功能需要有效授权，但未找到可用的 license。
        ReferenceExist: 该资源仍被其他实体引用，无法删除。
        Unauthorized: '`app_key` 缺失、无效或已过期。'
        BalanceNotEnough: 账户余额不足，无法执行需要计费的操作。
        AccessDenied: 身份认证通过，但 RBAC 权限不足以执行该操作。
        RouteNotFound: 请求的 URL 路径不是已知路由。
        MethodNotAllowed: 当前路径不接受所使用的 HTTP 方法。
        UndonedOrderExist: 账户存在未完成的订单，请稍后重试。
        RequestLocked: 因连续失败被临时锁定。
        EntityTooLarge: 请求体超过允许的最大长度。
        RequestTooFrequently: 命中限流（全局、账户级或集成级）。
        RequestVerifyRequired: 操作需要二次验证码，但未提供。
        DangerousOperation: 危险操作，需要进行 MFA 验证。
        InternalError: 服务端未预期错误。反馈问题请附上 `request_id`。
        ServiceUnavailable: 后端依赖不可用，请稍后重试。
      example: InvalidParameter
    QueryFrame:
      type: object
      description: >-
        类型化列式表格或时序 Frame。同一 Frame 的所有字段拥有相同数量的值。`time_series` Frame 包含一个 time
        字段和一个 float 字段，序列标签位于 float 字段。
      required:
        - kind
        - fields
      properties:
        kind:
          type: string
          enum:
            - table
            - time_series
          description: Frame 类型：`table` 为通用表格，`time_series` 为时序（恰含一个 time 字段和一个 float 字段）。
        fields:
          type: array
          items:
            $ref: '#/components/schemas/QueryField'
          description: Frame 的列集合；同一 Frame 内所有字段的 `values` 长度一致，第 i 行由各字段 `values[i]` 组成。
    QuerySample:
      type: object
      required:
        - labels
        - value
      properties:
        labels:
          type: object
          additionalProperties:
            type: string
          description: 样本的完整标签集合；可为空对象但字段始终存在。
        value:
          description: 有限数值，或非有限浮点数的 JSON 安全表示。
          oneOf:
            - type: number
            - type: string
              enum:
                - NaN
                - +Inf
                - '-Inf'
    QueryField:
      type: object
      description: >-
        一个类型化列。`string` 字段包含字符串或 null；`time` 字段包含 RFC 3339 Nano 字符串或
        null；`float` 字段包含数字、null 或特殊字符串 `NaN`、`+Inf`、`-Inf`。
      required:
        - name
        - type
        - values
      properties:
        name:
          type: string
          description: 列名；时序 Frame 的 float 字段上以 `labels` 区分不同序列，`name` 通常为指标名。
        type:
          type: string
          enum:
            - string
            - float
            - time
          description: >-
            列值类型，决定 `values` 元素的编码：`string` 为字符串或 null，`float` 为数值/`NaN`/`±Inf`
            字符串或 null，`time` 为 RFC 3339 Nano 字符串或 null。
        labels:
          type: object
          description: 序列标签，出现在时序 Frame 的 float 字段上。
          additionalProperties:
            type: string
        values:
          type: array
          items:
            oneOf:
              - type: string
              - type: number
              - type: 'null'
          description: 该列的全部值，按行序排列；长度与 Frame 内其他字段一致。
  responses:
    BadRequest:
      description: 请求非法 — 通常是参数缺失或格式不正确。
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
          examples:
            missingParameter:
              value:
                request_id: 01HK8XQE3Z7JM2NTFQ5YJ8P9R4
                error:
                  code: InvalidParameter
                  message: The specified parameter is not valid.
    Unauthorized:
      description: app_key 缺失或无效。
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
          examples:
            missingAppKey:
              value:
                request_id: 01HK8XQE3Z7JM2NTFQ5YJ8P9R4
                error:
                  code: Unauthorized
                  message: You are unauthorized.
    Forbidden:
      description: app_key 有效但没有执行该操作的权限。
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
          examples:
            noEditPermission:
              value:
                request_id: 01HK8XQE3Z7JM2NTFQ5YJ8P9R4
                error:
                  code: AccessDenied
                  message: Access Denied.
    TooManyRequests:
      description: 命中限流。可能是全局 API 限流、账户级限流或集成级限流。限流按账户聚合。
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
          examples:
            rateLimited:
              value:
                request_id: 01HK8XQE3Z7JM2NTFQ5YJ8P9R4
                error:
                  code: RequestTooFrequently
                  message: Request too frequently.
    ServerError:
      description: 服务端未预期错误。反馈问题时请携带 request_id。
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
          examples:
            internal:
              value:
                request_id: 01HK8XQE3Z7JM2NTFQ5YJ8P9R4
                error:
                  code: InternalError
                  message: >-
                    We encountered an internal error, and it has been reported.
                    Please try again later.
    ServiceUnavailable:
      description: 当前部署未开启或无法访问 ServiceMap 子系统。反馈问题时请附带 request_id。
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
          examples:
            serviceMapDisabled:
              value:
                request_id: 01HK8XQE3Z7JM2NTFQ5YJ8P9R4
                error:
                  code: ServiceUnavailable
                  message: servicemap store is not initialized
  securitySchemes:
    AppKeyAuth:
      type: apiKey
      in: query
      name: app_key
      description: >-
        在 Flashduty 控制台 账户 → APP Key 中签发的 app_key。调用任何公开 API
        时都必须携带。它等同于所属账户的身份凭证，请妥善保管。

````