Skip to content

Commit

Permalink
logrus: add deployment channel as field to the logs
Browse files Browse the repository at this point in the history
  • Loading branch information
schuellerf committed Aug 7, 2024
1 parent 2da3a73 commit 9006836
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 11 deletions.
23 changes: 12 additions & 11 deletions cmd/osbuild-composer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@ import (
)

type ComposerConfigFile struct {
Koji KojiAPIConfig `toml:"koji"`
Worker WorkerAPIConfig `toml:"worker"`
WeldrAPI WeldrAPIConfig `toml:"weldr_api"`
DistroAliases map[string]string `toml:"distro_aliases" env:"DISTRO_ALIASES"`
LogLevel string `toml:"log_level"`
LogFormat string `toml:"log_format"`
DNFJson string `toml:"dnf-json"`
SplunkHost string `env:"SPLUNK_HEC_HOST"`
SplunkPort string `env:"SPLUNK_HEC_PORT"`
SplunkToken string `env:"SPLUNK_HEC_TOKEN"`
GlitchTipDSN string `env:"GLITCHTIP_DSN"`
Koji KojiAPIConfig `toml:"koji"`
Worker WorkerAPIConfig `toml:"worker"`
WeldrAPI WeldrAPIConfig `toml:"weldr_api"`
DistroAliases map[string]string `toml:"distro_aliases" env:"DISTRO_ALIASES"`
LogLevel string `toml:"log_level"`
LogFormat string `toml:"log_format"`
DNFJson string `toml:"dnf-json"`
SplunkHost string `env:"SPLUNK_HEC_HOST"`
SplunkPort string `env:"SPLUNK_HEC_PORT"`
SplunkToken string `env:"SPLUNK_HEC_TOKEN"`
GlitchTipDSN string `env:"GLITCHTIP_DSN"`
DeploymentChannel string `env:"CHANNEL"`
}

type KojiAPIConfig struct {
Expand Down
4 changes: 4 additions & 0 deletions cmd/osbuild-composer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ func main() {
logrus.Warn("GLITCHTIP_DSN not configured, skipping initializing Sentry/Glitchtip")
}

if config.DeploymentChannel != "" {
logrus.AddHook(&common.EnvironmentHook{Channel: config.DeploymentChannel})
}

stateDir, ok := os.LookupEnv("STATE_DIRECTORY")
if !ok {
logrus.Fatal("STATE_DIRECTORY is not set. Is the service file missing StateDirectory=?")
Expand Down
26 changes: 26 additions & 0 deletions internal/common/environment_hook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package common

import (
"github.com/sirupsen/logrus"
)

type EnvironmentHook struct {
Channel string
}

func (h *EnvironmentHook) Levels() []logrus.Level {
return []logrus.Level{
logrus.DebugLevel,
logrus.InfoLevel,
logrus.WarnLevel,
logrus.ErrorLevel,
logrus.FatalLevel,
logrus.PanicLevel,
}
}

func (h *EnvironmentHook) Fire(e *logrus.Entry) error {
e.Data["channel"] = h.Channel

return nil
}
30 changes: 30 additions & 0 deletions internal/common/environment_hook_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package common

import (
"bytes"
"testing"

"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
)

func makeLogrus(buf *bytes.Buffer) *logrus.Logger {
return &logrus.Logger{
Out: buf,
Formatter: &logrus.TextFormatter{
DisableTimestamp: true,
DisableColors: true,
},
Hooks: make(logrus.LevelHooks),
Level: logrus.DebugLevel,
}

}

func TestInfoWithEnvironment(t *testing.T) {
buf := &bytes.Buffer{}
l := makeLogrus(buf)
l.AddHook(&EnvironmentHook{Channel: "test_framework"})
l.Info("test message")
require.Equal(t, "level=info msg=\"test message\" channel=test_framework\n", buf.String())
}
8 changes: 8 additions & 0 deletions templates/openshift/composer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ objects:
optional: true
- name: DISTRO_ALIASES
value: ${DISTRO_ALIASES}
- name: CHANNEL
value: ${CHANNEL}
ports:
- name: composer-api
protocol: TCP
Expand Down Expand Up @@ -341,3 +343,9 @@ parameters:
- description: Distro name aliases
name: DISTRO_ALIASES
value: "rhel-7=rhel-7.9,rhel-8=rhel-8.10,rhel-9=rhel-9.4,rhel-10=rhel-10.0"
- name: CHANNEL
value: "local"
description: >
Channel where this pod is deployed.
This is appended to the logs. Usually something like
"local", "staging" or "production".

0 comments on commit 9006836

Please sign in to comment.