(ctx context.Context)
| 48 | } |
| 49 | |
| 50 | func start(ctx context.Context) { |
| 51 | logger, logFile, err := log.New() |
| 52 | if err != nil { |
| 53 | fmt.Println("Failed to start the logger for the CLI", err) |
| 54 | return |
| 55 | } |
| 56 | utils.LogFile = logFile |
| 57 | |
| 58 | // If KEPLOY_DEBUG_FILE is set, tee debug-level log records into that |
| 59 | // file in addition to whatever sinks log.New configured. Used by |
| 60 | // `keploy cloud replay` to capture the agent container's debug |
| 61 | // stream into the user's keploy folder via a bind mount; the |
| 62 | // support-bundle pipeline picks the file up automatically since it |
| 63 | // lives under cfg.Path. Also useful as a generic "give me the full |
| 64 | // debug log without --debug printing it to my terminal" knob for |
| 65 | // any keploy invocation. |
| 66 | debugFile, debugSink := maybeAttachDebugFileSink(logger) |
| 67 | if debugFile != nil { |
| 68 | defer func() { |
| 69 | if debugSink != nil { |
| 70 | if ferr := debugSink.Flush(); ferr != nil { |
| 71 | logger.Warn("KEPLOY_DEBUG_FILE: flush failed", zap.Error(ferr)) |
| 72 | } |
| 73 | } |
| 74 | if cerr := debugFile.Close(); cerr != nil { |
| 75 | logger.Warn("KEPLOY_DEBUG_FILE: close failed", zap.Error(cerr)) |
| 76 | } |
| 77 | }() |
| 78 | } |
| 79 | |
| 80 | // Start pprof HTTP server for live profiling if PPROF_PORT is set |
| 81 | if pprofPort := os.Getenv("PPROF_PORT"); pprofPort != "" { |
| 82 | go func() { |
| 83 | addr := "localhost:" + pprofPort |
| 84 | logger.Info("pprof server starting", zap.String("addr", addr)) |
| 85 | if err := http.ListenAndServe(addr, nil); err != nil { |
| 86 | logger.Error("pprof server failed; check that PPROF_PORT is a valid port and not already in use, or unset the env var to disable", |
| 87 | zap.String("port", pprofPort), zap.Error(err)) |
| 88 | } |
| 89 | }() |
| 90 | } |
| 91 | |
| 92 | // Set soft memory limit for GC if GOMEMLIMIT_MB is set. |
| 93 | // This makes Go's GC more aggressive when approaching the limit, |
| 94 | // reducing peak memory during load without affecting normal operation. |
| 95 | if memLimitMB := os.Getenv("GOMEMLIMIT_MB"); memLimitMB != "" { |
| 96 | mb, err := strconv.ParseInt(memLimitMB, 10, 64) |
| 97 | if err != nil || mb <= 0 { |
| 98 | logger.Info("invalid GOMEMLIMIT_MB value, ignoring; expected a positive integer (e.g. GOMEMLIMIT_MB=150)", |
| 99 | zap.String("value", memLimitMB)) |
| 100 | } else { |
| 101 | debug.SetMemoryLimit(mb * 1024 * 1024) |
| 102 | logger.Info("GOMEMLIMIT set", zap.Int64("MB", mb)) |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | // Start background memory monitor only when GOMEMLIMIT_MB is set. |
| 107 | // Calls FreeOSMemory on sharp heap drops to force fast OS page release. |
no test coverage detected