test(golang): add test to function addRowToTimeseries (#3282)

Co-authored-by: jplanckeel-externe <jplanckeel.externe@bedrockstreaming.com>
This commit is contained in:
Jeremy PLANCKEEL 2022-11-09 14:41:26 +01:00 committed by Aliaksandr Valialkin
parent 2091693f16
commit 87375b004a
No known key found for this signature in database
GPG Key ID: A72BEC6CD3D0DED1

View File

@ -465,6 +465,328 @@ func TestScrapeWorkScrapeInternalSuccess(t *testing.T) {
`)
}
func TestAddRowToTimeseries(t *testing.T) {
f := func(r *parser.Row, cfg *ScrapeWork, needRelabel bool, dataExpected string) {
t.Helper()
wc := &writeRequestCtx{}
timeseriesExpected := parseData(dataExpected)
timestamp := int64(123000)
var sw scrapeWork
sw.Config = cfg
sw.addRowToTimeseries(wc, r, timestamp, needRelabel)
wr := wc.writeRequest
var expectEqualError error
tsExpected := timeseriesExpected[:len(wr.Timeseries)]
if err := expectEqualTimeseries(wr.Timeseries, tsExpected); err != nil {
expectEqualError = fmt.Errorf("%w\ngot\n%v\nwant\n%v", err, wr.Timeseries, tsExpected)
}
if expectEqualError != nil {
t.Fatalf("unexpected error: %s", expectEqualError)
}
}
//With HonorLabels=false
f(
&parser.Row{
Metric: "metric",
Tags: []parser.Tag{
{
Key: "a",
Value: "f",
},
},
Value: 0,
Timestamp: int64(123000),
},
&ScrapeWork{
ScrapeTimeout: time.Second * 42,
Labels: []prompbmarshal.Label{},
ExternalLabels: []prompbmarshal.Label{},
HonorLabels: false,
},
false,
`
metric{a="f"} 0 123
`)
f(
&parser.Row{
Metric: "metric",
Tags: []parser.Tag{},
Value: 0,
Timestamp: int64(123000),
},
&ScrapeWork{
ScrapeTimeout: time.Second * 42,
Labels: []prompbmarshal.Label{
{
Name: "a",
Value: "f",
},
},
ExternalLabels: []prompbmarshal.Label{},
HonorLabels: false,
},
false,
`
metric{a="f"} 0 123
`)
f(
&parser.Row{
Metric: "metric",
Tags: []parser.Tag{},
Value: 0,
Timestamp: int64(123000),
},
&ScrapeWork{
ScrapeTimeout: time.Second * 42,
Labels: []prompbmarshal.Label{},
ExternalLabels: []prompbmarshal.Label{
{
Name: "a",
Value: "f",
},
},
HonorLabels: false,
},
false,
`
metric{a="f"} 0 123
`)
f(
&parser.Row{
Metric: "metric",
Tags: []parser.Tag{
{
Key: "a",
Value: "b",
},
},
Value: 0,
Timestamp: int64(123000),
},
&ScrapeWork{
ScrapeTimeout: time.Second * 42,
Labels: []prompbmarshal.Label{
{
Name: "a",
Value: "f",
},
},
ExternalLabels: []prompbmarshal.Label{},
HonorLabels: false,
},
false,
`
metric{a="f",exported_a="b"} 0 123
`)
f(
&parser.Row{
Metric: "metric",
Tags: []parser.Tag{
{
Key: "a",
Value: "b",
},
},
Value: 0,
Timestamp: int64(123000),
},
&ScrapeWork{
ScrapeTimeout: time.Second * 42,
Labels: []prompbmarshal.Label{},
ExternalLabels: []prompbmarshal.Label{
{
Name: "a",
Value: "f",
},
},
HonorLabels: false,
},
false,
`
metric{a="f",exported_a="b"} 0 123
`)
f(
&parser.Row{
Metric: "metric",
Tags: []parser.Tag{},
Value: 0,
Timestamp: int64(123000),
},
&ScrapeWork{
ScrapeTimeout: time.Second * 42,
Labels: []prompbmarshal.Label{
{
Name: "a",
Value: "e",
},
},
ExternalLabels: []prompbmarshal.Label{
{
Name: "a",
Value: "f",
},
},
HonorLabels: false,
},
false,
`
metric{a="f",exported_a="e"} 0 123
`)
f(
&parser.Row{
Metric: "metric",
Tags: []parser.Tag{
{
Key: "a",
Value: "b",
},
},
Value: 0,
Timestamp: int64(123000),
},
&ScrapeWork{
ScrapeTimeout: time.Second * 42,
Labels: []prompbmarshal.Label{
{
Name: "a",
Value: "c",
},
},
ExternalLabels: []prompbmarshal.Label{
{
Name: "a",
Value: "d",
},
},
HonorLabels: false,
},
false,
`
metric{a="d",exported_a="c"} 0 123
`)
// With HonorLabels=true
f(
&parser.Row{
Metric: "metric",
Tags: []parser.Tag{
{
Key: "a",
Value: "b",
},
},
Value: 0,
Timestamp: int64(123000),
},
&ScrapeWork{
ScrapeTimeout: time.Second * 42,
Labels: []prompbmarshal.Label{
{
Name: "a",
Value: "f",
},
},
ExternalLabels: []prompbmarshal.Label{},
HonorLabels: true,
},
false,
`
metric{a="b"} 0 123
`)
f(
&parser.Row{
Metric: "metric",
Tags: []parser.Tag{
{
Key: "a",
Value: "b",
},
},
Value: 0,
Timestamp: int64(123000),
},
&ScrapeWork{
ScrapeTimeout: time.Second * 42,
Labels: []prompbmarshal.Label{},
ExternalLabels: []prompbmarshal.Label{
{
Name: "a",
Value: "f",
},
},
HonorLabels: true,
},
false,
`
metric{a="b"} 0 123
`)
f(
&parser.Row{
Metric: "metric",
Tags: []parser.Tag{},
Value: 0,
Timestamp: int64(123000),
},
&ScrapeWork{
ScrapeTimeout: time.Second * 42,
Labels: []prompbmarshal.Label{
{
Name: "a",
Value: "e",
},
},
ExternalLabels: []prompbmarshal.Label{
{
Name: "a",
Value: "f",
},
},
HonorLabels: true,
},
false,
`
metric{a="e"} 0 123
`)
f(
&parser.Row{
Metric: "metric",
Tags: []parser.Tag{
{
Key: "a",
Value: "b",
},
},
Value: 0,
Timestamp: int64(123000),
},
&ScrapeWork{
ScrapeTimeout: time.Second * 42,
Labels: []prompbmarshal.Label{
{
Name: "a",
Value: "c",
},
},
ExternalLabels: []prompbmarshal.Label{
{
Name: "a",
Value: "d",
},
},
HonorLabels: true,
},
false,
`
metric{a="b"} 0 123
`)
}
func parseData(data string) []prompbmarshal.TimeSeries {
var rows parser.Rows
errLogger := func(s string) {