Skip to content

Commit c7e0d81

Browse files
committed
Use structured logging instead of fmt.Fprintf for gRPC retry
1 parent b92241f commit c7e0d81

File tree

3 files changed

+18
-28
lines changed

3 files changed

+18
-28
lines changed

cmd/emulator/start/start.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -196,17 +196,17 @@ func Cmd(config StartConfig) *cobra.Command {
196196
storageMBPerFLOW = parseCadenceUFix64(conf.StorageMBPerFLOW, "storage-per-flow")
197197
}
198198

199-
// Recompute chain ID and service address accurately for fork mode by querying the node.
200-
forkHost := conf.ForkHost
201-
resolvedChainID := flowChainID
202-
203-
if forkHost != "" {
204-
parsed, err := server.DetectRemoteChainID(forkHost)
205-
if err != nil {
206-
Exit(1, fmt.Sprintf("failed to detect remote chain id from %s: %v", forkHost, err))
207-
}
208-
resolvedChainID = parsed
199+
// Recompute chain ID and service address accurately for fork mode by querying the node.
200+
forkHost := conf.ForkHost
201+
resolvedChainID := flowChainID
202+
203+
if forkHost != "" {
204+
parsed, err := server.DetectRemoteChainID(logger, forkHost)
205+
if err != nil {
206+
Exit(1, fmt.Sprintf("failed to detect remote chain id from %s: %v", forkHost, err))
209207
}
208+
resolvedChainID = parsed
209+
}
210210

211211
serverConf := &server.Config{
212212
GRPCPort: conf.Port,

server/server.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ func NewEmulatorServer(logger *zerolog.Logger, conf *Config) (*EmulatorServer, e
183183
// Derive chain ID for the emulator setup.
184184
resolvedChainID := conf.ChainID
185185
if conf.ForkHost != "" && conf.ChainID == "" {
186-
parsed, err := DetectRemoteChainID(conf.ForkHost)
186+
parsed, err := DetectRemoteChainID(logger, conf.ForkHost)
187187
if err != nil {
188188
logger.Error().Err(err).Str("forkHost", conf.ForkHost).Msg("❗ Failed to detect remote chain ID")
189189
return nil, fmt.Errorf("failed to detect remote chain ID from %s: %w", conf.ForkHost, err)
@@ -279,35 +279,35 @@ func NewEmulatorServer(logger *zerolog.Logger, conf *Config) (*EmulatorServer, e
279279
}
280280

281281
// detectRemoteChainID connects to the remote access node and fetches network parameters to obtain the chain ID.
282-
func DetectRemoteChainID(url string) (flowgo.ChainID, error) {
283-
_, _ = fmt.Fprintf(os.Stdout, "Detecting chain ID for: %s\n", url)
282+
func DetectRemoteChainID(logger *zerolog.Logger, url string) (flowgo.ChainID, error) {
283+
logger.Info().Str("url", url).Msg("Detecting chain ID")
284284

285285
// Expect raw host:port
286286
conn, err := grpc.NewClient(
287287
url,
288288
grpc.WithTransportCredentials(insecure.NewCredentials()),
289-
utils.DefaultGRPCRetryInterceptor(),
289+
utils.DefaultGRPCRetryInterceptorWithLogger(logger),
290290
)
291291
if err != nil {
292-
_, _ = fmt.Fprintf(os.Stderr, "❌ Failed to connect to %s: %v\n", url, err)
292+
logger.Error().Err(err).Str("url", url).Msg("Failed to create gRPC client")
293293
return "", err
294294
}
295295
defer func() { _ = conn.Close() }()
296296

297297
client := flowaccess.NewAccessAPIClient(conn)
298298
resp, err := client.GetNetworkParameters(context.Background(), &flowaccess.GetNetworkParametersRequest{})
299299
if err != nil {
300-
_, _ = fmt.Fprintf(os.Stderr, "❌ GetNetworkParameters failed for %s: %v\n", url, err)
300+
logger.Error().Err(err).Str("url", url).Msg("GetNetworkParameters failed")
301301
return "", err
302302
}
303303

304304
if resp == nil {
305-
_, _ = fmt.Fprintf(os.Stderr, "❌ GetNetworkParameters returned nil response for: %s\n", url)
305+
logger.Error().Str("url", url).Msg("GetNetworkParameters returned nil response")
306306
return "", fmt.Errorf("GetNetworkParameters returned nil response")
307307
}
308308

309309
chainID := flowgo.ChainID(resp.ChainId)
310-
_, _ = fmt.Fprintf(os.Stdout, "✅ Detected chain ID: %s from %s\n", string(chainID), url)
310+
logger.Info().Str("url", url).Str("chainId", string(chainID)).Msg("Successfully detected chain ID")
311311

312312
return chainID, nil
313313
}

utils/grpc.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ package utils
2020

2121
import (
2222
"context"
23-
"fmt"
24-
"os"
2523
"time"
2624

2725
"github.com/rs/zerolog"
@@ -86,8 +84,6 @@ func retryInterceptorWithLogger(
8684

8785
for attempt := 0; attempt < defaultMaxAttempts; attempt++ {
8886
if attempt > 0 {
89-
_, _ = fmt.Fprintf(os.Stdout, "🔄 Retry attempt %d/%d for %s after %v (last error: %v)\n",
90-
attempt+1, defaultMaxAttempts, method, backoff, lastErr)
9187
log.Warn().
9288
Str("method", method).
9389
Int("attempt", attempt+1).
@@ -110,17 +106,12 @@ func retryInterceptorWithLogger(
110106

111107
lastErr = invoker(ctx, method, req, reply, cc, opts...)
112108
if lastErr == nil {
113-
if attempt > 0 {
114-
_, _ = fmt.Fprintf(os.Stdout, "✅ Call succeeded for %s on attempt %d\n", method, attempt+1)
115-
}
116109
return nil
117110
}
118111

119112
// Check if error is retryable
120113
code := status.Code(lastErr)
121114
isRetryable := isRetryableCode(code)
122-
_, _ = fmt.Fprintf(os.Stderr, "❌ Call failed for %s (attempt %d): %v (code: %s, retryable: %v)\n",
123-
method, attempt+1, lastErr, code.String(), isRetryable)
124115
log.Error().
125116
Str("method", method).
126117
Int("attempt", attempt+1).
@@ -134,7 +125,6 @@ func retryInterceptorWithLogger(
134125
}
135126
}
136127

137-
_, _ = fmt.Fprintf(os.Stderr, "❌ Max retry attempts (%d) exhausted for %s: %v\n", defaultMaxAttempts, method, lastErr)
138128
log.Error().
139129
Str("method", method).
140130
Int("maxAttempts", defaultMaxAttempts).

0 commit comments

Comments
 (0)