From 879a968e216a1b7909ef1fa32b6ac74b32e22bd1 Mon Sep 17 00:00:00 2001 From: jt-dd <112463504+jt-dd@users.noreply.github.com> Date: Wed, 29 May 2024 13:41:42 +0200 Subject: [PATCH] [RC4] Fix for datadog logs output (#196) * fix for datadog logs output * updating mocks * PR comment --- pkg/globals/application.go | 18 +++++++++++--- pkg/ingestor/puller/mocks/mock_puller.go | 11 +++++---- pkg/kubehound/graph/edge/mocks/edge.go | 21 ++++++++-------- pkg/telemetry/log/logger.go | 31 ++++++++++++++++++------ pkg/telemetry/profiler/profiler.go | 2 +- pkg/telemetry/tracer/tracer.go | 2 +- 6 files changed, 56 insertions(+), 29 deletions(-) diff --git a/pkg/globals/application.go b/pkg/globals/application.go index 98603a6bb..90bc5021f 100644 --- a/pkg/globals/application.go +++ b/pkg/globals/application.go @@ -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 ( diff --git a/pkg/ingestor/puller/mocks/mock_puller.go b/pkg/ingestor/puller/mocks/mock_puller.go index 4565c93f7..fafb3074c 100644 --- a/pkg/ingestor/puller/mocks/mock_puller.go +++ b/pkg/ingestor/puller/mocks/mock_puller.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.33.1. DO NOT EDIT. +// Code generated by mockery v2.20.0. DO NOT EDIT. package mocks @@ -161,12 +161,13 @@ func (_c *DataPuller_Pull_Call) RunAndReturn(run func(context.Context, string, s return _c } -// NewDataPuller creates a new instance of DataPuller. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. -// The first argument is typically a *testing.T value. -func NewDataPuller(t interface { +type mockConstructorTestingTNewDataPuller interface { mock.TestingT Cleanup(func()) -}) *DataPuller { +} + +// NewDataPuller creates a new instance of DataPuller. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +func NewDataPuller(t mockConstructorTestingTNewDataPuller) *DataPuller { mock := &DataPuller{} mock.Mock.Test(t) diff --git a/pkg/kubehound/graph/edge/mocks/edge.go b/pkg/kubehound/graph/edge/mocks/edge.go index 5e3846647..6ac41d820 100644 --- a/pkg/kubehound/graph/edge/mocks/edge.go +++ b/pkg/kubehound/graph/edge/mocks/edge.go @@ -71,13 +71,13 @@ func (_c *Builder_BatchSize_Call) RunAndReturn(run func() int) *Builder_BatchSiz return _c } -// Initialize provides a mock function with given fields: cfg -func (_m *Builder) Initialize(cfg *config.EdgeBuilderConfig) error { - ret := _m.Called(cfg) +// Initialize provides a mock function with given fields: cfg, runtime +func (_m *Builder) Initialize(cfg *config.EdgeBuilderConfig, runtime *config.DynamicConfig) error { + ret := _m.Called(cfg, runtime) var r0 error - if rf, ok := ret.Get(0).(func(*config.EdgeBuilderConfig) error); ok { - r0 = rf(cfg) + if rf, ok := ret.Get(0).(func(*config.EdgeBuilderConfig, *config.DynamicConfig) error); ok { + r0 = rf(cfg, runtime) } else { r0 = ret.Error(0) } @@ -92,13 +92,14 @@ type Builder_Initialize_Call struct { // Initialize is a helper method to define mock.On call // - cfg *config.EdgeBuilderConfig -func (_e *Builder_Expecter) Initialize(cfg interface{}) *Builder_Initialize_Call { - return &Builder_Initialize_Call{Call: _e.mock.On("Initialize", cfg)} +// - runtime *config.DynamicConfig +func (_e *Builder_Expecter) Initialize(cfg interface{}, runtime interface{}) *Builder_Initialize_Call { + return &Builder_Initialize_Call{Call: _e.mock.On("Initialize", cfg, runtime)} } -func (_c *Builder_Initialize_Call) Run(run func(cfg *config.EdgeBuilderConfig)) *Builder_Initialize_Call { +func (_c *Builder_Initialize_Call) Run(run func(cfg *config.EdgeBuilderConfig, runtime *config.DynamicConfig)) *Builder_Initialize_Call { _c.Call.Run(func(args mock.Arguments) { - run(args[0].(*config.EdgeBuilderConfig)) + run(args[0].(*config.EdgeBuilderConfig), args[1].(*config.DynamicConfig)) }) return _c } @@ -108,7 +109,7 @@ func (_c *Builder_Initialize_Call) Return(_a0 error) *Builder_Initialize_Call { return _c } -func (_c *Builder_Initialize_Call) RunAndReturn(run func(*config.EdgeBuilderConfig) error) *Builder_Initialize_Call { +func (_c *Builder_Initialize_Call) RunAndReturn(run func(*config.EdgeBuilderConfig, *config.DynamicConfig) error) *Builder_Initialize_Call { _c.Call.Return(run) return _c } diff --git a/pkg/telemetry/log/logger.go b/pkg/telemetry/log/logger.go index 9ef249339..8264aac05 100644 --- a/pkg/telemetry/log/logger.go +++ b/pkg/telemetry/log/logger.go @@ -2,6 +2,7 @@ package log import ( "context" + "os" "strconv" "sync" @@ -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, @@ -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} } @@ -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{} + } +} diff --git a/pkg/telemetry/profiler/profiler.go b/pkg/telemetry/profiler/profiler.go index 2a146cf96..aab885ca4 100644 --- a/pkg/telemetry/profiler/profiler.go +++ b/pkg/telemetry/profiler/profiler.go @@ -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, diff --git a/pkg/telemetry/tracer/tracer.go b/pkg/telemetry/tracer/tracer.go index 89dfb3343..6c59b6e72 100644 --- a/pkg/telemetry/tracer/tracer.go +++ b/pkg/telemetry/tracer/tracer.go @@ -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),