fix: correct telemetry identifier and visibility when containerized
Three related fixes for running Monitor inside a container with /:/host:ro bind-mounted and network_mode: host. * config: when HF_MONITER_ROOTFS is set, read the default identifier from <rootFS>/etc/hostname instead of os.Hostname(). Under network_mode: host the UTS namespace is not shared, so os.Hostname() returns a random docker-assigned string that changes across recreations, causing the backend to treat each restart as a new server. * telemetry: log gopsutil errors from BuildPayload instead of silently swallowing them. Previously a missing /host mount would send a payload full of zeroed fields with no indication of failure. * docker-compose: drop the 'ports:' block. It is silently ignored under network_mode: host (the bridge server binds directly on the host's 127.0.0.1:MONITOR_PORT).
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
@@ -32,9 +33,19 @@ func Load(path string) (Config, error) {
|
||||
}
|
||||
|
||||
func LoadWithOverrides(path string, overrides Overrides) (Config, error) {
|
||||
// If running inside a container with the host FS bind-mounted, prefer
|
||||
// the host's /etc/hostname for the default identifier. The container's
|
||||
// own os.Hostname() is a docker-assigned random string under
|
||||
// network_mode: host (UTS namespace is not shared).
|
||||
rootFSEarly := getenvAny([]string{"HF_MONITER_ROOTFS", "HF_MONITOR_ROOTFS"}, "")
|
||||
defaultIdentifier := hostHostname(rootFSEarly)
|
||||
if defaultIdentifier == "" {
|
||||
defaultIdentifier = hostnameOr("unknown-host")
|
||||
}
|
||||
|
||||
cfg := Config{
|
||||
BackendURL: getenvAny([]string{"HF_MONITER_BACKEND_URL", "HF_MONITOR_BACKEND_URL"}, "https://monitor.hangman-lab.top"),
|
||||
Identifier: getenvAny([]string{"HF_MONITER_IDENTIFIER", "HF_MONITOR_IDENTIFIER"}, hostnameOr("unknown-host")),
|
||||
Identifier: getenvAny([]string{"HF_MONITER_IDENTIFIER", "HF_MONITOR_IDENTIFIER"}, defaultIdentifier),
|
||||
APIKey: getenvAny([]string{"HF_MONITER_API_KEY", "HF_MONITOR_API_KEY"}, ""),
|
||||
ReportIntervalSec: getenvIntAny([]string{"HF_MONITER_REPORT_INTERVAL", "HF_MONITOR_REPORT_INTERVAL"}, 30),
|
||||
LogLevel: getenvAny([]string{"HF_MONITER_LOG_LEVEL", "HF_MONITOR_LOG_LEVEL"}, "info"),
|
||||
@@ -153,11 +164,25 @@ func getenvIntAny(keys []string, fallback int) int {
|
||||
}
|
||||
|
||||
func hostnameOr(fallback string) string {
|
||||
name, err := os.Hostname()
|
||||
if err != nil || name == "" {
|
||||
return fallback
|
||||
if name, err := os.Hostname(); err == nil && name != "" {
|
||||
return name
|
||||
}
|
||||
return name
|
||||
return fallback
|
||||
}
|
||||
|
||||
// hostHostname reads the hostname from <rootFS>/etc/hostname. Used when
|
||||
// Monitor runs inside a container and wants the host's hostname rather
|
||||
// than the container's UTS namespace hostname (which docker randomizes
|
||||
// unless hostname: is set).
|
||||
func hostHostname(rootFS string) string {
|
||||
if rootFS == "" {
|
||||
return ""
|
||||
}
|
||||
buf, err := os.ReadFile(filepath.Join(rootFS, "etc", "hostname"))
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return strings.TrimSpace(string(buf))
|
||||
}
|
||||
|
||||
func applyHostFSEnv(rootFS string) {
|
||||
|
||||
Reference in New Issue
Block a user