From 95701597b1d709543ea22a4b6ff9b28b14a2d4fc Mon Sep 17 00:00:00 2001 From: Noah Dietz Date: Thu, 6 Jul 2023 11:30:14 -0700 Subject: [PATCH] fix(logadmin): use consistent filter in paging example (#8221) Without supplying a `timestamp` in the `ListLogEntries.Filter`, the client by default sets one for the last twenty-four hours when the `EntryIterator` is created. Since this example creates a new `EntryIterator` on each request, a new `timestamp` of a slightly different twenty-four hour window is added. When the `Next Page` button is pressed, it reuses the `NextPageToken` from the previous call, but that was created for a different twenty-four hour window `Filter`. This creates a mismatch between the requested `Filter` and the `Filter` used to create the `NextPageToken`, resulting in an error. To address this, we create the filter in the main function before server start up. In this filter, we set a timestamp **explicitly**. Note: I made this a `fix` commit because this is part of the GoDoc examples and we need this to appear as the latest release, otherwise no one will see it until another release comes along. Fixes #8186. --- logging/logadmin/example_paging_test.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/logging/logadmin/example_paging_test.go b/logging/logadmin/example_paging_test.go index 4f71f492d753..99968df84e75 100644 --- a/logging/logadmin/example_paging_test.go +++ b/logging/logadmin/example_paging_test.go @@ -22,6 +22,7 @@ import ( "html/template" "log" "net/http" + "time" "cloud.google.com/go/logging" "cloud.google.com/go/logging/logadmin" @@ -31,6 +32,7 @@ import ( var ( client *logadmin.Client projectID = flag.String("project-id", "", "ID of the project to use") + filter string ) func ExampleClient_Entries_pagination() { @@ -49,6 +51,15 @@ func ExampleClient_Entries_pagination() { log.Fatalf("creating logging client: %v", err) } + // Filter for logs of a specific name. + logName := fmt.Sprintf(`logName = "projects/%s/logs/testlog"`, *projectID) + + // Filter for logs from the last twenty-four hours. + yesterday := time.Now().Add(-24 * time.Hour).UTC() + dayAgo := fmt.Sprintf("timestamp >= %q", yesterday.Format(time.RFC3339)) + + filter = fmt.Sprintf("%s AND %s", logName, dayAgo) + http.HandleFunc("/entries", handleEntries) log.Print("listening on 8080") log.Fatal(http.ListenAndServe(":8080", nil)) @@ -67,7 +78,6 @@ var pageTemplate = template.Must(template.New("").Parse(` func handleEntries(w http.ResponseWriter, r *http.Request) { ctx := context.Background() - filter := fmt.Sprintf(`logName = "projects/%s/logs/testlog"`, *projectID) it := client.Entries(ctx, logadmin.Filter(filter)) var entries []*logging.Entry nextTok, err := iterator.NewPager(it, 5, r.URL.Query().Get("pageToken")).NextPage(&entries)