Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(logging/logadmin): allow logging PageSize to override #9409

Merged
merged 10 commits into from Mar 6, 2024
7 changes: 7 additions & 0 deletions logging/logadmin/logadmin.go
Expand Up @@ -223,6 +223,13 @@ type newestFirst struct{}

func (newestFirst) set(r *logpb.ListLogEntriesRequest) { r.OrderBy = "timestamp desc" }

// PageSize provide a way to override number of results to return from each request.
func PageSize(p int32) EntriesOption { return pageSize(p) }

type pageSize int32

func (p pageSize) set(r *logpb.ListLogEntriesRequest) { r.PageSize = int32(p) }

// Entries returns an EntryIterator for iterating over log entries. By default,
// the log entries will be restricted to those from the project passed to
// NewClient. This may be overridden by passing a ProjectIDs option. Requires ReadScope or AdminScope.
Expand Down
90 changes: 74 additions & 16 deletions logging/logadmin/logadmin_test.go
Expand Up @@ -288,31 +288,89 @@ func TestListLogEntriesRequest(t *testing.T) {
resourceNames []string
filterPrefix string
orderBy string
pageSize int32
}{
// Timestamp default does not override user's filter
{[]EntriesOption{NewestFirst(), Filter(`timestamp > "2020-10-30T15:39:09Z"`)},
[]string{"projects/PROJECT_ID"}, `timestamp > "2020-10-30T15:39:09Z"`, "timestamp desc"},
{[]EntriesOption{NewestFirst(), Filter("f")},
[]string{"projects/PROJECT_ID"}, "f AND timestamp >= \"", "timestamp desc"},
{[]EntriesOption{ProjectIDs([]string{"foo"})},
[]string{"projects/foo"}, "timestamp >= \"", ""},
{[]EntriesOption{ResourceNames([]string{"folders/F", "organizations/O"})},
[]string{"folders/F", "organizations/O"}, "timestamp >= \"", ""},
{[]EntriesOption{NewestFirst(), Filter("f"), ProjectIDs([]string{"foo"})},
[]string{"projects/foo"}, "f AND timestamp >= \"", "timestamp desc"},
{[]EntriesOption{NewestFirst(), Filter("f"), ProjectIDs([]string{"foo"})},
[]string{"projects/foo"}, "f AND timestamp >= \"", "timestamp desc"},
// If there are repeats, last one wins.
{[]EntriesOption{NewestFirst(), Filter("no"), ProjectIDs([]string{"foo"}), Filter("f")},
[]string{"projects/foo"}, "f AND timestamp >= \"", "timestamp desc"},
{
// default resource name and timestamp filter
opts: []EntriesOption{
NewestFirst(),
Filter(`timestamp > "2020-10-30T15:39:09Z"`),
},
resourceNames: []string{"projects/PROJECT_ID"},
filterPrefix: `timestamp > "2020-10-30T15:39:09Z"`,
orderBy: "timestamp desc",
},
{
// default resource name and user's filter
opts: []EntriesOption{
NewestFirst(),
Filter("f"),
},
resourceNames: []string{"projects/PROJECT_ID"},
filterPrefix: "f AND timestamp >= \"",
orderBy: "timestamp desc",
},
{
// user's project id and default timestamp filter
opts: []EntriesOption{
ProjectIDs([]string{"foo"}),
},
resourceNames: []string{"projects/foo"},
filterPrefix: "timestamp >= \"",
orderBy: "",
},
{
// user's resource name and default timestamp filter
opts: []EntriesOption{
ResourceNames([]string{"folders/F", "organizations/O"}),
},
resourceNames: []string{"folders/F", "organizations/O"},
filterPrefix: "timestamp >= \"",
orderBy: "",
},
{
// user's project id and user's options
opts: []EntriesOption{
NewestFirst(),
Filter("f"),
ProjectIDs([]string{"foo"}),
},
resourceNames: []string{"projects/foo"},
filterPrefix: "f AND timestamp >= \"",
orderBy: "timestamp desc",
},
{
// user's project id with multiple filter options
opts: []EntriesOption{
NewestFirst(),
Filter("no"),
ProjectIDs([]string{"foo"}),
Filter("f"),
},
resourceNames: []string{"projects/foo"},
filterPrefix: "f AND timestamp >= \"",
orderBy: "timestamp desc",
},
{
// user's project id and custom page size
opts: []EntriesOption{
ProjectIDs([]string{"foo"}),
PageSize(100),
},
resourceNames: []string{"projects/foo"},
filterPrefix: "timestamp >= \"",
pageSize: 100,
},
} {
got := listLogEntriesRequest("projects/PROJECT_ID", test.opts)
want := &logpb.ListLogEntriesRequest{
ResourceNames: test.resourceNames,
Filter: test.filterPrefix,
OrderBy: test.orderBy,
PageSize: test.pageSize,
}
if !testutil.Equal(got.ResourceNames, want.ResourceNames) || !strings.HasPrefix(got.Filter, want.Filter) || got.OrderBy != want.OrderBy {
if !testutil.Equal(got.ResourceNames, want.ResourceNames) || !strings.HasPrefix(got.Filter, want.Filter) || got.OrderBy != want.OrderBy || got.PageSize != want.PageSize {
t.Errorf("got: %v; want %v (mind wanted Filter is prefix)", got, want)
}
}
Expand Down