Skip to content

Commit

Permalink
[RC4] Fix for datadog logs output (#196)
Browse files Browse the repository at this point in the history
* fix for datadog logs output

* updating mocks

* PR comment
  • Loading branch information
jt-dd committed May 29, 2024
1 parent 3e3ff1d commit 879a968
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 29 deletions.
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

0 comments on commit 879a968

Please sign in to comment.