Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions otelcli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func DefaultConfig() Config {
EventTime: "now",
CfgFile: "",
Verbose: false,
Fail: false,
}
}

Expand Down Expand Up @@ -78,6 +79,7 @@ type Config struct {

CfgFile string `json:"config_file"`
Verbose bool `json:"verbose"`
Fail bool `json:"fail"`
}

// UnmarshalJSON makes sure that any Config loaded from JSON has its default
Expand Down
7 changes: 6 additions & 1 deletion otelcli/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ type Diagnostics struct {
// hack: ignores the Diagnostics assigned to it and writes to the global
func (Diagnostics) Handle(err error) {
diagnostics.OtelError = err.Error() // write to the global
softLog("OpenTelemetry error: %s", err)

if config.Fail {
softFail("OpenTelemetry error: %s", err)
} else {
softLog("OpenTelemetry error: %s", err)
}
}

// ToMap returns the Diagnostics struct as a string map for testing.
Expand Down
12 changes: 9 additions & 3 deletions otelcli/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,17 @@ func softLog(format string, a ...interface{}) {
log.Printf(format, a...)
}

// softFail only calls through to log if otel-cli was run with the --verbose
// flag, then immediately exits with status 0.
// softFail calls through to softLog (which logs only if otel-cli was run with the --verbose
// flag), then immediately exits - with status 0 by default, or 1 if --fail was
// set (a la `curl --fail`)
func softFail(format string, a ...interface{}) {
softLog(format, a...)
os.Exit(0)

if !config.Fail {
os.Exit(0)
} else {
os.Exit(1)
}
}

// flattenStringMap takes a string map and returns it flattened into a string with
Expand Down
2 changes: 2 additions & 0 deletions otelcli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ func addCommonParams(cmd *cobra.Command) {
cmd.Flags().StringVar(&config.Timeout, "timeout", defaults.Timeout, "timeout for otel-cli operations, all timeouts in otel-cli use this value")
// --verbose tells otel-cli to actually log errors to stderr instead of failing silently
cmd.Flags().BoolVar(&config.Verbose, "verbose", defaults.Verbose, "print errors on failure instead of always being silent")
// --fail causes a non-zero exit status on error
cmd.Flags().BoolVar(&config.Fail, "fail", defaults.Fail, "on failure, exit with a non-zero status")

var common_env_flags = map[string]string{
"endpoint": "OTEL_EXPORTER_OTLP_ENDPOINT",
Expand Down