Skip to content

Commit 4359f40

Browse files
authored
Log when dropping metrics due to missing process_start_time_seconds (#1921)
Report error via obsreport.EndMetricsReceiveOp and return error in transaction Commit() Fixes #969
1 parent 015827f commit 4359f40

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

receiver/prometheusreceiver/internal/transaction.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ const (
4747
var errMetricNameNotFound = errors.New("metricName not found from labels")
4848
var errTransactionAborted = errors.New("transaction aborted")
4949
var errNoJobInstance = errors.New("job or instance cannot be found from labels")
50+
var errNoStartTimeMetrics = errors.New("process_start_time_seconds metric is missing")
5051

5152
// A transaction is corresponding to an individual scrape operation or stale report.
5253
// That said, whenever prometheus receiver scrapped a target metric endpoint a page of raw metrics is returned,
@@ -157,12 +158,17 @@ func (tr *transaction) Commit() error {
157158
}
158159

159160
if tr.useStartTimeMetric {
160-
// AdjustStartTime - startTime has to be non-zero in this case.
161+
// startTime is mandatory in this case, but may be zero when the
162+
// process_start_time_seconds metric is missing from the target endpoint.
161163
if tr.metricBuilder.startTime == 0.0 {
162-
metrics = []*metricspb.Metric{}
163-
} else {
164-
adjustStartTime(tr.metricBuilder.startTime, metrics)
164+
// Since we are unable to adjust metrics properly, we will drop them
165+
// and return an error.
166+
err = errNoStartTimeMetrics
167+
obsreport.EndMetricsReceiveOp(ctx, dataformat, 0, 0, err)
168+
return err
165169
}
170+
171+
adjustStartTime(tr.metricBuilder.startTime, metrics)
166172
} else {
167173
// AdjustMetrics - jobsMap has to be non-nil in this case.
168174
// Note: metrics could be empty after adjustment, which needs to be checked before passing it on to ConsumeMetricsData()

receiver/prometheusreceiver/internal/transaction_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,21 @@ func Test_transaction(t *testing.T) {
131131
// assert.Len(t, ocmds[0].Metrics, 1)
132132
})
133133

134+
t.Run("Error when start time is zero", func(t *testing.T) {
135+
sink := new(exportertest.SinkMetricsExporter)
136+
tr := newTransaction(context.Background(), nil, true, "", rn, ms, sink, testLogger)
137+
if _, got := tr.Add(goodLabels, time.Now().Unix()*1000, 1.0); got != nil {
138+
t.Errorf("expecting error == nil from Add() but got: %v\n", got)
139+
}
140+
tr.metricBuilder.startTime = 0 // zero value means the start time metric is missing
141+
got := tr.Commit()
142+
if got == nil {
143+
t.Error("expecting error from Commit() but got nil")
144+
} else if got.Error() != errNoStartTimeMetrics.Error() {
145+
t.Errorf("expected error %q but got %q", errNoStartTimeMetrics, got)
146+
}
147+
})
148+
134149
t.Run("Drop NaN value", func(t *testing.T) {
135150
sink := new(exportertest.SinkMetricsExporter)
136151
tr := newTransaction(context.Background(), nil, true, "", rn, ms, sink, testLogger)

0 commit comments

Comments
 (0)