logrus: add deployment channel as field to the logs
This commit is contained in:
parent
2da3a73308
commit
9006836afc
5 changed files with 80 additions and 11 deletions
|
|
@ -23,6 +23,7 @@ type ComposerConfigFile struct {
|
|||
SplunkPort string `env:"SPLUNK_HEC_PORT"`
|
||||
SplunkToken string `env:"SPLUNK_HEC_TOKEN"`
|
||||
GlitchTipDSN string `env:"GLITCHTIP_DSN"`
|
||||
DeploymentChannel string `env:"CHANNEL"`
|
||||
}
|
||||
|
||||
type KojiAPIConfig struct {
|
||||
|
|
|
|||
|
|
@ -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=?")
|
||||
|
|
|
|||
26
internal/common/environment_hook.go
Normal file
26
internal/common/environment_hook.go
Normal file
|
|
@ -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
internal/common/environment_hook_test.go
Normal file
30
internal/common/environment_hook_test.go
Normal file
|
|
@ -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())
|
||||
}
|
||||
|
|
@ -126,6 +126,8 @@ objects:
|
|||
optional: true
|
||||
- name: DISTRO_ALIASES
|
||||
value: ${DISTRO_ALIASES}
|
||||
- name: CHANNEL
|
||||
value: ${CHANNEL}
|
||||
ports:
|
||||
- name: composer-api
|
||||
protocol: TCP
|
||||
|
|
@ -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".
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue