Skip to content

Commit 9570159

Browse files
authored
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.
1 parent 6c28f23 commit 9570159

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

logging/logadmin/example_paging_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"html/template"
2323
"log"
2424
"net/http"
25+
"time"
2526

2627
"cloud.google.com/go/logging"
2728
"cloud.google.com/go/logging/logadmin"
@@ -31,6 +32,7 @@ import (
3132
var (
3233
client *logadmin.Client
3334
projectID = flag.String("project-id", "", "ID of the project to use")
35+
filter string
3436
)
3537

3638
func ExampleClient_Entries_pagination() {
@@ -49,6 +51,15 @@ func ExampleClient_Entries_pagination() {
4951
log.Fatalf("creating logging client: %v", err)
5052
}
5153

54+
// Filter for logs of a specific name.
55+
logName := fmt.Sprintf(`logName = "projects/%s/logs/testlog"`, *projectID)
56+
57+
// Filter for logs from the last twenty-four hours.
58+
yesterday := time.Now().Add(-24 * time.Hour).UTC()
59+
dayAgo := fmt.Sprintf("timestamp >= %q", yesterday.Format(time.RFC3339))
60+
61+
filter = fmt.Sprintf("%s AND %s", logName, dayAgo)
62+
5263
http.HandleFunc("/entries", handleEntries)
5364
log.Print("listening on 8080")
5465
log.Fatal(http.ListenAndServe(":8080", nil))
@@ -67,7 +78,6 @@ var pageTemplate = template.Must(template.New("").Parse(`
6778

6879
func handleEntries(w http.ResponseWriter, r *http.Request) {
6980
ctx := context.Background()
70-
filter := fmt.Sprintf(`logName = "projects/%s/logs/testlog"`, *projectID)
7181
it := client.Entries(ctx, logadmin.Filter(filter))
7282
var entries []*logging.Entry
7383
nextTok, err := iterator.NewPager(it, 5, r.URL.Query().Get("pageToken")).NextPage(&entries)

0 commit comments

Comments
 (0)