Skip to content
This repository has been archived by the owner on Jul 19, 2023. It is now read-only.

Validates that push request contains at least one sample #550

Merged
merged 1 commit into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkg/distributor/distributor.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@ func (d *Distributor) Push(ctx context.Context, req *connect.Request[pushv1.Push
profiles = append(profiles, &profileTracker{profile: series})
}

if totalProfiles == 0 {
return nil, connect.NewError(connect.CodeInvalidArgument, fmt.Errorf("no profiles received"))
}

// validate the request
for _, series := range req.Msg.Series {
if err := validation.ValidateLabels(d.limits, tenantID, series.Labels); err != nil {
Expand Down
32 changes: 32 additions & 0 deletions pkg/distributor/distributor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,38 @@ func Test_Limits(t *testing.T) {
})
}

func TestBadPushRequest(t *testing.T) {
mux := http.NewServeMux()
ing := newFakeIngester(t, false)
d, err := New(Config{
DistributorRing: ringConfig,
}, testhelper.NewMockRing([]ring.InstanceDesc{
{Addr: "foo"},
}, 3), func(addr string) (client.PoolClient, error) {
return ing, nil
}, newOverrides(t), nil, log.NewLogfmtLogger(os.Stdout))

require.NoError(t, err)
mux.Handle(pushv1connect.NewPusherServiceHandler(d, connect.WithInterceptors(tenant.NewAuthInterceptor(true))))
s := httptest.NewServer(mux)
defer s.Close()

client := pushv1connect.NewPusherServiceClient(http.DefaultClient, s.URL, connect.WithInterceptors(tenant.NewAuthInterceptor(true)))

_, err = client.Push(tenant.InjectTenantID(context.Background(), "foo"), connect.NewRequest(&pushv1.PushRequest{
Series: []*pushv1.RawProfileSeries{
{
Labels: []*typesv1.LabelPair{
{Name: "cluster", Value: "us-central1"},
{Name: "__name__", Value: "cpu"},
},
},
},
}))
require.Error(t, err)
require.Equal(t, connect.CodeInvalidArgument, connect.CodeOf(err))
}

func newOverrides(t *testing.T) *validation.Overrides {
t.Helper()
return validation.MockOverrides(func(defaults *validation.Limits, tenantLimits map[string]*validation.Limits) {
Expand Down