From 9b9a115f9556a1678187fe7c7480d3f2bd766a77 Mon Sep 17 00:00:00 2001 From: Julius Volz Date: Mon, 12 Aug 2013 14:51:15 +0200 Subject: [PATCH] Add method to merge a LabelSet into a Metric with collision avoidance. Colliding labels can happen e.g. when an exporter job is scraped and already includes "job" labels for its samples in /metrics. In this case, a collisionPrefix of "exporter_" is added to the colliding target labels, but the specifics (the collision prefix) are managed by Prometheus, not the client library. --- model/metric.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/model/metric.go b/model/metric.go index c5a6b7f..0b7e04a 100644 --- a/model/metric.go +++ b/model/metric.go @@ -63,3 +63,18 @@ func (m Metric) String() string { return fmt.Sprintf("%s{%s}", metricName, strings.Join(labelStrings, ", ")) } } + +func (m Metric) MergeFromLabelSet(labels LabelSet, collisionPrefix LabelName) { + for k, v := range labels { + if collisionPrefix != "" { + for { + if _, exists := m[k]; !exists { + break + } + k = collisionPrefix + k + } + } + + m[k] = v + } +}