diff --git a/.chloggen/prometheus-version-name-in-metrics.yaml b/.chloggen/prometheus-version-name-in-metrics.yaml new file mode 100644 index 000000000000..87828cbe4bbd --- /dev/null +++ b/.chloggen/prometheus-version-name-in-metrics.yaml @@ -0,0 +1,16 @@ +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: enhancement + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: prometheusreceiver + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: All receivers are setting receiver name and version when sending data. This change introduces the same behaviour to the prometheus receiver. + +# One or more tracking issues related to the change +issues: [20902] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: diff --git a/receiver/prometheusreceiver/internal/transaction.go b/receiver/prometheusreceiver/internal/transaction.go index b85620880f0e..2024b6b80e47 100644 --- a/receiver/prometheusreceiver/internal/transaction.go +++ b/receiver/prometheusreceiver/internal/transaction.go @@ -28,6 +28,7 @@ import ( "github.com/prometheus/prometheus/model/value" "github.com/prometheus/prometheus/scrape" "github.com/prometheus/prometheus/storage" + "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/consumer" "go.opentelemetry.io/collector/featuregate" "go.opentelemetry.io/collector/obsreport" @@ -41,6 +42,7 @@ import ( const ( targetMetricName = "target_info" + receiverName = "otelcol/prometheusreceiver" ) type transaction struct { @@ -52,6 +54,7 @@ type transaction struct { externalLabels labels.Labels nodeResource pcommon.Resource logger *zap.Logger + buildInfo component.BuildInfo metricAdjuster MetricsAdjuster obsrecv *obsreport.Receiver // Used as buffer to calculate series ref hash. @@ -75,6 +78,7 @@ func newTransaction( metricAdjuster: metricAdjuster, externalLabels: externalLabels, logger: settings.Logger, + buildInfo: settings.BuildInfo, obsrecv: obsrecv, bufBytes: make([]byte, 0, 1024), normalizer: prometheustranslator.NewNormalizer(registry), @@ -207,7 +211,10 @@ func (t *transaction) getMetrics(resource pcommon.Resource) (pmetric.Metrics, er md := pmetric.NewMetrics() rms := md.ResourceMetrics().AppendEmpty() resource.CopyTo(rms.Resource()) - metrics := rms.ScopeMetrics().AppendEmpty().Metrics() + ils := rms.ScopeMetrics().AppendEmpty() + ils.Scope().SetName(receiverName) + ils.Scope().SetVersion(t.buildInfo.Version) + metrics := ils.Metrics() for _, mf := range t.families { mf.appendMetric(metrics, t.normalizer) diff --git a/receiver/prometheusreceiver/internal/transaction_test.go b/receiver/prometheusreceiver/internal/transaction_test.go index 24d9ea46e2ba..bc6e4a33e6b3 100644 --- a/receiver/prometheusreceiver/internal/transaction_test.go +++ b/receiver/prometheusreceiver/internal/transaction_test.go @@ -130,6 +130,28 @@ func TestTransactionAppendResource(t *testing.T) { require.Equal(t, expectedResource, gotResource) } +func TestReceiverVersionAndNameAreAttached(t *testing.T) { + sink := new(consumertest.MetricsSink) + tr := newTransaction(scrapeCtx, &startTimeAdjuster{startTime: startTimestamp}, sink, nil, receivertest.NewNopCreateSettings(), nopObsRecv(t), featuregate.GlobalRegistry()) + _, err := tr.Append(0, labels.FromMap(map[string]string{ + model.InstanceLabel: "localhost:8080", + model.JobLabel: "test", + model.MetricNameLabel: "counter_test", + }), time.Now().Unix()*1000, 1.0) + assert.NoError(t, err) + assert.NoError(t, tr.Commit()) + + expectedResource := CreateResource("test", "localhost:8080", labels.FromStrings(model.SchemeLabel, "http")) + mds := sink.AllMetrics() + require.Len(t, mds, 1) + gotResource := mds[0].ResourceMetrics().At(0).Resource() + require.Equal(t, expectedResource, gotResource) + + gotScope := mds[0].ResourceMetrics().At(0).ScopeMetrics().At(0).Scope() + require.Equal(t, receiverName, gotScope.Name()) + require.Equal(t, component.NewDefaultBuildInfo().Version, gotScope.Version()) +} + func TestTransactionCommitErrorWhenAdjusterError(t *testing.T) { goodLabels := labels.FromMap(map[string]string{ model.InstanceLabel: "localhost:8080",