mirror of
https://github.com/moby/moby.git
synced 2026-01-11 18:51:37 +00:00
daemon: minor cleanups for getting system info
- rename variables that shadowed functions - use strconv.FormatBool instead of fmt.Sprintf to reduce some allocations - use structured logs for some warnings - remove some intermediate vars in favor of early returns Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -105,7 +106,7 @@ func (daemon *Daemon) SystemInfo(ctx context.Context) (*system.Info, error) {
|
||||
func (daemon *Daemon) SystemVersion(ctx context.Context) (types.Version, error) {
|
||||
defer metrics.StartTimer(hostInfoFunctions.WithValues("system_version"))()
|
||||
|
||||
kernelVersion := kernelVersion(ctx)
|
||||
kernelVer := kernelVersion(ctx)
|
||||
cfg := daemon.config()
|
||||
|
||||
v := types.Version{
|
||||
@@ -121,8 +122,8 @@ func (daemon *Daemon) SystemVersion(ctx context.Context) (types.Version, error)
|
||||
"Os": runtime.GOOS,
|
||||
"Arch": runtime.GOARCH,
|
||||
"BuildTime": dockerversion.BuildTime,
|
||||
"KernelVersion": kernelVersion,
|
||||
"Experimental": fmt.Sprintf("%t", cfg.Experimental),
|
||||
"KernelVersion": kernelVer,
|
||||
"Experimental": strconv.FormatBool(cfg.Experimental),
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -136,7 +137,7 @@ func (daemon *Daemon) SystemVersion(ctx context.Context) (types.Version, error)
|
||||
Os: runtime.GOOS,
|
||||
Arch: runtime.GOARCH,
|
||||
BuildTime: dockerversion.BuildTime,
|
||||
KernelVersion: kernelVersion,
|
||||
KernelVersion: kernelVer,
|
||||
Experimental: cfg.Experimental,
|
||||
}
|
||||
|
||||
@@ -286,38 +287,37 @@ func (daemon *Daemon) fillDefaultAddressPools(ctx context.Context, v *system.Inf
|
||||
func hostName(ctx context.Context) string {
|
||||
ctx, span := tracing.StartSpan(ctx, "hostName")
|
||||
defer span.End()
|
||||
hostname := ""
|
||||
if hn, err := os.Hostname(); err != nil {
|
||||
log.G(ctx).Warnf("Could not get hostname: %v", err)
|
||||
} else {
|
||||
hostname = hn
|
||||
hn, err := os.Hostname()
|
||||
if err != nil {
|
||||
log.G(ctx).WithError(err).Warn("Could not get hostname")
|
||||
return ""
|
||||
}
|
||||
return hostname
|
||||
return hn
|
||||
}
|
||||
|
||||
func kernelVersion(ctx context.Context) string {
|
||||
ctx, span := tracing.StartSpan(ctx, "kernelVersion")
|
||||
defer span.End()
|
||||
|
||||
var kernelVersion string
|
||||
var ver string
|
||||
if kv, err := kernel.GetKernelVersion(); err != nil {
|
||||
log.G(ctx).Warnf("Could not get kernel version: %v", err)
|
||||
log.G(ctx).WithError(err).Warn("Could not get kernel version")
|
||||
} else {
|
||||
kernelVersion = kv.String()
|
||||
ver = kv.String()
|
||||
}
|
||||
return kernelVersion
|
||||
return ver
|
||||
}
|
||||
|
||||
func memInfo(ctx context.Context) *meminfo.Memory {
|
||||
ctx, span := tracing.StartSpan(ctx, "memInfo")
|
||||
defer span.End()
|
||||
|
||||
memInfo, err := meminfo.Read()
|
||||
mi, err := meminfo.Read()
|
||||
if err != nil {
|
||||
log.G(ctx).Errorf("Could not read system memory info: %v", err)
|
||||
memInfo = &meminfo.Memory{}
|
||||
log.G(ctx).WithError(err).Error("Could not read system memory info")
|
||||
return &meminfo.Memory{}
|
||||
}
|
||||
return memInfo
|
||||
return mi
|
||||
}
|
||||
|
||||
func operatingSystem(ctx context.Context) (operatingSystem string) {
|
||||
@@ -327,12 +327,12 @@ func operatingSystem(ctx context.Context) (operatingSystem string) {
|
||||
defer metrics.StartTimer(hostInfoFunctions.WithValues("operating_system"))()
|
||||
|
||||
if s, err := operatingsystem.GetOperatingSystem(); err != nil {
|
||||
log.G(ctx).Warnf("Could not get operating system name: %v", err)
|
||||
log.G(ctx).WithError(err).Warn("Could not get operating system name")
|
||||
} else {
|
||||
operatingSystem = s
|
||||
}
|
||||
if inContainer, err := operatingsystem.IsContainerized(); err != nil {
|
||||
log.G(ctx).Errorf("Could not determine if daemon is containerized: %v", err)
|
||||
log.G(ctx).WithError(err).Error("Could not determine if daemon is containerized")
|
||||
operatingSystem += " (error determining if containerized)"
|
||||
} else if inContainer {
|
||||
operatingSystem += " (containerized)"
|
||||
@@ -349,7 +349,8 @@ func osVersion(ctx context.Context) (version string) {
|
||||
|
||||
version, err := operatingsystem.GetOperatingSystemVersion()
|
||||
if err != nil {
|
||||
log.G(ctx).Warnf("Could not get operating system version: %v", err)
|
||||
log.G(ctx).WithError(err).Warn("Could not get operating system version")
|
||||
return ""
|
||||
}
|
||||
|
||||
return version
|
||||
|
||||
Reference in New Issue
Block a user