Skip to content

Commit cdc84ae

Browse files
authored
Improve formatting of timestamp too new/old error (#3967)
* Improved error formatting for timestamp too new/old Signed-off-by: Marco Pracucci <[email protected]> * Directly return the TenantID() error instead of generating a new one Signed-off-by: Marco Pracucci <[email protected]> * Reuse a shared error for 'ingester stopping' Signed-off-by: Marco Pracucci <[email protected]>
1 parent ec958eb commit cdc84ae

File tree

5 files changed

+15
-14
lines changed

5 files changed

+15
-14
lines changed

pkg/distributor/distributor_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1724,7 +1724,7 @@ func TestDistributorValidation(t *testing.T) {
17241724
TimestampMs: int64(past),
17251725
Value: 2,
17261726
}},
1727-
err: httpgrpc.Errorf(http.StatusBadRequest, "sample for 'testmetric' has timestamp too old: %d", past),
1727+
err: httpgrpc.Errorf(http.StatusBadRequest, `timestamp too old: %d metric: "testmetric"`, past),
17281728
},
17291729

17301730
// Test validation fails for samples from the future.
@@ -1734,7 +1734,7 @@ func TestDistributorValidation(t *testing.T) {
17341734
TimestampMs: int64(future),
17351735
Value: 4,
17361736
}},
1737-
err: httpgrpc.Errorf(http.StatusBadRequest, "sample for 'testmetric' has timestamp too new: %d", future),
1737+
err: httpgrpc.Errorf(http.StatusBadRequest, `timestamp too new: %d metric: "testmetric"`, future),
17381738
},
17391739

17401740
// Test maximum labels names per series.

pkg/ingester/ingester.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ const (
4949
var (
5050
// This is initialised if the WAL is enabled and the records are fetched from this pool.
5151
recordPool sync.Pool
52+
53+
errIngesterStopping = errors.New("ingester stopping")
5254
)
5355

5456
// Config for an Ingester.
@@ -452,7 +454,7 @@ func (i *Ingester) Push(ctx context.Context, req *cortexpb.WriteRequest) (*corte
452454

453455
userID, err := tenant.TenantID(ctx)
454456
if err != nil {
455-
return nil, fmt.Errorf("no user id")
457+
return nil, err
456458
}
457459

458460
// Given metadata is a best-effort approach, and we don't halt on errors
@@ -535,7 +537,7 @@ func (i *Ingester) append(ctx context.Context, userID string, labels labelPairs,
535537
}
536538
}()
537539
if i.stopped {
538-
return fmt.Errorf("ingester stopping")
540+
return errIngesterStopping
539541
}
540542

541543
// getOrCreateSeries copies the memory for `labels`, except on the error path.
@@ -624,7 +626,7 @@ func (i *Ingester) appendMetadata(userID string, m *cortexpb.MetricMetadata) err
624626
i.userStatesMtx.RLock()
625627
if i.stopped {
626628
i.userStatesMtx.RUnlock()
627-
return fmt.Errorf("ingester stopping")
629+
return errIngesterStopping
628630
}
629631
i.userStatesMtx.RUnlock()
630632

@@ -955,7 +957,7 @@ func (i *Ingester) MetricsMetadata(ctx context.Context, req *client.MetricsMetad
955957

956958
userID, err := tenant.TenantID(ctx)
957959
if err != nil {
958-
return nil, fmt.Errorf("no user id")
960+
return nil, err
959961
}
960962

961963
userMetadata := i.getUserMetadata(userID)

pkg/ingester/ingester_v2.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,7 @@ func (i *Ingester) v2Push(ctx context.Context, req *cortexpb.WriteRequest) (*cor
693693

694694
userID, err := tenant.TenantID(ctx)
695695
if err != nil {
696-
return nil, fmt.Errorf("no user id")
696+
return nil, err
697697
}
698698

699699
db, err := i.getOrCreateTSDB(userID, false)
@@ -705,7 +705,7 @@ func (i *Ingester) v2Push(ctx context.Context, req *cortexpb.WriteRequest) (*cor
705705
i.userStatesMtx.RLock()
706706
if i.stopped {
707707
i.userStatesMtx.RUnlock()
708-
return nil, fmt.Errorf("ingester stopping")
708+
return nil, errIngesterStopping
709709
}
710710
i.userStatesMtx.RUnlock()
711711

pkg/ingester/user_state.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package ingester
22

33
import (
44
"context"
5-
"fmt"
65
"net/http"
76
"sync"
87
"time"
@@ -181,7 +180,7 @@ func (us *userStates) teardown() {
181180
func (us *userStates) getViaContext(ctx context.Context) (*userState, bool, error) {
182181
userID, err := tenant.TenantID(ctx)
183182
if err != nil {
184-
return nil, false, fmt.Errorf("no user id")
183+
return nil, false, err
185184
}
186185
state, ok := us.get(userID)
187186
return state, ok, nil

pkg/util/validation/validate.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ const (
3737
errLabelNameTooLong = "label name too long: %.200q metric %.200q"
3838
errLabelValueTooLong = "label value too long: %.200q metric %.200q"
3939
errTooManyLabels = "series has too many labels (actual: %d, limit: %d) series: '%s'"
40-
errTooOld = "sample for '%s' has timestamp too old: %d"
41-
errTooNew = "sample for '%s' has timestamp too new: %d"
40+
errTooOld = "timestamp too old: %d metric: %.200q"
41+
errTooNew = "timestamp too new: %d metric: %.200q"
4242
errDuplicateLabelName = "duplicate label name: %.200q metric %.200q"
4343
errLabelsNotSorted = "labels not sorted: %.200q metric %.200q"
4444

@@ -98,12 +98,12 @@ type SampleValidationConfig interface {
9898
func ValidateSample(cfg SampleValidationConfig, userID string, metricName string, s cortexpb.Sample) error {
9999
if cfg.RejectOldSamples(userID) && model.Time(s.TimestampMs) < model.Now().Add(-cfg.RejectOldSamplesMaxAge(userID)) {
100100
DiscardedSamples.WithLabelValues(greaterThanMaxSampleAge, userID).Inc()
101-
return httpgrpc.Errorf(http.StatusBadRequest, errTooOld, metricName, model.Time(s.TimestampMs))
101+
return httpgrpc.Errorf(http.StatusBadRequest, errTooOld, model.Time(s.TimestampMs), metricName)
102102
}
103103

104104
if model.Time(s.TimestampMs) > model.Now().Add(cfg.CreationGracePeriod(userID)) {
105105
DiscardedSamples.WithLabelValues(tooFarInFuture, userID).Inc()
106-
return httpgrpc.Errorf(http.StatusBadRequest, errTooNew, metricName, model.Time(s.TimestampMs))
106+
return httpgrpc.Errorf(http.StatusBadRequest, errTooNew, model.Time(s.TimestampMs), metricName)
107107
}
108108

109109
return nil

0 commit comments

Comments
 (0)