Skip to content

Commit

Permalink
chore: use ErrorS instead of Fatalf for structured logging (#638)
Browse files Browse the repository at this point in the history
Signed-off-by: Anish Ramasekar <anish.ramasekar@gmail.com>
  • Loading branch information
aramase committed Sep 15, 2021
1 parent 144e943 commit 5f67a21
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
18 changes: 12 additions & 6 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ func main() {

if *versionInfo {
if err := version.PrintVersion(); err != nil {
klog.Fatalf("failed to print version, err: %+v", err)
klog.ErrorS(err, "failed to print version")
os.Exit(1)
}
os.Exit(0)
}
Expand All @@ -78,7 +79,8 @@ func main() {
// initialize metrics exporter before creating measurements
err := metrics.InitMetricsExporter(*metricsBackend, *prometheusPort)
if err != nil {
klog.Fatalf("failed to initialize metrics exporter, error: %+v", err)
klog.ErrorS(err, "failed to initialize metrics exporter")
os.Exit(1)
}

if *provider.ConstructPEMChain {
Expand All @@ -89,26 +91,30 @@ func main() {
}
// Add csi-secrets-store user agent to adal requests
if err := adal.AddToUserAgent(version.GetUserAgent()); err != nil {
klog.Fatalf("failed to add user agent to adal: %+v", err)
klog.ErrorS(err, "failed to add user agent to adal")
os.Exit(1)
}
// Initialize and run the gRPC server
proto, addr, err := utils.ParseEndpoint(*endpoint)
if err != nil {
klog.Fatalf("failed to parse endpoint, err: %+v", err)
klog.ErrorS(err, "failed to parse endpoint")
os.Exit(1)
}

if proto == "unix" {
if runtime.GOOS != "windows" {
addr = "/" + addr
}
if err := os.Remove(addr); err != nil && !os.IsNotExist(err) {
klog.Fatalf("failed to remove %s, error: %s", addr, err.Error())
klog.ErrorS(err, "failed to remove socket", "addr", addr)
os.Exit(1)
}
}

listener, err := net.Listen(proto, addr)
if err != nil {
klog.Fatalf("failed to listen: %v", err)
klog.ErrorS(err, "failed to listen", "proto", proto, "addr", addr)
os.Exit(1)
}

opts := []grpc.ServerOption{
Expand Down
2 changes: 1 addition & 1 deletion pkg/metrics/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const prometheusExporter = "prometheus"

func InitMetricsExporter(metricsBackend string, prometheusPort int) error {
mb := strings.ToLower(metricsBackend)
klog.Infof("metrics backend: %s", mb)
klog.InfoS("intializing metrics backend", "backend", mb)
switch mb {
// Prometheus is the only exporter for now
case prometheusExporter:
Expand Down
4 changes: 3 additions & 1 deletion pkg/server/healthz.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net"
"net/http"
"net/url"
"os"
"time"

"github.com/pkg/errors"
Expand All @@ -25,7 +26,8 @@ func (h *HealthZ) Serve() {
serveMux := http.NewServeMux()
serveMux.HandleFunc(h.HealthCheckURL.EscapedPath(), h.ServeHTTP)
if err := http.ListenAndServe(h.HealthCheckURL.Host, serveMux); err != nil && errors.Is(err, http.ErrServerClosed) {
klog.Fatalf("failed to start health check server, err: %w", err)
klog.ErrorS(err, "failed to start health check server")
os.Exit(1)
}
}

Expand Down

0 comments on commit 5f67a21

Please sign in to comment.