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:
- I ensured that the Telegraf configuration includes the correct keys (measurement and timestamp).
- 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?