Skip to content

Commit

Permalink
move label parsing to before getting the mapping, mapping labels over…
Browse files Browse the repository at this point in the history
…ride parsed labels

Signed-off-by: glightfoot <[email protected]>
  • Loading branch information
glightfoot committed Jun 19, 2020
1 parent d17a563 commit f6262ec
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 28 deletions.
3 changes: 2 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func TestProcessLine(t *testing.T) {
{
line: "my.nomap.metric.novalue 9001 ",
name: "my_nomap_metric_novalue",
mappingLabels: nil,
mappingLabels: prometheus.Labels{},
value: float64(9001),
willFail: true,
},
Expand All @@ -104,6 +104,7 @@ func TestProcessLine(t *testing.T) {
line: "my.mapped.strict.metric 55 1534620625",
name: "my_mapped_strict_metric",
value: float64(55),
mappingLabels: prometheus.Labels{},
mappingPresent: true,
willFail: false,
strict: true,
Expand Down
45 changes: 18 additions & 27 deletions pkg/line/line.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,45 +122,36 @@ func ProcessLine(line string, metricmapper metricmapper.MetricMapper, sampleCh c

originalName := parts[0]

var name string
var err error
labels := make(prometheus.Labels)
name, err := parseMetricNameAndTags(originalName, labels, tagErrors)
if err != nil {
level.Info(logger).Log("msg", "Invalid tags", "line", line)
return
}

mapping, labels, mappingPresent := metricmapper.GetMapping(originalName, mapper.MetricTypeGauge)
// check to ensure the same tags are present
if validKeys := metricNameKeysIndex.checkNameAndKeys(name, labels); !validKeys {
level.Info(logger).Log("msg", "Dropped because metric keys do not match previously used keys", "line", line)
invalidMetrics.Inc()

if (mappingPresent && mapping.Action == mapper.ActionTypeDrop) || (!mappingPresent && strictMatch) {
return
}

if mappingPresent {
parsedLabels := make(prometheus.Labels)
mapping, mlabels, mappingPresent := metricmapper.GetMapping(name, mapper.MetricTypeGauge)

if _, err = parseMetricNameAndTags(originalName, parsedLabels, tagErrors); err != nil {
level.Info(logger).Log("msg", "Invalid tags", "line", line)
return
}
if (mappingPresent && mapping.Action == mapper.ActionTypeDrop) || (!mappingPresent && strictMatch) {
return
}

if mappingPresent {
name = invalidMetricChars.ReplaceAllString(mapping.Name, "_")
// check to ensure the same tags are present
if validKeys := metricNameKeysIndex.checkNameAndKeys(name, parsedLabels); !validKeys {
level.Info(logger).Log("msg", "Dropped because metric keys do not match previously used keys", "line", line)
invalidMetrics.Inc()

return
// append labels from the mapping to those parsed, with mapping labels overriding
for k, v := range mlabels {
labels[k] = v
}
} else {
labels = make(prometheus.Labels)
name, err = parseMetricNameAndTags(originalName, labels, tagErrors)
if err != nil {
level.Info(logger).Log("msg", "Invalid tags", "line", line)
return
}
name = invalidMetricChars.ReplaceAllString(name, "_")
// check to ensure the same tags are present
if validKeys := metricNameKeysIndex.checkNameAndKeys(name, labels); !validKeys {
level.Info(logger).Log("msg", "Dropped because metric keys do not match previously used keys", "line", line)
invalidMetrics.Inc()
return
}
}

value, err := strconv.ParseFloat(parts[1], 64)
Expand Down

0 comments on commit f6262ec

Please sign in to comment.