简体   繁体   English

如何在 Telegraf 中过滤 JSON 响应

[英]How to filter JSON Response in Telegraf

I am trying to ingest JSON metric data from HTTP-Endpoints using Telegraf's HTTP Input Plugin , and write it to a Postgresql database using the Postgresql Output plugin .我正在尝试使用 Telegraf 的HTTP Input Plugin从 HTTP-Endpoints 摄取 JSON 指标数据,并使用Postgresql Output plugin将其写入 Postgresql 数据库。 This is working as expected.这按预期工作。

However, some of the JSON-Responses are very long .但是,一些 JSON-Responses 非常长。 Since the PostgreSQL Output plugin creates a column for every field, these tables become very long and cluttered with information I don't need.由于 PostgreSQL 输出插件为每个字段创建一列,因此这些表变得非常长并且堆满了我不需要的信息。

For those endpoints where the data cannot be altered at the sender's side (For example: RabbitMQ's Premade API), is there a way to filter the JSON Response in Telegraf?对于那些在发送方无法更改数据的端点(例如:RabbitMQ 的 Premade API),有没有办法在 Telegraf 中过滤 JSON 响应?

For example: I have the following JSON-Response:例如:我有以下 JSON 响应:

{
    "message_stats": {
        "publish": 1,
        "publish_details": {
            "rate": 0.0
        }
    }
}

I am only interested in message_stats.publish_details.rate .我只对message_stats.publish_details.rate感兴趣。 I do not want message_stats.publish to appear in my database.我不希望message_stats.publish出现在我的数据库中。 How can I achieve that?我怎样才能做到这一点?

One of the way how to solve this issue is using json_query parameter.如何解决此问题的方法之一是使用json_query参数。 In this parameter, you can use special syntax for filtering the response:在此参数中,您可以使用特殊语法来过滤响应:

    [[inputs.http]]
  ## One or more URLs from which to read formatted metrics
  urls = [
    "https://url-to-metrics-endpoint"
  ]
  # next field replace default metrics name "http" to your custom name:
  name_override = "custom_name"
  # set true if you want to ignore self signed certificate validation:
  insecure_skip_verify = true 
  data_format = "json"
  timeout = "5s"
  # Here you can filter your JSON using special syntax:
  json_query = "message_stats.publish_details.rate"

This is easiest example.这是最简单的例子。 More details about syntax you can find here (for example how to work with arrays and a much more): https://github.com/tidwall/gjson#path-syntax您可以在此处找到有关语法的更多详细信息(例如如何使用数组等等): https : //github.com/tidwall/gjson#path-syntax

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM