Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[prometheusreceiver] Add attributes from target_info metric #11034

Merged
merged 4 commits into from
Jun 23, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 🧰

Expand Down
24 changes: 24 additions & 0 deletions receiver/prometheusreceiver/internal/otlp_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ import (
"go.uber.org/zap"
)

const (
targetMetricName = "target_info"
dashpole marked this conversation as resolved.
Show resolved Hide resolved
)

type transaction struct {
isNew bool
ctx context.Context
Expand Down Expand Up @@ -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 {
gouthamve marked this conversation as resolved.
Show resolved Hide resolved
return 0, t.AddTargetInfo(labels)
}

return 0, t.metricBuilder.AddDataPoint(labels, atMs, value)
}

Expand Down Expand Up @@ -157,6 +167,20 @@ 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 {
gouthamve marked this conversation as resolved.
Show resolved Hide resolved
continue
}

attrs.UpsertString(lbl.Name, lbl.Value)
}

return nil
}

func pdataTimestampFromFloat64(ts float64) pcommon.Timestamp {
secs := int64(ts)
nanos := int64((ts - float64(secs)) * 1e9)
Expand Down
48 changes: 48 additions & 0 deletions receiver/prometheusreceiver/metrics_receiver_labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}),
},
},
}),
})
}