Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RC4] Fix for datadog logs output #196

Merged
merged 3 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions pkg/globals/application.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
package globals

import (
"os"
)

const (
DDServiceName = "kubehound"
DDEnv = "prod"
DDServiceName = "kubehound"
DefaultDDEnv = "dev"
DefaultComponent = "kubehound-ingestor"
)

func ProdEnv() bool {
return false
func GetDDEnv() string {
env := os.Getenv("DD_ENV")
if env == "" {
return DefaultDDEnv
}

return env
}

const (
Expand Down
11 changes: 6 additions & 5 deletions pkg/ingestor/puller/mocks/mock_puller.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 11 additions & 10 deletions pkg/kubehound/graph/edge/mocks/edge.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 23 additions & 8 deletions pkg/telemetry/log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package log

import (
"context"
"os"
"strconv"
"sync"

Expand All @@ -18,14 +19,10 @@ type LoggerConfig struct {
DD bool // Whether Datadog integration is enabled.
}

const (
DefaultComponent = "kubehound"
)

var globalConfig = LoggerConfig{
Tags: logrus.Fields{
globals.TagService: globals.DDServiceName,
globals.TagComponent: DefaultComponent,
globals.TagComponent: globals.DefaultComponent,
},
Mu: &sync.Mutex{},
DD: true,
Expand Down Expand Up @@ -59,9 +56,7 @@ func spanID(span tracer.Span) string {
// Base returns the base logger for the application.
func Base() *KubehoundLogger {
logger := logrus.WithFields(globalConfig.Tags)
if globals.ProdEnv() {
logger.Logger.SetFormatter(&logrus.JSONFormatter{})
}
logger.Logger.SetFormatter(GetLogrusFormatter())

return &KubehoundLogger{logger}
}
Expand Down Expand Up @@ -121,3 +116,23 @@ func Trace(ctx context.Context, opts ...LoggerOption) *KubehoundLogger {

return &KubehoundLogger{logger}
}

func GetLogrusFormatter() logrus.Formatter {
switch logFormat := os.Getenv("KH_LOG_FORMAT"); {
// Datadog require the logged field to be "message" and not "msg"
case logFormat == "dd":
formatter := &logrus.JSONFormatter{
FieldMap: logrus.FieldMap{
logrus.FieldKeyMsg: "message",
},
}

return formatter
case logFormat == "json":
return &logrus.JSONFormatter{}
case logFormat == "text":
return &logrus.TextFormatter{}
default:
return &logrus.TextFormatter{}
}
}
2 changes: 1 addition & 1 deletion pkg/telemetry/profiler/profiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func Initialize(cfg *config.KubehoundConfig) {
opts := []profiler.Option{
profiler.WithService(globals.DDServiceName),
profiler.WithEnv(globals.DDEnv),
profiler.WithEnv(globals.GetDDEnv()),
profiler.WithVersion(config.BuildVersion),
profiler.WithProfileTypes(
profiler.CPUProfile,
Expand Down
2 changes: 1 addition & 1 deletion pkg/telemetry/tracer/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
func Initialize(cfg *config.KubehoundConfig) {
// Default options
opts := []tracer.StartOption{
tracer.WithEnv(globals.DDEnv),
tracer.WithEnv(globals.GetDDEnv()),
tracer.WithService(globals.DDServiceName),
tracer.WithServiceVersion(config.BuildVersion),
tracer.WithLogStartup(false),
Expand Down
Loading