From 4aea59e1983a91d607e54a61bbc1ba64260728dd Mon Sep 17 00:00:00 2001 From: Goutham Veeramachaneni Date: Tue, 14 Jun 2022 17:44:43 +0200 Subject: [PATCH 1/3] [prometheusreceiver] Add attributes from target_info metric Signed-off-by: Goutham Veeramachaneni --- .../internal/otlp_transaction.go | 26 ++++++++++ .../metrics_receiver_labels_test.go | 48 +++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/receiver/prometheusreceiver/internal/otlp_transaction.go b/receiver/prometheusreceiver/internal/otlp_transaction.go index e6432d96f6d7..33dfe798ab97 100644 --- a/receiver/prometheusreceiver/internal/otlp_transaction.go +++ b/receiver/prometheusreceiver/internal/otlp_transaction.go @@ -32,6 +32,10 @@ import ( "go.uber.org/zap" ) +const ( + targetMetricName = "target_info" +) + type transaction struct { isNew bool ctx context.Context @@ -89,6 +93,12 @@ func (t *transaction) Append(ref storage.SeriesRef, labels labels.Labels, atMs i } } + // For the `target_info` metric we need to convert it to resource attributes. + metricName := labels.Get(model.MetricNameLabel) + if metricName == targetMetricName { + return 0, t.AddTargetInfo(labels) + } + return 0, t.metricBuilder.AddDataPoint(labels, atMs, value) } @@ -157,6 +167,22 @@ func (t *transaction) Rollback() error { return nil } +func (t *transaction) AddTargetInfo(labels labels.Labels) error { + attrs := t.nodeResource.Attributes() + + for _, lbl := range labels { + if lbl.Name == model.JobLabel || lbl.Name == model.InstanceLabel || lbl.Name == model.MetricNameLabel { + continue + } + + // TODO: Do I need to sanitise (desantise)? + // TODO: What if there is already an attribute? Is Upsert correct? + attrs.UpsertString(lbl.Name, lbl.Value) + } + + return nil +} + func pdataTimestampFromFloat64(ts float64) pcommon.Timestamp { secs := int64(ts) nanos := int64((ts - float64(secs)) * 1e9) diff --git a/receiver/prometheusreceiver/metrics_receiver_labels_test.go b/receiver/prometheusreceiver/metrics_receiver_labels_test.go index 1bb8ad09ab87..409bde0795e0 100644 --- a/receiver/prometheusreceiver/metrics_receiver_labels_test.go +++ b/receiver/prometheusreceiver/metrics_receiver_labels_test.go @@ -685,3 +685,51 @@ func verifyRelabelJobInstance(t *testing.T, td *testData, rms []*pmetric.Resourc }), }) } + +const targetResourceAttsInTargetInfo = ` +# HELP jvm_memory_bytes_used Used bytes of a given JVM memory area. +# TYPE jvm_memory_bytes_used gauge +jvm_memory_bytes_used{area="heap"} 100 +# HELP target_info has the resource attributes +# TYPE target_info gauge +target_info{foo="bar", team="infra"} 1 +` + +func TestTargetInfoResourceAttributes(t *testing.T) { + targets := []*testData{ + { + name: "target1", + pages: []mockPrometheusResponse{ + {code: 200, data: targetResourceAttsInTargetInfo}, + }, + validateFunc: verifyTargetInfoResourceAttributes, + }, + } + + testComponent(t, targets, false, "") +} + +func verifyTargetInfoResourceAttributes(t *testing.T, td *testData, rms []*pmetric.ResourceMetrics) { + verifyNumValidScrapeResults(t, td, rms) + require.Greater(t, len(rms), 0, "At least one resource metric should be present") + + wantAttributes := td.attributes + wantAttributes.InsertString("foo", "bar") + wantAttributes.InsertString("team", "infra") + + metrics1 := rms[0].ScopeMetrics().At(0).Metrics() + ts1 := metrics1.At(0).Gauge().DataPoints().At(0).Timestamp() + doCompare(t, "relabel-job-instance", wantAttributes, rms[0], []testExpectation{ + assertMetricPresent("jvm_memory_bytes_used", + compareMetricType(pmetric.MetricDataTypeGauge), + []dataPointExpectation{ + { + numberPointComparator: []numberPointComparator{ + compareTimestamp(ts1), + compareDoubleValue(100), + compareAttributes(map[string]string{"area": "heap"}), + }, + }, + }), + }) +} From e14a0369817dd0ed07f6e40ab64a20635c1b6924 Mon Sep 17 00:00:00 2001 From: Goutham Veeramachaneni Date: Wed, 15 Jun 2022 09:33:50 +0200 Subject: [PATCH 2/3] Add changelog entry Signed-off-by: Goutham Veeramachaneni --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d16713432c22..aa8bbc17848d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,7 @@ - `mezmoexporter`: add logging for HTTP errors (#10875) - `signalfxexporter`: Enable the exporting of seven Kubernetes metrics used in Splunk/SignalFx content by default (#11032) - `googlecloudexporter`: Support writing to multiple GCP projects by setting the `gcp.project.id` resource attribute, and support service account impersonation (#11051) +- `prometheusreceiver`: Add `target_info` labels to resource attributes. (#11034) ### 🧰 Bug fixes 🧰 From 61edbf8de7985ee0781fe2a1537acd19948214e5 Mon Sep 17 00:00:00 2001 From: Goutham Veeramachaneni Date: Thu, 16 Jun 2022 11:09:07 +0200 Subject: [PATCH 3/3] Remove TODOs Signed-off-by: Goutham Veeramachaneni --- receiver/prometheusreceiver/internal/otlp_transaction.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/receiver/prometheusreceiver/internal/otlp_transaction.go b/receiver/prometheusreceiver/internal/otlp_transaction.go index 33dfe798ab97..4675db0a6a17 100644 --- a/receiver/prometheusreceiver/internal/otlp_transaction.go +++ b/receiver/prometheusreceiver/internal/otlp_transaction.go @@ -175,8 +175,6 @@ func (t *transaction) AddTargetInfo(labels labels.Labels) error { continue } - // TODO: Do I need to sanitise (desantise)? - // TODO: What if there is already an attribute? Is Upsert correct? attrs.UpsertString(lbl.Name, lbl.Value) }