Skip to content

Commit

Permalink
Make sure metrics with different labels are handled properly.
Browse files Browse the repository at this point in the history
  • Loading branch information
travisgroth committed Aug 21, 2018
1 parent f84f85d commit 247f94c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
11 changes: 6 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,9 @@ func (c *graphiteCollector) processLine(line string) {
log.Infof("Invalid part count of %d in line: %s", len(parts), line)
return
}
originalName := parts[0]
var name string
mapping, labels, present := c.mapper.GetMapping(parts[0], "graphite")
mapping, labels, present := c.mapper.GetMapping(originalName, "graphite")

if (present && mapping.Action == mapper.ActionTypeDrop) || (!present && c.strictMatch) {
return
Expand All @@ -118,7 +119,7 @@ func (c *graphiteCollector) processLine(line string) {
if present {
name = invalidMetricChars.ReplaceAllString(mapping.Name, "_")
} else {
name = invalidMetricChars.ReplaceAllString(parts[0], "_")
name = invalidMetricChars.ReplaceAllString(originalName, "_")
}

value, err := strconv.ParseFloat(parts[1], 64)
Expand All @@ -132,12 +133,12 @@ func (c *graphiteCollector) processLine(line string) {
return
}
sample := graphiteSample{
OriginalName: parts[0],
OriginalName: originalName,
Name: name,
Value: value,
Labels: labels,
Type: prometheus.GaugeValue,
Help: fmt.Sprintf("Graphite metric %s", parts[0]),
Help: fmt.Sprintf("Graphite metric %s", originalName),
Timestamp: time.Unix(int64(timestamp), int64(math.Mod(timestamp, 1.0)*1e9)),
}
lastProcessed.Set(float64(time.Now().UnixNano()) / 1e9)
Expand All @@ -154,7 +155,7 @@ func (c *graphiteCollector) processSamples() {
return
}
c.mu.Lock()
c.samples[sample.Name] = sample
c.samples[sample.OriginalName] = sample
c.mu.Unlock()
case <-ticker:
// Garbage collect expired samples.
Expand Down
14 changes: 12 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"strings"
"testing"

"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -51,7 +52,15 @@ func TestProcessLine(t *testing.T) {
present: true,
value: float64(9001),
},

{
line: "my.simple.metric.baz 9002 1534620625",
name: "my_simple_metric",
labels: map[string]string{
"baz": "bat",
},
present: true,
value: float64(9002),
},
{
line: "my.nomap.metric 9001 1534620625",
name: "my_nomap_metric",
Expand Down Expand Up @@ -113,7 +122,8 @@ func TestProcessLine(t *testing.T) {

c.ch <- nil
for _, k := range testCases {
sample := c.samples[k.name]
originalName := strings.Split(k.line, " ")[0]
sample := c.samples[originalName]
if k.willFail {
assert.Nil(t, sample, "Found %s", k.name)
} else {
Expand Down

0 comments on commit 247f94c

Please sign in to comment.