对 kafka-rest 的 CURL 请求

CURL request to kafka-rest

我已经使用 curl 调用在 kafka-rest 上执行了 HTTP POST。一个请求成功但另一个请求(具有不同的 json)返回 422 错误 ({"error_code":422,"message":"Unrecognized field: receiver"}).

工作要求

    curl -X POST -H "Content-Type: application/vnd.kafka.json.v2+json" -H "Accept: application/vnd.kafka.v2+json" --data '{"records":[{"value":{"foo":"bar"}}]}' "http://localhost:8082/topics/jsontest"

输出

{"offsets":[{"partition":0,"offset":0,"error_code":null,"error":null}],"key_schema_id":null,"value_schema_id":null}

请求无效

 curl -X POST -H "Content-Type: application/vnd.kafka.json.v2+json" -H "Accept: application/vnd.kafka.v2+json" --data @alert-request.json "http://localhost:8082/topics/jsontest"

输出

{"error_code":422,"message":"Unrecognized field: receiver"}

警报-request.json

{
  "receiver": "email-logs",
  "status": "firing",
  "alerts": [
    {
      "status": "firing",
      "labels": {
        "alertname": "CPUUsageAbove20%",
        "instance": "node-exporter:9100",
        "job": "node-exporter",
        "monitor": "my-project",
        "severity": "warn",
        "team": "raptors"
      },
      "annotations": {
        "dashboard": "www.prometheus.io",
        "description": "CPU usage on node-exporter:9100 has reached 60"
      },
      "startsAt": "2020-04-30T08:03:17.309164516Z",
      "endsAt": "0001-01-01T00:00:00Z",
      "generatorURL": "http://0ab6d9955c65:9090/graph?g0.expr=60+%3E+job%3Anode_cpu_seconds%3Ausage+%3E+20\u0026g0.tab=1",
      "fingerprint": "9c558a8c20c2ba08"
    }
  ],
  "groupLabels": {
    "alertname": "CPUUsageAbove20%",
    "team": "raptors"
  },
  "commonLabels": {
    "alertname": "CPUUsageAbove20%",
    "instance": "node-exporter:9100",
    "job": "node-exporter",
    "monitor": "my-project",
    "severity": "warn",
    "team": "raptors"
  },
  "commonAnnotations": {
    "dashboard": "www.prometheus.io",
    "description": "CPU usage on node-exporter:9100 has reached 60"
  },
  "externalURL": "http://5493399b56dc:9093",
  "version": "4",
  "groupKey": "{}/{team=~\"^(?:(raptors|leafs))$\"}:{alertname=\"CPUUsageAbove20%\", team=\"raptors\"}"
}

我需要使用 kafka-rest 在 json 上面写到 kafka,我使用 curl 调用在 kafka-rest 上执行 HTTP post 但它返回错误。如何使用kafka-rest(使用curl)成功写入kafka?

响应中的错误代码为"error_code":422表示请求负载不正确。

根据kafka-rest的API spec,有效的请求负载结构之一是

{
  "records": [
    {
      "key": "recordKey",
      "value": "recordValue"
    }
  ]
}

因此,您的 alert-request.json 文件应修改为 records

value 字段下的内容
{
  "records": [
    {
      "key": "recordKey",
      "value": {
        "receiver": "email-logs",
        "status": "firing",
        "alerts": [
          {
            "status": "firing",
            "labels": {
              "alertname": "CPUUsageAbove20%",
              "instance": "node-exporter:9100",
              "job": "node-exporter",
              "monitor": "my-project",
              "severity": "warn",
              "team": "raptors"
            },
            "annotations": {
              "dashboard": "www.prometheus.io",
              "description": "CPU usage on node-exporter:9100 has reached 60"
            },
            "startsAt": "2020-04-30T08:03:17.309164516Z",
            "endsAt": "0001-01-01T00:00:00Z",
            "generatorURL": "http://0ab6d9955c65:9090/graph?g0.expr=60+%3E+job%3Anode_cpu_seconds%3Ausage+%3E+20\u0026g0.tab=1",
            "fingerprint": "9c558a8c20c2ba08"
          }
        ],
        "groupLabels": {
          "alertname": "CPUUsageAbove20%",
          "team": "raptors"
        },
        "commonLabels": {
          "alertname": "CPUUsageAbove20%",
          "instance": "node-exporter:9100",
          "job": "node-exporter",
          "monitor": "my-project",
          "severity": "warn",
          "team": "raptors"
        },
        "commonAnnotations": {
          "dashboard": "www.prometheus.io",
          "description": "CPU usage on node-exporter:9100 has reached 60"
        },
        "externalURL": "http://5493399b56dc:9093",
        "version": "4",
        "groupKey": "{}/{team=~\"^(?:(raptors|leafs))$\"}:{alertname=\"CPUUsageAbove20%\", team=\"raptors\"}"
      }
    }
  ]
}

请注意,key 字段是可选的,您可以提供适合您的用例的任何唯一键。