Skip to content

Commit 2d95426

Browse files
committed
receiver/prometheusremotewrite: Add two fields timestamp and value.
Convert sample timestamps from milliseconds to nanoseconds. Set datapoint values using SetDoubleValue based on incoming sample values. Update tests in translateV2 to verify that samples are correctly ingested and their timestamps properly converted. Fixes part of #37277. Signed-off-by: sujal shah <[email protected]>
1 parent ea3c2b8 commit 2d95426

File tree

3 files changed

+84
-32
lines changed

3 files changed

+84
-32
lines changed

.chloggen/prw-timestamp-handling.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: receiver/prometheusremotewrite
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: "Add value and timestamp handling in the Prometheus Remote Write receiver by using SetDoubleValue and SetTimestamp"
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [37277]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: [user]

receiver/prometheusremotewritereceiver/receiver.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
3-
43
package prometheusremotewritereceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/prometheusremotewritereceiver"
54

65
import (
@@ -267,17 +266,24 @@ func addHistogramDatapoints(_ pmetric.ResourceMetrics, _ labels.Labels, _ writev
267266
}
268267

269268
// addDatapoints adds the labels to the datapoints attributes.
270-
// TODO: We're still not handling several fields that make a datapoint complete, e.g. StartTimestamp,
271-
// Timestamp, Value, etc.
272-
func addDatapoints(datapoints pmetric.NumberDataPointSlice, ls labels.Labels, _ writev2.TimeSeries) {
273-
attributes := datapoints.AppendEmpty().Attributes()
274-
275-
for _, l := range ls {
276-
if l.Name == "instance" || l.Name == "job" || // Become resource attributes "service.name", "service.instance.id" and "service.namespace"
277-
l.Name == labels.MetricName || // Becomes metric name
278-
l.Name == "otel_scope_name" || l.Name == "otel_scope_version" { // Becomes scope name and version
279-
continue
269+
// TODO: We're still not handling the StartTimestamp.
270+
func addDatapoints(datapoints pmetric.NumberDataPointSlice, ls labels.Labels, ts writev2.TimeSeries) {
271+
// Add samples from the timeseries
272+
for _, sample := range ts.Samples {
273+
dp := datapoints.AppendEmpty()
274+
275+
// Set timestamp in nanoseconds (Prometheus uses milliseconds)
276+
dp.SetTimestamp(pcommon.Timestamp(sample.Timestamp * 1e6))
277+
dp.SetDoubleValue(sample.Value)
278+
279+
attributes := dp.Attributes()
280+
for _, l := range ls {
281+
if l.Name == "instance" || l.Name == "job" || // Become resource attributes
282+
l.Name == labels.MetricName || // Becomes metric name
283+
l.Name == "otel_scope_name" || l.Name == "otel_scope_version" { // Becomes scope name and version
284+
continue
285+
}
286+
attributes.PutStr(l.Name, l.Value)
280287
}
281-
attributes.PutStr(l.Name, l.Value)
282288
}
283289
}

receiver/prometheusremotewritereceiver/receiver_test.go

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/prometheus/prometheus/storage/remote"
1919
"github.com/stretchr/testify/assert"
2020
"go.opentelemetry.io/collector/consumer/consumertest"
21+
"go.opentelemetry.io/collector/pdata/pcommon"
2122
"go.opentelemetry.io/collector/pdata/pmetric"
2223
"go.opentelemetry.io/collector/receiver/receivertest"
2324

@@ -149,17 +150,17 @@ func TestTranslateV2(t *testing.T) {
149150
Timeseries: []writev2.TimeSeries{
150151
{
151152
Metadata: writev2.Metadata{Type: writev2.Metadata_METRIC_TYPE_GAUGE},
152-
LabelsRefs: []uint32{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 16}, // Same scope: scope_name: scope1. scope_version v1
153+
LabelsRefs: []uint32{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 16},
153154
Samples: []writev2.Sample{{Value: 1, Timestamp: 1}},
154155
},
155156
{
156157
Metadata: writev2.Metadata{Type: writev2.Metadata_METRIC_TYPE_GAUGE},
157-
LabelsRefs: []uint32{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 16}, // Same scope: scope_name: scope1. scope_version v1
158+
LabelsRefs: []uint32{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 16},
158159
Samples: []writev2.Sample{{Value: 2, Timestamp: 2}},
159160
},
160161
{
161162
Metadata: writev2.Metadata{Type: writev2.Metadata_METRIC_TYPE_GAUGE},
162-
LabelsRefs: []uint32{1, 2, 3, 4, 5, 6, 11, 12, 13, 14, 17, 18}, // Different scope: scope_name: scope2. scope_version v2
163+
LabelsRefs: []uint32{1, 2, 3, 4, 5, 6, 11, 12, 13, 14, 17, 18},
163164
Samples: []writev2.Sample{{Value: 3, Timestamp: 3}},
164165
},
165166
},
@@ -171,19 +172,30 @@ func TestTranslateV2(t *testing.T) {
171172
rmAttributes1.PutStr("service.namespace", "service-x")
172173
rmAttributes1.PutStr("service.name", "test")
173174
rmAttributes1.PutStr("service.instance.id", "107cn001")
175+
174176
sm1 := rm1.ScopeMetrics().AppendEmpty()
175177
sm1.Scope().SetName("scope1")
176178
sm1.Scope().SetVersion("v1")
177-
sm1Attributes := sm1.Metrics().AppendEmpty().SetEmptyGauge().DataPoints().AppendEmpty().Attributes()
178-
sm1Attributes.PutStr("d", "e")
179-
sm2Attributes := sm1.Metrics().AppendEmpty().SetEmptyGauge().DataPoints().AppendEmpty().Attributes()
180-
sm2Attributes.PutStr("d", "e")
179+
180+
dp1 := sm1.Metrics().AppendEmpty().SetEmptyGauge().DataPoints().AppendEmpty()
181+
dp1.SetTimestamp(pcommon.Timestamp(1 * 1e6))
182+
dp1.SetDoubleValue(1.0)
183+
dp1.Attributes().PutStr("d", "e")
184+
185+
dp2 := sm1.Metrics().AppendEmpty().SetEmptyGauge().DataPoints().AppendEmpty()
186+
dp2.SetTimestamp(pcommon.Timestamp(2 * 1e6))
187+
dp2.SetDoubleValue(2.0)
188+
dp2.Attributes().PutStr("d", "e")
181189

182190
sm2 := rm1.ScopeMetrics().AppendEmpty()
183191
sm2.Scope().SetName("scope2")
184192
sm2.Scope().SetVersion("v2")
185-
sm3Attributes := sm2.Metrics().AppendEmpty().SetEmptyGauge().DataPoints().AppendEmpty().Attributes()
186-
sm3Attributes.PutStr("foo", "bar")
193+
194+
dp3 := sm2.Metrics().AppendEmpty().SetEmptyGauge().DataPoints().AppendEmpty()
195+
dp3.SetTimestamp(pcommon.Timestamp(3 * 1e6))
196+
dp3.SetDoubleValue(3.0)
197+
dp3.Attributes().PutStr("foo", "bar")
198+
187199
return expected
188200
}(),
189201
expectedStats: remote.WriteResponseStats{},
@@ -224,23 +236,30 @@ func TestTranslateV2(t *testing.T) {
224236
rmAttributes1.PutStr("service.namespace", "service-x")
225237
rmAttributes1.PutStr("service.name", "test")
226238
rmAttributes1.PutStr("service.instance.id", "107cn001")
239+
227240
sm1 := rm1.ScopeMetrics().AppendEmpty()
228-
sm1Attributes := sm1.Metrics().AppendEmpty().SetEmptyGauge().DataPoints().AppendEmpty().Attributes()
229-
sm1Attributes.PutStr("d", "e")
230-
sm1Attributes.PutStr("foo", "bar")
231-
// Since we don't check "scope_name" and "scope_version", we end up with duplicated scope metrics for repeated series.
232-
// TODO: Properly handle scope metrics.
233-
sm2Attributes := sm1.Metrics().AppendEmpty().SetEmptyGauge().DataPoints().AppendEmpty().Attributes()
234-
sm2Attributes.PutStr("d", "e")
235-
sm2Attributes.PutStr("foo", "bar")
241+
dp1 := sm1.Metrics().AppendEmpty().SetEmptyGauge().DataPoints().AppendEmpty()
242+
dp1.SetTimestamp(pcommon.Timestamp(1 * 1e6))
243+
dp1.SetDoubleValue(1.0)
244+
dp1.Attributes().PutStr("d", "e")
245+
dp1.Attributes().PutStr("foo", "bar")
246+
247+
dp2 := sm1.Metrics().AppendEmpty().SetEmptyGauge().DataPoints().AppendEmpty()
248+
dp2.SetTimestamp(pcommon.Timestamp(2 * 1e6))
249+
dp2.SetDoubleValue(2.0)
250+
dp2.Attributes().PutStr("d", "e")
251+
dp2.Attributes().PutStr("foo", "bar")
236252

237253
rm2 := expected.ResourceMetrics().AppendEmpty()
238254
rmAttributes2 := rm2.Resource().Attributes()
239255
rmAttributes2.PutStr("service.name", "foo")
240256
rmAttributes2.PutStr("service.instance.id", "bar")
241-
mAttributes2 := rm2.ScopeMetrics().AppendEmpty().Metrics().AppendEmpty().SetEmptyGauge().DataPoints().AppendEmpty().Attributes()
242-
mAttributes2.PutStr("d", "e")
243-
mAttributes2.PutStr("foo", "bar")
257+
258+
dp3 := rm2.ScopeMetrics().AppendEmpty().Metrics().AppendEmpty().SetEmptyGauge().DataPoints().AppendEmpty()
259+
dp3.SetTimestamp(pcommon.Timestamp(2 * 1e6))
260+
dp3.SetDoubleValue(2.0)
261+
dp3.Attributes().PutStr("d", "e")
262+
dp3.Attributes().PutStr("foo", "bar")
244263

245264
return expected
246265
}(),

0 commit comments

Comments
 (0)