Skip to content

Commit

Permalink
feat(logging/logadmin): allow logging PageSize to override (#9409)
Browse files Browse the repository at this point in the history
* feat: allow logging PageSize to override

* add test for pageSize set

* make test cases more readable

- remove duplicated test case

---------

Co-authored-by: Kevin Zheng <[email protected]>
  • Loading branch information
ShubhamRasal and gkevinzheng committed Mar 6, 2024
1 parent e68777c commit 5ca0271
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 16 deletions.
7 changes: 7 additions & 0 deletions logging/logadmin/logadmin.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 5ca0271

Please sign in to comment.