简体   繁体   English

有没有办法让我使用电报从标题/正文中提取请求信息

[英]Is there a way for me to extract request information from headers/body using telegraf

So I have telegraf installed on one of our ec2 instances.所以我在我们的一个 ec2 实例上安装了 telegraf。 And on the instance we have a spring boot application installed servicing POST requests and forwarding them on.在实例上,我们安装了一个 Spring Boot 应用程序,为 POST 请求提供服务并转发它们。

I was wondering if it is possible to extract information from requests coming into this service such as header information or key value pairs from a json body and then forward this information onto telegraf?我想知道是否可以从进入此服务的请求中提取信息,例如来自 json 主体的标头信息或键值对,然后将此信息转发到电报?

Telegraf would be then push this information to influxdb.然后 Telegraf 会将这些信息推送到 influxdb。

The idea is that we will extract this information and display using grafana so we can visualise how many requests are coming from where.我们的想法是我们将提取这些信息并使用 grafana 显示,以便我们可以看到有多少请求来自哪里。

I know there are some plugins like http_listener but I can't tell from the readme if this could possibly work or if there is a better way to get this working?我知道有一些像 http_listener 这样的插件,但是我无法从自述文件中判断这是否可行,或者是否有更好的方法可以使其正常工作?

Thanks for any information you can provide in advance!感谢您提前提供的任何信息!

You could try work with this sample .您可以尝试使用此 示例 The sample code could be:示例代码可以是:

[[inputs.http]]
# URL for your data in JSON format
urls = ["https://yourURL/sample.json"]

# Overwrite measurement name from default `http` to `someOtherMeasurement`
name_override = "someOtherMeasurement"

# Data from HTTP in JSON format
data_format = "json_v2"

      # Add a subtable to use the `json_v2` parser
      [[inputs.http.json_v2]]

          # Add an object subtable for to parse a JSON object
          [[inputs.http.json_v2.object]]

              # Parse data in `data.stations` path only
              path = "data.stations"

              #Set station metadata as tags
              tags = ["yourTags"]

              # Latest station information reported at `last_reported`
              timestamp_key = "theTimeStamp"

              # Time is reported in unix timestamp format
              timestamp_format = "unix"
              
              # all other json key-value pairs will be turned into fields

An excerpt of original HTTP JSON response is:原始 HTTP JSON 响应的摘录是:

{
   "data":{
      "stations":[
         {
            "last_reported":1655171050,
            "is_renting":1,
            "num_bikes_available":21,
            "is_installed":1,
            "legacy_id":"72",
            "station_status":"active",
            "num_ebikes_available":2,
            "is_returning":1,
            "eightd_has_available_keys":false,
            "num_docks_disabled":0,
            "station_id":"72",
            "num_bikes_disabled":1,
            "num_docks_available":32
         },
         "..."
      ]
   },
   "last_updated":1655171085,
   "ttl":5
}

Above Telegraf script will turn the JSON into following line protocol:上面的 Telegraf 脚本会将 JSON 转换为以下行协议:

someOtherMeasurement,station_id=72 eightd_has_available_keys=false,is_installed=1,is_renting=1,is_returning=1,legacy_id="72",num_bikes_available=21,num_bikes_disabled=1,num_docks_available=32,num_docks_disabled=1,num_ebikes_available=2,station_status="active" 1655171050000000000

You could play with the sample JSON with Telegraf to update your configurations.您可以使用 Telegraf 使用示例 JSON 来更新您的配置。 Happy coding.快乐编码。

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

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