最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Telegraf HTTP Listener Plugin Returning 400 "Bad Request" for JSON Data with Measurement Key - Stack

programmeradmin4浏览0评论

I am trying to send JSON data to Telegraf's http_listener_v2 plugin, but I keep receiving a 400: "Bad Request" error. My data format includes a "measurement" field, but I am unsure if my Telegraf configuration is correct, or if my JSON format is not aligned with Telegraf's expectations.

Here is my Python code to generate the logs:

import requests
import json
import time
from datetime import datetime, timezone

TELEGRAF_URL = "http://telegraf:8080"

while True:
    log_data = {
        "measurement": "log",  # Measurement name
        "timestamp": datetime.now(timezone.utc).isoformat(),
        "message": "Test log",
        "status": 200
    }
    
    try:
        response = requests.post(TELEGRAF_URL, json=log_data)
        if response.status_code != 204:
            print('changes')
            print(f"Erreur {response.status_code}: {response.text}")
    except Exception as e:
        print(f"Erreur d'envoi : {e}")
    
    print(log_data)
    time.sleep(1)

Telegraf Configuration:

  service_address = ":8080"
  paths = ["/"]
  data_format = "json"
  json_time_key = "timestamp"
  json_time_format = "iso8601"
  json_name_key = "measurement"  # Measurement key

Error Message: When I run the Python script, I get the following output:

Script version: v2.2 - Using json_v2 format
changes
Erreur 400: {"error":"http: bad request"}
{'measurement': 'log', 'time': '2025-03-19T11:45:03.369950+00:00', 'fields': {'message': 'Test log', 'status': 200}}
changes
Erreur 400: {"error":"http: bad request"}
{'measurement': 'log', 'time': '2025-03-19T11:45:04.375164+00:00', 'fields': {'message': 'Test log', 'status': 200}}

What I've Tried:

  1. I ensured that the Telegraf configuration includes the correct keys (measurement and timestamp).
  2. I checked the structure of my JSON, ensuring it includes the "measurement" key.

Question:


Is the Telegraf configuration correct for handling the "measurement" key in JSON format?

How can I fix this issue and successfully send the JSON data to Telegraf without getting the 400 "Bad Request" error?

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论