jsongovictoriametrics

VictoriaMetrics: missing 'metric' object


Would you please tell me what is correct json payload to send to VictoriaMetrics

JSON payload I'm sending and getting error:

[{"metric":{"__name__":"cpu_usage_percent","instance":"localhost:9100","job":"Alexs-MBP.Home"},"timestamps":[1736670067000],"values":[7.737105]}]

Error:

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:

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

***************** UPDATE - FIXED **********************

Incorrect json

[{"metric":{"__name__":"cpu_usage_percent","instance":"localhost:9100","job":"Alexs-MBP.Home"},"timestamps":[1736799662748],"values":[4.090150356292725]}]

Correct json (silly mistake - used [] array)

{"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)
}

Solution

  • From https://docs.victoriametrics.com/#how-to-import-time-series-data and https://docs.victoriametrics.com/#json-line-format, that endpoint want a JSON-line payload, that is, something like

    {"foo": "bar 1"}
    {"foo": "bar 2"}
    

    while if I look at your payload, it is plain JSON, that is, the outer data structure is a JSON array:

    [
      {"foo": "bar 1"},
      {"foo": "bar 2"},
      ...
    ]
    

    For more information about JSON-line, see for example https://jsonlines.org/.

    Creating JSON lines with Go is simple, it can be done with the stdlib json package: instead of marshaling the whole slice, iterate over the slice and marshal the individual elements.