From 7b1ff5bff744912eefae6d1f6a63765c48f514b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juraj=20Mich=C3=A1lek?= Date: Wed, 6 Dec 2023 17:24:16 +0100 Subject: [PATCH] chore: fix prometheus remote write exporter not setting timestamp on created metric (#29503) As disucssed on cncf [slack](https://cloud-native.slack.com/archives/C01LSCJBXDZ/p1700938910822949) fixing the issue with _created metrics where prometheus backend refuses it due to "too old" due to not setting a timestamp for the sample created. The timestamp used is the timestamp for the original sample the correspondent _created metrics is generated for. **Link to tracking Issue:** #24915 Tested locally with prometheus compatible backend. --------- Co-authored-by: Anthony Mirabella Co-authored-by: David Ashpole --- ...write-set-timestamp-on-created-metric.yaml | 27 +++++++++++++++++++ .../prometheusremotewrite/helper.go | 8 +++--- .../prometheusremotewrite/helper_test.go | 4 +-- .../number_data_points.go | 2 +- .../number_data_points_test.go | 2 +- 5 files changed, 36 insertions(+), 7 deletions(-) create mode 100755 .chloggen/-prometheus-remote-write-set-timestamp-on-created-metric.yaml diff --git a/.chloggen/-prometheus-remote-write-set-timestamp-on-created-metric.yaml b/.chloggen/-prometheus-remote-write-set-timestamp-on-created-metric.yaml new file mode 100755 index 000000000000..2e26721374cc --- /dev/null +++ b/.chloggen/-prometheus-remote-write-set-timestamp-on-created-metric.yaml @@ -0,0 +1,27 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: bug_fix + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: exporter/prometheusremotewrite + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: prometheusremotewrite exporter fix created metrics missing timestamp + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [ 24915 ] + +# (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: + +# If your change doesn't affect end users or the exported elements of any package, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [] diff --git a/pkg/translator/prometheusremotewrite/helper.go b/pkg/translator/prometheusremotewrite/helper.go index 55a3b6d55cf4..ca5794bda551 100644 --- a/pkg/translator/prometheusremotewrite/helper.go +++ b/pkg/translator/prometheusremotewrite/helper.go @@ -347,7 +347,7 @@ func addSingleHistogramDataPoint(pt pmetric.HistogramDataPoint, resource pcommon startTimestamp := pt.StartTimestamp() if settings.ExportCreatedMetric && startTimestamp != 0 { labels := createLabels(createdSuffix) - addCreatedTimeSeriesIfNeeded(tsMap, labels, startTimestamp, metric.Type().String()) + addCreatedTimeSeriesIfNeeded(tsMap, labels, startTimestamp, pt.Timestamp(), metric.Type().String()) } } @@ -516,7 +516,7 @@ func addSingleSummaryDataPoint(pt pmetric.SummaryDataPoint, resource pcommon.Res startTimestamp := pt.StartTimestamp() if settings.ExportCreatedMetric && startTimestamp != 0 { createdLabels := createLabels(baseName + createdSuffix) - addCreatedTimeSeriesIfNeeded(tsMap, createdLabels, startTimestamp, metric.Type().String()) + addCreatedTimeSeriesIfNeeded(tsMap, createdLabels, startTimestamp, pt.Timestamp(), metric.Type().String()) } } @@ -526,6 +526,7 @@ func addCreatedTimeSeriesIfNeeded( series map[string]*prompb.TimeSeries, labels []prompb.Label, startTimestamp pcommon.Timestamp, + timestamp pcommon.Timestamp, metricType string, ) { sig := timeSeriesSignature(metricType, &labels) @@ -534,7 +535,8 @@ func addCreatedTimeSeriesIfNeeded( Labels: labels, Samples: []prompb.Sample{ { // convert ns to ms - Value: float64(convertTimeStamp(startTimestamp)), + Value: float64(convertTimeStamp(startTimestamp)), + Timestamp: convertTimeStamp(timestamp), }, }, } diff --git a/pkg/translator/prometheusremotewrite/helper_test.go b/pkg/translator/prometheusremotewrite/helper_test.go index d7203f4508b1..0ce9d8dbd043 100644 --- a/pkg/translator/prometheusremotewrite/helper_test.go +++ b/pkg/translator/prometheusremotewrite/helper_test.go @@ -706,7 +706,7 @@ func TestAddSingleSummaryDataPoint(t *testing.T) { timeSeriesSignature(pmetric.MetricTypeSummary.String(), &createdLabels): { Labels: createdLabels, Samples: []prompb.Sample{ - {Value: float64(convertTimeStamp(ts))}, + {Value: float64(convertTimeStamp(ts)), Timestamp: convertTimeStamp(ts)}, }, }, } @@ -816,7 +816,7 @@ func TestAddSingleHistogramDataPoint(t *testing.T) { timeSeriesSignature(pmetric.MetricTypeHistogram.String(), &createdLabels): { Labels: createdLabels, Samples: []prompb.Sample{ - {Value: float64(convertTimeStamp(ts))}, + {Value: float64(convertTimeStamp(ts)), Timestamp: convertTimeStamp(ts)}, }, }, } diff --git a/pkg/translator/prometheusremotewrite/number_data_points.go b/pkg/translator/prometheusremotewrite/number_data_points.go index 5614424c6ead..e4c938d2fa5b 100644 --- a/pkg/translator/prometheusremotewrite/number_data_points.go +++ b/pkg/translator/prometheusremotewrite/number_data_points.go @@ -96,7 +96,7 @@ func addSingleSumNumberDataPoint( nameStr, name+createdSuffix, ) - addCreatedTimeSeriesIfNeeded(series, createdLabels, startTimestamp, metric.Type().String()) + addCreatedTimeSeriesIfNeeded(series, createdLabels, startTimestamp, pt.Timestamp(), metric.Type().String()) } } } diff --git a/pkg/translator/prometheusremotewrite/number_data_points_test.go b/pkg/translator/prometheusremotewrite/number_data_points_test.go index 39b13fcd3d9d..2740d7027fbe 100644 --- a/pkg/translator/prometheusremotewrite/number_data_points_test.go +++ b/pkg/translator/prometheusremotewrite/number_data_points_test.go @@ -162,7 +162,7 @@ func TestAddSingleSumNumberDataPoint(t *testing.T) { timeSeriesSignature(pmetric.MetricTypeSum.String(), &createdLabels): { Labels: createdLabels, Samples: []prompb.Sample{ - {Value: float64(convertTimeStamp(ts))}, + {Value: float64(convertTimeStamp(ts)), Timestamp: convertTimeStamp(ts)}, }, }, }