Skip to content
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

Check for empty tag value before metric allocation #2404

Closed
wants to merge 4 commits into from
Closed
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
7 changes: 6 additions & 1 deletion metric/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,18 @@ func New(
// pre-allocate exact size of the tags slice
taglen := 0
for k, v := range tags {
// TODO check that length of tag key & value are > 0
if len(k) == 0 || len(v) == 0 {
continue
}
taglen += 2 + len(escape(k, "tagkey")) + len(escape(v, "tagval"))
}
m.tags = make([]byte, taglen)

i := 0
for k, v := range tags {
if len(k) == 0 || len(v) == 0 {
continue
}
m.tags[i] = ','
i++
i += copy(m.tags[i:], escape(k, "tagkey"))
Expand Down
23 changes: 23 additions & 0 deletions metric/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -625,3 +625,26 @@ func TestNewMetricFailNaN(t *testing.T) {
_, err := New("cpu", tags, fields, now)
assert.NoError(t, err)
}

func TestEmptyTagValueOrKey(t *testing.T) {
now := time.Now()

tags := map[string]string{
"host": "localhost",
"emptytag": "",
"": "valuewithoutkey",
}
fields := map[string]interface{}{
"usage_idle": float64(99),
}
m, err := New("cpu", tags, fields, now)

assert.True(t, m.HasTag("host"))
assert.False(t, m.HasTag("emptytag"))
assert.Equal(t,
fmt.Sprintf("cpu,host=localhost usage_idle=99 %d\n", now.UnixNano()),
m.String())

assert.NoError(t, err)

}
8 changes: 3 additions & 5 deletions plugins/inputs/prometheus/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,9 @@ func TestParseValidPrometheus(t *testing.T) {
"gauge": float64(1),
}, metrics[0].Fields())
assert.Equal(t, map[string]string{
"osVersion": "CentOS Linux 7 (Core)",
"dockerVersion": "1.8.2",
"kernelVersion": "3.10.0-229.20.1.el7.x86_64",
"cadvisorRevision": "",
"cadvisorVersion": "",
"osVersion": "CentOS Linux 7 (Core)",
"dockerVersion": "1.8.2",
"kernelVersion": "3.10.0-229.20.1.el7.x86_64",
}, metrics[0].Tags())

// Counter value
Expand Down
2 changes: 1 addition & 1 deletion plugins/parsers/nagios/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestParseValidOutput(t *testing.T) {
assert.Equal(t, map[string]interface{}{
"value": float64(0.008457),
}, metrics[0].Fields())
assert.Equal(t, map[string]string{"unit": ""}, metrics[0].Tags())
assert.Equal(t, map[string]string{}, metrics[0].Tags())
}

func TestParseInvalidOutput(t *testing.T) {
Expand Down