Skip to content

Export LabelsFromName func from CollectD receiver #117

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
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 14 additions & 11 deletions receiver/collectdreceiver/collectd.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func (r *collectDRecord) pointTypeInstance(attrs map[string]string, parts []byte
return parts
}

instanceName, extractedAttrs := labelsFromName(r.TypeInstance)
instanceName, extractedAttrs := LabelsFromName(r.TypeInstance)
if instanceName != "" {
if len(parts) > 0 {
parts = append(parts, '.')
Expand All @@ -210,13 +210,16 @@ func (r *collectDRecord) pointTypeInstance(attrs map[string]string, parts []byte
return parts
}

// labelsFromName tries to pull out dimensions out of name in the format name[k=v,f=x]-morename
// would return name-morename and extract dimensions (k,v) and (f,x)
// if we encounter something we don't expect use original name.
// This is a bit complicated to avoid allocations, string.split allocates, while slices
// inside same function, do not.
func labelsFromName(val *string) (instanceName string, toAddDims map[string]string) {
instanceName = *val
// LabelsFromName tries to pull out dimensions out of name in the format
// "name[k=v,f=x]-more_name".
// For the example above it would return "name-more_name" and extract dimensions
// (k,v) and (f,x).
// If something unexpected is encountered it returns the original metric name.
//
// The code tries to avoid allocation by using local slices and avoiding calls
// to functions like strings.Slice.
func LabelsFromName(val *string) (metricName string, labels map[string]string) {
metricName = *val
index := strings.Index(*val, "[")
if index > -1 {
left := (*val)[:index]
Expand Down Expand Up @@ -244,8 +247,8 @@ func labelsFromName(val *string) (instanceName string, toAddDims map[string]stri
prev = cindex + 1
cindex = strings.Index(dimensions[prev:], ",") + prev
}
toAddDims = working
instanceName = left + rest
labels = working
metricName = left + rest
}
}
return
Expand All @@ -267,7 +270,7 @@ func parseAndAddLabels(labels map[string]string, pluginInstance *string, host *s
}

func parseNameForLabels(labels map[string]string, key string, val *string) {
instanceName, toAddDims := labelsFromName(val)
instanceName, toAddDims := LabelsFromName(val)

for k, v := range toAddDims {
if _, exists := labels[k]; !exists {
Expand Down
49 changes: 49 additions & 0 deletions receiver/collectdreceiver/collectd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,3 +384,52 @@ var wantMetricsData = []*metricspb.Metric{
},
},
}

func TestLabelsFromName(t *testing.T) {
tests := []struct {
name string
wantMetricName string
wantLabels map[string]string
}{
{
name: "simple",
wantMetricName: "simple",
},
{
name: "single[k=v]",
wantMetricName: "single",
wantLabels: map[string]string{
"k": "v",
},
},
{
name: "a.b.c.[k=v].d",
wantMetricName: "a.b.c..d",
wantLabels: map[string]string{
"k": "v",
},
},
{
name: "a.b[k0=v0,k1=v1,k2=v2].c",
wantMetricName: "a.b.c",
wantLabels: map[string]string{
"k0": "v0", "k1": "v1", "k2": "v2",
},
},
{
name: "empty[]",
wantMetricName: "empty[]",
},
{
name: "mal.formed[k_no_sep]",
wantMetricName: "mal.formed[k_no_sep]",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotMetricName, gotLabels := LabelsFromName(&tt.name)
assert.Equal(t, tt.wantMetricName, gotMetricName)
assert.Equal(t, tt.wantLabels, gotLabels)
})
}
}