Skip to content

Commit f01fd63

Browse files
pracuccigouthamve
authored andcommitted
Added user ID to ingester push soft/hard errors (#1960)
Signed-off-by: Marco Pracucci <[email protected]>
1 parent 47df95a commit f01fd63

File tree

4 files changed

+23
-12
lines changed

4 files changed

+23
-12
lines changed

pkg/ingester/errors.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"net/http"
66

7+
"github.com/pkg/errors"
78
"github.com/prometheus/prometheus/pkg/labels"
89
"github.com/weaveworks/common/httpgrpc"
910
)
@@ -49,6 +50,11 @@ func makeMetricLimitError(errorType string, labels labels.Labels, err error) err
4950
}
5051
}
5152

53+
func (e *validationError) WrapWithUser(userID string) *validationError {
54+
e.err = wrapWithUser(e.err, userID)
55+
return e
56+
}
57+
5258
func (e *validationError) Error() string {
5359
if e.err == nil {
5460
return e.errorType
@@ -66,3 +72,7 @@ func (e *validationError) WrappedError() error {
6672
Body: []byte(e.Error()),
6773
})
6874
}
75+
76+
func wrapWithUser(err error, userID string) error {
77+
return errors.Wrapf(err, "user=%s", userID)
78+
}

pkg/ingester/ingester.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,13 +308,13 @@ func (i *Ingester) Push(ctx old_ctx.Context, req *client.WriteRequest) (*client.
308308
continue
309309
}
310310

311-
return nil, err
311+
return nil, wrapWithUser(err, userID)
312312
}
313313
}
314314
client.ReuseSlice(req.Timeseries)
315315

316316
if lastPartialErr != nil {
317-
return &client.WriteResponse{}, lastPartialErr.WrappedError()
317+
return &client.WriteResponse{}, lastPartialErr.WrapWithUser(userID).WrappedError()
318318
}
319319
return &client.WriteResponse{}, nil
320320
}

pkg/ingester/ingester_v2.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (i *Ingester) v2Push(ctx old_ctx.Context, req *client.WriteRequest) (*clien
8989

9090
db, err := i.getOrCreateTSDB(userID, false)
9191
if err != nil {
92-
return nil, err
92+
return nil, wrapWithUser(err, userID)
9393
}
9494

9595
// Ensure the ingester shutdown procedure hasn't started
@@ -141,14 +141,14 @@ func (i *Ingester) v2Push(ctx old_ctx.Context, req *client.WriteRequest) (*clien
141141

142142
// The error looks an issue on our side, so we should rollback
143143
if rollbackErr := app.Rollback(); rollbackErr != nil {
144-
level.Warn(util.Logger).Log("msg", "failed to rollback on error", "userID", userID, "err", rollbackErr)
144+
level.Warn(util.Logger).Log("msg", "failed to rollback on error", "user", userID, "err", rollbackErr)
145145
}
146146

147-
return nil, err
147+
return nil, wrapWithUser(err, userID)
148148
}
149149
}
150150
if err := app.Commit(); err != nil {
151-
return nil, err
151+
return nil, wrapWithUser(err, userID)
152152
}
153153

154154
// Increment metrics only if the samples have been successfully committed.
@@ -160,7 +160,7 @@ func (i *Ingester) v2Push(ctx old_ctx.Context, req *client.WriteRequest) (*clien
160160
client.ReuseSlice(req.Timeseries)
161161

162162
if lastPartialErr != nil {
163-
return &client.WriteResponse{}, httpgrpc.Errorf(http.StatusBadRequest, lastPartialErr.Error())
163+
return &client.WriteResponse{}, httpgrpc.Errorf(http.StatusBadRequest, wrapWithUser(lastPartialErr, userID).Error())
164164
}
165165
return &client.WriteResponse{}, nil
166166
}

pkg/ingester/ingester_v2_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
func TestIngester_v2Push(t *testing.T) {
3030
metricLabelAdapters := []client.LabelAdapter{{Name: labels.MetricName, Value: "test"}}
3131
metricLabels := client.FromLabelAdaptersToLabels(metricLabelAdapters)
32+
userID := "test"
3233

3334
tests := map[string]struct {
3435
reqs []*client.WriteRequest
@@ -71,7 +72,7 @@ func TestIngester_v2Push(t *testing.T) {
7172
[]client.Sample{{Value: 1, TimestampMs: 9}},
7273
client.API),
7374
},
74-
expectedErr: httpgrpc.Errorf(http.StatusBadRequest, tsdb.ErrOutOfOrderSample.Error()),
75+
expectedErr: httpgrpc.Errorf(http.StatusBadRequest, wrapWithUser(tsdb.ErrOutOfOrderSample, userID).Error()),
7576
expectedIngested: []client.TimeSeries{
7677
{Labels: metricLabelAdapters, Samples: []client.Sample{{Value: 2, TimestampMs: 10}}},
7778
},
@@ -95,7 +96,7 @@ func TestIngester_v2Push(t *testing.T) {
9596
[]client.Sample{{Value: 1, TimestampMs: 1575043969 - (86400 * 1000)}},
9697
client.API),
9798
},
98-
expectedErr: httpgrpc.Errorf(http.StatusBadRequest, tsdb.ErrOutOfBounds.Error()),
99+
expectedErr: httpgrpc.Errorf(http.StatusBadRequest, wrapWithUser(tsdb.ErrOutOfBounds, userID).Error()),
99100
expectedIngested: []client.TimeSeries{
100101
{Labels: metricLabelAdapters, Samples: []client.Sample{{Value: 2, TimestampMs: 1575043969}}},
101102
},
@@ -119,7 +120,7 @@ func TestIngester_v2Push(t *testing.T) {
119120
[]client.Sample{{Value: 1, TimestampMs: 1575043969}},
120121
client.API),
121122
},
122-
expectedErr: httpgrpc.Errorf(http.StatusBadRequest, tsdb.ErrAmendSample.Error()),
123+
expectedErr: httpgrpc.Errorf(http.StatusBadRequest, wrapWithUser(tsdb.ErrAmendSample, userID).Error()),
123124
expectedIngested: []client.TimeSeries{
124125
{Labels: metricLabelAdapters, Samples: []client.Sample{{Value: 2, TimestampMs: 1575043969}}},
125126
},
@@ -147,7 +148,7 @@ func TestIngester_v2Push(t *testing.T) {
147148
defer i.Shutdown()
148149
defer cleanup()
149150

150-
ctx := user.InjectOrgID(context.Background(), "test")
151+
ctx := user.InjectOrgID(context.Background(), userID)
151152

152153
// Wait until the ingester is ACTIVE
153154
test.Poll(t, 100*time.Millisecond, ring.ACTIVE, func() interface{} {
@@ -470,7 +471,7 @@ func TestIngester_v2Push_ShouldNotCreateTSDBIfNotInActiveState(t *testing.T) {
470471
req := &client.WriteRequest{}
471472

472473
res, err := i.v2Push(ctx, req)
473-
assert.Equal(t, fmt.Errorf(errTSDBCreateIncompatibleState, "PENDING"), err)
474+
assert.Equal(t, wrapWithUser(fmt.Errorf(errTSDBCreateIncompatibleState, "PENDING"), userID).Error(), err.Error())
474475
assert.Nil(t, res)
475476

476477
// Check if the TSDB has been created

0 commit comments

Comments
 (0)