Files
Hui WangandGitHub 1670b10d44 app/vmagent: add time series metadata support
By default, `vmagent` doesn't parse
[metadata](https://github.com/prometheus/docs/blob/main/docs/instrumenting/exposition_formats.md)
when scraping targets, and drops metadata that received via [Prometheus remote write v1(https://prometheus.io/docs/specs/prw/remote_write_spec/) or
[OpenTelemetryprotocol](https://github.com/open-telemetry/opentelemetryproto/blob/v1.7.0/opentelemetry/proto/metrics/v1/metrics.proto).

To enable parsing metadata when scraping and sending metadata to the
configured `-remoteWrite.url`, set `-enableMetadata=true`.

Besides native metadata fields, vmagent also adds tenant info to
metadata when `-enableMultitenantHandlers` is enabled and data is sent
via the multitenant endpoints (/insert/<accountID>/<suffix>), allowing
storing metadata under different tenants in VictoriaMetrics cluster.
However, if `vm_account_id` or `vm_project_id labels` are added directly
in metrics labels and send to the [vminsert multitenantendpoints](https://docs.victoriametrics.com/victoriametrics/cluster-victoriametrics/#multitenancy-via-labels),
tenant info won't be attached in the metadata, and it will be stored in
the default tenant of VictoriaMetrics cluster.

part of https://github.com/VictoriaMetrics/VictoriaMetrics/issues/2974
2025-08-12 15:19:50 +02:00

215 lines
4.0 KiB
Go

package prompb_test
import (
"bytes"
"reflect"
"testing"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompb"
)
func TestWriteRequestMarshalUnmarshal(t *testing.T) {
// Verify that the marshaled protobuf is unmarshalled properly
f := func(wrm *prompb.WriteRequest) {
t.Helper()
data := wrm.MarshalProtobuf(nil)
wru := &prompb.WriteRequestUnmarshaler{}
wr, err := wru.UnmarshalProtobuf(data)
if err != nil {
t.Fatalf("cannot unmarshal protobuf: %s", err)
}
if !reflect.DeepEqual(wrm, wr) {
t.Fatalf("unmarshaled WriteRequest is not equal to the original\nGot:\n%+v\nWant:\n%+v", wr, wrm)
}
dataResult := wrm.MarshalProtobuf(nil)
if !bytes.Equal(dataResult, data) {
t.Fatalf("unexpected data obtained after marshaling\ngot\n%X\nwant\n%X", dataResult, data)
}
}
f(&prompb.WriteRequest{})
f(&prompb.WriteRequest{
Timeseries: []prompb.TimeSeries{
{
Labels: []prompb.Label{
{
Name: "__name__",
Value: "process_cpu_seconds_total",
},
{
Name: "instance",
Value: "host-123:4567",
},
{
Name: "job",
Value: "node-exporter",
},
},
},
},
})
f(&prompb.WriteRequest{
Timeseries: []prompb.TimeSeries{
{
Samples: []prompb.Sample{
{
Value: 123.3434,
Timestamp: 8939432423,
},
{
Value: -123.3434,
Timestamp: 18939432423,
},
},
},
},
})
f(&prompb.WriteRequest{
Timeseries: []prompb.TimeSeries{
{
Labels: []prompb.Label{
{
Name: "__name__",
Value: "process_cpu_seconds_total",
},
{
Name: "instance",
Value: "host-123:4567",
},
{
Name: "job",
Value: "node-exporter",
},
},
Samples: []prompb.Sample{
{
Value: 123.3434,
Timestamp: 8939432423,
},
{
Value: -123.3434,
Timestamp: 18939432423,
},
},
},
},
Metadata: []prompb.MetricMetadata{
{
// COUNTER = 1
Type: 1,
MetricFamilyName: "process_cpu_seconds_total",
Help: "Total user and system CPU time spent in seconds",
Unit: "seconds",
ProjectID: 123,
AccountID: 456,
},
},
})
f(&prompb.WriteRequest{
Timeseries: []prompb.TimeSeries{
{
Labels: []prompb.Label{
{
Name: "__name__",
Value: "process_cpu_seconds_total",
},
{
Name: "instance",
Value: "host-123:4567",
},
{
Name: "job",
Value: "node-exporter",
},
},
Samples: []prompb.Sample{
{
Value: 123.3434,
Timestamp: 8939432423,
},
{
Value: -123.3434,
Timestamp: 18939432423,
},
},
},
{
Labels: []prompb.Label{
{
Name: "foo",
Value: "bar",
},
},
Samples: []prompb.Sample{
{
Value: 9873,
},
},
},
},
Metadata: []prompb.MetricMetadata{
{
// COUNTER = 1
Type: 1,
MetricFamilyName: "process_cpu_seconds_total",
Help: "Total user and system CPU time spent in seconds",
Unit: "seconds",
ProjectID: 123,
AccountID: 456,
},
},
})
// only metadata
f(&prompb.WriteRequest{
Metadata: []prompb.MetricMetadata{
{
// COUNTER = 1
Type: 1,
MetricFamilyName: "process_cpu_seconds_total",
Help: "Total user and system CPU time spent in seconds",
Unit: "seconds",
ProjectID: 123,
AccountID: 456,
},
},
})
// only metadata several
f(&prompb.WriteRequest{
Metadata: []prompb.MetricMetadata{
{
// COUNTER = 1
Type: 1,
MetricFamilyName: "process_cpu_seconds_total",
Help: "Total user and system CPU time spent in seconds",
Unit: "seconds",
ProjectID: 123,
AccountID: 456,
},
{
// GAUGE = 2
Type: 2,
MetricFamilyName: "process_memory_bytes",
Help: "Total user and system memory in bytes",
Unit: "bytes",
ProjectID: 1,
AccountID: 2,
},
},
})
}