Skip to content

Commit fc0c7db

Browse files
committed
review: redact S2AccessToken in config logs, bound Close with context, return error from StorageWriter.Run double-call
1 parent cc1d744 commit fc0c7db

5 files changed

Lines changed: 41 additions & 21 deletions

File tree

server/cmd/api/main.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func main() {
102102
}
103103
captureSession := capturesession.NewCaptureSession(eventStream)
104104

105-
// Optional S2 durable storage sink.
105+
// Optional S2 storage sink.
106106
var storageWriter *events.StorageWriter
107107
if config.S2Basin != "" && config.S2AccessToken != "" && config.S2Stream != "" {
108108
slogger.Info("S2 storage enabled", "basin", config.S2Basin, "stream", config.S2Stream)
@@ -261,7 +261,9 @@ func main() {
261261
if storageWriter != nil {
262262
go func() {
263263
defer close(storageDone)
264-
storageWriter.Run(ctx) //nolint:errcheck
264+
if err := storageWriter.Run(ctx); err != nil && ctx.Err() == nil {
265+
slogger.Error("storage writer failed", "err", err)
266+
}
265267
}()
266268
} else {
267269
close(storageDone)
@@ -271,9 +273,6 @@ func main() {
271273
<-ctx.Done()
272274
slogger.Info("shutdown signal received")
273275

274-
// Step 1: shut down all HTTP servers and stop all event publishers (cdpmonitor,
275-
// captureSession) before draining the ring. This bounds the set of events that
276-
// can arrive after Run exits so Drain sees a stable tail.
277276
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 10*time.Second)
278277
defer shutdownCancel()
279278
g, _ := errgroup.WithContext(shutdownCtx)
@@ -296,16 +295,15 @@ func main() {
296295
slogger.Error("server failed to shutdown", "err", err)
297296
}
298297

299-
// Step 2: wait for Run to return (it exits on ctx cancellation), then drain any
300-
// events that arrived between the last Read and HTTP shutdown, then flush S2.
298+
// wait for Run to return, then drain any events that arrived and flush S2.
301299
<-storageDone
302300
if storageWriter != nil {
303301
drainCtx, drainCancel := context.WithTimeout(context.Background(), 5*time.Second)
304302
defer drainCancel()
305303
if err := storageWriter.Drain(drainCtx); err != nil {
306304
slogger.Warn("storage writer drain incomplete", "err", err)
307305
}
308-
if err := storageWriter.Close(); err != nil {
306+
if err := storageWriter.Close(drainCtx); err != nil {
309307
slogger.Error("storage writer close failed", "err", err)
310308
}
311309
}

server/cmd/config/config.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package config
22

33
import (
44
"fmt"
5+
"log/slog"
56
"time"
67

78
"github.com/kelseyhightower/envconfig"
@@ -42,6 +43,31 @@ type Config struct {
4243
S2Stream string `envconfig:"S2_STREAM" default:""`
4344
}
4445

46+
// LogValue implements slog.LogValuer, redacting secret fields.
47+
func (c *Config) LogValue() slog.Value {
48+
s2AccessToken := ""
49+
if c.S2AccessToken != "" {
50+
s2AccessToken = "[redacted]"
51+
}
52+
return slog.GroupValue(
53+
slog.Int("port", c.Port),
54+
slog.Int("frame_rate", c.FrameRate),
55+
slog.Int("display_num", c.DisplayNum),
56+
slog.Int("max_size_mb", c.MaxSizeInMB),
57+
slog.String("output_dir", c.OutputDir),
58+
slog.String("ffmpeg_path", c.PathToFFmpeg),
59+
slog.Int("devtools_proxy_port", c.DevToolsProxyPort),
60+
slog.Bool("log_cdp_messages", c.LogCDPMessages),
61+
slog.Duration("scale_to_zero_cooldown", c.ScaleToZeroCooldown),
62+
slog.Int("chromedriver_proxy_port", c.ChromeDriverProxyPort),
63+
slog.String("chromedriver_upstream_addr", c.ChromeDriverUpstreamAddr),
64+
slog.String("devtools_proxy_addr", c.DevToolsProxyAddr),
65+
slog.String("s2_basin", c.S2Basin),
66+
slog.String("s2_access_token", s2AccessToken),
67+
slog.String("s2_stream", c.S2Stream),
68+
)
69+
}
70+
4571
// Load loads configuration from environment variables
4672
func Load() (*Config, error) {
4773
var config Config

server/lib/events/eventsstorage.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package events
22

33
import (
44
"context"
5+
"fmt"
56
"log/slog"
67
"sync"
78
"sync/atomic"
@@ -11,7 +12,7 @@ import (
1112
// Append is called serially from StorageWriter.Run and need not be thread-safe.
1213
type Storage interface {
1314
Append(ctx context.Context, env Envelope) error
14-
Close() error
15+
Close(ctx context.Context) error
1516
}
1617

1718
// StorageWriter drains the ring buffer and forwards each envelope to the
@@ -43,12 +44,12 @@ func NewStorageWriter(es *EventStream, storage Storage, log *slog.Logger) *Stora
4344

4445
// Run reads from the ring buffer and appends each envelope to storage until
4546
// ctx is cancelled. Returns the context error on clean shutdown. Must be
46-
// called at most once; panics on a second call.
47+
// called at most once; returns an error on a second call.
4748
func (w *StorageWriter) Run(ctx context.Context) error {
4849
firstCall := false
4950
w.once.Do(func() { firstCall = true })
5051
if !firstCall {
51-
panic("events: StorageWriter.Run called more than once")
52+
return fmt.Errorf("events: StorageWriter.Run called more than once")
5253
}
5354

5455
for {
@@ -96,12 +97,7 @@ func (w *StorageWriter) processResult(ctx context.Context, res ReadResult) error
9697
return nil
9798
}
9899

99-
// AppendErrors returns the total number of Append failures since Run started.
100-
func (w *StorageWriter) AppendErrors() uint64 {
101-
return w.appendErrors.Load()
102-
}
103-
104100
// Close drains in-flight writes and releases backend resources.
105-
func (w *StorageWriter) Close() error {
106-
return w.storage.Close()
101+
func (w *StorageWriter) Close(ctx context.Context) error {
102+
return w.storage.Close(ctx)
107103
}

server/lib/events/eventsstorage_writer_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func (m *mockBackend) Append(_ context.Context, env Envelope) error {
3030
return nil
3131
}
3232

33-
func (m *mockBackend) Close() error { return nil }
33+
func (m *mockBackend) Close(_ context.Context) error { return nil }
3434

3535
func (m *mockBackend) envelopes() []Envelope {
3636
m.mu.Lock()

server/lib/events/s2storage.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func (s *S2Storage) Append(_ context.Context, env Envelope) error {
117117

118118
// Close cancels in-flight ack goroutines, waits for them to drain, then closes
119119
// the producer (which flushes the S2 batcher to the network).
120-
func (s *S2Storage) Close() error {
120+
func (s *S2Storage) Close(ctx context.Context) error {
121121
close(s.shutdownCh)
122-
return s.producer.close(context.Background())
122+
return s.producer.close(ctx)
123123
}

0 commit comments

Comments
 (0)