mirror of
https://github.com/hanzoai/o11y-foundry.git
synced 2026-10-30 18:26:45 +00:00
## Summary Reshape foundryctl's output contract so it composes with agents and shell pipelines. Three changes that fall out of the same observation: **stdout, stderr, and exit codes should be a stable contract — today they're conflated.** - **Logger to stderr, silent by default.** `instrumentation.NewLogger` writes to `os.Stderr` (was `os.Stdout`) and uses `slog.DiscardHandler` unless `--debug` is set. Matches helm/aws CLI convention. stdout is now reserved for command results. - **Typed exit codes.** `internal/errors.typ` carries a `code int` field; `errors.ExitCode(err)` walks the wrap chain via `errors.As` and returns a deterministic code: `InvalidInput=2, NotFound=3, Unsupported=4, Internal=5, Fatal=6`, untyped=1, nil=0. Compact 1-6 scheme rather than sysexits.h because sysexits would collapse `Internal` and `Fatal` into the same `EX_SOFTWARE (70)`, losing a distinction we care about (Fatal = recovered panic; Internal = expected-but-failed path). `cmd/foundryctl/main.go` now exits with `errors.ExitCode(err)`. - **`fmt.Errorf` forbidden by lint.** New forbidigo rules in `.golangci.yml` ban `fmt.Errorf` and `fmt.Print*` / `print` / `println`. 168 call sites across 38 files converted to `errors.Newf` / `errors.Wrapf` with contextually-chosen types. Narrow exception for `internal/errors/error_test.go` so the ExitCode wrap-chain test stays authentic. Sites of note for review: - `internal/errors/type.go` — exit code mapping table (one source of truth) - `internal/errors/error.go` — `ExitCode(err)` helper - `internal/instrumentation/logger.go` — `slog.DiscardHandler` default - `.golangci.yml` — forbidigo config + the single narrow test exclusion ## Test plan - [x] `go build ./...` - [x] `go test ./...` (all packages pass; new `TestExitCode` covers 9 cases: nil, untyped, each typed, fmt.Errorf-wrapped, foundry-Wrapf-wrapped) - [x] `golangci-lint run ./...` — 0 issues - [x] Manual: `foundryctl forge` on a missing file exits non-zero and emits nothing on stdout (logs hidden); `--debug` emits JSON on stderr only - [x] Manual: end-to-end forge + cast against the docker/compose flavor still produces a healthy stack
83 lines
2.0 KiB
Go
83 lines
2.0 KiB
Go
package main
|
|
|
|
import (
|
|
"log/slog"
|
|
"os"
|
|
"runtime"
|
|
|
|
"github.com/signoz/foundry/internal/domain"
|
|
foundryerrors "github.com/signoz/foundry/internal/errors"
|
|
"github.com/signoz/foundry/internal/instrumentation"
|
|
"github.com/signoz/foundry/internal/ledger"
|
|
"github.com/signoz/foundry/internal/ledger/noopledger"
|
|
"github.com/signoz/foundry/internal/ledger/segmentledger"
|
|
"github.com/signoz/foundry/internal/writer"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var (
|
|
rootLogger *slog.Logger
|
|
rootTracker ledger.Ledger
|
|
)
|
|
|
|
// newRoot is wired as rootCmd.PersistentPreRunE so it fires after persistent
|
|
// flags are parsed and before any command's RunE runs.
|
|
func newRoot(_ *cobra.Command, _ []string) error {
|
|
rootLogger = instrumentation.NewLogger(commonCfg.Debug)
|
|
|
|
config := ledger.NewConfig()
|
|
if commonCfg.NoLedger {
|
|
config.Enabled = false
|
|
}
|
|
|
|
switch config.Provider() {
|
|
case "segment":
|
|
rootTracker = segmentledger.New(config)
|
|
default:
|
|
rootTracker = noopledger.New()
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func closeRoot() {
|
|
if rootTracker != nil {
|
|
_ = rootTracker.Close()
|
|
}
|
|
}
|
|
|
|
func recoverRunE(
|
|
event domain.Event,
|
|
runE func(cmd *cobra.Command, args []string) (domain.Properties, error),
|
|
) func(cmd *cobra.Command, args []string) error {
|
|
return func(cmd *cobra.Command, args []string) (err error) {
|
|
ctx := cmd.Context()
|
|
props := domain.NewProperties()
|
|
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
buf := make([]byte, 4096)
|
|
n := runtime.Stack(buf, false)
|
|
|
|
errp := foundryerrors.Newf(foundryerrors.TypeFatal, "%v", r).WithStacktrace(string(buf[:n]))
|
|
err = errp
|
|
}
|
|
|
|
if err != nil {
|
|
rootLogger.ErrorContext(ctx, event.String()+" failed", foundryerrors.LogAttr(err))
|
|
rootTracker.Track(ctx, event.Failed(), props.WithError(err))
|
|
if commonCfg.Format == "json" {
|
|
_ = writer.WriteOutput(os.Stdout, foundryerrors.EnvelopeOf(err))
|
|
cmd.SilenceErrors = true
|
|
}
|
|
return
|
|
}
|
|
|
|
rootTracker.Track(ctx, event.Succeeded(), props.WithSuccess())
|
|
}()
|
|
|
|
props, err = runE(cmd, args)
|
|
return err
|
|
}
|
|
}
|