> ## 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.

# 创建数据源

> 创建新的监控数据源，`payload` 中须包含对应类型的配置块。

## 限制说明

| 项目   | 说明                             |
| ---- | ------------------------------ |
| 速率限制 | 每个账户 **1,000 次/分钟**；**50 次/秒** |
| 权限要求 | **数据源管理**（`monit`）             |

## 使用说明

* `type_ident` 必须为以下之一：`prometheus`、`loki`、`mysql`、`oracle`、`postgres`、`clickhouse`、`elasticsearch`、`sls`、`victorialogs`。
* `edge_cluster_name` 指定使用该数据源进行规则评估的 Monitors Edge 集群。
* 对于 `elasticsearch`，`payload.elasticsearch.deployment` 须设为 `cloud` 或 `self-managed`。
* 每次调用都会记录到账户审计日志，请不要把敏感信息放在请求字段中。


## OpenAPI

````yaml /api-reference/monitors.openapi.zh.json post /monit/datasource/create
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: 监控服务开通及数据预览工具。
paths:
  /monit/datasource/create:
    post:
      tags:
        - Monitors/告警数据源
      summary: 创建数据源
      description: 创建新的监控数据源，`payload` 中须包含对应类型的配置块。
      operationId: monit-datasource-write-create
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/DataSourceUpsertRequest'
            example:
              type_ident: prometheus
              name: 生产 Prometheus
              note: 生产环境 Prometheus
              address: http://prometheus.example.com:9090
              edge_cluster_name: default
              payload:
                prometheus:
                  basic_auth_enabled: false
      responses:
        '200':
          description: 成功
          content:
            application/json:
              schema:
                allOf:
                  - $ref: '#/components/schemas/SuccessEnvelope'
                  - type: object
                    properties:
                      data:
                        $ref: '#/components/schemas/DataSourceItem'
              example:
                request_id: 01HK8XQE3Z7JM2NTFQ5YJ8P9R4
                data:
                  id: 10
                  type_ident: prometheus
                  name: 生产 Prometheus
                  enabled: true
                  edge_cluster_name: default
                  updated_at: 1712000000
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '429':
          $ref: '#/components/responses/TooManyRequests'
        '500':
          $ref: '#/components/responses/ServerError'
components:
  schemas:
    DataSourceUpsertRequest:
      type: object
      description: >-
        创建或更新数据源的请求体。`id` 仅在更新时必填。`address` 除 Elasticsearch `deployment=cloud`
        外均为必填。
      required:
        - type_ident
        - name
        - edge_cluster_name
        - payload
      properties:
        id:
          type: integer
          format: uint64
          description: 数据源 ID。更新时必填，创建时省略。
        type_ident:
          type: string
          description: >-
            数据源类型标识，可选值：`prometheus`、`loki`、`mysql`、`oracle`、`postgres`、`clickhouse`、`elasticsearch`、`sls`、`victorialogs`。
        name:
          type: string
          description: 数据源显示名称。
        note:
          type: string
          description: 可选描述。
        address:
          type: string
          description: >-
            连接地址。Prometheus/Loki/VictoriaLogs 为 HTTP
            URL；MySQL/Oracle/Postgres/ClickHouse 为 `host:port`；SLS 为不含
            http/https 前缀的 endpoint；Elasticsearch cloud 部署无需填写。
        payload:
          $ref: '#/components/schemas/DSPayload'
          description: 类型相关配置块，必须包含与 `type_ident` 匹配的键。
        edge_cluster_name:
          type: string
          description: 负责使用该数据源评估规则的 Monitors Edge 集群名称。
    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
    DataSourceItem:
      type: object
      description: 单个监控数据源。
      required:
        - id
        - account_id
        - type_ident
        - name
        - enabled
        - note
        - address
        - edge_cluster_name
        - updated_at
      properties:
        id:
          type: integer
          format: uint64
          description: 唯一数据源 ID。
        account_id:
          type: integer
          format: uint64
          description: 账户 ID。
        type_ident:
          type: string
          description: >-
            数据源类型标识，可选值：`prometheus`、`loki`、`mysql`、`oracle`、`postgres`、`clickhouse`、`elasticsearch`、`sls`、`victorialogs`。
        name:
          type: string
          description: 数据源显示名称。
        enabled:
          type: boolean
          description: 数据源是否启用。
        note:
          type: string
          description: 可选描述。
        address:
          type: string
          description: >-
            连接地址。Prometheus/Loki/VictoriaLogs 为 HTTP
            URL；MySQL/Oracle/Postgres/ClickHouse 为 `host:port`；SLS 为不含
            http/https 前缀的 endpoint。
        payload:
          $ref: '#/components/schemas/DSPayload'
        edge_cluster_name:
          type: string
          description: 负责使用该数据源评估规则的 Monitors Edge 集群名称。
        updated_at:
          type: integer
          format: int64
          description: 最后更新时间，Unix 时间戳（秒）。
    DSPayload:
      type: object
      description: 与类型相关的数据源配置，仅包含与 `type_ident` 匹配的配置块。
      properties:
        prometheus:
          $ref: '#/components/schemas/DSPrometheusConfig'
        loki:
          $ref: '#/components/schemas/DSLokiConfig'
        mysql:
          $ref: '#/components/schemas/DSMySQLConfig'
        oracle:
          $ref: '#/components/schemas/DSOracleConfig'
        postgres:
          $ref: '#/components/schemas/DSPostgresConfig'
        clickhouse:
          $ref: '#/components/schemas/DSClickHouseConfig'
        elasticsearch:
          $ref: '#/components/schemas/DSElasticSearchConfig'
        sls:
          $ref: '#/components/schemas/DSSLSConfig'
        victorialogs:
          $ref: '#/components/schemas/DSVictoriaLogsConfig'
    ErrorResponse:
      type: object
      description: 错误响应结构。`error` 必填，`data` 不存在。
      properties:
        request_id:
          type: string
          example: 01HK8XQE3Z7JM2NTFQ5YJ8P9R4
        error:
          $ref: '#/components/schemas/DutyError'
      required:
        - request_id
        - error
    DSPrometheusConfig:
      type: object
      description: Prometheus 数据源配置，TLS 字段继承自 TLSClientConfig。
      properties:
        basic_auth_enabled:
          type: boolean
          description: 启用 HTTP Basic 认证。
        basic_auth_username:
          type: string
          description: Basic 认证用户名。
        basic_auth_password:
          type: string
          description: Basic 认证密码。
        headers:
          type: array
          items:
            type: string
          description: '自定义 HTTP 请求头，格式为 `Key: Value`。'
        params:
          type: array
          items:
            type: string
          description: 自定义查询参数，格式为 `key=value`。
        tls_ca:
          type: string
        tls_cert:
          type: string
        tls_key:
          type: string
        tls_key_pwd:
          type: string
        tls_skip_verify:
          type: boolean
        tls_server_name:
          type: string
        tls_min_version:
          type: string
        tls_max_version:
          type: string
    DSLokiConfig:
      type: object
      description: Loki 数据源配置，TLS 字段继承自 TLSClientConfig。
      properties:
        basic_auth_enabled:
          type: boolean
        basic_auth_username:
          type: string
        basic_auth_password:
          type: string
        headers:
          type: array
          items:
            type: string
        params:
          type: array
          items:
            type: string
        tls_ca:
          type: string
        tls_cert:
          type: string
        tls_key:
          type: string
        tls_key_pwd:
          type: string
        tls_skip_verify:
          type: boolean
        tls_server_name:
          type: string
        tls_min_version:
          type: string
        tls_max_version:
          type: string
    DSMySQLConfig:
      type: object
      description: MySQL 数据源配置，TLS 字段继承自 TLSClientConfig。
      properties:
        username:
          type: string
        password:
          type: string
        open_conns:
          type: integer
          description: 最大打开连接数。
        idle_conns:
          type: integer
          description: 最大空闲连接数。
        lifetime_seconds:
          type: integer
          format: int64
          description: 连接最大生命周期（秒）。
        timeout_mills:
          type: integer
          format: int64
          description: 查询超时时间（毫秒）。
        tls_ca:
          type: string
        tls_cert:
          type: string
        tls_key:
          type: string
        tls_key_pwd:
          type: string
        tls_skip_verify:
          type: boolean
        tls_server_name:
          type: string
        tls_min_version:
          type: string
        tls_max_version:
          type: string
    DSOracleConfig:
      type: object
      description: Oracle 数据源配置。
      properties:
        username:
          type: string
        password:
          type: string
        options:
          type: object
          additionalProperties:
            type: string
          description: 额外连接参数（键值对）。
        open_conns:
          type: integer
        idle_conns:
          type: integer
        lifetime_seconds:
          type: integer
          format: int64
        timeout_mills:
          type: integer
          format: int64
    DSPostgresConfig:
      type: object
      description: PostgreSQL 数据源配置。
      properties:
        username:
          type: string
        password:
          type: string
        open_conns:
          type: integer
        idle_conns:
          type: integer
        lifetime_seconds:
          type: integer
          format: int64
        timeout_mills:
          type: integer
          format: int64
        tls_ca:
          type: string
        tls_cert:
          type: string
        tls_key:
          type: string
    DSClickHouseConfig:
      type: object
      description: ClickHouse 数据源配置，TLS 字段继承自 TLSClientConfig。
      properties:
        database:
          type: string
          description: 认证用默认数据库。
        username:
          type: string
        password:
          type: string
        open_conns:
          type: integer
        idle_conns:
          type: integer
        lifetime_seconds:
          type: integer
          format: int64
        timeout_mills:
          type: integer
          format: int64
        max_execution_seconds:
          type: integer
          format: int64
          description: 最大查询执行时间（秒）。
        dial_timeout_mills:
          type: integer
          format: int64
          description: 拨号超时（毫秒）。
        tls_enabled:
          type: boolean
        tls_ca:
          type: string
        tls_cert:
          type: string
        tls_key:
          type: string
        tls_key_pwd:
          type: string
        tls_skip_verify:
          type: boolean
        tls_server_name:
          type: string
        tls_min_version:
          type: string
        tls_max_version:
          type: string
    DSElasticSearchConfig:
      type: object
      description: Elasticsearch 数据源配置。
      properties:
        deployment:
          type: string
          enum:
            - cloud
            - self-managed
          description: 部署类型。`cloud` 使用 Elastic Cloud；`self-managed` 使用自托管集群。
        timeout_mills:
          type: integer
          format: int64
        cloud_id:
          type: string
          description: Elastic Cloud 部署 ID，仅用于 `cloud` 部署。
        api_key:
          type: string
          description: Elastic Cloud API 密钥，仅用于 `cloud` 部署。
        username:
          type: string
          description: '`self-managed` 部署的用户名。'
        password:
          type: string
        service_token:
          type: string
          description: 服务令牌，若设置则覆盖用户名/密码认证。
        tls_ca:
          type: string
        certificate_fingerprint:
          type: string
        headers:
          type: array
          items:
            type: string
    DSSLSConfig:
      type: object
      description: 阿里云日志服务（SLS）数据源配置。
      properties:
        access_key_id:
          type: string
          description: 阿里云 Access Key ID。
        access_key_secret:
          type: string
          description: 阿里云 Access Key Secret。
        headers:
          type: array
          items:
            type: string
          description: 自定义 HTTP 请求头。
    DSVictoriaLogsConfig:
      type: object
      description: VictoriaLogs 数据源配置，TLS 字段继承自 TLSClientConfig。
      properties:
        basic_auth_enabled:
          type: boolean
        basic_auth_username:
          type: string
        basic_auth_password:
          type: string
        headers:
          type: array
          items:
            type: string
        params:
          type: array
          items:
            type: string
        tls_ca:
          type: string
        tls_cert:
          type: string
        tls_key:
          type: string
        tls_key_pwd:
          type: string
        tls_skip_verify:
          type: boolean
        tls_server_name:
          type: string
        tls_min_version:
          type: string
        tls_max_version:
          type: string
    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
    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
  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.
    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.
  securitySchemes:
    AppKeyAuth:
      type: apiKey
      in: query
      name: app_key
      description: >-
        在 Flashduty 控制台 账户 → APP Key 中签发的 app_key。调用任何公开 API
        时都必须携带。它等同于所属账户的身份凭证，请妥善保管。

````