Skip to content

Commit 3f53352

Browse files
committed
Handle OutOfRange status gracefully
1 parent 1592d5b commit 3f53352

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

internal/auditlog/tail.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,32 @@ import (
1111
logging "cloud.google.com/go/logging/apiv2"
1212
"cloud.google.com/go/logging/apiv2/loggingpb"
1313
"google.golang.org/genproto/googleapis/cloud/audit"
14+
"google.golang.org/grpc/codes"
15+
"google.golang.org/grpc/status"
1416
)
1517

1618
func Tail(ctx context.Context, projectID, clusterName string, cb func(*audit.AuditLog) error) error {
19+
for {
20+
select {
21+
case <-ctx.Done():
22+
return ctx.Err()
23+
default:
24+
}
25+
26+
if err := tailLogs(ctx, projectID, clusterName, cb); err != nil {
27+
if grpcErr, ok := status.FromError(err); ok && grpcErr.Code() == codes.OutOfRange {
28+
// Expected error case:
29+
// "rpc error: code = OutOfRange desc = Session has run for the maximum allowed duration of 1h. To
30+
// continue, start a new session with the same request"
31+
slog.Warn("session expired, restarting", slog.Any("error", err))
32+
continue
33+
}
34+
return fmt.Errorf("log tailing failed: %w", err)
35+
}
36+
}
37+
}
38+
39+
func tailLogs(ctx context.Context, projectID, clusterName string, cb func(*audit.AuditLog) error) error {
1740
client, err := logging.NewClient(ctx)
1841
if err != nil {
1942
return fmt.Errorf("failed to create client: %w", err)
@@ -46,10 +69,10 @@ func Tail(ctx context.Context, projectID, clusterName string, cb func(*audit.Aud
4669
return fmt.Errorf("stream send failed: %w", err)
4770
}
4871

49-
return read(ctx, stream, cb)
72+
return readStream(ctx, stream, cb)
5073
}
5174

52-
func read(ctx context.Context, stream loggingpb.LoggingServiceV2_TailLogEntriesClient, cb func(*audit.AuditLog) error) error {
75+
func readStream(ctx context.Context, stream loggingpb.LoggingServiceV2_TailLogEntriesClient, cb func(*audit.AuditLog) error) error {
5376
for {
5477
select {
5578
case <-ctx.Done():

0 commit comments

Comments
 (0)