From d21bcd2b012f3d67bba60fca680b9ff0e73a6cb6 Mon Sep 17 00:00:00 2001 From: Matthew McPherrin Date: Wed, 19 Apr 2023 13:34:34 -0400 Subject: [PATCH 1/2] Use a common Command() function This moves command() from cmd/ into core.Command() and uses it from the log and main package, ensuring we have a single implementation of path.Base(os.Args[0]) instead of scattering that method around. --- cmd/boulder/main.go | 4 ++-- cmd/shell.go | 17 ++++++----------- core/util.go | 5 +++++ log/log.go | 7 ++++--- 4 files changed, 17 insertions(+), 16 deletions(-) diff --git a/cmd/boulder/main.go b/cmd/boulder/main.go index 4fd8776274e..12754701ff6 100644 --- a/cmd/boulder/main.go +++ b/cmd/boulder/main.go @@ -4,7 +4,6 @@ import ( "flag" "fmt" "os" - "path" _ "github.com/letsencrypt/boulder/cmd/admin-revoker" _ "github.com/letsencrypt/boulder/cmd/akamai-purger" @@ -32,6 +31,7 @@ import ( _ "github.com/letsencrypt/boulder/cmd/orphan-finder" _ "github.com/letsencrypt/boulder/cmd/reversed-hostname-checker" _ "github.com/letsencrypt/boulder/cmd/rocsp-tool" + "github.com/letsencrypt/boulder/core" "github.com/letsencrypt/boulder/cmd" ) @@ -61,7 +61,7 @@ func readAndValidateConfigFile(name, filename string) error { } func main() { - cmd.LookupCommand(path.Base(os.Args[0]))() + cmd.LookupCommand(core.Command())() } func init() { diff --git a/cmd/shell.go b/cmd/shell.go index e198d11a976..4b24a50a771 100644 --- a/cmd/shell.go +++ b/cmd/shell.go @@ -1,4 +1,4 @@ -// This package provides utilities that underlie the specific commands. +// Package cmd provides utilities that underlie the specific commands. package cmd import ( @@ -14,7 +14,6 @@ import ( "net/http/pprof" "os" "os/signal" - "path" "runtime" "strings" "syscall" @@ -22,8 +21,6 @@ import ( "github.com/go-redis/redis/v8" "github.com/go-sql-driver/mysql" - "github.com/letsencrypt/boulder/strictyaml" - "github.com/letsencrypt/validator/v10" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/collectors" "github.com/prometheus/client_golang/prometheus/promhttp" @@ -31,12 +28,10 @@ import ( "github.com/letsencrypt/boulder/core" blog "github.com/letsencrypt/boulder/log" + "github.com/letsencrypt/boulder/strictyaml" + "github.com/letsencrypt/validator/v10" ) -func command() string { - return path.Base(os.Args[0]) -} - // Because we don't know when this init will be called with respect to // flag.Parse() and other flag definitions, we can't rely on the regular // flag mechanism. But this one is fine. @@ -174,7 +169,7 @@ func NewLogger(logConf SyslogConfig) blog.Logger { "", "", syslog.LOG_INFO, // default, not actually used - command()) + core.Command()) FailOnError(err, "Could not connect to Syslog") syslogLevel := int(syslog.LOG_INFO) if logConf.SyslogLevel != 0 { @@ -221,7 +216,7 @@ func newVersionCollector() prometheus.Collector { Name: "version", Help: fmt.Sprintf( "A metric with a constant value of '1' labeled by the short commit-id (buildId), build timestamp in RFC3339 format (buildTime), and Go release tag like 'go1.3' (goVersion) from which %s was built.", - command(), + core.Command(), ), ConstLabels: prometheus.Labels{ "buildId": core.GetBuildID(), @@ -415,7 +410,7 @@ func ValidateYAMLConfig(cv *ConfigValidator, in io.Reader) error { // VersionString produces a friendly Application version string. func VersionString() string { - return fmt.Sprintf("Versions: %s=(%s %s) Golang=(%s) BuildHost=(%s)", command(), core.GetBuildID(), core.GetBuildTime(), runtime.Version(), core.GetBuildHost()) + return fmt.Sprintf("Versions: %s=(%s %s) Golang=(%s) BuildHost=(%s)", core.Command(), core.GetBuildID(), core.GetBuildTime(), runtime.Version(), core.GetBuildHost()) } // CatchSignals blocks until a SIGTERM, SIGINT, or SIGHUP is received, then diff --git a/core/util.go b/core/util.go index 6949e456300..5ecebb45bc3 100644 --- a/core/util.go +++ b/core/util.go @@ -16,6 +16,7 @@ import ( "math/big" mrand "math/rand" "os" + "path" "reflect" "regexp" "sort" @@ -298,3 +299,7 @@ func IsASCII(str string) bool { } return true } + +func Command() string { + return path.Base(os.Args[0]) +} diff --git a/log/log.go b/log/log.go index 4cb12c65b1b..75262337dc7 100644 --- a/log/log.go +++ b/log/log.go @@ -10,13 +10,14 @@ import ( "io" "log/syslog" "os" - "path" "runtime" "strings" "sync" "github.com/jmhodges/clock" "golang.org/x/term" + + "github.com/letsencrypt/boulder/core" ) // A Logger logs messages with explicit priority levels. It is @@ -92,7 +93,7 @@ func newStdoutWriter(level int) *stdoutWriter { } } - prefix := fmt.Sprintf("%s %s %s[%d]:", shortHostname, datacenter, path.Base(os.Args[0]), os.Getpid()) + prefix := fmt.Sprintf("%s %s %s[%d]:", shortHostname, datacenter, core.Command(), os.Getpid()) return &stdoutWriter{ prefix: prefix, @@ -269,7 +270,7 @@ func (w *stdoutWriter) logAtLevel(level syslog.Priority, msg string, a ...interf w.clk.Now().UTC().Format("2006-01-02T15:04:05.000000+00:00Z"), w.prefix, int(level), - path.Base(os.Args[0]), + core.Command(), checkSummed(msg), reset); err != nil { panic(fmt.Sprintf("failed to write to stdout: %v\n", err)) From 00635e7fbead2fbbd9a20e35a98894a266234c19 Mon Sep 17 00:00:00 2001 From: Matthew McPherrin Date: Fri, 21 Apr 2023 15:11:27 -0400 Subject: [PATCH 2/2] Use the moved command in otel setup --- cmd/shell.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/shell.go b/cmd/shell.go index 4d4be902b67..2400becf90a 100644 --- a/cmd/shell.go +++ b/cmd/shell.go @@ -305,7 +305,7 @@ func newOpenTelemetry(config OpenTelemetryConfig, logger blog.Logger) func(ctx c resource.Default(), resource.NewWithAttributes( semconv.SchemaURL, - semconv.ServiceNameKey.String(command()), + semconv.ServiceNameKey.String(core.Command()), semconv.ServiceVersionKey.String(core.GetBuildID()), ), )