Skip to content

Commit

Permalink
Fixing panic on prometheus collector when label has ,
Browse files Browse the repository at this point in the history
  • Loading branch information
vjsamuel committed Apr 7, 2017
1 parent 30d9baa commit 1a3e1ed
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ https://github.com/elastic/beats/compare/v5.1.1...master[Check the HEAD diff]
- Add labels to the Docker healthcheck metricset output. {pull}3707[3707]
- Make system process metricset honor the cpu_ticks config option. {issue}3590[3590]
- Support common.Time in mapstriface.toTime() {pull}3812[3812]

- Fixing panic on prometheus collector when label has , {pull}3947[3947]
*Packetbeat*

*Winlogbeat*
Expand Down
15 changes: 15 additions & 0 deletions metricbeat/module/prometheus/collector/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,21 @@ func TestDecodeLine(t *testing.T) {
},
},
},
{
Line: `apiserver_request_count{client="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36",code="200",contentType="",resource="elasticsearchclusters",verb="LIST"} 1`,
Event: PromEvent{
key: "apiserver_request_count",
value: int64(1),
labelHash: `client="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36",code="200",contentType="",resource="elasticsearchclusters",verb="LIST"`,
labels: common.MapStr{
"client": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36",
"code": int64(200),
"contentType": "",
"resource": "elasticsearchclusters",
"verb": "LIST",
},
},
},
}

for _, test := range tests {
Expand Down
7 changes: 4 additions & 3 deletions metricbeat/module/prometheus/collector/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ type PromEvent struct {
// NewPromEvent creates a prometheus event based on the given string
func NewPromEvent(line string) PromEvent {
// Separate key and value
split := strings.Split(line, " ")
splitPos := strings.LastIndex(line, " ")
split := []string{line[:splitPos], line[splitPos+1:]}

promEvent := PromEvent{
key: split[0],
Expand Down Expand Up @@ -54,11 +55,11 @@ func extractLabels(labelsString string) common.MapStr {
keyValuePairs := common.MapStr{}

// Extract labels
labels := strings.Split(labelsString, ",")
labels := strings.Split(labelsString, "\",")
for _, label := range labels {
keyValue := strings.Split(label, "=")
// Remove " from value
keyValue[1] = keyValue[1][1 : len(keyValue[1])-1]
keyValue[1] = strings.Trim(keyValue[1], "\"")

// Converts value to int or float if needed
keyValuePairs[keyValue[0]] = convertValue(keyValue[1])
Expand Down
5 changes: 4 additions & 1 deletion metricbeat/module/prometheus/stats/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ func (m *MetricSet) Fetch() (common.MapStr, error) {
if line[0] == '#' || strings.Contains(line, "quantile=") {
continue
}
split := strings.Split(line, " ")

splitPos := strings.LastIndex(line, " ")
split := []string{line[:splitPos], line[splitPos+1:]}

entries[split[0]] = split[1]
}

Expand Down

0 comments on commit 1a3e1ed

Please sign in to comment.