Skip to content

Commit aa98c64

Browse files
committed
prometheusreceiver: fix created timestamp conversion from ms to s
Fixes open-telemetry#39912. Signed-off-by: Ridwan Sharif <[email protected]>
1 parent d1f343c commit aa98c64

File tree

3 files changed

+63
-32
lines changed

3 files changed

+63
-32
lines changed

.chloggen/prometheus-ct-hot-fix.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: bug_fix
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: prometheusreceiver
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note:
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: [39912]
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: Convert ms to s when setting start time on the metric
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: []

receiver/prometheusreceiver/internal/metricfamily.go

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,19 @@ type metricFamily struct {
3636
// a couple data complexValue (buckets and count/sum), a group of a metric family always share a same set of tags. for
3737
// simple types like counter and gauge, each data point is a group of itself
3838
type metricGroup struct {
39-
mtype pmetric.MetricType
40-
ts int64
41-
ls labels.Labels
42-
count float64
43-
hasCount bool
44-
sum float64
45-
hasSum bool
46-
created float64
47-
value float64
48-
hValue *histogram.Histogram
49-
fhValue *histogram.FloatHistogram
50-
complexValue []*dataPoint
51-
exemplars pmetric.ExemplarSlice
39+
mtype pmetric.MetricType
40+
ts int64
41+
ls labels.Labels
42+
count float64
43+
hasCount bool
44+
sum float64
45+
hasSum bool
46+
createdSeconds float64
47+
value float64
48+
hValue *histogram.Histogram
49+
fhValue *histogram.FloatHistogram
50+
complexValue []*dataPoint
51+
exemplars pmetric.ExemplarSlice
5252
}
5353

5454
func newMetricFamily(metricName string, mc scrape.MetricMetadataStore, logger *zap.Logger) *metricFamily {
@@ -143,8 +143,8 @@ func (mg *metricGroup) toDistributionPoint(dest pmetric.HistogramDataPointSlice)
143143

144144
// The timestamp MUST be in retrieved from milliseconds and converted to nanoseconds.
145145
tsNanos := timestampFromMs(mg.ts)
146-
if mg.created != 0 {
147-
point.SetStartTimestamp(timestampFromFloat64(mg.created))
146+
if mg.createdSeconds != 0 {
147+
point.SetStartTimestamp(timestampFromFloat64(mg.createdSeconds))
148148
} else if !removeStartTimeAdjustment.IsEnabled() {
149149
// metrics_adjuster adjusts the startTimestamp to the initial scrape timestamp
150150
point.SetStartTimestamp(tsNanos)
@@ -221,8 +221,8 @@ func (mg *metricGroup) toExponentialHistogramDataPoints(dest pmetric.Exponential
221221
}
222222

223223
tsNanos := timestampFromMs(mg.ts)
224-
if mg.created != 0 {
225-
point.SetStartTimestamp(timestampFromFloat64(mg.created))
224+
if mg.createdSeconds != 0 {
225+
point.SetStartTimestamp(timestampFromFloat64(mg.createdSeconds))
226226
} else if !removeStartTimeAdjustment.IsEnabled() {
227227
// metrics_adjuster adjusts the startTimestamp to the initial scrape timestamp
228228
point.SetStartTimestamp(tsNanos)
@@ -315,8 +315,8 @@ func (mg *metricGroup) toSummaryPoint(dest pmetric.SummaryDataPointSlice) {
315315
// The timestamp MUST be in retrieved from milliseconds and converted to nanoseconds.
316316
tsNanos := timestampFromMs(mg.ts)
317317
point.SetTimestamp(tsNanos)
318-
if mg.created != 0 {
319-
point.SetStartTimestamp(timestampFromFloat64(mg.created))
318+
if mg.createdSeconds != 0 {
319+
point.SetStartTimestamp(timestampFromFloat64(mg.createdSeconds))
320320
} else if !removeStartTimeAdjustment.IsEnabled() {
321321
// metrics_adjuster adjusts the startTimestamp to the initial scrape timestamp
322322
point.SetStartTimestamp(tsNanos)
@@ -329,8 +329,8 @@ func (mg *metricGroup) toNumberDataPoint(dest pmetric.NumberDataPointSlice) {
329329
point := dest.AppendEmpty()
330330
// gauge/undefined types have no start time.
331331
if mg.mtype == pmetric.MetricTypeSum {
332-
if mg.created != 0 {
333-
point.SetStartTimestamp(timestampFromFloat64(mg.created))
332+
if mg.createdSeconds != 0 {
333+
point.SetStartTimestamp(timestampFromFloat64(mg.createdSeconds))
334334
} else if !removeStartTimeAdjustment.IsEnabled() {
335335
// metrics_adjuster adjusts the startTimestamp to the initial scrape timestamp
336336
point.SetStartTimestamp(tsNanos)
@@ -398,7 +398,7 @@ func (mf *metricFamily) addSeries(seriesRef uint64, metricName string, ls labels
398398
mg.count = v
399399
mg.hasCount = true
400400
case metricName == mf.metadata.Metric+metricSuffixCreated:
401-
mg.created = v
401+
mg.createdSeconds = v
402402
default:
403403
boundary, err := getBoundary(mf.mtype, ls)
404404
if err != nil {
@@ -408,11 +408,11 @@ func (mf *metricFamily) addSeries(seriesRef uint64, metricName string, ls labels
408408
}
409409
case pmetric.MetricTypeExponentialHistogram:
410410
if metricName == mf.metadata.Metric+metricSuffixCreated {
411-
mg.created = v
411+
mg.createdSeconds = v
412412
}
413413
case pmetric.MetricTypeSum:
414414
if metricName == mf.metadata.Metric+metricSuffixCreated {
415-
mg.created = v
415+
mg.createdSeconds = v
416416
} else {
417417
mg.value = v
418418
}
@@ -425,9 +425,9 @@ func (mf *metricFamily) addSeries(seriesRef uint64, metricName string, ls labels
425425
return nil
426426
}
427427

428-
func (mf *metricFamily) addCreationTimestamp(seriesRef uint64, ls labels.Labels, atMs, created int64) {
428+
func (mf *metricFamily) addCreationTimestamp(seriesRef uint64, ls labels.Labels, atMs, ctMs int64) {
429429
mg := mf.loadMetricGroupOrCreate(seriesRef, ls, atMs)
430-
mg.created = float64(created)
430+
mg.createdSeconds = float64(ctMs) / 1000.0
431431
}
432432

433433
func (mf *metricFamily) addExponentialHistogramSeries(seriesRef uint64, metricName string, ls labels.Labels, t int64, h *histogram.Histogram, fh *histogram.FloatHistogram) error {

receiver/prometheusreceiver/internal/transaction_test.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -669,18 +669,20 @@ func TestAppendCTZeroSample(t *testing.T) {
669669
sink := new(consumertest.MetricsSink)
670670
tr := newTransaction(scrapeCtx, &nopAdjuster{}, sink, labels.EmptyLabels(), receivertest.NewNopSettings(receivertest.NopType), nopObsRecv(t), false, false)
671671

672+
var atMs, ctMs int64
673+
atMs, ctMs = 200, 100
672674
_, err := tr.AppendCTZeroSample(0, labels.FromStrings(
673675
model.InstanceLabel, "0.0.0.0:8855",
674676
model.JobLabel, "test",
675677
model.MetricNameLabel, "counter_test",
676-
), 200, 100)
678+
), atMs, ctMs)
677679
assert.NoError(t, err)
678680

679681
_, err = tr.Append(0, labels.FromStrings(
680682
model.InstanceLabel, "0.0.0.0:8855",
681683
model.JobLabel, "test",
682684
model.MetricNameLabel, "counter_test",
683-
), 200, 100)
685+
), atMs, 100)
684686
assert.NoError(t, err)
685687

686688
assert.NoError(t, tr.Commit())
@@ -692,7 +694,7 @@ func TestAppendCTZeroSample(t *testing.T) {
692694
require.Equal(t, 1, mds[0].MetricCount())
693695
require.Equal(
694696
t,
695-
pcommon.Timestamp(100000000000),
697+
pcommon.NewTimestampFromTime(time.UnixMilli(ctMs)),
696698
mds[0].ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(0).Sum().DataPoints().At(0).StartTimestamp(),
697699
)
698700
}
@@ -701,18 +703,20 @@ func TestAppendHistogramCTZeroSample(t *testing.T) {
701703
sink := new(consumertest.MetricsSink)
702704
tr := newTransaction(scrapeCtx, &nopAdjuster{}, sink, labels.EmptyLabels(), receivertest.NewNopSettings(receivertest.NopType), nopObsRecv(t), false, true)
703705

706+
var atMs, ctMs int64
707+
atMs, ctMs = 200, 100
704708
_, err := tr.AppendHistogramCTZeroSample(0, labels.FromStrings(
705709
model.InstanceLabel, "0.0.0.0:8855",
706710
model.JobLabel, "test",
707711
model.MetricNameLabel, "hist_test_bucket",
708-
), 200, 100, nil, nil)
712+
), atMs, ctMs, nil, nil)
709713
assert.NoError(t, err)
710714

711715
_, err = tr.AppendHistogram(0, labels.FromStrings(
712716
model.InstanceLabel, "0.0.0.0:8855",
713717
model.JobLabel, "test",
714718
model.MetricNameLabel, "hist_test_bucket",
715-
), 200, tsdbutil.GenerateTestHistogram(1), tsdbutil.GenerateTestFloatHistogram(1))
719+
), atMs, tsdbutil.GenerateTestHistogram(1), tsdbutil.GenerateTestFloatHistogram(1))
716720

717721
assert.NoError(t, err)
718722

@@ -725,7 +729,7 @@ func TestAppendHistogramCTZeroSample(t *testing.T) {
725729
require.Equal(t, 1, mds[0].MetricCount())
726730
require.Equal(
727731
t,
728-
pcommon.Timestamp(100000000000),
732+
pcommon.NewTimestampFromTime(time.UnixMilli(ctMs)),
729733
mds[0].ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(0).ExponentialHistogram().DataPoints().At(0).StartTimestamp(),
730734
)
731735
}

0 commit comments

Comments
 (0)