Skip to content

Commit 87998c4

Browse files
receiver/prometheusremotewrite: Add two fields timestamp and value. (#37895)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description 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. <!-- Issue number (e.g. #1234) or full URL to issue, if applicable. --> #### Link to tracking issue Fixes part of #37277. <!--Please delete paragraphs that you did not use before submitting.--> Signed-off-by: sujal shah <[email protected]>
1 parent 57892ad commit 87998c4

File tree

3 files changed

+85
-32
lines changed

3 files changed

+85
-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 * int64(time.Millisecond)))
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: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"net/http"
1111
"net/http/httptest"
1212
"testing"
13+
"time"
1314

1415
"github.com/gogo/protobuf/proto"
1516
"github.com/golang/snappy"
@@ -18,6 +19,7 @@ import (
1819
"github.com/prometheus/prometheus/storage/remote"
1920
"github.com/stretchr/testify/assert"
2021
"go.opentelemetry.io/collector/consumer/consumertest"
22+
"go.opentelemetry.io/collector/pdata/pcommon"
2123
"go.opentelemetry.io/collector/pdata/pmetric"
2224
"go.opentelemetry.io/collector/receiver/receivertest"
2325

@@ -150,17 +152,17 @@ func TestTranslateV2(t *testing.T) {
150152
Timeseries: []writev2.TimeSeries{
151153
{
152154
Metadata: writev2.Metadata{Type: writev2.Metadata_METRIC_TYPE_GAUGE},
153-
LabelsRefs: []uint32{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 16}, // Same scope: scope_name: scope1. scope_version v1
155+
LabelsRefs: []uint32{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 16},
154156
Samples: []writev2.Sample{{Value: 1, Timestamp: 1}},
155157
},
156158
{
157159
Metadata: writev2.Metadata{Type: writev2.Metadata_METRIC_TYPE_GAUGE},
158-
LabelsRefs: []uint32{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 16}, // Same scope: scope_name: scope1. scope_version v1
160+
LabelsRefs: []uint32{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 16},
159161
Samples: []writev2.Sample{{Value: 2, Timestamp: 2}},
160162
},
161163
{
162164
Metadata: writev2.Metadata{Type: writev2.Metadata_METRIC_TYPE_GAUGE},
163-
LabelsRefs: []uint32{1, 2, 3, 4, 5, 6, 11, 12, 13, 14, 17, 18}, // Different scope: scope_name: scope2. scope_version v2
165+
LabelsRefs: []uint32{1, 2, 3, 4, 5, 6, 11, 12, 13, 14, 17, 18},
164166
Samples: []writev2.Sample{{Value: 3, Timestamp: 3}},
165167
},
166168
},
@@ -172,19 +174,30 @@ func TestTranslateV2(t *testing.T) {
172174
rmAttributes1.PutStr("service.namespace", "service-x")
173175
rmAttributes1.PutStr("service.name", "test")
174176
rmAttributes1.PutStr("service.instance.id", "107cn001")
177+
175178
sm1 := rm1.ScopeMetrics().AppendEmpty()
176179
sm1.Scope().SetName("scope1")
177180
sm1.Scope().SetVersion("v1")
178-
sm1Attributes := sm1.Metrics().AppendEmpty().SetEmptyGauge().DataPoints().AppendEmpty().Attributes()
179-
sm1Attributes.PutStr("d", "e")
180-
sm2Attributes := sm1.Metrics().AppendEmpty().SetEmptyGauge().DataPoints().AppendEmpty().Attributes()
181-
sm2Attributes.PutStr("d", "e")
181+
182+
dp1 := sm1.Metrics().AppendEmpty().SetEmptyGauge().DataPoints().AppendEmpty()
183+
dp1.SetTimestamp(pcommon.Timestamp(1 * int64(time.Millisecond)))
184+
dp1.SetDoubleValue(1.0)
185+
dp1.Attributes().PutStr("d", "e")
186+
187+
dp2 := sm1.Metrics().AppendEmpty().SetEmptyGauge().DataPoints().AppendEmpty()
188+
dp2.SetTimestamp(pcommon.Timestamp(2 * int64(time.Millisecond)))
189+
dp2.SetDoubleValue(2.0)
190+
dp2.Attributes().PutStr("d", "e")
182191

183192
sm2 := rm1.ScopeMetrics().AppendEmpty()
184193
sm2.Scope().SetName("scope2")
185194
sm2.Scope().SetVersion("v2")
186-
sm3Attributes := sm2.Metrics().AppendEmpty().SetEmptyGauge().DataPoints().AppendEmpty().Attributes()
187-
sm3Attributes.PutStr("foo", "bar")
195+
196+
dp3 := sm2.Metrics().AppendEmpty().SetEmptyGauge().DataPoints().AppendEmpty()
197+
dp3.SetTimestamp(pcommon.Timestamp(3 * int64(time.Millisecond)))
198+
dp3.SetDoubleValue(3.0)
199+
dp3.Attributes().PutStr("foo", "bar")
200+
188201
return expected
189202
}(),
190203
expectedStats: remote.WriteResponseStats{},
@@ -225,23 +238,30 @@ func TestTranslateV2(t *testing.T) {
225238
rmAttributes1.PutStr("service.namespace", "service-x")
226239
rmAttributes1.PutStr("service.name", "test")
227240
rmAttributes1.PutStr("service.instance.id", "107cn001")
241+
228242
sm1 := rm1.ScopeMetrics().AppendEmpty()
229-
sm1Attributes := sm1.Metrics().AppendEmpty().SetEmptyGauge().DataPoints().AppendEmpty().Attributes()
230-
sm1Attributes.PutStr("d", "e")
231-
sm1Attributes.PutStr("foo", "bar")
232-
// Since we don't check "scope_name" and "scope_version", we end up with duplicated scope metrics for repeated series.
233-
// TODO: Properly handle scope metrics.
234-
sm2Attributes := sm1.Metrics().AppendEmpty().SetEmptyGauge().DataPoints().AppendEmpty().Attributes()
235-
sm2Attributes.PutStr("d", "e")
236-
sm2Attributes.PutStr("foo", "bar")
243+
dp1 := sm1.Metrics().AppendEmpty().SetEmptyGauge().DataPoints().AppendEmpty()
244+
dp1.SetTimestamp(pcommon.Timestamp(1 * int64(time.Millisecond)))
245+
dp1.SetDoubleValue(1.0)
246+
dp1.Attributes().PutStr("d", "e")
247+
dp1.Attributes().PutStr("foo", "bar")
248+
249+
dp2 := sm1.Metrics().AppendEmpty().SetEmptyGauge().DataPoints().AppendEmpty()
250+
dp2.SetTimestamp(pcommon.Timestamp(2 * int64(time.Millisecond)))
251+
dp2.SetDoubleValue(2.0)
252+
dp2.Attributes().PutStr("d", "e")
253+
dp2.Attributes().PutStr("foo", "bar")
237254

238255
rm2 := expected.ResourceMetrics().AppendEmpty()
239256
rmAttributes2 := rm2.Resource().Attributes()
240257
rmAttributes2.PutStr("service.name", "foo")
241258
rmAttributes2.PutStr("service.instance.id", "bar")
242-
mAttributes2 := rm2.ScopeMetrics().AppendEmpty().Metrics().AppendEmpty().SetEmptyGauge().DataPoints().AppendEmpty().Attributes()
243-
mAttributes2.PutStr("d", "e")
244-
mAttributes2.PutStr("foo", "bar")
259+
260+
dp3 := rm2.ScopeMetrics().AppendEmpty().Metrics().AppendEmpty().SetEmptyGauge().DataPoints().AppendEmpty()
261+
dp3.SetTimestamp(pcommon.Timestamp(2 * int64(time.Millisecond)))
262+
dp3.SetDoubleValue(2.0)
263+
dp3.Attributes().PutStr("d", "e")
264+
dp3.Attributes().PutStr("foo", "bar")
245265

246266
return expected
247267
}(),

0 commit comments

Comments
 (0)