你能告诉我发送给 VictoriaMetrics 的正确 JSON 负载是什么吗?
我正在发送 JSON 有效负载并收到错误:
[{"metric":{"__name__":"cpu_usage_percent","instance":"localhost:9100","job":"Alexs-MBP.Home"},"timestamps":[1736670067000],"values":[7.737105]}]
错误:
2025-01-12T08:21:09.324Z error VictoriaMetrics/lib/protoparser/vmimport/parser.go:239 skipping json line "[{\"metric\":{\"__name__\":\"cpu_usage_percent\",\"instance\":\"localhost:9100\",\"job\":\"Alexs-MBP.Home\"},\"timestamps\":[1736670067000],\"values\":[7.737105]}]" because of error: missing `metric` object
功能:
func sendMetricToVictoria(metricName string, value float32, timestampStr string) error {
timestamp, err := strconv.ParseInt(timestampStr, 10, 64)
if err != nil {
return fmt.Errorf("invalid timestamp format: %v", err)
}
// Get Hostname
hostname, err := os.Hostname()
if err != nil {
return fmt.Errorf("error getting hostname: %v", err)
}
// Prepare the payload for VictoriaMetrics
data := []map[string]interface{}{
{
"metric": map[string]interface{}{
"__name__": metricName, // Name of the metric
"job": hostname, // Add the job label
"instance": "localhost:9100", // Add the instance label
},
"values": []float32{value},
"timestamps": []int64{timestamp * 1000}, // Convert seconds to milliseconds
},
}
log.Printf("JSON being sent: %v\n", data)
// Send data to VictoriaMetrics
return sendToVictoriaMetrics(data)
}
API:
http://127.0.0.1:8428/api/v1/import
***************** 更新 - 已修复 ***********************
json 错误
[{"metric":{"__name__":"cpu_usage_percent","instance":"localhost:9100","job":"Alexs-MBP.Home"},"timestamps":[1736799662748],"values":[4.090150356292725]}]
正确的 json(愚蠢的错误 - 使用 [] 数组)
{"metric":{"__name__":"cpu_usage_percent","instance":"localhost:9100","job":"Alexs-MBP.Home"},"timestamps":[1736800254000],"values":[5.096073627471924]}
func sendMetricToVictoria(metricName string, value float32, timestampStr string) error {
timestamp, err := strconv.ParseInt(timestampStr, 10, 64)
if err != nil {
return fmt.Errorf("invalid timestamp format: %v", err)
}
// Get Hostname
hostname, err := os.Hostname()
if err != nil {
return fmt.Errorf("error getting hostname: %v", err)
}
// Prepare the payload for VictoriaMetrics
data := map[string]interface{}{
"metric": map[string]string{
"__name__": metricName, // Metric name
"job": hostname, // Job label
"instance": "localhost:9100", // Instance label
},
"values": []float64{float64(value)}, // Convert to float64 as required by VictoriaMetrics
"timestamps": []int64{timestamp * 1000}, // Convert to milliseconds
}
// Log the JSON for debugging
jsonData, _ := json.MarshalIndent(data, "", " ")
log.Printf("Sending JSON to VictoriaMetrics: %s\n", string(jsonData))
// Send data to VictoriaMetrics
return sendToVictoriaMetrics(data)
}
从https://docs.victoriametrics.com/#how-to-import-time-series-data和https://docs.victoriametrics.com/#json-line-format来看,该端点需要JSON 行有效负载,即类似
如果我查看你的有效负载,它是纯 JSON,也就是说,外部数据结构是一个 JSON 数组:
有关 JSON-line 的更多信息,请参阅https://jsonlines.org/。
使用 Go 创建 JSON 行很简单,可以使用 stdlib json 包来完成:不是编组整个切片,而是遍历切片并编组各个元素。
请参阅此 go 代码示例https://go.dev/play/p/LqLMWdNOBD9
当然,这是非常简单的实现,没有批处理或发送多行。您可以在这里和这里找到更多用于发送数据的类似于生产的设置。