From 89432f861eccf942e3a34b3c46e5d131e20eee77 Mon Sep 17 00:00:00 2001 From: "Matt T. Proud" Date: Sat, 10 Aug 2013 14:46:19 +0200 Subject: [PATCH] Remove base labels. --- extraction/metricfamilyprocessor.go | 9 --- extraction/metricfamilyprocessor_test.go | 13 ++-- extraction/processor.go | 33 ++------- extraction/processor0_0_1.go | 3 +- extraction/processor0_0_1_test.go | 43 +++++------ extraction/processor0_0_2.go | 8 +-- extraction/processor0_0_2_test.go | 92 ++++++++++++------------ 7 files changed, 78 insertions(+), 123 deletions(-) diff --git a/extraction/metricfamilyprocessor.go b/extraction/metricfamilyprocessor.go index c0376d9..c16f5ea 100644 --- a/extraction/metricfamilyprocessor.go +++ b/extraction/metricfamilyprocessor.go @@ -81,9 +81,6 @@ func extractCounter(out Ingester, o *ProcessOptions, f *dto.MetricFamily) error sample.Metric = model.Metric{} metric := sample.Metric - for l, v := range o.BaseLabels { - metric[l] = v - } for _, p := range m.Label { metric[model.LabelName(p.GetName())] = model.LabelValue(p.GetValue()) } @@ -111,9 +108,6 @@ func extractGauge(out Ingester, o *ProcessOptions, f *dto.MetricFamily) error { sample.Metric = model.Metric{} metric := sample.Metric - for l, v := range o.BaseLabels { - metric[l] = v - } for _, p := range m.Label { metric[model.LabelName(p.GetName())] = model.LabelValue(p.GetValue()) } @@ -143,9 +137,6 @@ func extractSummary(out Ingester, o *ProcessOptions, f *dto.MetricFamily) error sample.Metric = model.Metric{} metric := sample.Metric - for l, v := range o.BaseLabels { - metric[l] = v - } for _, p := range m.Label { metric[model.LabelName(p.GetName())] = model.LabelValue(p.GetValue()) } diff --git a/extraction/metricfamilyprocessor_test.go b/extraction/metricfamilyprocessor_test.go index 5e3b38d..54b8003 100644 --- a/extraction/metricfamilyprocessor_test.go +++ b/extraction/metricfamilyprocessor_test.go @@ -38,8 +38,7 @@ func (s *metricFamilyProcessorScenario) test(t *testing.T, set int) { i := strings.NewReader(s.in) o := &ProcessOptions{ - Timestamp: testTime, - BaseLabels: model.LabelSet{"base": "label"}, + Timestamp: testTime, } err := MetricFamilyProcessor.ProcessSingle(i, s, o) @@ -72,12 +71,12 @@ func TestMetricFamilyProcessor(t *testing.T) { { Samples: model.Samples{ &model.Sample{ - Metric: model.Metric{"base": "label", "name": "request_count", "some_label_name": "some_label_value"}, + Metric: model.Metric{"name": "request_count", "some_label_name": "some_label_value"}, Value: -42, Timestamp: testTime, }, &model.Sample{ - Metric: model.Metric{"base": "label", "name": "request_count", "another_label_name": "another_label_value"}, + Metric: model.Metric{"name": "request_count", "another_label_name": "another_label_value"}, Value: 84, Timestamp: testTime, }, @@ -91,17 +90,17 @@ func TestMetricFamilyProcessor(t *testing.T) { { Samples: model.Samples{ &model.Sample{ - Metric: model.Metric{"base": "label", "name": "request_count", "some_label_name": "some_label_value", "quantile": "0.99"}, + Metric: model.Metric{"name": "request_count", "some_label_name": "some_label_value", "quantile": "0.99"}, Value: -42, Timestamp: testTime, }, &model.Sample{ - Metric: model.Metric{"base": "label", "name": "request_count", "some_label_name": "some_label_value", "quantile": "0.999"}, + Metric: model.Metric{"name": "request_count", "some_label_name": "some_label_value", "quantile": "0.999"}, Value: -84, Timestamp: testTime, }, &model.Sample{ - Metric: model.Metric{"base": "label", "name": "request_count", "another_label_name": "another_label_value", "quantile": "0.5"}, + Metric: model.Metric{"name": "request_count", "another_label_name": "another_label_value", "quantile": "0.5"}, Value: 10, Timestamp: testTime, }, diff --git a/extraction/processor.go b/extraction/processor.go index 68c0861..48b5feb 100644 --- a/extraction/processor.go +++ b/extraction/processor.go @@ -14,6 +14,7 @@ package extraction import ( + "fmt" "io" "time" @@ -25,9 +26,6 @@ import ( type ProcessOptions struct { // Timestamp is added to each value interpreted from the stream. Timestamp time.Time - - // BaseLabels are labels that are accumulated onto each sample, if any. - BaseLabels model.LabelSet } // Ingester consumes result streams in whatever way is desired by the user. @@ -58,31 +56,6 @@ func labelSet(labels map[string]string) model.LabelSet { return labelset } -// Helper function to merge a target's base labels ontop of the labels of an -// exported sample. If a label is already defined in the exported sample, we -// assume that we are scraping an intermediate exporter and attach -// "exporter_"-prefixes to Prometheus' own base labels. -func mergeTargetLabels(entityLabels, targetLabels model.LabelSet) model.LabelSet { - if targetLabels == nil { - targetLabels = model.LabelSet{} - } - - result := model.LabelSet{} - - for label, value := range entityLabels { - result[label] = value - } - - for label, labelValue := range targetLabels { - if _, exists := result[label]; exists { - result[model.ExporterLabelPrefix+label] = labelValue - } else { - result[label] = labelValue - } - } - return result -} - // Result encapsulates the outcome from processing samples from a source. type Result struct { Err error @@ -96,15 +69,18 @@ func (r *Result) equal(o *Result) bool { if r.Err != o.Err { if r.Err == nil || o.Err == nil { + fmt.Println("err nil") return false } if r.Err.Error() != o.Err.Error() { + fmt.Println("err str") return false } } if len(r.Samples) != len(o.Samples) { + fmt.Println("samples len") return false } @@ -112,6 +88,7 @@ func (r *Result) equal(o *Result) bool { other := o.Samples[i] if !mine.Equal(other) { + fmt.Println("samples", mine, other) return false } } diff --git a/extraction/processor0_0_1.go b/extraction/processor0_0_1.go index 62d3076..50d4cf4 100644 --- a/extraction/processor0_0_1.go +++ b/extraction/processor0_0_1.go @@ -71,8 +71,7 @@ func (p *processor001) ProcessSingle(in io.Reader, out Ingester, o *ProcessOptio pendingSamples := model.Samples{} for _, entity := range entities { for _, value := range entity.Metric.Value { - entityLabels := labelSet(entity.BaseLabels).Merge(labelSet(value.Labels)) - labels := mergeTargetLabels(entityLabels, o.BaseLabels) + labels := labelSet(entity.BaseLabels).Merge(labelSet(value.Labels)) switch entity.Metric.MetricType { case gauge001, counter001: diff --git a/extraction/processor0_0_1_test.go b/extraction/processor0_0_1_test.go index ca248be..2e1b337 100644 --- a/extraction/processor0_0_1_test.go +++ b/extraction/processor0_0_1_test.go @@ -29,7 +29,6 @@ var test001Time = time.Now() type testProcessor001ProcessScenario struct { in string - baseLabels model.LabelSet expected, actual []*Result err error } @@ -46,8 +45,7 @@ func (s *testProcessor001ProcessScenario) test(t test.Tester, set int) { } options := &ProcessOptions{ - Timestamp: test001Time, - BaseLabels: s.baseLabels, + Timestamp: test001Time, } if err := Processor001.ProcessSingle(reader, s, options); !test.ErrorEqual(s.err, err) { t.Fatalf("%d. expected err of %s, got %s", set, s.err, err) @@ -75,101 +73,98 @@ func testProcessor001Process(t test.Tester) { }, { in: "test0_0_1-0_0_2.json", - baseLabels: model.LabelSet{ - model.JobLabel: "batch_exporter", - }, expected: []*Result{ { Samples: model.Samples{ &model.Sample{ - Metric: model.Metric{"service": "zed", model.MetricNameLabel: "rpc_calls_total", "job": "batch_job", "exporter_job": "batch_exporter"}, + Metric: model.Metric{"service": "zed", model.MetricNameLabel: "rpc_calls_total", "job": "batch_job"}, Value: 25, Timestamp: test001Time, }, &model.Sample{ - Metric: model.Metric{"service": "bar", model.MetricNameLabel: "rpc_calls_total", "job": "batch_job", "exporter_job": "batch_exporter"}, + Metric: model.Metric{"service": "bar", model.MetricNameLabel: "rpc_calls_total", "job": "batch_job"}, Value: 25, Timestamp: test001Time, }, &model.Sample{ - Metric: model.Metric{"service": "foo", model.MetricNameLabel: "rpc_calls_total", "job": "batch_job", "exporter_job": "batch_exporter"}, + Metric: model.Metric{"service": "foo", model.MetricNameLabel: "rpc_calls_total", "job": "batch_job"}, Value: 25, Timestamp: test001Time, }, &model.Sample{ - Metric: model.Metric{"percentile": "0.010000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "zed", "job": "batch_exporter"}, + Metric: model.Metric{"percentile": "0.010000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "zed"}, Value: 0.0459814091918713, Timestamp: test001Time, }, &model.Sample{ - Metric: model.Metric{"percentile": "0.010000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "bar", "job": "batch_exporter"}, + Metric: model.Metric{"percentile": "0.010000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "bar"}, Value: 78.48563317257356, Timestamp: test001Time, }, &model.Sample{ - Metric: model.Metric{"percentile": "0.010000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "foo", "job": "batch_exporter"}, + Metric: model.Metric{"percentile": "0.010000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "foo"}, Value: 15.890724674774395, Timestamp: test001Time, }, &model.Sample{ - Metric: model.Metric{"percentile": "0.050000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "zed", "job": "batch_exporter"}, + Metric: model.Metric{"percentile": "0.050000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "zed"}, Value: 0.0459814091918713, Timestamp: test001Time, }, &model.Sample{ - Metric: model.Metric{"percentile": "0.050000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "bar", "job": "batch_exporter"}, + Metric: model.Metric{"percentile": "0.050000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "bar"}, Value: 78.48563317257356, Timestamp: test001Time, }, &model.Sample{ - Metric: model.Metric{"percentile": "0.050000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "foo", "job": "batch_exporter"}, + Metric: model.Metric{"percentile": "0.050000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "foo"}, Value: 15.890724674774395, Timestamp: test001Time, }, &model.Sample{ - Metric: model.Metric{"percentile": "0.500000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "zed", "job": "batch_exporter"}, + Metric: model.Metric{"percentile": "0.500000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "zed"}, Value: 0.6120456642749681, Timestamp: test001Time, }, &model.Sample{ - Metric: model.Metric{"percentile": "0.500000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "bar", "job": "batch_exporter"}, + Metric: model.Metric{"percentile": "0.500000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "bar"}, Value: 97.31798360385088, Timestamp: test001Time, }, &model.Sample{ - Metric: model.Metric{"percentile": "0.500000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "foo", "job": "batch_exporter"}, + Metric: model.Metric{"percentile": "0.500000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "foo"}, Value: 84.63044031436561, Timestamp: test001Time, }, &model.Sample{ - Metric: model.Metric{"percentile": "0.900000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "zed", "job": "batch_exporter"}, + Metric: model.Metric{"percentile": "0.900000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "zed"}, Value: 1.355915069887731, Timestamp: test001Time, }, &model.Sample{ - Metric: model.Metric{"percentile": "0.900000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "bar", "job": "batch_exporter"}, + Metric: model.Metric{"percentile": "0.900000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "bar"}, Value: 109.89202084295582, Timestamp: test001Time, }, &model.Sample{ - Metric: model.Metric{"percentile": "0.900000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "foo", "job": "batch_exporter"}, + Metric: model.Metric{"percentile": "0.900000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "foo"}, Value: 160.21100853053224, Timestamp: test001Time, }, &model.Sample{ - Metric: model.Metric{"percentile": "0.990000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "zed", "job": "batch_exporter"}, + Metric: model.Metric{"percentile": "0.990000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "zed"}, Value: 1.772733213161236, Timestamp: test001Time, }, &model.Sample{ - Metric: model.Metric{"percentile": "0.990000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "bar", "job": "batch_exporter"}, + Metric: model.Metric{"percentile": "0.990000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "bar"}, Value: 109.99626121011262, Timestamp: test001Time, }, &model.Sample{ - Metric: model.Metric{"percentile": "0.990000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "foo", "job": "batch_exporter"}, + Metric: model.Metric{"percentile": "0.990000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "foo"}, Value: 172.49828748957728, Timestamp: test001Time, }, diff --git a/extraction/processor0_0_2.go b/extraction/processor0_0_2.go index 2a278d9..14a3e5a 100644 --- a/extraction/processor0_0_2.go +++ b/extraction/processor0_0_2.go @@ -68,8 +68,7 @@ func (p *processor002) ProcessSingle(in io.Reader, out Ingester, o *ProcessOptio } for _, counter := range values { - entityLabels := labelSet(entity.BaseLabels).Merge(labelSet(counter.Labels)) - labels := mergeTargetLabels(entityLabels, o.BaseLabels) + labels := labelSet(entity.BaseLabels).Merge(labelSet(counter.Labels)) pendingSamples = append(pendingSamples, &model.Sample{ Metric: model.Metric(labels), @@ -91,9 +90,8 @@ func (p *processor002) ProcessSingle(in io.Reader, out Ingester, o *ProcessOptio for _, histogram := range values { for percentile, value := range histogram.Values { - entityLabels := labelSet(entity.BaseLabels).Merge(labelSet(histogram.Labels)) - entityLabels[model.LabelName("percentile")] = model.LabelValue(percentile) - labels := mergeTargetLabels(entityLabels, o.BaseLabels) + labels := labelSet(entity.BaseLabels).Merge(labelSet(histogram.Labels)) + labels[model.LabelName("percentile")] = model.LabelValue(percentile) pendingSamples = append(pendingSamples, &model.Sample{ Metric: model.Metric(labels), diff --git a/extraction/processor0_0_2_test.go b/extraction/processor0_0_2_test.go index 8b5d77e..4adf153 100644 --- a/extraction/processor0_0_2_test.go +++ b/extraction/processor0_0_2_test.go @@ -30,7 +30,6 @@ var test002Time = time.Now() type testProcessor002ProcessScenario struct { in string - baseLabels model.LabelSet expected, actual []*Result err error } @@ -47,8 +46,7 @@ func (s *testProcessor002ProcessScenario) test(t test.Tester, set int) { } options := &ProcessOptions{ - Timestamp: test002Time, - BaseLabels: s.baseLabels, + Timestamp: test002Time, } if err := Processor002.ProcessSingle(reader, s, options); !test.ErrorEqual(s.err, err) { t.Fatalf("%d. expected err of %s, got %s", set, s.err, err) @@ -76,100 +74,98 @@ func testProcessor002Process(t test.Tester) { }, { in: "test0_0_1-0_0_2.json", - baseLabels: model.LabelSet{ - model.JobLabel: "batch_exporter", - }, expected: []*Result{ { Samples: model.Samples{ &model.Sample{ - Metric: model.Metric{"service": "zed", model.MetricNameLabel: "rpc_calls_total", "job": "batch_job", "exporter_job": "batch_exporter"}, + Metric: model.Metric{"service": "zed", model.MetricNameLabel: "rpc_calls_total", "job": "batch_job"}, Value: 25, Timestamp: test002Time, }, &model.Sample{ - Metric: model.Metric{"service": "bar", model.MetricNameLabel: "rpc_calls_total", "job": "batch_job", "exporter_job": "batch_exporter"}, + Metric: model.Metric{"service": "bar", model.MetricNameLabel: "rpc_calls_total", "job": "batch_job"}, Value: 25, Timestamp: test002Time, }, &model.Sample{ - Metric: model.Metric{"service": "foo", model.MetricNameLabel: "rpc_calls_total", "job": "batch_job", "exporter_job": "batch_exporter"}, + Metric: model.Metric{"service": "foo", model.MetricNameLabel: "rpc_calls_total", "job": "batch_job"}, Value: 25, Timestamp: test002Time, }, &model.Sample{ - Metric: model.Metric{"percentile": "0.010000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "zed", "job": "batch_exporter"}, + Metric: model.Metric{"percentile": "0.010000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "zed"}, Value: 0.0459814091918713, Timestamp: test002Time, }, &model.Sample{ - Metric: model.Metric{"percentile": "0.010000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "bar", "job": "batch_exporter"}, + Metric: model.Metric{"percentile": "0.010000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "bar"}, Value: 78.48563317257356, Timestamp: test002Time, }, &model.Sample{ - Metric: model.Metric{"percentile": "0.010000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "foo", "job": "batch_exporter"}, + Metric: model.Metric{"percentile": "0.010000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "foo"}, Value: 15.890724674774395, Timestamp: test002Time, }, - &model.Sample{ - Metric: model.Metric{"percentile": "0.050000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "zed", "job": "batch_exporter"}, - Value: 0.0459814091918713, - Timestamp: test002Time, - }, - &model.Sample{ - Metric: model.Metric{"percentile": "0.050000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "bar", "job": "batch_exporter"}, - Value: 78.48563317257356, - Timestamp: test002Time, - }, - &model.Sample{ - Metric: model.Metric{"percentile": "0.050000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "foo", "job": "batch_exporter"}, - Value: 15.890724674774395, - Timestamp: test002Time, - }, - &model.Sample{ - Metric: model.Metric{"percentile": "0.500000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "zed", "job": "batch_exporter"}, - Value: 0.6120456642749681, - Timestamp: test002Time, - }, - &model.Sample{ - Metric: model.Metric{"percentile": "0.500000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "bar", "job": "batch_exporter"}, - Value: 97.31798360385088, - Timestamp: test002Time, - }, - &model.Sample{ - Metric: model.Metric{"percentile": "0.500000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "foo", "job": "batch_exporter"}, - Value: 84.63044031436561, - Timestamp: test002Time, - }, &model.Sample{ - Metric: model.Metric{"percentile": "0.900000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "zed", "job": "batch_exporter"}, + Metric: model.Metric{"percentile": "0.050000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "zed"}, + Value: 0.0459814091918713, + Timestamp: test002Time, + }, + &model.Sample{ + Metric: model.Metric{"percentile": "0.050000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "bar"}, + Value: 78.48563317257356, + Timestamp: test002Time, + }, + &model.Sample{ + Metric: model.Metric{"percentile": "0.050000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "foo"}, + Value: 15.890724674774395, + Timestamp: test002Time, + }, + &model.Sample{ + Metric: model.Metric{"percentile": "0.500000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "zed"}, + Value: 0.6120456642749681, + Timestamp: test002Time, + }, + &model.Sample{ + + Metric: model.Metric{"percentile": "0.500000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "bar"}, + Value: 97.31798360385088, + Timestamp: test002Time, + }, + &model.Sample{ + Metric: model.Metric{"percentile": "0.500000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "foo"}, + Value: 84.63044031436561, + Timestamp: test002Time, + }, + &model.Sample{ + Metric: model.Metric{"percentile": "0.900000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "zed"}, Value: 1.355915069887731, Timestamp: test002Time, }, &model.Sample{ - Metric: model.Metric{"percentile": "0.900000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "bar", "job": "batch_exporter"}, + Metric: model.Metric{"percentile": "0.900000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "bar"}, Value: 109.89202084295582, Timestamp: test002Time, }, &model.Sample{ - Metric: model.Metric{"percentile": "0.900000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "foo", "job": "batch_exporter"}, + Metric: model.Metric{"percentile": "0.900000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "foo"}, Value: 160.21100853053224, Timestamp: test002Time, }, &model.Sample{ - Metric: model.Metric{"percentile": "0.990000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "zed", "job": "batch_exporter"}, + Metric: model.Metric{"percentile": "0.990000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "zed"}, Value: 1.772733213161236, Timestamp: test002Time, }, &model.Sample{ - Metric: model.Metric{"percentile": "0.990000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "bar", "job": "batch_exporter"}, + Metric: model.Metric{"percentile": "0.990000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "bar"}, Value: 109.99626121011262, Timestamp: test002Time, }, &model.Sample{ - Metric: model.Metric{"percentile": "0.990000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "foo", "job": "batch_exporter"}, + Metric: model.Metric{"percentile": "0.990000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "foo"}, Value: 172.49828748957728, Timestamp: test002Time, },