-
Notifications
You must be signed in to change notification settings - Fork 46
/
diagnostics.go
58 lines (52 loc) · 2.11 KB
/
diagnostics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package otelcli
import (
"strconv"
"strings"
)
// package global Diagnostics handle, written to from all over otel-cli
// and used in e.g. otel-cli status to show internal state
var Diag Diagnostics
// Diagnostics is a place to put things that are useful for testing and
// diagnosing issues with otel-cli. The only user-facing feature that should be
// using these is otel-cli status.
type Diagnostics struct {
CliArgs []string `json:"cli_args"`
IsRecording bool `json:"is_recording"`
ConfigFileLoaded bool `json:"config_file_loaded"`
NumArgs int `json:"number_of_args"`
DetectedLocalhost bool `json:"detected_localhost"`
InsecureSkipVerify bool `json:"insecure_skip_verify"`
ParsedTimeoutMs int64 `json:"parsed_timeout_ms"`
Endpoint string `json:"endpoint"` // the computed endpoint, not the raw config val
EndpointSource string `json:"endpoint_source"`
Error string `json:"error"`
ExecExitCode int `json:"exec_exit_code"`
Retries int `json:"retries"`
}
// ToMap returns the Diag struct as a string map for testing.
func (d *Diagnostics) ToStringMap() map[string]string {
return map[string]string{
"cli_args": strings.Join(d.CliArgs, " "),
"is_recording": strconv.FormatBool(d.IsRecording),
"config_file_loaded": strconv.FormatBool(d.ConfigFileLoaded),
"number_of_args": strconv.Itoa(d.NumArgs),
"detected_localhost": strconv.FormatBool(d.DetectedLocalhost),
"parsed_timeout_ms": strconv.FormatInt(d.ParsedTimeoutMs, 10),
"endpoint": d.Endpoint,
"endpoint_source": d.EndpointSource,
"error": d.Error,
}
}
// SetError sets the diagnostics Error to the error's string if it's
// not nil and returns the same error so it can be inlined in return.
func (d *Diagnostics) SetError(err error) error {
if err != nil {
Diag.Error = err.Error()
}
return err
}
// GetExitCode() is a helper for Cobra to retrieve the exit code, mainly
// used by exec to make otel-cli return the child program's exit code.
func GetExitCode() int {
return Diag.ExecExitCode
}