Skip to content

fix(metrics): return errors instead of panic/log.Fatalf in library code#557

Open
k4leung4 wants to merge 1 commit into
chainguard-dev:mainfrom
k4leung4:kleung/return-errors-instead-of-panic
Open

fix(metrics): return errors instead of panic/log.Fatalf in library code#557
k4leung4 wants to merge 1 commit into
chainguard-dev:mainfrom
k4leung4:kleung/return-errors-instead-of-panic

Conversation

@k4leung4
Copy link
Copy Markdown
Contributor

Summary

  • SetupTracer now returns (func(), error) instead of panicking on failure
  • RegisterAndServe/RegisterListenAndServe use clog instead of stdlib log, and ignore http.ErrServerClosed on shutdown
  • Fix log.Fatalft.Fatalf in tls_test.go
  • Remove stdlib log import from metrics/server.go

Motivation

Library code should not call panic or log.Fatalf — these terminate the entire process and prevent callers from implementing graceful error handling. ~149 callers in mono use defer metrics.SetupTracer(ctx)() which would crash if the trace exporter fails to initialize.

Breaking change

SetupTracer signature: func(context.Context) func()func(context.Context) (func(), error)

Callers need to change from:

defer metrics.SetupTracer(ctx)()

to:

shutdownTracer, err := metrics.SetupTracer(ctx)
if err != nil {
    log.Fatalf("SetupTracer() = %v", err)
}
defer shutdownTracer()

Test plan

  • go test ./... -count=1 -race passes
  • CI passes

🤖 Generated with Claude Code

SetupTracer now returns (func(), error) instead of panicking on failure,
letting callers decide how to handle the error. RegisterAndServe and
RegisterListenAndServe now use clog instead of stdlib log, and ignore
http.ErrServerClosed on shutdown. Fix log.Fatalf in tls_test.go to use
t.Fatalf.

BREAKING CHANGE: SetupTracer signature changed from func() to (func(), error).
~149 callers in mono using `defer metrics.SetupTracer(ctx)()` will need
updating to handle the error.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Signed-off-by: Kenny Leung <kleung@chainguard.dev>
@k4leung4 k4leung4 marked this pull request as ready for review March 26, 2026 20:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants