Files
VictoriaMetrics/lib/promrelabel/debug_test.go
Zhu Jiekun 6117b2ead9 VMUI/relabel debug: Allow labels textarea input without curly braces (#9950)
### Describe Your Changes

Fixed #9900

relax the validation for the labels text area. It now accepts input
labels without being enclosed in curly braces.

The following input format should be supported now:


```
	metric_name
	metric_name{label1="value1"}
	{__name__="metric_name", label1="value1"}
	__name__="metric_name", label1="value1"
	label1="value1"
```

### Checklist

The following checks are **mandatory**:

- [x] My change adheres to [VictoriaMetrics contributing
guidelines](https://docs.victoriametrics.com/victoriametrics/contributing/#pull-request-checklist).
- [x] My change adheres to [VictoriaMetrics development
goals](https://docs.victoriametrics.com/victoriametrics/goals/).

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Max Kotliar <mkotlyar@victoriametrics.com>
2025-11-13 12:41:38 +02:00

45 lines
1.5 KiB
Go

package promrelabel
import (
"bytes"
"html"
"testing"
"github.com/valyala/fastjson"
)
// TestWriteRelabelDebugSupportFormats verifies the relabeling debug input, rules and output.
func TestWriteRelabelDebugSupportFormats(t *testing.T) {
f := func(input, rule, expect string) {
// execute
outputWriter := bytes.NewBuffer(nil)
writeRelabelDebug(outputWriter, false, "", input, rule, "json", nil)
// the response is in JSON with HTML content, extract the `resultingLabels` in JSON and unescape it.
resultingLabels := fastjson.GetString(outputWriter.Bytes(), `resultingLabels`)
resultingLabels = html.UnescapeString(resultingLabels)
// verify
if resultingLabels != expect {
t.Fatalf(`expected "%s", got "%s"`, expect, resultingLabels)
}
}
// test pure parsing
// ruleTestParsing rule should NOT drop anything. it should ask `writeRelabelDebug` to respond with whatever the input is (after parsing).
ruleTestParsing := `
- action: labeldrop
regex: "a_not_exist_label"
`
f(`metric_name`, ruleTestParsing, `metric_name`)
f(`metric_name{label1="value1"}`, ruleTestParsing, `metric_name{label1="value1"}`)
f(`{__name__="metric_name", label1="value1"}`, ruleTestParsing, `metric_name{label1="value1"}`)
f(`__name__="metric_name", label1="value1"`, ruleTestParsing, `metric_name{label1="value1"}`)
f(`_name__="metric_name"`, ruleTestParsing, `{_name__="metric_name"}`)
// special case: incorrect input format
f(`{_name__="metric_name"`, ruleTestParsing, ``)
f(`_name__="metric_name}"`, ruleTestParsing, ``)
f(`metrics_name}"`, ruleTestParsing, ``)
}