fix: resolve GELF hostname at adapter creation, not package init - #5
Open
aarontc wants to merge 1 commit into
Open
fix: resolve GELF hostname at adapter creation, not package init#5aarontc wants to merge 1 commit into
aarontc wants to merge 1 commit into
Conversation
The HA launcher sets SYSLOG_HOSTNAME inside main(), which runs after all
package init() functions. Reading the hostname in gelf.init() froze it to the
"{{.Container.Config.Hostname}}" fallback before the launcher could apply the
configured value, so that literal template string was shipped as the GELF
source for every message. Resolve the hostname in NewGelfAdapter (called by
runner.Run, after the launcher populates the env) and store it per-adapter.
Worked before the in-process launcher because the old shell wrapper exported
SYSLOG_HOSTNAME and exec'd logspout as a separate process.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fix: resolve GELF hostname at adapter creation, not package init
Summary
Since the native Home Assistant launcher was introduced (the change that
"directly launch[es] logspout and parse[s] the config from there instead of
using a shell wrapper", HA app v1.12.0), the GELF adapter ships the literal,
unrendered string
{{.Container.Config.Hostname}}as the messagehost(Graylog source) field, ignoring the configured
hostnameoption.This breaks any downstream tooling that filters by
source(e.g. Graylogsearches/streams scoped to a host), because every container from the host now
shares the bogus source
{{.Container.Config.Hostname}}instead of theconfigured value.
Root cause: Go package-init ordering
adapters/gelf/gelf.goresolved the hostname in packageinit()and cachedit in a package global:
The native launcher sets
SYSLOG_HOSTNAMEfrom the addon config insidemain()(launcher.BuildEnvironment→os.Setenv). Go runs every packageinit()beforemain(), andcmd/launcherimports the adapterstransitively (
modules→adapters/gelf). So the ordering is:gelf.init()runs →SYSLOG_HOSTNAMEis still unset and/etc/host_hostnameis absent → the global is frozen to the fallback default
"{{.Container.Config.Hostname}}".main()→launcher.Run()→os.Setenv("SYSLOG_HOSTNAME", <configured>)—too late; the global was already set.
Stream()stamps the frozen literal on every message.It worked before the native launcher because the old shell wrapper exported
SYSLOG_HOSTNAMEand thenexec'd logspout as a separate process, so theenvironment was already in place when
init()ran. Moving the launcherin-process exposed the latent init-time read.
(The default value
"{{.Container.Config.Hostname}}"is a Gotext/templatethat the upstream adapter this is "based on"
micahhausler/logspout-gelf
renders per message; this fork uses it as a raw string. That's why the literal
template text appears rather than a rendered hostname.)
Fix
Resolve the hostname when the adapter is created (
NewGelfAdapter, invokedby
runner.Runafter the launcher has populated the environment) and store iton the
Adapter, instead of reading it ininit():The launcher always sets
SYSLOG_HOSTNAME(defaulting tohomeassistantwhenthe option is empty), so the template-default branch is no longer reached in the
addon; the configured
hostnamenow reaches Graylog as the source again.Tests
Added two tests in
adapters/gelf/gelf_test.go:TestGelfAdapterResolvesHostnameAtCreation— setsSYSLOG_HOSTNAMEafter package init (simulating the launcher) and asserts
NewGelfAdapterresolves it. This fails against the old init-time read and is the direct
regression guard.
TestGelfStreamUsesAdapterHostname— asserts the per-adapter hostname isstamped on each emitted message's
Host.go test ./adapters/gelf/ ./launcher/passes;go build ./cmd/launchersucceeds;
gofmtclean.Optional follow-up (not in this PR)
To make the
{{.Container.Config.Hostname}}default behave as intended fornon-addon users, render
hostnameas atext/templateagainst eachrouter.Message(restoring the upstream micahhausler behavior). Happy to addthis if desired.