From bf217cfc225bfc899102fafaafa45fa5ea3e9ad6 Mon Sep 17 00:00:00 2001 From: Matt Johnstone Date: Sun, 6 Oct 2024 16:58:15 +0200 Subject: [PATCH] implemented jeffs nits --- cmd/solana_exporter/slots.go | 2 +- cmd/solana_exporter/utils.go | 18 ++---------------- pkg/rpc/client.go | 9 +++++---- 3 files changed, 8 insertions(+), 21 deletions(-) diff --git a/cmd/solana_exporter/slots.go b/cmd/solana_exporter/slots.go index f8cf953..75ddf73 100644 --- a/cmd/solana_exporter/slots.go +++ b/cmd/solana_exporter/slots.go @@ -208,7 +208,7 @@ func (c *SlotWatcher) fetchAndEmitBlockProduction(ctx context.Context, endSlot i // make sure the bounds are contained within the epoch we are currently watching: if err := c.checkValidSlotRange(startSlot, endSlot); err != nil { - panic(err) + klog.Fatalf("invalid slot range: %v", err) } // fetch block production: diff --git a/cmd/solana_exporter/utils.go b/cmd/solana_exporter/utils.go index 35e9adf..0ea2505 100644 --- a/cmd/solana_exporter/utils.go +++ b/cmd/solana_exporter/utils.go @@ -1,25 +1,11 @@ package main import ( - "encoding/json" - "fmt" + "k8s.io/klog/v2" ) func assertf(condition bool, format string, args ...any) { if !condition { - panic(fmt.Errorf(format, args...)) + klog.Fatalf(format, args...) } } - -// prettyPrint is just a useful debugging function -func prettyPrint(obj any) { - // For pretty-printed JSON, use json.MarshalIndent - prettyJSON, err := json.MarshalIndent(obj, "", " ") - if err != nil { - fmt.Println("Error marshalling to pretty JSON:", err, ". obj: ", obj) - return - } - - // Print the pretty JSON string - fmt.Println(string(prettyJSON)) -} diff --git a/pkg/rpc/client.go b/pkg/rpc/client.go index 6126d1f..ee7367f 100644 --- a/pkg/rpc/client.go +++ b/pkg/rpc/client.go @@ -92,14 +92,14 @@ func (c *Client) getResponse(ctx context.Context, method string, params []any, r request := &rpcRequest{Version: "2.0", ID: 1, Method: method, Params: params} buffer, err := json.Marshal(request) if err != nil { - panic(err) + klog.Fatalf("failed to marshal request: %v", err) } klog.V(2).Infof("jsonrpc request: %s", string(buffer)) // make request: req, err := http.NewRequestWithContext(ctx, "POST", c.rpcAddr, bytes.NewBuffer(buffer)) if err != nil { - panic(err) + klog.Fatalf("failed to create request: %v", err) } req.Header.Set("content-type", "application/json") @@ -177,7 +177,7 @@ func (c *Client) GetBlockProduction( ) (*BlockProduction, error) { // can't provide a last slot without a first: if firstSlot == nil && lastSlot != nil { - panic("can't provide a last slot without a first!") + klog.Fatalf("can't provide a last slot without a first!") } // format params: @@ -190,7 +190,8 @@ func (c *Client) GetBlockProduction( if lastSlot != nil { // make sure first and last slot are in order: if *firstSlot > *lastSlot { - panic(fmt.Errorf("last slot %v is greater than first slot %v", *lastSlot, *firstSlot)) + err := fmt.Errorf("last slot %v is greater than first slot %v", *lastSlot, *firstSlot) + klog.Fatalf("%v", err) } blockRange["lastSlot"] = *lastSlot }