Skip to content

Support unnamed groups in carbon receiver regex parser #39137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .chloggen/carbon-unnamed-regex-groups.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: carbonreceiver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Support unnamed groups in carbon receiver regex parser

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [39137]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [user]
6 changes: 6 additions & 0 deletions receiver/carbonreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ func TestLoadConfig(t *testing.T) {
},
MetricType: "cumulative",
},
{
Regexp: `(optional_prefix\.)?(?P<key_just>test)\.(?P<key_match>.*)`,
},
{
Regexp: `(experiment(?P<key_experiment>[0-9]+)\.)?(?P<key_just>test)\.(?P<key_match>.*)`,
},
{
Regexp: `(?P<key_just>test)\.(?P<key_match>.*)`,
},
Expand Down
15 changes: 12 additions & 3 deletions receiver/carbonreceiver/protocol/regex_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,19 @@ func (rpp *regexPathParser) ParsePath(path string, parsedPath *ParsedPath) error
attributes := pcommon.NewMap()

for i := 1; i < len(ms); i++ {
if strings.HasPrefix(nms[i], metricNameCapturePrefix) {
metricNameLookup[nms[i]] = ms[i]
groupName, groupValue := nms[i], ms[i]
if groupName == "" {
// Skip unnamed groups.
continue
}
if groupValue == "" {
// Skip unmatched groups.
continue
}
if strings.HasPrefix(groupName, metricNameCapturePrefix) {
metricNameLookup[groupName] = groupValue
} else {
attributes.PutStr(nms[i][len(keyCapturePrefix):], ms[i])
attributes.PutStr(groupName[len(keyCapturePrefix):], groupValue)
}
}

Expand Down
142 changes: 142 additions & 0 deletions receiver/carbonreceiver/protocol/regex_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,148 @@ func Test_regexParser_parsePath(t *testing.T) {
}
}

func Test_regexParser_parsePath_simple_unnamed_group(t *testing.T) {
config := RegexParserConfig{
Rules: []*RegexRule{
{
Regexp: `(prefix\.)?(?P<key_svc>[^.]+)\.(?P<key_host>[^.]+)\.cpu\.seconds`,
NamePrefix: "cpu_seconds",
},
},
}

require.NoError(t, compileRegexRules(config.Rules))
rp := &regexPathParser{
rules: config.Rules,
}

tests := []struct {
name string
path string
wantName string
wantAttributes pcommon.Map
wantMetricType TargetMetricType
wantErr bool
}{
{
name: "no_rule_match",
path: "service_name.host01.rpc.duration.seconds",
wantName: "service_name.host01.rpc.duration.seconds",
wantAttributes: pcommon.NewMap(),
},
{
name: "match_no_prefix",
path: "service_name.host00.cpu.seconds",
wantName: "cpu_seconds",
wantAttributes: func() pcommon.Map {
m := pcommon.NewMap()
m.PutStr("svc", "service_name")
m.PutStr("host", "host00")
return m
}(),
},
{
name: "match_optional_prefix",
path: "prefix.service_name.host00.cpu.seconds",
wantName: "cpu_seconds",
wantAttributes: func() pcommon.Map {
m := pcommon.NewMap()
m.PutStr("svc", "service_name")
m.PutStr("host", "host00")
return m
}(),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := ParsedPath{}
err := rp.ParsePath(tt.path, &got)
if tt.wantErr {
assert.Error(t, err)
return
}

assert.Equal(t, tt.wantName, got.MetricName)
assert.Equal(t, tt.wantAttributes, got.Attributes)
assert.Equal(t, tt.wantMetricType, got.MetricType)
})
}
}

func Test_regexParser_parsePath_key_inside_unnamed_group(t *testing.T) {
config := RegexParserConfig{
Rules: []*RegexRule{
{
Regexp: `(job=(?P<key_job>[^.]+).)?(?P<key_svc>[^.]+)\.(?P<key_host>[^.]+)\.cpu\.seconds`,
NamePrefix: "cpu_seconds",
MetricType: string(GaugeMetricType),
},
},
}

require.NoError(t, compileRegexRules(config.Rules))
rp := &regexPathParser{
rules: config.Rules,
}

tests := []struct {
name string
path string
wantName string
wantAttributes pcommon.Map
wantMetricType TargetMetricType
wantErr bool
}{
{
name: "no_rule_match",
path: "service_name.host01.rpc.duration.seconds",
wantName: "service_name.host01.rpc.duration.seconds",
wantAttributes: pcommon.NewMap(),
},
{
name: "match_missing_optional_key",
path: "service_name.host00.cpu.seconds",
wantName: "cpu_seconds",
wantAttributes: func() pcommon.Map {
m := pcommon.NewMap()
m.PutStr("svc", "service_name")
m.PutStr("host", "host00")
return m
}(),
wantMetricType: GaugeMetricType,
},
{
name: "match_present_optional_key",
path: "job=71972c09-de94-4a4e-a8a7-ad3de050a141.service_name.host00.cpu.seconds",
wantName: "cpu_seconds",
wantAttributes: func() pcommon.Map {
m := pcommon.NewMap()
m.PutStr("job", "71972c09-de94-4a4e-a8a7-ad3de050a141")
m.PutStr("svc", "service_name")
m.PutStr("host", "host00")
return m
}(),
wantMetricType: GaugeMetricType,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := ParsedPath{}
err := rp.ParsePath(tt.path, &got)
if tt.wantErr {
assert.Error(t, err)
return
}

assert.Equal(t, tt.wantName, got.MetricName)
assert.Equal(t, tt.wantAttributes, got.Attributes)
assert.Equal(t, tt.wantMetricType, got.MetricType)
})
}
}

var res struct {
name string
attributes pcommon.Map
Expand Down
6 changes: 5 additions & 1 deletion receiver/carbonreceiver/testdata/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ carbon/regex:
# type is used to select the metric type to be set, the default is
# "gauge", the other alternative is "cumulative".
type: cumulative
# The second rule for this "regex" parser.
# The second rule matches metric with or without a prefix.
- regexp: "(optional_prefix\\.)?(?P<key_just>test)\\.(?P<key_match>.*)"
# The third rule parses an optional dimension with key "experiment".
- regexp: "(experiment(?P<key_experiment>[0-9]+)\\.)?(?P<key_just>test)\\.(?P<key_match>.*)"
# The forth rule for this "regex" parser.
- regexp: "(?P<key_just>test)\\.(?P<key_match>.*)"
# Name separator is used when concatenating named regular expression
# captures prefixed with "name_"
Expand Down
Loading