From c8c54cadf02e48e2af0e7206b462ab5819cad15a Mon Sep 17 00:00:00 2001 From: Thomas Labarussias Date: Sat, 25 Jan 2025 00:56:45 +0100 Subject: [PATCH] use a global method for the logs Signed-off-by: Thomas Labarussias --- config.go | 41 ++++++++------- handlers.go | 15 +++--- internal/pkg/batcher/batcher_test.go | 4 +- internal/pkg/utils/utils.go | 37 ++++++++++++++ main.go | 70 +++++++++++++------------- otlpmetrics.go | 12 +++-- outputs/alertmanager.go | 4 +- outputs/aws.go | 58 ++++++++++----------- outputs/awssecuritylake.go | 32 ++++++------ outputs/azure.go | 25 +++++----- outputs/client.go | 75 +++++++++++++--------------- outputs/cliq.go | 9 ++-- outputs/cloudevents.go | 12 +++-- outputs/datadog.go | 7 +-- outputs/datadog_logs.go | 10 ++-- outputs/discord.go | 7 +-- outputs/dynatrace.go | 7 +-- outputs/dynatrace_test.go | 4 +- outputs/elasticsearch.go | 27 +++++----- outputs/fission.go | 14 +++--- outputs/gcp.go | 45 ++++++++--------- outputs/gcpcloudrun.go | 7 +-- outputs/googlechat.go | 10 ++-- outputs/gotify.go | 9 ++-- outputs/grafana.go | 9 ++-- outputs/influxdb.go | 7 +-- outputs/kafka.go | 26 +++++----- outputs/kafkarest.go | 7 +-- outputs/kubeless.go | 14 +++--- outputs/loki.go | 9 ++-- outputs/mattermost.go | 10 ++-- outputs/mqtt.go | 15 +++--- outputs/n8n.go | 7 +-- outputs/nats.go | 15 +++--- outputs/nodered.go | 7 +-- outputs/openfaas.go | 14 +++--- outputs/openobserve.go | 7 +-- outputs/opsgenie.go | 7 +-- outputs/otlp_traces.go | 17 ++++--- outputs/otlp_traces_init.go | 7 +-- outputs/otlp_traces_test.go | 3 +- outputs/otlpmetrics/otlpmetrics.go | 11 ++-- outputs/pagerduty.go | 8 +-- outputs/policyreport.go | 42 ++++++++-------- outputs/quickwit.go | 11 ++-- outputs/rabbitmq.go | 18 ++++--- outputs/redis.go | 16 +++--- outputs/rocketchat.go | 10 ++-- outputs/slack.go | 10 ++-- outputs/smtp.go | 21 ++++---- outputs/spyderbat.go | 19 +++---- outputs/stan.go | 13 +++-- outputs/statsd.go | 15 +++--- outputs/sumologic.go | 9 ++-- outputs/syslog.go | 11 ++-- outputs/talon.go | 5 +- outputs/teams.go | 7 +-- outputs/tekton.go | 4 +- outputs/telegram.go | 9 ++-- outputs/timescaledb.go | 15 +++--- outputs/wavefront.go | 16 +++--- outputs/webex.go | 9 ++-- outputs/webhook.go | 7 +-- outputs/webui.go | 4 +- outputs/yandex.go | 20 ++++---- outputs/zincsearch.go | 7 +-- stats_prometheus.go | 7 +-- types/types.go | 4 +- 68 files changed, 566 insertions(+), 484 deletions(-) create mode 100644 internal/pkg/utils/utils.go diff --git a/config.go b/config.go index 76689dd02..dae4c360d 100644 --- a/config.go +++ b/config.go @@ -4,7 +4,6 @@ package main import ( "fmt" - "log" "net" "os" "path" @@ -16,11 +15,11 @@ import ( "text/template" "time" - "github.com/falcosecurity/falcosidekick/outputs/otlpmetrics" - kingpin "github.com/alecthomas/kingpin/v2" "github.com/spf13/viper" + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" + "github.com/falcosecurity/falcosidekick/outputs/otlpmetrics" "github.com/falcosecurity/falcosidekick/types" ) @@ -425,7 +424,7 @@ func init() { // Merge http outputs defaults with other outputs defaults if _, ok := outputDefaults[name]; ok { - panic(fmt.Sprintf("key %v already set in the output defaults", name)) + utils.Log(utils.FatalLvl, "", fmt.Sprintf("key %v already set in the output defaults", name)) } outputDefaults[name] = dst } @@ -599,7 +598,7 @@ func getConfig() *types.Configuration { v.AddConfigPath(d) err := v.ReadInConfig() if err != nil { - log.Printf("[ERROR] : Error when reading config file : %v\n", err) + utils.Log(utils.ErrorLvl, "", fmt.Sprintf("Error when reading config file : %v", err)) } } v.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) @@ -622,7 +621,7 @@ func getConfig() *types.Configuration { c.Elasticsearch.CustomHeaders = v.GetStringMapString("Elasticsearch.CustomHeaders") if err := v.Unmarshal(c); err != nil { - log.Printf("[ERROR] : Error unmarshalling config : %s", err) + utils.Log(utils.ErrorLvl, "", fmt.Sprintf("Error unmarshalling config : %s", err)) } if value, present := os.LookupEnv("TLSSERVER_NOTLSPATHS"); present { @@ -642,7 +641,7 @@ func getConfig() *types.Configuration { if s := os.Getenv(tagkeys[1][1:]); s != "" { c.Customfields[tagkeys[0]] = s } else { - log.Printf("[ERROR] : Can't find env var %v for custom fields", tagkeys[1][1:]) + utils.Log(utils.ErrorLvl, "", fmt.Sprintf("Can't find env var %v for custom fields", tagkeys[1][1:])) } } else { c.Customfields[tagkeys[0]] = tagkeys[1] @@ -657,7 +656,7 @@ func getConfig() *types.Configuration { tagkeys := strings.Split(label, ":") if len(tagkeys) == 2 { if _, err := template.New("").Parse(tagkeys[1]); err != nil { - log.Printf("[ERROR] : Error parsing templated fields %v : %s", tagkeys[0], err) + utils.Log(utils.ErrorPrefix, "", fmt.Sprintf("Error parsing templated fields %v : %s", tagkeys[0], err)) } else { c.Templatedfields[tagkeys[0]] = tagkeys[1] } @@ -693,7 +692,7 @@ func getConfig() *types.Configuration { labelName, labelValue, found := strings.Cut(labelData, ":") labelName, labelValue = strings.TrimSpace(labelName), strings.TrimSpace(labelValue) if !promKVNameRegex.MatchString(labelName) { - log.Printf("[ERROR] : AlertManager - Extra label name '%v' is not valid", labelName) + utils.Log(utils.ErrorLvl, "", fmt.Sprintf("AlertManager - Extra label name '%v' is not valid", labelName)) } else if found { c.Alertmanager.ExtraLabels[labelName] = labelValue } else { @@ -708,7 +707,7 @@ func getConfig() *types.Configuration { annotationName, annotationValue, found := strings.Cut(annotationData, ":") annotationName, annotationValue = strings.TrimSpace(annotationName), strings.TrimSpace(annotationValue) if !promKVNameRegex.MatchString(annotationName) { - log.Printf("[ERROR] : AlertManager - Extra annotation name '%v' is not valid", annotationName) + utils.Log(utils.ErrorLvl, "", fmt.Sprintf("AlertManager - Extra annotation name '%v' is not valid", annotationName)) } else if found { c.Alertmanager.ExtraAnnotations[annotationName] = annotationValue } else { @@ -723,12 +722,12 @@ func getConfig() *types.Configuration { priorityString, severityValue, found := strings.Cut(severitymatch, ":") priority := types.Priority(priorityString) if priority == types.Default { - log.Printf("[ERROR] : AlertManager - Priority '%v' is not a valid falco priority level", priorityString) + utils.Log(utils.ErrorLvl, "", fmt.Sprintf("AlertManager - Priority '%v' is not a valid falco priority level", priorityString)) continue } else if found { c.Alertmanager.CustomSeverityMap[priority] = strings.TrimSpace(severityValue) } else { - log.Printf("[ERROR] : AlertManager - No severity given to '%v' (tuple extracted: '%v')", priorityString, severitymatch) + utils.Log(utils.ErrorLvl, "", fmt.Sprintf("AlertManager - No severity given to '%v' (tuple extracted: '%v')", priorityString, severitymatch)) } } } @@ -763,7 +762,7 @@ func getConfig() *types.Configuration { envName, envValue, found := strings.Cut(extraEnvVarData, ":") envName, envValue = strings.TrimSpace(envName), strings.TrimSpace(envValue) if !promKVNameRegex.MatchString(envName) { - log.Printf("[ERROR] : OTLPTraces - Extra Env Var name '%v' is not valid", envName) + utils.Log(utils.ErrorLvl, "", fmt.Sprintf("OTLPTraces - Extra Env Var name '%v' is not valid", envName)) } else if found { c.OTLP.Traces.ExtraEnvVars[envName] = envValue } else { @@ -778,7 +777,7 @@ func getConfig() *types.Configuration { envName, envValue, found := strings.Cut(extraEnvVarData, ":") envName, envValue = strings.TrimSpace(envName), strings.TrimSpace(envValue) if !promKVNameRegex.MatchString(envName) { - log.Printf("[ERROR] : OTLPMetrics - Extra Env Var name '%v' is not valid", envName) + utils.Log(utils.ErrorLvl, "", fmt.Sprintf("OTLPMetrics - Extra Env Var name '%v' is not valid", envName)) } else if found { c.OTLP.Metrics.ExtraEnvVars[envName] = envValue } else { @@ -795,15 +794,15 @@ func getConfig() *types.Configuration { } if c.ListenPort == 0 || c.ListenPort > 65536 { - log.Fatalf("[ERROR] : Bad listening port number\n") + utils.Log(utils.FatalLvl, "", "Bad listening port number") } if c.TLSServer.NoTLSPort == 0 || c.TLSServer.NoTLSPort > 65536 { - log.Fatalf("[ERROR] : Bad noTLS server port number\n") + utils.Log(utils.FatalLvl, "", "Bad noTLS server port number") } if ip := net.ParseIP(c.ListenAddress); c.ListenAddress != "" && ip == nil { - log.Fatalf("[ERROR] : Failed to parse ListenAddress") + utils.Log(utils.FatalLvl, "", "Failed to parse ListenAddress") } if c.Loki.ExtraLabels != "" { @@ -840,17 +839,17 @@ func getConfig() *types.Configuration { for _, threshold := range thresholds { values := strings.SplitN(threshold, ":", 2) if len(values) != 2 { - log.Printf("[ERROR] : AlertManager - Fail to parse threshold - No priority given for threshold %v", threshold) + utils.Log(utils.ErrorLvl, "AlertManager", fmt.Sprintf("Fail to parse threshold - No priority given for threshold %v", threshold)) continue } valueString := strings.TrimSpace(values[0]) valueInt, err := strconv.ParseInt(valueString, 10, 64) if len(values) != 2 || err != nil { - log.Printf("[ERROR] : AlertManager - Fail to parse threshold - Atoi fail %v", threshold) + utils.Log(utils.ErrorLvl, "AlertManager", fmt.Sprintf("Fail to parse threshold - Atoi fail %v", threshold)) continue } if p := strings.TrimSpace(values[1]); p == "" { - log.Printf("[ERROR] : AlertManager - Priority '%v' is not a valid falco priority level", p) + utils.Log(utils.ErrorLvl, "AlertManager", fmt.Sprintf("Priority '%v' is not a valid falco priority level", p)) continue } priority := types.Priority(strings.TrimSpace(values[1])) @@ -949,7 +948,7 @@ func getMessageFormatTemplate(output, temp string) *template.Template { var err error t, err := template.New(output).Parse(temp) if err != nil { - log.Fatalf("[ERROR] : Error compiling %v message template : %v\n", output, err) + utils.Log(utils.FatalLvl, "", fmt.Sprintf("Error compiling %v message template : %v", output, err)) } return t } diff --git a/handlers.go b/handlers.go index e34b0e40b..646d2ee14 100644 --- a/handlers.go +++ b/handlers.go @@ -6,9 +6,7 @@ import ( "bytes" "encoding/json" "fmt" - "go.opentelemetry.io/otel/attribute" "io" - "log" "net/http" "sort" "strconv" @@ -16,10 +14,13 @@ import ( "text/template" "time" - "github.com/falcosecurity/falcosidekick/types" "github.com/google/uuid" + "go.opentelemetry.io/otel/attribute" "golang.org/x/text/cases" "golang.org/x/text/language" + + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" + "github.com/falcosecurity/falcosidekick/types" ) const ( @@ -90,7 +91,7 @@ func healthHandler(w http.ResponseWriter, r *http.Request) { // testHandler sends a test event to all enabled outputs. func testHandler(w http.ResponseWriter, r *http.Request) { - r.Body = io.NopCloser(bytes.NewReader([]byte(`{"output":"This is a test from falcosidekick","priority":"Debug","hostname": "falcosidekick", "rule":"Test rule", "time":"` + time.Now().UTC().Format(time.RFC3339) + `","output_fields": {"proc.name":"falcosidekick","user.name":"falcosidekick"}, "tags":["test","example"]}`))) + r.Body = io.NopCloser(bytes.NewReader([]byte(`{"output":"This is a test from falcosidekick","source":"debug","priority":"Debug","hostname":"falcosidekick", "rule":"Test rule","time":"` + time.Now().UTC().Format(time.RFC3339) + `","output_fields":{"proc.name":"falcosidekick","user.name":"falcosidekick"},"tags":["test","example"]}`))) mainHandler(w, r) } @@ -148,12 +149,12 @@ func newFalcoPayload(payload io.Reader) (types.FalcoPayload, error) { for key, value := range config.Templatedfields { tmpl, err := template.New("").Parse(value) if err != nil { - log.Printf("[ERROR] : Parsing error for templated field '%v': %v\n", key, err) + utils.Log(utils.ErrorLvl, "", fmt.Sprintf("Parsing error for templated field '%v': %v", key, err)) continue } v := new(bytes.Buffer) if err := tmpl.Execute(v, falcopayload.OutputFields); err != nil { - log.Printf("[ERROR] : Parsing error for templated field '%v': %v\n", key, err) + utils.Log(utils.ErrorLvl, "", fmt.Sprintf("Parsing error for templated field '%v': %v", key, err)) } templatedFields += key + "=" + v.String() + " " falcopayload.OutputFields[key] = v.String() @@ -281,7 +282,7 @@ func newFalcoPayload(payload io.Reader) (types.FalcoPayload, error) { } if config.Debug { - log.Printf("[DEBUG] : Falco's payload : %v\n", falcopayload.String()) + utils.Log(utils.DebugLvl, "", fmt.Sprintf("Falco's payload : %v", falcopayload.String())) } return falcopayload, nil diff --git a/internal/pkg/batcher/batcher_test.go b/internal/pkg/batcher/batcher_test.go index 314db15a4..59c6736f5 100644 --- a/internal/pkg/batcher/batcher_test.go +++ b/internal/pkg/batcher/batcher_test.go @@ -8,10 +8,10 @@ import ( "testing" "time" - "github.com/falcosecurity/falcosidekick/types" - "github.com/google/go-cmp/cmp" "github.com/google/uuid" + + "github.com/falcosecurity/falcosidekick/types" ) func TestElasticsearchBatcher(t *testing.T) { diff --git a/internal/pkg/utils/utils.go b/internal/pkg/utils/utils.go new file mode 100644 index 000000000..8cdd83f3b --- /dev/null +++ b/internal/pkg/utils/utils.go @@ -0,0 +1,37 @@ +// SPDX-License-Identifier: MIT OR Apache-2.0 + +package utils + +import "fmt" + +const ( + InfoLvl string = "info" + InfoPrefix string = "[INFO] " + ErrorLvl string = "error" + ErrorPrefix string = "[ERROR]" + DebugLvl string = "debug" + DebugPrefix string = "[DEBUG]" + WarningLvl string = "warning" + WarningPrefix string = "[WARN] " + FatalLvl string = "fatal" + FatalPrefix string = "[FATAL]" +) + +func Log(level, output, msg string) { + var prefix string + switch level { + case InfoLvl: + prefix = InfoPrefix + case ErrorLvl: + prefix = ErrorPrefix + case DebugLvl: + prefix = DebugPrefix + case WarningLvl: + prefix = WarningPrefix + } + if output != "" { + fmt.Printf("%v : %v - %v", prefix, output, msg) + } else { + fmt.Printf("%v : %v", prefix, msg) + } +} diff --git a/main.go b/main.go index 3322ba0f3..c8b0fd979 100644 --- a/main.go +++ b/main.go @@ -7,8 +7,6 @@ import ( "crypto/tls" "crypto/x509" "fmt" - "github.com/falcosecurity/falcosidekick/outputs/otlpmetrics" - "log" "net/http" "net/url" "os" @@ -18,10 +16,11 @@ import ( "github.com/DataDog/datadog-go/statsd" "github.com/embano1/memlog" - "github.com/prometheus/client_golang/prometheus/promhttp" + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" "github.com/falcosecurity/falcosidekick/outputs" + "github.com/falcosecurity/falcosidekick/outputs/otlpmetrics" "github.com/falcosecurity/falcosidekick/types" ) @@ -564,7 +563,7 @@ func init() { var err error kubelessClient, err = outputs.NewKubelessClient(config, stats, promStats, otlpMetrics, statsdClient, dogstatsdClient) if err != nil { - log.Printf("[ERROR] : Kubeless - %v\n", err) + utils.Log(utils.ErrorLvl, kubelessClient.OutputType, err.Error()) config.Kubeless.Namespace = "" config.Kubeless.Function = "" } else { @@ -596,7 +595,7 @@ func init() { var err error openfaasClient, err = outputs.NewOpenfaasClient(config, stats, promStats, otlpMetrics, statsdClient, dogstatsdClient) if err != nil { - log.Printf("[ERROR] : OpenFaaS - %v\n", err) + utils.Log(utils.ErrorLvl, openfaasClient.OutputType, err.Error()) } else { outputs.EnabledOutputs = append(outputs.EnabledOutputs, "OpenFaaS") } @@ -606,7 +605,7 @@ func init() { var err error tektonClient, err = outputs.NewClient("Tekton", config.Tekton.EventListener, config.Tekton.CommonConfig, *initClientArgs) if err != nil { - log.Printf("[ERROR] : Tekton - %v\n", err) + utils.Log(utils.ErrorLvl, tektonClient.OutputType, err.Error()) } else { outputs.EnabledOutputs = append(outputs.EnabledOutputs, "Tekton") } @@ -626,7 +625,7 @@ func init() { var err error wavefrontClient, err = outputs.NewWavefrontClient(config, stats, promStats, otlpMetrics, statsdClient, dogstatsdClient) if err != nil { - log.Printf("[ERROR] : Wavefront - %v\n", err) + utils.Log(utils.ErrorLvl, wavefrontClient.OutputType, err.Error()) config.Wavefront.EndpointHost = "" } else { outputs.EnabledOutputs = append(outputs.EnabledOutputs, "Wavefront") @@ -637,7 +636,7 @@ func init() { var err error fissionClient, err = outputs.NewFissionClient(config, stats, promStats, otlpMetrics, statsdClient, dogstatsdClient) if err != nil { - log.Printf("[ERROR] : Fission - %v\n", err) + utils.Log(utils.ErrorLvl, fissionClient.OutputType, err.Error()) } else { outputs.EnabledOutputs = append(outputs.EnabledOutputs, outputs.Fission) } @@ -672,7 +671,7 @@ func init() { yandexClient, err = outputs.NewYandexClient(config, stats, promStats, otlpMetrics, statsdClient, dogstatsdClient) if err != nil { config.Yandex.S3.Bucket = "" - log.Printf("[ERROR] : Yandex - %v\n", err) + utils.Log(utils.ErrorLvl, yandexClient.OutputType, err.Error()) } else { if config.Yandex.S3.Bucket != "" { outputs.EnabledOutputs = append(outputs.EnabledOutputs, "YandexS3") @@ -685,7 +684,7 @@ func init() { yandexClient, err = outputs.NewYandexClient(config, stats, promStats, otlpMetrics, statsdClient, dogstatsdClient) if err != nil { config.Yandex.DataStreams.StreamName = "" - log.Printf("[ERROR] : Yandex - %v\n", err) + utils.Log(utils.ErrorLvl, yandexClient.OutputType, err.Error()) } else { if config.Yandex.DataStreams.StreamName != "" { outputs.EnabledOutputs = append(outputs.EnabledOutputs, "YandexDataStreams") @@ -698,7 +697,7 @@ func init() { syslogClient, err = outputs.NewSyslogClient(config, stats, promStats, otlpMetrics, statsdClient, dogstatsdClient) if err != nil { config.Syslog.Host = "" - log.Printf("[ERROR] : Syslog - %v\n", err) + utils.Log(utils.ErrorLvl, syslogClient.OutputType, err.Error()) } else { outputs.EnabledOutputs = append(outputs.EnabledOutputs, "Syslog") } @@ -709,7 +708,7 @@ func init() { mqttClient, err = outputs.NewMQTTClient(config, stats, promStats, otlpMetrics, statsdClient, dogstatsdClient) if err != nil { config.MQTT.Broker = "" - log.Printf("[ERROR] : MQTT - %v\n", err) + utils.Log(utils.ErrorLvl, mqttClient.OutputType, err.Error()) } else { outputs.EnabledOutputs = append(outputs.EnabledOutputs, "MQTT") } @@ -742,7 +741,7 @@ func init() { spyderbatClient, err = outputs.NewSpyderbatClient(config, stats, promStats, otlpMetrics, statsdClient, dogstatsdClient) if err != nil { config.Spyderbat.OrgUID = "" - log.Printf("[ERROR] : Spyderbat - %v\n", err) + utils.Log(utils.ErrorLvl, spyderbatClient.OutputType, err.Error()) } else { outputs.EnabledOutputs = append(outputs.EnabledOutputs, "Spyderbat") } @@ -753,7 +752,7 @@ func init() { timescaleDBClient, err = outputs.NewTimescaleDBClient(config, stats, promStats, otlpMetrics, statsdClient, dogstatsdClient) if err != nil { config.TimescaleDB.Host = "" - log.Printf("[ERROR] : TimescaleDB - %v\n", err) + utils.Log(utils.ErrorLvl, timescaleDBClient.OutputType, err.Error()) } else { outputs.EnabledOutputs = append(outputs.EnabledOutputs, "TimescaleDB") } @@ -779,7 +778,7 @@ func init() { config.Telegram.ChatID = "" config.Telegram.Token = "" - log.Printf("[ERROR] : Telegram - %v\n", err) + utils.Log(utils.ErrorLvl, telegramClient.OutputType, err.Error()) } else { outputs.EnabledOutputs = append(outputs.EnabledOutputs, "Telegram") } @@ -809,7 +808,7 @@ func init() { if config.Dynatrace.APIToken != "" && config.Dynatrace.APIUrl != "" { var err error dynatraceApiUrl := strings.TrimRight(config.Dynatrace.APIUrl, "/") + "/v2/logs/ingest" - dynatraceClient, err = outputs.NewClient("Dynatrace", dynatraceApiUrl, types.CommonConfig{CheckCert: config.Dynatrace.CheckCert}, *initClientArgs) + dynatraceClient, err = outputs.NewClient("Dynatrace,", dynatraceApiUrl, types.CommonConfig{CheckCert: config.Dynatrace.CheckCert}, *initClientArgs) if err != nil { config.Dynatrace.APIToken = "" config.Dynatrace.APIUrl = "" @@ -837,7 +836,7 @@ func init() { outputs.EnabledOutputs = append(outputs.EnabledOutputs, "OTLPMetrics") fn := func() { if err := shutDownFunc(context.TODO()); err != nil { - log.Printf("[ERROR] : OTLP Metrics - Error: %v\n", err) + utils.Log(utils.ErrorLvl, "OTLP Metrics", err.Error()) } } shutDownFuncs = append(shutDownFuncs, fn) @@ -854,8 +853,8 @@ func init() { } } - log.Printf("[INFO] : Falco Sidekick version: %s\n", GetVersionInfo().GitVersion) - log.Printf("[INFO] : Enabled Outputs : %s\n", outputs.EnabledOutputs) + utils.Log(utils.InfoLvl, "", fmt.Sprintf("Falco Sidekick version: %s", GetVersionInfo().GitVersion)) + utils.Log(utils.InfoLvl, "", fmt.Sprintf("Enabled Outputs: %s", outputs.EnabledOutputs)) } @@ -864,7 +863,7 @@ func main() { defer shutdown() } if config.Debug { - log.Printf("[INFO] : Debug mode : %v", config.Debug) + utils.Log(utils.InfoPrefix, "", fmt.Sprintf("Debug mode: %v", config.Debug)) } routes := map[string]http.Handler{ @@ -886,11 +885,11 @@ func main() { if ok { delete(routes, r) if config.Debug { - log.Printf("[DEBUG] : %s is served on http", r) + utils.Log(utils.DebugLvl, "", fmt.Sprintf("%s is served on http", r)) } HTTPServeMux.Handle(r, handler) } else { - log.Printf("[WARN] : tlsserver.notlspaths has unknown path '%s'", r) + utils.Log(utils.WarningLvl, "", fmt.Sprintf("tlsserver.notlspaths has unknown path '%s'", r)) } } } @@ -913,12 +912,12 @@ func main() { if config.TLSServer.Deploy { if config.TLSServer.MutualTLS { if config.Debug { - log.Printf("[DEBUG] : running mTLS server") + utils.Log(utils.DebugLvl, "", "running mTLS server") } caCert, err := os.ReadFile(config.TLSServer.CaCertFile) if err != nil { - log.Printf("[ERROR] : %v\n", err.Error()) + utils.Log(utils.ErrorLvl, "", err.Error()) } caCertPool := x509.NewCertPool() caCertPool.AppendCertsFromPEM(caCert) @@ -932,16 +931,16 @@ func main() { } if config.Debug && !config.TLSServer.MutualTLS { - log.Printf("[DEBUG] : running TLS server") + utils.Log(utils.DebugLvl, "", "running TLS server") } if len(config.TLSServer.NoTLSPaths) == 0 { - log.Printf("[WARN] : tlsserver.deploy is true but tlsserver.notlspaths is empty, change tlsserver.deploy to true to deploy two servers, at least for /ping endpoint") + utils.Log(utils.WarningLvl, "", "tlsserver.deploy is true but tlsserver.notlspaths is empty, change tlsserver.deploy to true to deploy two servers, at least for /ping endpoint") } if len(config.TLSServer.NoTLSPaths) != 0 { if config.Debug { - log.Printf("[DEBUG] : running HTTP server for endpoints defined in tlsserver.notlspaths") + utils.Log(utils.DebugLvl, "", "running HTTP server for endpoints defined in tlsserver.notlspaths") } httpServer := &http.Server{ @@ -953,30 +952,31 @@ func main() { WriteTimeout: 60 * time.Second, IdleTimeout: 60 * time.Second, } - log.Printf("[INFO] : Falcosidekick is up and listening on %s:%d for TLS and %s:%d for non-TLS", config.ListenAddress, config.ListenPort, config.ListenAddress, config.TLSServer.NoTLSPort) + utils.Log(utils.InfoLvl, "", fmt.Sprintf("Falcosidekick is up and listening on %s:%d for TLS and %s:%d for non-TLS", config.ListenAddress, config.ListenPort, config.ListenAddress, config.TLSServer.NoTLSPort)) errs := make(chan error, 1) go serveTLS(server, errs) go serveHTTP(httpServer, errs) - log.Fatal(<-errs) + err := <-errs + utils.Log(utils.FatalLvl, "", err.Error()) } else { - log.Printf("[INFO] : Falcosidekick is up and listening on %s:%d", config.ListenAddress, config.ListenPort) + utils.Log(utils.InfoLvl, "", fmt.Sprintf("Falcosidekick is up and listening on %s:%d", config.ListenAddress, config.ListenPort)) if err := server.ListenAndServeTLS(config.TLSServer.CertFile, config.TLSServer.KeyFile); err != nil { - log.Fatalf("[ERROR] : %v", err.Error()) + utils.Log(utils.FatalLvl, "", err.Error()) } } } else { if config.Debug { - log.Printf("[DEBUG] : running HTTP server") + utils.Log(utils.DebugLvl, "", "running HTTP server") } if config.TLSServer.MutualTLS { - log.Printf("[WARN] : tlsserver.deploy is false but tlsserver.mutualtls is true, change tlsserver.deploy to true to use mTLS") + utils.Log(utils.WarningLvl, "", "tlsserver.deploy is false but tlsserver.mutualtls is true, change tlsserver.deploy to true to use mTLS") } - log.Printf("[INFO] : Falcosidekick is up and listening on %s:%d", config.ListenAddress, config.ListenPort) + utils.Log(utils.InfoLvl, "", fmt.Sprintf("Falcosidekick is up and listening on %s:%d", config.ListenAddress, config.ListenPort)) if err := server.ListenAndServe(); err != nil { - log.Fatalf("[ERROR] : %v", err.Error()) + utils.Log(utils.FatalLvl, "", err.Error()) } } } diff --git a/otlpmetrics.go b/otlpmetrics.go index b1e831955..9b93e857d 100644 --- a/otlpmetrics.go +++ b/otlpmetrics.go @@ -1,11 +1,13 @@ package main import ( - "github.com/falcosecurity/falcosidekick/outputs/otlpmetrics" - "github.com/falcosecurity/falcosidekick/types" - "log" + "fmt" "regexp" "strings" + + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" + "github.com/falcosecurity/falcosidekick/outputs/otlpmetrics" + "github.com/falcosecurity/falcosidekick/types" ) func newOTLPMetrics(config *types.Configuration) *otlpmetrics.OTLPMetrics { @@ -47,7 +49,7 @@ func newOTLPFalcoMatchesCounter(config *types.Configuration) otlpmetrics.Counter } for i := range config.Customfields { if !regOTLPLabels.MatchString(i) { - log.Printf("[ERROR] : Custom field '%v' is not a valid OTLP metric attribute name", i) + utils.Log(utils.ErrorLvl, "", fmt.Sprintf("Custom field '%v' is not a valid OTLP metric attribute name", i)) continue } supportedAttributes = append(supportedAttributes, i) @@ -55,7 +57,7 @@ func newOTLPFalcoMatchesCounter(config *types.Configuration) otlpmetrics.Counter for _, i := range config.OTLP.Metrics.ExtraAttributesList { if !regOTLPLabels.MatchString(strings.ReplaceAll(i, ".", "_")) { - log.Printf("[ERROR] : Extra field '%v' is not a valid OTLP metric attribute name", i) + utils.Log(utils.ErrorLvl, "", fmt.Sprintf("Extra field '%v' is not a valid OTLP metric attribute name", i)) continue } supportedAttributes = append(supportedAttributes, strings.ReplaceAll(i, ".", "_")) diff --git a/outputs/alertmanager.go b/outputs/alertmanager.go index 498b7399a..fb198a9eb 100644 --- a/outputs/alertmanager.go +++ b/outputs/alertmanager.go @@ -5,7 +5,6 @@ package outputs import ( "encoding/json" "fmt" - "log" "net/http" "regexp" "sort" @@ -15,6 +14,7 @@ import ( "go.opentelemetry.io/otel/attribute" + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" "github.com/falcosecurity/falcosidekick/types" ) @@ -168,7 +168,7 @@ func (c *Client) AlertmanagerPost(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "alertmanager", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "alertmanager"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : AlertManager - %v\n", err) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return } diff --git a/outputs/aws.go b/outputs/aws.go index a94a711ba..713b3a172 100644 --- a/outputs/aws.go +++ b/outputs/aws.go @@ -8,9 +8,6 @@ import ( "encoding/json" "errors" "fmt" - "github.com/falcosecurity/falcosidekick/outputs/otlpmetrics" - "go.opentelemetry.io/otel/attribute" - "log" "net/url" "os" "strings" @@ -30,7 +27,10 @@ import ( "github.com/aws/aws-sdk-go/service/sqs" "github.com/aws/aws-sdk-go/service/sts" "github.com/google/uuid" + "go.opentelemetry.io/otel/attribute" + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" + "github.com/falcosecurity/falcosidekick/outputs/otlpmetrics" "github.com/falcosecurity/falcosidekick/types" ) @@ -51,7 +51,7 @@ func NewAWSClient(config *types.Configuration, stats *types.Statistics, promStat var err error region, err = metaClient.Region() if err != nil { - log.Printf("[ERROR] : AWS - Error while getting region from Metadata AWS Session: %v\n", err.Error()) + utils.Log(utils.ErrorLvl, "AWS", fmt.Sprintf("Error while getting region from Metadata AWS Session: %v", err.Error())) return nil, errors.New("error getting region from metadata") } } @@ -61,7 +61,7 @@ func NewAWSClient(config *types.Configuration, stats *types.Statistics, promStat err2 := os.Setenv("AWS_SECRET_ACCESS_KEY", config.AWS.SecretAccessKey) err3 := os.Setenv("AWS_DEFAULT_REGION", region) if err1 != nil || err2 != nil || err3 != nil { - log.Println("[ERROR] : AWS - Error setting AWS env vars") + utils.Log(utils.ErrorLvl, "AWS", "Error setting AWS env vars") return nil, errors.New("error setting AWS env vars") } } @@ -79,7 +79,7 @@ func NewAWSClient(config *types.Configuration, stats *types.Statistics, promStat } assumedRole, err := stsSvc.AssumeRole(stsArIn) if err != nil { - log.Println("[ERROR] : AWS - Error while Assuming Role") + utils.Log(utils.ErrorLvl, "AWS", "Error while Assuming Role") return nil, errors.New("error while assuming role") } awscfg.Credentials = credentials.NewStaticCredentials( @@ -91,14 +91,14 @@ func NewAWSClient(config *types.Configuration, stats *types.Statistics, promStat sess, err := session.NewSession(awscfg) if err != nil { - log.Printf("[ERROR] : AWS - Error while creating AWS Session: %v\n", err.Error()) + utils.Log(utils.ErrorLvl, "AWS", fmt.Sprintf("Error while creating AWS Session: %v", err.Error())) return nil, errors.New("error while creating AWS Session") } if config.AWS.CheckIdentity { _, err = sts.New(sess).GetCallerIdentity(&sts.GetCallerIdentityInput{}) if err != nil { - log.Printf("[ERROR] : AWS - Error while getting AWS Token: %v\n", err.Error()) + utils.Log(utils.ErrorLvl, "AWS", fmt.Sprintf("Error while getting AWS Token: %v", err.Error())) return nil, errors.New("error while getting AWS Token") } } @@ -106,7 +106,7 @@ func NewAWSClient(config *types.Configuration, stats *types.Statistics, promStat var endpointURL *url.URL endpointURL, err = url.Parse(config.AWS.SQS.URL) if err != nil { - log.Printf("[ERROR] : AWS SQS - %v\n", err.Error()) + utils.Log(utils.ErrorLvl, "AWS SQS", err.Error()) return nil, ErrClientCreation } @@ -145,16 +145,16 @@ func (c *Client) InvokeLambda(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "awslambda", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "awslambda"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : %v Lambda - %v\n", c.OutputType, err.Error()) + utils.Log(utils.ErrorLvl, c.OutputType+" Lambda", err.Error()) return } if c.Config.Debug { r, _ := base64.StdEncoding.DecodeString(*resp.LogResult) - log.Printf("[DEBUG] : %v Lambda result : %v\n", c.OutputType, string(r)) + utils.Log(utils.DebugLvl, c.OutputType+" Lambda", fmt.Sprintf("result : %v", string(r))) } - log.Printf("[INFO] : %v Lambda - Invoke OK (%v)\n", c.OutputType, *resp.StatusCode) + utils.Log(utils.InfoLvl, c.OutputType+" Lambda", fmt.Sprintf("Invoke OK (%v)", *resp.StatusCode)) go c.CountMetric("outputs", 1, []string{"output:awslambda", "status:ok"}) c.Stats.AWSLambda.Add("ok", 1) c.PromStats.Outputs.With(map[string]string{"destination": "awslambda", "status": "ok"}).Inc() @@ -182,15 +182,15 @@ func (c *Client) SendMessage(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "awssqs", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "awssqs"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : %v SQS - %v\n", c.OutputType, err.Error()) + utils.Log(utils.ErrorLvl, c.OutputType+" SQS", err.Error()) return } if c.Config.Debug { - log.Printf("[DEBUG] : %v SQS - MD5OfMessageBody : %v\n", c.OutputType, *resp.MD5OfMessageBody) + utils.Log(utils.DebugLvl, c.OutputType+" SQS", fmt.Sprintf("MD5OfMessageBody : %v", *resp.MD5OfMessageBody)) } - log.Printf("[INFO] : %v SQS - Send Message OK (%v)\n", c.OutputType, *resp.MessageId) + utils.Log(utils.InfoLvl, c.OutputType+" SQS", fmt.Sprintf("Send Message OK (%v)", *resp.MessageId)) go c.CountMetric("outputs", 1, []string{"output:awssqs", "status:ok"}) c.Stats.AWSSQS.Add(OK, 1) c.PromStats.Outputs.With(map[string]string{"destination": "awssqs", "status": "ok"}).Inc() @@ -224,14 +224,14 @@ func (c *Client) UploadS3(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "awss3", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "awss3"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : %v S3 - %v\n", c.OutputType, err.Error()) + utils.Log(utils.ErrorLvl, c.OutputType+" S3", err.Error()) return } if resp.SSECustomerAlgorithm != nil { - log.Printf("[INFO] : %v S3 - Upload payload OK (%v)\n", c.OutputType, *resp.SSECustomerKeyMD5) + utils.Log(utils.InfoLvl, c.OutputType+" S3", fmt.Sprintf("Upload payload OK (%v)", *resp.SSECustomerKeyMD5)) } else { - log.Printf("[INFO] : %v S3 - Upload payload OK\n", c.OutputType) + utils.Log(utils.InfoLvl, c.OutputType+" S3", "Upload payload OK") } go c.CountMetric("outputs", 1, []string{"output:awss3", "status:ok"}) @@ -305,7 +305,7 @@ func (c *Client) PublishTopic(falcopayload types.FalcoPayload) { if c.Config.Debug { p, _ := json.Marshal(msg) - log.Printf("[DEBUG] : %v SNS - Message : %v\n", c.OutputType, string(p)) + utils.Log(utils.DebugLvl, c.OutputType+" SNS", fmt.Sprintf("Message : %v", string(p))) } c.Stats.AWSSNS.Add("total", 1) @@ -316,11 +316,11 @@ func (c *Client) PublishTopic(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "awssns", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "awssns"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : %v SNS - %v\n", c.OutputType, err.Error()) + utils.Log(utils.ErrorLvl, c.OutputType+" SNS", err.Error()) return } - log.Printf("[INFO] : %v SNS - Send to topic OK (%v)\n", c.OutputType, *resp.MessageId) + utils.Log(utils.DebugLvl, c.OutputType+" SNS", fmt.Sprintf("Send to topic OK (%v)", *resp.MessageId)) go c.CountMetric("outputs", 1, []string{"output:awssns", "status:ok"}) c.Stats.AWSSNS.Add(OK, 1) c.PromStats.Outputs.With(map[string]string{"destination": "awssns", "status": OK}).Inc() @@ -338,7 +338,7 @@ func (c *Client) SendCloudWatchLog(falcopayload types.FalcoPayload) { if c.Config.AWS.CloudWatchLogs.LogStream == "" { streamName := "falcosidekick-logstream" - log.Printf("[INFO] : %v CloudWatchLogs - Log Stream not configured creating one called %s\n", c.OutputType, streamName) + utils.Log(utils.InfoLvl, c.OutputType+" CloudWatchLogs", fmt.Sprintf("Log Stream not configured creating one called %s", streamName)) inputLogStream := &cloudwatchlogs.CreateLogStreamInput{ LogGroupName: aws.String(c.Config.AWS.CloudWatchLogs.LogGroup), LogStreamName: aws.String(streamName), @@ -347,14 +347,14 @@ func (c *Client) SendCloudWatchLog(falcopayload types.FalcoPayload) { _, err := svc.CreateLogStream(inputLogStream) if err != nil { if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == cloudwatchlogs.ErrCodeResourceAlreadyExistsException { - log.Printf("[INFO] : %v CloudWatchLogs - Log Stream %s already exist, reusing...\n", c.OutputType, streamName) + utils.Log(utils.InfoLvl, c.OutputType+" CloudWatchLogs", fmt.Sprintf("Log Stream %s already exist, reusing...", streamName)) } else { go c.CountMetric("outputs", 1, []string{"output:awscloudwatchlogs", "status:error"}) c.Stats.AWSCloudWatchLogs.Add(Error, 1) c.PromStats.Outputs.With(map[string]string{"destination": "awscloudwatchlogs", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "awscloudwatchlogs"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : %v CloudWatchLogs - %v\n", c.OutputType, err.Error()) + utils.Log(utils.ErrorLvl, c.OutputType+" CloudWatchLogs", err.Error()) return } } @@ -381,11 +381,11 @@ func (c *Client) SendCloudWatchLog(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "awscloudwatchlogs", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "awscloudwatchlogs"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : %v CloudWatchLogs - %v\n", c.OutputType, err.Error()) + utils.Log(utils.ErrorLvl, c.OutputType+" CloudWatchLogs", err.Error()) return } - log.Printf("[INFO] : %v CloudWatchLogs - Send Log OK (%v)\n", c.OutputType, resp.String()) + utils.Log(utils.InfoLvl, c.OutputType+" CloudWatchLogs", fmt.Sprintf("Send Log OK (%v)", resp.String())) go c.CountMetric("outputs", 1, []string{"output:awscloudwatchlogs", "status:ok"}) c.Stats.AWSCloudWatchLogs.Add(OK, 1) c.PromStats.Outputs.With(map[string]string{"destination": "awscloudwatchlogs", "status": OK}).Inc() @@ -398,7 +398,7 @@ func (c *Client) putLogEvents(svc *cloudwatchlogs.CloudWatchLogs, input *cloudwa resp, err := svc.PutLogEvents(input) if err != nil { if exception, ok := err.(*cloudwatchlogs.InvalidSequenceTokenException); ok { - log.Printf("[INFO] : %v Refreshing token for LogGroup: %s LogStream: %s", c.OutputType, *input.LogGroupName, *input.LogStreamName) + utils.Log(utils.InfoLvl, c.OutputType+" CloudWatchLogs", fmt.Sprintf("Refreshing token for LogGroup: %s LogStream: %s", *input.LogGroupName, *input.LogStreamName)) input.SequenceToken = exception.ExpectedSequenceToken return c.putLogEvents(svc, input) @@ -430,11 +430,11 @@ func (c *Client) PutRecord(falcoPayLoad types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "awskinesis", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "awskinesis"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : %v Kinesis - %v\n", c.OutputType, err.Error()) + utils.Log(utils.ErrorLvl, c.OutputType+" Kinesis", err.Error()) return } - log.Printf("[INFO] : %v Kinesis - Put Record OK (%v)\n", c.OutputType, resp.SequenceNumber) + utils.Log(utils.InfoLvl, c.OutputType+" Kinesis", fmt.Sprintf("Put Record OK (%v)", resp.SequenceNumber)) go c.CountMetric("outputs", 1, []string{"output:awskinesis", "status:ok"}) c.Stats.AWSKinesis.Add(OK, 1) c.PromStats.Outputs.With(map[string]string{"destination": "awskinesis", "status": "ok"}).Inc() diff --git a/outputs/awssecuritylake.go b/outputs/awssecuritylake.go index ffca2403c..a1137fb76 100644 --- a/outputs/awssecuritylake.go +++ b/outputs/awssecuritylake.go @@ -7,9 +7,7 @@ import ( "encoding/json" "errors" "fmt" - "go.opentelemetry.io/otel/attribute" "io" - "log" "time" "github.com/aws/aws-sdk-go/aws" @@ -18,7 +16,9 @@ import ( "github.com/google/uuid" "github.com/xitongsys/parquet-go-source/mem" "github.com/xitongsys/parquet-go/writer" + "go.opentelemetry.io/otel/attribute" + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" "github.com/falcosecurity/falcosidekick/types" ) @@ -206,10 +206,10 @@ func (c *Client) EnqueueSecurityLake(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "awssecuritylake.", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "awssecuritylake"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : %v SecurityLake - %v\n", c.OutputType, err) + utils.Log(utils.ErrorLvl, c.OutputType+" SecurityLake", err.Error()) return } - log.Printf("[INFO] : %v SecurityLake - Event queued (%v)\n", c.OutputType, falcopayload.UUID) + utils.Log(utils.InfoLvl, c.OutputType+" SecurityLake", fmt.Sprintf("Event queued (%v)", falcopayload.UUID)) *c.Config.AWS.SecurityLake.WriteOffset = offset } @@ -238,7 +238,7 @@ func (c *Client) processNextBatch() error { c.PromStats.Outputs.With(map[string]string{"destination": "awssecuritylake.", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "awssecuritylake"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : %v SecurityLake - %v\n", c.OutputType, err) + utils.Log(utils.ErrorLvl, c.OutputType+" SecurityLake", err.Error()) // ctx currently not handled in main // https://github.com/falcosecurity/falcosidekick/pull/390#discussion_r1081690326 return err @@ -259,7 +259,7 @@ func (c *Client) processNextBatch() error { earliest, err, ) - log.Printf("[ERROR] : %v SecurityLake - %v\n", c.OutputType, msg) + utils.Log(utils.ErrorLvl, c.OutputType+" SecurityLake", msg.Error()) awslake.ReadOffset = &earliest return err } @@ -271,7 +271,7 @@ func (c *Client) processNextBatch() error { c.PromStats.Outputs.With(map[string]string{"destination": "awssecuritylake.", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "awssecuritylake"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : %v SecurityLake - %v\n", c.OutputType, err) + utils.Log(utils.ErrorLvl, c.OutputType+" SecurityLake", err.Error()) return err } } @@ -316,42 +316,42 @@ func (c *Client) writeParquet(uid string, records []memlog.Record) error { ACL: aws.String(s3.ObjectCannedACLBucketOwnerFullControl), }) if err != nil { - log.Printf("[ERROR] : %v SecurityLake - Upload parquet file %s.parquet Failed: %v\n", c.OutputType, uid, err) + utils.Log(utils.ErrorLvl, c.OutputType+" SecurityLake", fmt.Sprintf("Upload parquet file %s.parquet Failed: %v", uid, err)) return err } if resp.SSECustomerAlgorithm != nil { - log.Printf("[INFO] : %v SecurityLake - Upload parquet file %s.parquet OK (%v) (%v events) \n", c.OutputType, uid, *resp.SSECustomerKeyMD5, len(records)) + utils.Log(utils.InfoLvl, c.OutputType+" SecurityLake", fmt.Sprintf("Upload parquet file %s.parquet OK (%v) (%v events)", uid, *resp.SSECustomerKeyMD5, len(records))) } else { - log.Printf("[INFO] : %v SecurityLake - Upload parquet file %s.parquet OK (%v events)\n", c.OutputType, uid, len(records)) + utils.Log(utils.InfoLvl, c.OutputType+" SecurityLake", fmt.Sprintf("Upload parquet file %s.parquet OK (%v events)\n", uid, len(records))) } return nil }) if err != nil { - log.Printf("[ERROR] : %v SecurityLake - Can't create the parquet file %s.parquet: %v\n", c.OutputType, uid, err) + utils.Log(utils.ErrorLvl, c.OutputType+" SecurityLake", fmt.Sprintf("Can't create the parquet file %s.parquet: %v", uid, err)) return err } pw, err := writer.NewParquetWriter(fw, new(OCSFSecurityFinding), 10) if err != nil { - log.Printf("[ERROR] : %v SecurityLake - Can't create the parquet writer: %v\n", c.OutputType, err) + utils.Log(utils.ErrorLvl, c.OutputType+" SecurityLake", fmt.Sprintf("Can't create the parquet writer: %v", err)) return err } for _, i := range records { var f types.FalcoPayload if err := json.Unmarshal(i.Data, &f); err != nil { - log.Printf("[ERROR] : %v SecurityLake - Unmarshalling error: %v\n", c.OutputType, err) + utils.Log(utils.ErrorLvl, c.OutputType+" SecurityLake", fmt.Sprintf("Unmarshalling error: %v", err)) continue } o := NewOCSFSecurityFinding(f) if err = pw.Write(o); err != nil { - log.Printf("[ERROR] : %v SecurityLake - Parquet writer error: %v\n", c.OutputType, err) + utils.Log(utils.ErrorLvl, c.OutputType+" SecurityLake", fmt.Sprintf("Parquet writer error: %v", err)) continue } } if err = pw.WriteStop(); err != nil { - log.Printf("[ERROR] : %v SecurityLake - Can't stop the parquet writer: %v\n", c.OutputType, err) + utils.Log(utils.ErrorLvl, c.OutputType+" SecurityLake", fmt.Sprintf("Can't stop the parquet writer: %v", err)) } if err = fw.Close(); err != nil { - log.Printf("[ERROR] : %v SecurityLake - Can't close the parquet file %s.parquet: %v\n", c.OutputType, uid, err) + utils.Log(utils.ErrorLvl, c.OutputType+" SecurityLake", fmt.Sprintf("Can't close the parquet file %s.parquet: %v", uid, err)) return err } return nil diff --git a/outputs/azure.go b/outputs/azure.go index b887149a1..51018e32e 100644 --- a/outputs/azure.go +++ b/outputs/azure.go @@ -5,15 +5,16 @@ package outputs import ( "context" "encoding/json" - "github.com/falcosecurity/falcosidekick/outputs/otlpmetrics" - "go.opentelemetry.io/otel/attribute" - "log" + "fmt" "time" "github.com/Azure/azure-sdk-for-go/sdk/azidentity" azeventhubs "github.com/Azure/azure-sdk-for-go/sdk/messaging/azeventhubs" "github.com/DataDog/datadog-go/statsd" + "go.opentelemetry.io/otel/attribute" + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" + "github.com/falcosecurity/falcosidekick/outputs/otlpmetrics" "github.com/falcosecurity/falcosidekick/types" ) @@ -38,48 +39,48 @@ func (c *Client) EventHubPost(falcopayload types.FalcoPayload) { ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) defer cancel() - log.Printf("[INFO] : %v EventHub - Try sending event", c.OutputType) + utils.Log(utils.InfoLvl, c.OutputType+" EventHub", "Try sending event") defaultAzureCred, err := azidentity.NewDefaultAzureCredential(nil) if err != nil { c.setEventHubErrorMetrics() - log.Printf("[ERROR] : %v EventHub - %v\n", c.OutputType, err.Error()) + utils.Log(utils.ErrorLvl, c.OutputType+" EventHub", err.Error()) return } producerClient, err := azeventhubs.NewProducerClient(c.Config.Azure.EventHub.Namespace, c.Config.Azure.EventHub.Name, defaultAzureCred, nil) if err != nil { c.setEventHubErrorMetrics() - log.Printf("[ERROR] : %v EventHub - %v\n", c.OutputType, err.Error()) + utils.Log(utils.ErrorLvl, c.OutputType+" EventHub", err.Error()) return } defer producerClient.Close(ctx) - log.Printf("[INFO] : %v EventHub - Hub client created\n", c.OutputType) + utils.Log(utils.InfoLvl, c.OutputType+" EventHub", "Hub client created") data, err := json.Marshal(falcopayload) if err != nil { c.setEventHubErrorMetrics() - log.Printf("[ERROR] : Cannot marshal payload: %v", err.Error()) + utils.Log(utils.ErrorLvl, c.OutputType+" EventHub", fmt.Sprintf("Cannot marshal payload: %v", err)) return } batch, err := producerClient.NewEventDataBatch(ctx, nil) if err != nil { c.setEventHubErrorMetrics() - log.Printf("[ERROR] : Cannot marshal payload: %v", err.Error()) + utils.Log(utils.ErrorLvl, c.OutputType+" EventHub", fmt.Sprintf("Cannot marshal payload: %v", err)) return } if err := batch.AddEventData(&azeventhubs.EventData{Body: data}, nil); err != nil { c.setEventHubErrorMetrics() - log.Printf("[ERROR] : Cannot marshal payload: %v", err.Error()) + utils.Log(utils.ErrorLvl, c.OutputType+" EventHub", fmt.Sprintf("Cannot marshal payload: %v", err)) return } producerClient.SendEventDataBatch(ctx, batch, nil) if err := producerClient.SendEventDataBatch(ctx, batch, nil); err != nil { c.setEventHubErrorMetrics() - log.Printf("[ERROR] : %v EventHub - %v\n", c.OutputType, err.Error()) + utils.Log(utils.ErrorLvl, c.OutputType+" EventHub", err.Error()) return } @@ -89,7 +90,7 @@ func (c *Client) EventHubPost(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "azureeventhub", "status": OK}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "azureeventhub"), attribute.String("status", OK)).Inc() - log.Printf("[INFO] : %v EventHub - Publish OK", c.OutputType) + utils.Log(utils.InfoLvl, c.OutputType+" EventHub", "Publish OK") } // setEventHubErrorMetrics set the error stats diff --git a/outputs/client.go b/outputs/client.go index b8efa3c98..c287526ea 100644 --- a/outputs/client.go +++ b/outputs/client.go @@ -11,10 +11,7 @@ import ( "encoding/json" "errors" "fmt" - "github.com/falcosecurity/falcosidekick/outputs/otlpmetrics" - "github.com/segmentio/kafka-go" "io" - "log" "math" "net/http" "net/url" @@ -23,25 +20,25 @@ import ( "strings" "sync" - "golang.org/x/sync/semaphore" - crdClient "sigs.k8s.io/wg-policy-prototypes/policy-report/pkg/generated/v1alpha2/clientset/versioned" - gcpfunctions "cloud.google.com/go/functions/apiv1" - amqp "github.com/rabbitmq/amqp091-go" - wavefront "github.com/wavefronthq/wavefront-sdk-go/senders" - "cloud.google.com/go/pubsub" "cloud.google.com/go/storage" "github.com/DataDog/datadog-go/statsd" "github.com/aws/aws-sdk-go/aws/session" cloudevents "github.com/cloudevents/sdk-go/v2" - "k8s.io/client-go/kubernetes" - mqtt "github.com/eclipse/paho.mqtt.golang" timescaledb "github.com/jackc/pgx/v5/pgxpool" + amqp "github.com/rabbitmq/amqp091-go" redis "github.com/redis/go-redis/v9" + "github.com/segmentio/kafka-go" + wavefront "github.com/wavefronthq/wavefront-sdk-go/senders" + "golang.org/x/sync/semaphore" + "k8s.io/client-go/kubernetes" + crdClient "sigs.k8s.io/wg-policy-prototypes/policy-report/pkg/generated/v1alpha2/clientset/versioned" "github.com/falcosecurity/falcosidekick/internal/pkg/batcher" + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" + "github.com/falcosecurity/falcosidekick/outputs/otlpmetrics" "github.com/falcosecurity/falcosidekick/types" ) @@ -151,16 +148,16 @@ type Client struct { func NewClient(outputType string, defaultEndpointURL string, cfg types.CommonConfig, params types.InitClientArgs) (*Client, error) { reg := regexp.MustCompile(`(http|nats)(s?)://.*`) if !reg.MatchString(defaultEndpointURL) { - log.Printf("[ERROR] : %v - %v\n", outputType, "Bad Endpoint") + utils.Log(utils.ErrorLvl, outputType, "Bad Endpoint") return nil, ErrClientCreation } if _, err := url.ParseRequestURI(defaultEndpointURL); err != nil { - log.Printf("[ERROR] : %v - %v\n", outputType, err.Error()) + utils.Log(utils.ErrorLvl, outputType, err.Error()) return nil, ErrClientCreation } endpointURL, err := url.Parse(defaultEndpointURL) if err != nil { - log.Printf("[ERROR] : %v - %v\n", outputType, err.Error()) + utils.Log(utils.ErrorLvl, outputType, err.Error()) return nil, ErrClientCreation } return &Client{ @@ -211,7 +208,7 @@ func (c *Client) getInlinedBodyAsString(resp *http.Response) string { if contentEncoding == "gzip" { dec, err := decompressData(body) if err != nil { - log.Printf("[INFO] : %v - Failed to decompress response: %v", c.OutputType, err) + utils.Log(utils.ErrorLvl, c.OutputType, fmt.Sprintf("Failed to decompress response: %v", err)) return "" } body = dec @@ -267,10 +264,10 @@ func (c *Client) sendRequest(method string, payload interface{}, responseBody *s c.initOnce.Do(func() { if c.cfg.MaxConcurrentRequests == 0 { c.sem = semaphore.NewWeighted(math.MaxInt64) - log.Printf("[INFO] : %v - Max concurrent requests: unlimited", c.OutputType) + utils.Log(utils.InfoLvl, c.OutputType, "Max concurrent requests: unlimited") } else { c.sem = semaphore.NewWeighted(int64(c.cfg.MaxConcurrentRequests)) - log.Printf("[INFO] : %v - Max concurrent requests: %v", c.OutputType, c.cfg.MaxConcurrentRequests) + utils.Log(utils.InfoLvl, c.OutputType, fmt.Sprintf("Max concurrent requests: %v", c.cfg.MaxConcurrentRequests)) } }) @@ -278,7 +275,7 @@ func (c *Client) sendRequest(method string, payload interface{}, responseBody *s defer func(c *Client) { if err := recover(); err != nil { go c.CountMetric("outputs", 1, []string{"output:" + strings.ToLower(c.OutputType), "status:connectionrefused"}) - log.Printf("[ERROR] : %v - %s", c.OutputType, err) + utils.Log(utils.ErrorLvl, c.OutputType, fmt.Sprint(err)) } }(c) @@ -288,18 +285,18 @@ func (c *Client) sendRequest(method string, payload interface{}, responseBody *s case influxdbPayload: fmt.Fprintf(body, "%v", payload) if c.Config.Debug { - log.Printf("[DEBUG] : %v payload : %v\n", c.OutputType, body) + utils.Log(utils.DebugLvl, c.OutputType, fmt.Sprintf("payload : %v", body)) } case spyderbatPayload: zipper := gzip.NewWriter(body) if err := json.NewEncoder(zipper).Encode(payload); err != nil { - log.Printf("[ERROR] : %v - %s", c.OutputType, err) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) } zipper.Close() if c.Config.Debug { debugBody := new(bytes.Buffer) if err := json.NewEncoder(debugBody).Encode(payload); err == nil { - log.Printf("[DEBUG] : %v payload : %v\n", c.OutputType, debugBody) + utils.Log(utils.DebugLvl, c.OutputType, fmt.Sprintf("payload : %v", body)) } } case io.Reader: @@ -308,17 +305,17 @@ func (c *Client) sendRequest(method string, payload interface{}, responseBody *s reader = bytes.NewBuffer(v) default: if err := json.NewEncoder(body).Encode(payload); err != nil { - log.Printf("[ERROR] : %v - %s", c.OutputType, err) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) } if c.Config.Debug { - log.Printf("[DEBUG] : %v payload : %v\n", c.OutputType, body) + utils.Log(utils.DebugLvl, c.OutputType, fmt.Sprintf("payload : %v", body)) } } if c.EnableCompression { data, err := compressData(reader) if err != nil { - log.Printf("[ERROR] : %v - Failed to compress data: %v\n", c.OutputType, err.Error()) + utils.Log(utils.ErrorLvl, c.OutputType, fmt.Sprintf("Failed to compress data: %v", err)) return err } reader = bytes.NewBuffer(data) @@ -334,7 +331,7 @@ func (c *Client) sendRequest(method string, payload interface{}, responseBody *s req, err = http.NewRequest(method, c.EndpointURL.String(), reader) } if err != nil { - log.Printf("[ERROR] : %v - %v\n", c.OutputType, err.Error()) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return err } @@ -358,14 +355,14 @@ func (c *Client) sendRequest(method string, payload interface{}, responseBody *s ctx := context.Background() err = c.sem.Acquire(ctx, 1) if err != nil { - log.Printf("[ERROR] : %v - %v\n", c.OutputType, err.Error()) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return err } defer c.sem.Release(1) resp, err := client.Do(req) if err != nil { - log.Printf("[ERROR] : %v - %v\n", c.OutputType, err.Error()) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) go c.CountMetric("outputs", 1, []string{"output:" + strings.ToLower(c.OutputType), "status:connectionrefused"}) return err } @@ -376,7 +373,7 @@ func (c *Client) sendRequest(method string, payload interface{}, responseBody *s switch resp.StatusCode { case http.StatusOK, http.StatusCreated, http.StatusAccepted, http.StatusNoContent: //200, 201, 202, 204 - log.Printf("[INFO] : %v - %v OK (%v)\n", c.OutputType, method, resp.StatusCode) + utils.Log(utils.InfoLvl, c.OutputType, fmt.Sprintf("%v OK (%v)", method, resp.StatusCode)) ot := c.OutputType logResponse := ot == Kubeless || ot == Openfaas || ot == Fission if responseBody != nil || logResponse { @@ -388,42 +385,42 @@ func (c *Client) sendRequest(method string, payload interface{}, responseBody *s *responseBody = s } if logResponse { - log.Printf("[INFO] : %v - Function Response : %s\n", ot, s) + utils.Log(utils.InfoLvl, ot, fmt.Sprintf("Function Response : %s", s)) } } return nil case http.StatusBadRequest: //400 msg := c.getInlinedBodyAsString(resp) - log.Printf("[ERROR] : %v - %v (%v): %s\n", c.OutputType, ErrHeaderMissing, resp.StatusCode, msg) + utils.Log(utils.ErrorLvl, c.OutputType, fmt.Sprintf("%v (%v): %s", ErrHeaderMissing, resp.StatusCode, msg)) if msg != "" { return errors.New(msg) } return ErrHeaderMissing case http.StatusUnauthorized: //401 - log.Printf("[ERROR] : %v - %v (%v): %s\n", c.OutputType, ErrClientAuthenticationError, resp.StatusCode, c.getInlinedBodyAsString(resp)) + utils.Log(utils.ErrorLvl, c.OutputType, fmt.Sprintf("%v (%v): %s", ErrClientAuthenticationError, resp.StatusCode, c.getInlinedBodyAsString(resp))) return ErrClientAuthenticationError case http.StatusForbidden: //403 - log.Printf("[ERROR] : %v - %v (%v): %s\n", c.OutputType, ErrForbidden, resp.StatusCode, c.getInlinedBodyAsString(resp)) + utils.Log(utils.ErrorLvl, c.OutputType, fmt.Sprintf("%v (%v): %s", ErrForbidden, resp.StatusCode, c.getInlinedBodyAsString(resp))) return ErrForbidden case http.StatusNotFound: //404 - log.Printf("[ERROR] : %v - %v (%v): %s\n", c.OutputType, ErrNotFound, resp.StatusCode, c.getInlinedBodyAsString(resp)) + utils.Log(utils.ErrorLvl, c.OutputType, fmt.Sprintf("%v (%v): %s", ErrNotFound, resp.StatusCode, c.getInlinedBodyAsString(resp))) return ErrNotFound case http.StatusUnprocessableEntity: //422 - log.Printf("[ERROR] : %v - %v (%v): %s\n", c.OutputType, ErrUnprocessableEntityError, resp.StatusCode, c.getInlinedBodyAsString(resp)) + utils.Log(utils.ErrorLvl, c.OutputType, fmt.Sprintf("%v (%v): %s", ErrUnprocessableEntityError, resp.StatusCode, c.getInlinedBodyAsString(resp))) return ErrUnprocessableEntityError case http.StatusTooManyRequests: //429 - log.Printf("[ERROR] : %v - %v (%v): %s\n", c.OutputType, ErrTooManyRequest, resp.StatusCode, c.getInlinedBodyAsString(resp)) + utils.Log(utils.ErrorLvl, c.OutputType, fmt.Sprintf("%v (%v): %s", ErrTooManyRequest, resp.StatusCode, c.getInlinedBodyAsString(resp))) return ErrTooManyRequest case http.StatusInternalServerError: //500 - log.Printf("[ERROR] : %v - %v (%v)\n", c.OutputType, ErrTooManyRequest, resp.StatusCode) + utils.Log(utils.ErrorLvl, c.OutputType, fmt.Sprintf("%v (%v)", ErrTooManyRequest, resp.StatusCode)) return ErrInternalServer case http.StatusBadGateway: //502 msg := c.getInlinedBodyAsString(resp) fmt.Println(msg) - log.Printf("[ERROR] : %v - %v (%v)\n", c.OutputType, ErrTooManyRequest, resp.StatusCode) + utils.Log(utils.ErrorLvl, c.OutputType, fmt.Sprintf("%v (%v)", ErrTooManyRequest, resp.StatusCode)) return ErrBadGateway default: - log.Printf("[ERROR] : %v - unexpected Response (%v)\n", c.OutputType, resp.StatusCode) + utils.Log(utils.ErrorLvl, c.OutputType, fmt.Sprintf("unexpected Response (%v)", resp.StatusCode)) return errors.New(resp.Status) } } @@ -444,7 +441,7 @@ func (c *Client) httpClient() *http.Client { Transport: customTransport, } if err != nil { - log.Printf("[ERROR] : %v - %v\n", c.OutputType, err.Error()) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) } else { c.httpcli = client // cache the client instance for future http calls } diff --git a/outputs/cliq.go b/outputs/cliq.go index 1cf83f067..5aea500af 100644 --- a/outputs/cliq.go +++ b/outputs/cliq.go @@ -5,10 +5,11 @@ package outputs import ( "bytes" "fmt" - "go.opentelemetry.io/otel/attribute" - "log" "net/http" + "go.opentelemetry.io/otel/attribute" + + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" "github.com/falcosecurity/falcosidekick/types" ) @@ -79,7 +80,7 @@ func newCliqPayload(falcopayload types.FalcoPayload, config *types.Configuration if config.Cliq.MessageFormatTemplate != nil { buf := &bytes.Buffer{} if err := config.Cliq.MessageFormatTemplate.Execute(buf, falcopayload); err != nil { - log.Printf("[ERROR] : Cliq - Error expanding Cliq message %v", err) + utils.Log(utils.ErrorLvl, "Cliq", fmt.Sprintf("Error expanding Cliq message: %v", err)) } else { payload.Text = buf.String() @@ -175,7 +176,7 @@ func (c *Client) CliqPost(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "cliq", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "cliq"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : Cliq - %v\n", err) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return } diff --git a/outputs/cloudevents.go b/outputs/cloudevents.go index d1389c535..41bc2106b 100644 --- a/outputs/cloudevents.go +++ b/outputs/cloudevents.go @@ -4,11 +4,13 @@ package outputs import ( "context" + "fmt" + "go.opentelemetry.io/otel/attribute" - "log" cloudevents "github.com/cloudevents/sdk-go/v2" + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" "github.com/falcosecurity/falcosidekick/types" ) @@ -24,7 +26,7 @@ func (c *Client) CloudEventsSend(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "cloudevents", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "cloudevents"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : CloudEvents - NewDefaultClient : %v\n", err) + utils.Log(utils.ErrorLvl, c.OutputType, fmt.Sprintf("NewDefaultClient : %v", err)) return } c.CloudEventsClient = client @@ -50,7 +52,7 @@ func (c *Client) CloudEventsSend(falcopayload types.FalcoPayload) { } if err := event.SetData(cloudevents.ApplicationJSON, falcopayload); err != nil { - log.Printf("[ERROR] : CloudEvents, failed to set data : %v\n", err) + utils.Log(utils.ErrorLvl, c.OutputType, fmt.Sprintf("failed to set data : %v", err)) } if result := c.CloudEventsClient.Send(ctx, event); cloudevents.IsUndelivered(result) { @@ -59,7 +61,7 @@ func (c *Client) CloudEventsSend(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "cloudevents", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "cloudevents"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : CloudEvents - %v\n", result) + utils.Log(utils.ErrorLvl, c.OutputType, fmt.Sprintf("%v", result)) return } @@ -69,5 +71,5 @@ func (c *Client) CloudEventsSend(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "cloudevents", "status": OK}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "cloudevents"), attribute.String("status", OK)).Inc() - log.Printf("[INFO] : CloudEvents - Send OK\n") + utils.Log(utils.InfoLvl, c.OutputType, "Send OK") } diff --git a/outputs/datadog.go b/outputs/datadog.go index 499893d2a..ed123ae0d 100644 --- a/outputs/datadog.go +++ b/outputs/datadog.go @@ -4,10 +4,11 @@ package outputs import ( "fmt" - "go.opentelemetry.io/otel/attribute" - "log" "sort" + "go.opentelemetry.io/otel/attribute" + + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" "github.com/falcosecurity/falcosidekick/types" ) @@ -72,7 +73,7 @@ func (c *Client) DatadogPost(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "datadog", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "datadog"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : Datadog - %v\n", err) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return } diff --git a/outputs/datadog_logs.go b/outputs/datadog_logs.go index dd15fc5aa..79cfa78d3 100644 --- a/outputs/datadog_logs.go +++ b/outputs/datadog_logs.go @@ -3,12 +3,14 @@ package outputs import ( - "github.com/falcosecurity/falcosidekick/types" - "go.opentelemetry.io/otel/attribute" - "log" "net/http" "sort" "strings" + + "go.opentelemetry.io/otel/attribute" + + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" + "github.com/falcosecurity/falcosidekick/types" ) const ( @@ -61,7 +63,7 @@ func (c *Client) DatadogLogsPost(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "datadoglogs", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "datadoglogs"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : Datadog Logs - %v\n", err) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return } diff --git a/outputs/discord.go b/outputs/discord.go index 5ab0ea4cb..9891cf09e 100644 --- a/outputs/discord.go +++ b/outputs/discord.go @@ -4,11 +4,12 @@ package outputs import ( "fmt" - "go.opentelemetry.io/otel/attribute" - "log" "sort" "strings" + "go.opentelemetry.io/otel/attribute" + + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" "github.com/falcosecurity/falcosidekick/types" ) @@ -108,7 +109,7 @@ func (c *Client) DiscordPost(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "discord", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "discord"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : Discord - %v\n", err) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return } diff --git a/outputs/dynatrace.go b/outputs/dynatrace.go index 1eb16766a..9f69c00ec 100644 --- a/outputs/dynatrace.go +++ b/outputs/dynatrace.go @@ -3,13 +3,14 @@ package outputs import ( - "go.opentelemetry.io/otel/attribute" - "log" "net/http" "regexp" "strconv" "time" + "go.opentelemetry.io/otel/attribute" + + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" "github.com/falcosecurity/falcosidekick/types" ) @@ -125,7 +126,7 @@ func (c *Client) DynatracePost(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "dynatrace", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "dynatrace"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : Dynatrace - %v\n", err) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return } diff --git a/outputs/dynatrace_test.go b/outputs/dynatrace_test.go index d96a2ca21..4211b2db2 100644 --- a/outputs/dynatrace_test.go +++ b/outputs/dynatrace_test.go @@ -7,9 +7,9 @@ import ( "strconv" "testing" - "github.com/falcosecurity/falcosidekick/types" - "github.com/stretchr/testify/require" + + "github.com/falcosecurity/falcosidekick/types" ) func TestNewDynatracePayload(t *testing.T) { diff --git a/outputs/elasticsearch.go b/outputs/elasticsearch.go index d54b7a4be..62269d781 100644 --- a/outputs/elasticsearch.go +++ b/outputs/elasticsearch.go @@ -6,15 +6,16 @@ import ( "bytes" "encoding/json" "fmt" - "go.opentelemetry.io/otel/attribute" - "log" "net/http" "net/url" "regexp" "strings" "time" + "go.opentelemetry.io/otel/attribute" + "github.com/falcosecurity/falcosidekick/internal/pkg/batcher" + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" "github.com/falcosecurity/falcosidekick/types" ) @@ -49,7 +50,7 @@ func NewElasticsearchClient(params types.InitClientArgs) (*Client, error) { } if esCfg.Batching.Enabled { - log.Printf("[INFO] : %v - Batching enabled: %v max bytes, %v interval\n", c.OutputType, esCfg.Batching.BatchSize, esCfg.Batching.FlushInterval) + utils.Log(utils.InfoLvl, c.OutputType, fmt.Sprintf("Batching enabled: %v max bytes, %v interval", esCfg.Batching.BatchSize, esCfg.Batching.FlushInterval)) callbackFn := func(falcoPayloads []types.FalcoPayload, data []byte) { go c.elasticsearchPost("", data, falcoPayloads...) } @@ -62,7 +63,7 @@ func NewElasticsearchClient(params types.InitClientArgs) (*Client, error) { } if esCfg.EnableCompression { c.EnableCompression = true - log.Printf("[INFO] : %v - Compression enabled\n", c.OutputType) + utils.Log(utils.InfoLvl, c.OutputType, "Compression enabled") } return c, nil @@ -76,7 +77,7 @@ func (c *Client) ElasticsearchPost(falcopayload types.FalcoPayload) { payload, err := c.marshalESPayload(falcopayload) if err != nil { - log.Printf("[ERROR] : %v - Failed to marshal payload: %v\n", c.OutputType, err) + utils.Log(utils.ErrorLvl, c.OutputType, fmt.Sprintf("Failed to marshal payload: %v", err)) } c.elasticsearchPost(c.getIndex(), payload, falcopayload) @@ -99,7 +100,7 @@ func (c *Client) elasticsearchPost(index string, payload []byte, falcoPayloads . endpointURL, err := url.Parse(eURL) if err != nil { c.setElasticSearchErrorMetrics(sz) - log.Printf("[ERROR] : %v - %v\n", c.OutputType, err) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return } @@ -157,7 +158,7 @@ func (c *Client) elasticsearchPost(index string, payload []byte, falcoPayloads . return } if len(resp.Items) != len(falcoPayloads) { - log.Printf("[ERROR] : %v - mismatched %v responses with %v request payloads\n", c.OutputType, len(resp.Items), len(falcoPayloads)) + utils.Log(utils.ErrorLvl, c.OutputType, fmt.Sprintf("mismatched %v responses with %v request payloads", len(resp.Items), len(falcoPayloads))) c.setElasticSearchErrorMetrics(sz) return } @@ -207,7 +208,7 @@ func (c *Client) elasticsearchPost(index string, payload []byte, falcoPayloads . delete(payload.OutputFields, i) } } - log.Printf("[INFO] : %v - %v\n", c.OutputType, "attempt to POST again the payload without the wrong field") + utils.Log(utils.InfoLvl, c.OutputType, "attempt to POST again the payload without the wrong field") err = c.Post(payload, reqOpts...) if err != nil { c.setElasticSearchErrorMetrics(sz) @@ -229,11 +230,11 @@ func (c *Client) ElasticsearchCreateIndexTemplate(config types.ElasticsearchOutp d := c indexExists, err := c.isIndexTemplateExist(config) if err != nil { - log.Printf("[ERROR] : %v - %v\n", c.OutputType, err) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return err } if indexExists { - log.Printf("[INFO] : %v - %v\n", c.OutputType, "Index template already exists") + utils.Log(utils.InfoLvl, c.OutputType, "Index template already exists") return nil } @@ -247,16 +248,16 @@ func (c *Client) ElasticsearchCreateIndexTemplate(config types.ElasticsearchOutp m = strings.ReplaceAll(m, "${REPLICAS}", fmt.Sprintf("%v", config.NumberOfReplicas)) j := make(map[string]interface{}) if err := json.Unmarshal([]byte(m), &j); err != nil { - log.Printf("[ERROR] : %v - %v\n", c.OutputType, err) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return err } // create the index template by PUT if d.Put(j) != nil { - log.Printf("[ERROR] : %v - %v\n", c.OutputType, err) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return err } - log.Printf("[INFO] : %v - %v\n", c.OutputType, "Index template created") + utils.Log(utils.InfoLvl, c.OutputType, "Index template created") return nil } diff --git a/outputs/fission.go b/outputs/fission.go index 6525bc1ca..a195252ce 100644 --- a/outputs/fission.go +++ b/outputs/fission.go @@ -6,17 +6,17 @@ import ( "context" "encoding/json" "fmt" - "github.com/falcosecurity/falcosidekick/outputs/otlpmetrics" - "go.opentelemetry.io/otel/attribute" - "log" "net/http" "strconv" "github.com/DataDog/datadog-go/statsd" "github.com/google/uuid" + "go.opentelemetry.io/otel/attribute" "k8s.io/client-go/kubernetes" "k8s.io/client-go/tools/clientcmd" + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" + "github.com/falcosecurity/falcosidekick/outputs/otlpmetrics" "github.com/falcosecurity/falcosidekick/types" ) @@ -86,10 +86,10 @@ func (c *Client) FissionCall(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "Fission", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "Fission"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : %s - %v\n", Fission, err.Error()) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return } - log.Printf("[INFO] : %s - Function Response : %v\n", Fission, string(rawbody)) + utils.Log(utils.InfoLvl, c.OutputType, fmt.Sprintf("Function Response : %v", string(rawbody))) } else { c.ContentType = FissionContentType @@ -102,11 +102,11 @@ func (c *Client) FissionCall(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "Fission", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "Fission"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : %s - %v\n", Fission, err.Error()) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return } } - log.Printf("[INFO] : %s - Call Function \"%v\" OK\n", Fission, c.Config.Fission.Function) + utils.Log(utils.InfoLvl, c.OutputType, fmt.Sprintf("Call Function \"%v\" OK", c.Config.Fission.Function)) go c.CountMetric(Outputs, 1, []string{"output:Fission", "status:ok"}) c.Stats.Fission.Add(OK, 1) c.PromStats.Outputs.With(map[string]string{"destination": "Fission", "status": OK}).Inc() diff --git a/outputs/gcp.go b/outputs/gcp.go index 4d07fdd95..56e38ac98 100644 --- a/outputs/gcp.go +++ b/outputs/gcp.go @@ -8,21 +8,20 @@ import ( "encoding/json" "errors" "fmt" - "github.com/falcosecurity/falcosidekick/outputs/otlpmetrics" - "go.opentelemetry.io/otel/attribute" - "log" "time" gcpfunctions "cloud.google.com/go/functions/apiv1" - "cloud.google.com/go/storage" - gcpfunctionspb "google.golang.org/genproto/googleapis/cloud/functions/v1" - "cloud.google.com/go/pubsub" + "cloud.google.com/go/storage" "github.com/DataDog/datadog-go/statsd" "github.com/googleapis/gax-go/v2" + "go.opentelemetry.io/otel/attribute" "golang.org/x/oauth2/google" "google.golang.org/api/option" + gcpfunctionspb "google.golang.org/genproto/googleapis/cloud/functions/v1" + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" + "github.com/falcosecurity/falcosidekick/outputs/otlpmetrics" "github.com/falcosecurity/falcosidekick/types" ) @@ -31,7 +30,7 @@ func NewGCPClient(config *types.Configuration, stats *types.Statistics, promStat otlpMetrics *otlpmetrics.OTLPMetrics, statsdClient, dogstatsdClient *statsd.Client) (*Client, error) { base64decodedCredentialsData, err := base64.StdEncoding.DecodeString(config.GCP.Credentials) if err != nil { - log.Printf("[ERROR] : GCP - %v\n", "Error while base64-decoding GCP Credentials") + utils.Log(utils.ErrorLvl, "GCP", "Erroc.OutputTyper while base64-decoding GCP Credentials") return nil, errors.New("error while base64-decoding GCP Credentials") } @@ -44,19 +43,19 @@ func NewGCPClient(config *types.Configuration, stats *types.Statistics, promStat if googleCredentialsData != "" { credentials, err := google.CredentialsFromJSON(context.Background(), []byte(googleCredentialsData), pubsub.ScopePubSub) if err != nil { - log.Printf("[ERROR] : GCP PubSub - %v\n", "Error while loading GCP Credentials") + utils.Log(utils.ErrorLvl, "GCP PubSub", "Error while loading GCP Credentials") return nil, errors.New("error while loading GCP Credentials") } pubSubClient, err := pubsub.NewClient(context.Background(), config.GCP.PubSub.ProjectID, option.WithCredentials(credentials)) if err != nil { - log.Printf("[ERROR] : GCP PubSub - %v\n", "Error while creating GCP PubSub Client") + utils.Log(utils.ErrorLvl, "GCP PubSub", "Error while creating GCP PubSub Client") return nil, errors.New("error while creating GCP PubSub Client") } topicClient = pubSubClient.Topic(config.GCP.PubSub.Topic) } else { pubSubClient, err := pubsub.NewClient(context.Background(), config.GCP.PubSub.ProjectID) if err != nil { - log.Printf("[ERROR] : GCP PubSub - %v\n", "Error while creating GCP PubSub Client") + utils.Log(utils.ErrorLvl, "GCP PubSub", "Error while creating GCP PubSub Client") return nil, errors.New("error while creating GCP PubSub Client") } topicClient = pubSubClient.Topic(config.GCP.PubSub.Topic) @@ -66,12 +65,12 @@ func NewGCPClient(config *types.Configuration, stats *types.Statistics, promStat if config.GCP.Storage.Bucket != "" { credentials, err := google.CredentialsFromJSON(context.Background(), []byte(googleCredentialsData)) if err != nil { - log.Printf("[ERROR] : GCP Storage - %v\n", "Error while loading GCS Credentials") + utils.Log(utils.ErrorLvl, "GCP PubSub", "Error while loading GCS Credentials") return nil, errors.New("error while loading GCP Credentials") } storageClient, err = storage.NewClient(context.Background(), option.WithCredentials(credentials)) if err != nil { - log.Printf("[ERROR] : GCP Storage - %v\n", "Error while creating GCP Storage Client") + utils.Log(utils.ErrorLvl, "GCP PubSub", "Error while creating GCP Storage Client") return nil, errors.New("error while creating GCP Storage Client") } } @@ -80,18 +79,18 @@ func NewGCPClient(config *types.Configuration, stats *types.Statistics, promStat if googleCredentialsData != "" { credentials, err := google.CredentialsFromJSON(context.Background(), []byte(googleCredentialsData), gcpfunctions.DefaultAuthScopes()...) if err != nil { - log.Printf("[ERROR] : GCP CloudFunctions - %v\n", "Error while loading GCS Credentials") + utils.Log(utils.ErrorLvl, "GCP CloudFunctions", "Error while loading GCS Credentials") return nil, errors.New("error while loading GCP Credentials") } cloudFunctionsClient, err = gcpfunctions.NewCloudFunctionsClient(context.Background(), option.WithCredentials(credentials)) if err != nil { - log.Printf("[ERROR]: GCP CloudFunctions - %v\n", "Error while creating GCP CloudFunctions Client") + utils.Log(utils.ErrorLvl, "GCP CloudFunctions", "Error while creating GCP CloudFunctions Client") return nil, errors.New("error while creating GCP CloudFunctions Client") } } else { cloudFunctionsClient, err = gcpfunctions.NewCloudFunctionsClient(context.Background()) if err != nil { - log.Printf("[ERROR]: GCP CloudFunctions - %v\n", "Error while creating GCP CloudFunctions Client") + utils.Log(utils.ErrorLvl, "GCP CloudFunctions", "Error while creating GCP CloudFunctions Client") return nil, errors.New("error while creating GCP CloudFunctions Client") } } @@ -124,7 +123,7 @@ func (c *Client) GCPCallCloudFunction(falcopayload types.FalcoPayload) { }, gax.WithGRPCOptions()) if err != nil { - log.Printf("[ERROR] : GCPCloudFunctions - %v - %v\n", "Error while calling CloudFunction", err.Error()) + utils.Log(utils.ErrorLvl, c.OutputType+" CloudFunctions", fmt.Sprintf("Error while calling CloudFunction: %v", err)) c.Stats.GCPPubSub.Add(Error, 1) go c.CountMetric("outputs", 1, []string{"output:gcpcloudfunctions", "status:error"}) c.PromStats.Outputs.With(map[string]string{"destination": "gcpcloudfunctions", "status": Error}).Inc() @@ -133,7 +132,7 @@ func (c *Client) GCPCallCloudFunction(falcopayload types.FalcoPayload) { return } - log.Printf("[INFO] : GCPCloudFunctions - Call CloudFunction OK (%v)\n", result.ExecutionId) + utils.Log(utils.ErrorLvl, c.OutputType+" CloudFunctions", fmt.Sprintf("Call CloudFunction OK (%v)", result.ExecutionId)) c.Stats.GCPCloudFunctions.Add(OK, 1) go c.CountMetric("outputs", 1, []string{"output:gcpcloudfunctions", "status:ok"}) @@ -152,7 +151,7 @@ func (c *Client) GCPPublishTopic(falcopayload types.FalcoPayload) { result := c.GCPTopicClient.Publish(context.Background(), message) id, err := result.Get(context.Background()) if err != nil { - log.Printf("[ERROR] : GCPPubSub - %v - %v\n", "Error while publishing message", err.Error()) + utils.Log(utils.ErrorLvl, c.OutputType+" PubSub", fmt.Sprintf("Error while publishing message: %v", err)) c.Stats.GCPPubSub.Add(Error, 1) go c.CountMetric("outputs", 1, []string{"output:gcppubsub", "status:error"}) c.PromStats.Outputs.With(map[string]string{"destination": "gcppubsub", "status": Error}).Inc() @@ -161,7 +160,7 @@ func (c *Client) GCPPublishTopic(falcopayload types.FalcoPayload) { return } - log.Printf("[INFO] : GCPPubSub - Send to topic OK (%v)\n", id) + utils.Log(utils.InfoLvl, c.OutputType+" PubSub", fmt.Sprintf("Send to topic OK (%v)", id)) c.Stats.GCPPubSub.Add(OK, 1) go c.CountMetric("outputs", 1, []string{"output:gcppubsub", "status:ok"}) c.PromStats.Outputs.With(map[string]string{"destination": "gcppubsub", "status": OK}).Inc() @@ -185,7 +184,7 @@ func (c *Client) UploadGCS(falcopayload types.FalcoPayload) { bucketWriter := c.GCSStorageClient.Bucket(c.Config.GCP.Storage.Bucket).Object(key).NewWriter(context.Background()) n, err := bucketWriter.Write(payload) if err != nil { - log.Printf("[ERROR] : GCPStorage - %v - %v\n", "Error while Uploading message", err.Error()) + utils.Log(utils.ErrorLvl, c.OutputType+"Storage", fmt.Sprintf("Error while Uploading message: %v", err)) c.Stats.GCPStorage.Add(Error, 1) go c.CountMetric("outputs", 1, []string{"output:gcpstorage", "status:error"}) c.PromStats.Outputs.With(map[string]string{"destination": "gcpstorage", "status": Error}).Inc() @@ -194,7 +193,7 @@ func (c *Client) UploadGCS(falcopayload types.FalcoPayload) { return } if n == 0 { - log.Printf("[ERROR] : GCPStorage - %v\n", "Empty payload uploaded") + utils.Log(utils.ErrorLvl, c.OutputType+"Storage", "Empty payload uploaded") c.Stats.GCPStorage.Add(Error, 1) go c.CountMetric("outputs", 1, []string{"output:gcpstorage", "status:error"}) c.PromStats.Outputs.With(map[string]string{"destination": "gcpstorage", "status": Error}).Inc() @@ -203,7 +202,7 @@ func (c *Client) UploadGCS(falcopayload types.FalcoPayload) { return } if err := bucketWriter.Close(); err != nil { - log.Printf("[ERROR] : GCPStorage - %v - %v\n", "Error while closing the writer", err.Error()) + utils.Log(utils.ErrorLvl, c.OutputType+"Storage", fmt.Sprintf("Error while closing the writer: %v", err)) c.Stats.GCPStorage.Add(Error, 1) go c.CountMetric("outputs", 1, []string{"output:gcpstorage", "status:error"}) c.PromStats.Outputs.With(map[string]string{"destination": "gcpstorage", "status": Error}).Inc() @@ -212,7 +211,7 @@ func (c *Client) UploadGCS(falcopayload types.FalcoPayload) { return } - log.Printf("[INFO] : GCPStorage - Upload to bucket OK \n") + utils.Log(utils.InfoLvl, c.OutputType+"Storage", "Upload to bucket OK") c.Stats.GCPStorage.Add(OK, 1) go c.CountMetric("outputs", 1, []string{"output:gcpstorage", "status:ok"}) c.PromStats.Outputs.With(map[string]string{"destination": "gcpstorage", "status": OK}).Inc() diff --git a/outputs/gcpcloudrun.go b/outputs/gcpcloudrun.go index 3f00c62e3..5ea1ed25f 100644 --- a/outputs/gcpcloudrun.go +++ b/outputs/gcpcloudrun.go @@ -3,10 +3,11 @@ package outputs import ( - "go.opentelemetry.io/otel/attribute" - "log" "net/http" + "go.opentelemetry.io/otel/attribute" + + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" "github.com/falcosecurity/falcosidekick/types" ) @@ -25,7 +26,7 @@ func (c *Client) CloudRunFunctionPost(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "gcpcloudrun", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "gcpcloudrun"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : GCPCloudRun - %v\n", err.Error()) + utils.Log(utils.ErrorLvl, c.OutputType+"CloudRun", err.Error()) return } diff --git a/outputs/googlechat.go b/outputs/googlechat.go index 30c425415..4ac74a7f6 100644 --- a/outputs/googlechat.go +++ b/outputs/googlechat.go @@ -4,11 +4,13 @@ package outputs import ( "bytes" - "go.opentelemetry.io/otel/attribute" - "log" + "fmt" "sort" "strings" + "go.opentelemetry.io/otel/attribute" + + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" "github.com/falcosecurity/falcosidekick/types" ) @@ -47,7 +49,7 @@ func newGooglechatPayload(falcopayload types.FalcoPayload, config *types.Configu if config.Googlechat.MessageFormatTemplate != nil { buf := &bytes.Buffer{} if err := config.Googlechat.MessageFormatTemplate.Execute(buf, falcopayload); err != nil { - log.Printf("[ERROR] : GoogleChat - Error expanding Google Chat message %v", err) + utils.Log(utils.ErrorLvl, "GoogleChat", fmt.Sprintf("Error expanding Google Chat message: %v", err)) } else { messageText = buf.String() } @@ -110,7 +112,7 @@ func (c *Client) GooglechatPost(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "googlechat", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "googlechat"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : GoogleChat - %v\n", err) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return } diff --git a/outputs/gotify.go b/outputs/gotify.go index 2d6ab82ab..b0f0c2d57 100644 --- a/outputs/gotify.go +++ b/outputs/gotify.go @@ -5,12 +5,13 @@ package outputs import ( "bytes" "encoding/json" - "go.opentelemetry.io/otel/attribute" - "log" "net/http" "strings" textTemplate "text/template" + "go.opentelemetry.io/otel/attribute" + + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" "github.com/falcosecurity/falcosidekick/types" ) @@ -75,7 +76,7 @@ func newGotifyPayload(falcopayload types.FalcoPayload, config *types.Configurati err = ttmpl.Execute(&outtext, falcopayload) } if err != nil { - log.Printf("[ERROR] : Gotify - %v\n", err) + utils.Log(utils.ErrorLvl, "Gotify", err.Error()) return g } @@ -102,7 +103,7 @@ func (c *Client) GotifyPost(falcopayload types.FalcoPayload) { }) if err != nil { c.setGotifyErrorMetrics() - log.Printf("[ERROR] : Gotify - %v\n", err) + utils.Log(utils.ErrorLvl, "Gotify", err.Error()) return } diff --git a/outputs/grafana.go b/outputs/grafana.go index d3af9e876..98c0e517b 100644 --- a/outputs/grafana.go +++ b/outputs/grafana.go @@ -4,10 +4,11 @@ package outputs import ( "fmt" - "go.opentelemetry.io/otel/attribute" - "log" "net/http" + "go.opentelemetry.io/otel/attribute" + + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" "github.com/falcosecurity/falcosidekick/types" ) @@ -93,7 +94,7 @@ func (c *Client) GrafanaPost(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "grafana", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "grafana"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : Grafana - %v\n", err) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return } @@ -121,7 +122,7 @@ func (c *Client) GrafanaOnCallPost(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "grafanaoncall", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "grafanaoncall"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : Grafana OnCall - %v\n", err) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return } diff --git a/outputs/influxdb.go b/outputs/influxdb.go index 8f4f78567..f6d2fb7e6 100644 --- a/outputs/influxdb.go +++ b/outputs/influxdb.go @@ -3,11 +3,12 @@ package outputs import ( - "go.opentelemetry.io/otel/attribute" - "log" "net/http" "strings" + "go.opentelemetry.io/otel/attribute" + + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" "github.com/falcosecurity/falcosidekick/types" ) @@ -55,7 +56,7 @@ func (c *Client) InfluxdbPost(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "influxdb", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "influxdb"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : InfluxDB - %v\n", err) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return } diff --git a/outputs/kafka.go b/outputs/kafka.go index 92a99502e..81e518ee0 100644 --- a/outputs/kafka.go +++ b/outputs/kafka.go @@ -8,9 +8,6 @@ import ( "crypto/x509" "encoding/json" "fmt" - "github.com/falcosecurity/falcosidekick/outputs/otlpmetrics" - "go.opentelemetry.io/otel/attribute" - "log" "net" "strings" "time" @@ -19,7 +16,10 @@ import ( "github.com/segmentio/kafka-go" "github.com/segmentio/kafka-go/sasl/plain" "github.com/segmentio/kafka-go/sasl/scram" + "go.opentelemetry.io/otel/attribute" + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" + "github.com/falcosecurity/falcosidekick/outputs/otlpmetrics" "github.com/falcosecurity/falcosidekick/types" ) @@ -39,7 +39,7 @@ func NewKafkaClient(config *types.Configuration, stats *types.Statistics, promSt caCertPool, err := x509.SystemCertPool() if err != nil { - log.Printf("[ERROR] : Kafka - failed to initialize root CAs: %v", err) + utils.Log(utils.ErrorLvl, "Kafka", fmt.Sprintf("failed to initialize root CAs: %v", err)) } transport.TLS = &tls.Config{ @@ -76,7 +76,7 @@ func NewKafkaClient(config *types.Configuration, stats *types.Statistics, promSt } } if err != nil { - log.Printf("[ERROR] : Kafka - %v\n", err) + utils.Log(utils.ErrorLvl, "Kafka", err.Error()) return nil, err } @@ -102,7 +102,7 @@ func NewKafkaClient(config *types.Configuration, stats *types.Statistics, promSt case "round_robin": kafkaWriter.Balancer = &kafka.RoundRobin{} default: - log.Printf("[ERROR] : Kafka - unsupported balancer %q\n", config.Kafka.Balancer) + utils.Log(utils.ErrorLvl, "Kafka", fmt.Sprintf("unsupported balancer %q", config.Kafka.Balancer)) return nil, fmt.Errorf("unsupported balancer %q", config.Kafka.Balancer) } @@ -118,7 +118,7 @@ func NewKafkaClient(config *types.Configuration, stats *types.Statistics, promSt case "NONE": // leave as default, none default: - log.Printf("[ERROR] : Kafka - unsupported compression %q\n", config.Kafka.Compression) + utils.Log(utils.ErrorLvl, "Kafka", fmt.Sprintf("unsupported compression %q", config.Kafka.Compression)) return nil, fmt.Errorf("unsupported compression %q", config.Kafka.Compression) } @@ -130,7 +130,7 @@ func NewKafkaClient(config *types.Configuration, stats *types.Statistics, promSt case "NONE": kafkaWriter.RequiredAcks = kafka.RequireNone default: - log.Printf("[ERROR] : Kafka - unsupported required ACKs %q\n", config.Kafka.RequiredACKs) + utils.Log(utils.ErrorLvl, "Kafka", fmt.Sprintf("unsupported required ACKs %q", config.Kafka.RequiredACKs)) return nil, fmt.Errorf("unsupported required ACKs %q", config.Kafka.RequiredACKs) } @@ -155,7 +155,7 @@ func (c *Client) KafkaProduce(falcopayload types.FalcoPayload) { falcoMsg, err := json.Marshal(falcopayload) if err != nil { c.incrKafkaErrorMetrics(1) - log.Printf("[ERROR] : Kafka - %v - %v\n", "failed to marshalling message", err.Error()) + utils.Log(utils.ErrorLvl, c.OutputType, fmt.Sprintf("failed to marshalling message: %v", err)) return } @@ -167,11 +167,11 @@ func (c *Client) KafkaProduce(falcopayload types.FalcoPayload) { err = c.KafkaProducer.WriteMessages(context.Background(), kafkaMsg) if err != nil { c.incrKafkaErrorMetrics(1) - log.Printf("[ERROR] : Kafka - %v\n", err.Error()) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return } else { c.incrKafkaSuccessMetrics(1) - log.Printf("[INFO] : Kafka - Publish OK\n") + utils.Log(utils.InfoLvl, c.OutputType, "Publish OK") } } @@ -179,10 +179,10 @@ func (c *Client) KafkaProduce(falcopayload types.FalcoPayload) { func (c *Client) handleKafkaCompletion(messages []kafka.Message, err error) { if err != nil { c.incrKafkaErrorMetrics(len(messages)) - log.Printf("[ERROR] : Kafka (%d) - %v\n", len(messages), err) + utils.Log(utils.ErrorLvl, c.OutputType, fmt.Sprintf("(%d) %v", len(messages), err)) } else { c.incrKafkaSuccessMetrics(len(messages)) - log.Printf("[INFO] : Kafka (%d) - Publish OK\n", len(messages)) + utils.Log(utils.InfoLvl, c.OutputType, fmt.Sprintf("(%d) - Publish OK", len(messages))) } } diff --git a/outputs/kafkarest.go b/outputs/kafkarest.go index b52758549..dc58843e3 100644 --- a/outputs/kafkarest.go +++ b/outputs/kafkarest.go @@ -6,9 +6,10 @@ import ( "encoding/base64" "encoding/json" "fmt" + "go.opentelemetry.io/otel/attribute" - "log" + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" "github.com/falcosecurity/falcosidekick/types" ) @@ -41,7 +42,7 @@ func (c *Client) KafkaRestPost(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "kafkarest", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "kafkarest"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : Kafka Rest - %v - %v\n", "failed to marshalling message", err.Error()) + utils.Log(utils.ErrorLvl, c.OutputType, fmt.Sprintf("failed to marshalling message: %v", err)) return } @@ -60,7 +61,7 @@ func (c *Client) KafkaRestPost(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "kafkarest", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "kafkarest"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : Kafka Rest - %v\n", err.Error()) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return } diff --git a/outputs/kubeless.go b/outputs/kubeless.go index 144824b5f..193fac4c4 100644 --- a/outputs/kubeless.go +++ b/outputs/kubeless.go @@ -6,17 +6,17 @@ import ( "context" "encoding/json" "fmt" - "github.com/falcosecurity/falcosidekick/outputs/otlpmetrics" - "go.opentelemetry.io/otel/attribute" - "log" "net/http" "strconv" "github.com/DataDog/datadog-go/statsd" "github.com/google/uuid" + "go.opentelemetry.io/otel/attribute" "k8s.io/client-go/kubernetes" "k8s.io/client-go/tools/clientcmd" + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" + "github.com/falcosecurity/falcosidekick/outputs/otlpmetrics" "github.com/falcosecurity/falcosidekick/types" ) @@ -87,10 +87,10 @@ func (c *Client) KubelessCall(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "kubeless", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "kubeless"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : Kubeless - %v\n", err) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return } - log.Printf("[INFO] : Kubeless - Function Response : %v\n", string(rawbody)) + utils.Log(utils.InfoLvl, c.OutputType, fmt.Sprintf("Function Response : %v", string(rawbody))) } else { c.ContentType = KubelessContentType @@ -105,11 +105,11 @@ func (c *Client) KubelessCall(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "kubeless", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "kubeless"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : Kubeless - %v\n", err) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return } } - log.Printf("[INFO] : Kubeless - Call Function \"%v\" OK\n", c.Config.Kubeless.Function) + utils.Log(utils.InfoLvl, c.OutputType, fmt.Sprintf("Call Function \"%v\" OK", c.Config.Kubeless.Function)) go c.CountMetric(Outputs, 1, []string{"output:kubeless", "status:ok"}) c.Stats.Kubeless.Add(OK, 1) c.PromStats.Outputs.With(map[string]string{"destination": "kubeless", "status": OK}).Inc() diff --git a/outputs/loki.go b/outputs/loki.go index a9b4a4ced..6baa9ee5a 100644 --- a/outputs/loki.go +++ b/outputs/loki.go @@ -4,15 +4,18 @@ package outputs import ( "fmt" - "go.opentelemetry.io/otel/attribute" - "log" "net/http" "sort" "strings" + "go.opentelemetry.io/otel/attribute" + + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" "github.com/falcosecurity/falcosidekick/types" ) +const LokiOut string = "Loki" + type lokiPayload struct { Streams []lokiStream `json:"streams"` } @@ -110,7 +113,7 @@ func (c *Client) LokiPost(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "loki", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "loki"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : Loki - %v\n", err) + utils.Log(utils.ErrorLvl, LokiOut, err.Error()) return } diff --git a/outputs/mattermost.go b/outputs/mattermost.go index 665b79165..bd5f7ceeb 100644 --- a/outputs/mattermost.go +++ b/outputs/mattermost.go @@ -4,11 +4,13 @@ package outputs import ( "bytes" - "go.opentelemetry.io/otel/attribute" - "log" + "fmt" "sort" "strings" + "go.opentelemetry.io/otel/attribute" + + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" "github.com/falcosecurity/falcosidekick/types" ) @@ -79,7 +81,7 @@ func newMattermostPayload(falcopayload types.FalcoPayload, config *types.Configu if config.Mattermost.MessageFormatTemplate != nil { buf := &bytes.Buffer{} if err := config.Mattermost.MessageFormatTemplate.Execute(buf, falcopayload); err != nil { - log.Printf("[ERROR] : Mattermost - Error expanding Mattermost message %v", err) + utils.Log(utils.ErrorLvl, "Mattermost", fmt.Sprintf("Error expanding Mattermost message %v", err)) } else { messageText = buf.String() } @@ -134,7 +136,7 @@ func (c *Client) MattermostPost(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "mattermost", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "mattermost"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : Mattermost - %v\n", err) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return } diff --git a/outputs/mqtt.go b/outputs/mqtt.go index 297defc98..9d3ded1f7 100644 --- a/outputs/mqtt.go +++ b/outputs/mqtt.go @@ -4,14 +4,15 @@ package outputs import ( "crypto/tls" - "github.com/falcosecurity/falcosidekick/outputs/otlpmetrics" - "go.opentelemetry.io/otel/attribute" - "log" + "fmt" "github.com/DataDog/datadog-go/statsd" mqtt "github.com/eclipse/paho.mqtt.golang" "github.com/google/uuid" + "go.opentelemetry.io/otel/attribute" + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" + "github.com/falcosecurity/falcosidekick/outputs/otlpmetrics" "github.com/falcosecurity/falcosidekick/types" ) @@ -32,7 +33,7 @@ func NewMQTTClient(config *types.Configuration, stats *types.Statistics, promSta } } options.OnConnectionLost = func(client mqtt.Client, err error) { - log.Printf("[ERROR] : MQTT - Connection lost: %v\n", err.Error()) + utils.Log(utils.ErrorLvl, "MQTT", fmt.Sprintf("Connection lost: %v", err)) } client := mqtt.NewClient(options) @@ -61,7 +62,7 @@ func (c *Client) MQTTPublish(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "mqtt", "status": err.Error()}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "mqtt"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : %s - %v\n", MQTT, err.Error()) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return } defer c.MQTTClient.Disconnect(100) @@ -71,11 +72,11 @@ func (c *Client) MQTTPublish(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "mqtt", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "mqtt"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : %s - %v\n", MQTT, err.Error()) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return } - log.Printf("[INFO] : %s - Message published\n", MQTT) + utils.Log(utils.InfoLvl, c.OutputType, "Message published") go c.CountMetric(Outputs, 1, []string{"output:mqtt", "status:ok"}) c.Stats.MQTT.Add(OK, 1) c.PromStats.Outputs.With(map[string]string{"destination": "mqtt", "status": OK}).Inc() diff --git a/outputs/n8n.go b/outputs/n8n.go index a9e89607c..219445575 100644 --- a/outputs/n8n.go +++ b/outputs/n8n.go @@ -3,10 +3,11 @@ package outputs import ( - "go.opentelemetry.io/otel/attribute" - "log" "net/http" + "go.opentelemetry.io/otel/attribute" + + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" "github.com/falcosecurity/falcosidekick/types" ) @@ -29,7 +30,7 @@ func (c *Client) N8NPost(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "n8n", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "n8n"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : N8N - %v\n", err.Error()) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return } diff --git a/outputs/nats.go b/outputs/nats.go index 710165ea5..8243398d7 100644 --- a/outputs/nats.go +++ b/outputs/nats.go @@ -4,14 +4,13 @@ package outputs import ( "encoding/json" - "log" "regexp" "strings" - "go.opentelemetry.io/otel/attribute" - nats "github.com/nats-io/nats.go" + "go.opentelemetry.io/otel/attribute" + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" "github.com/falcosecurity/falcosidekick/types" ) @@ -34,7 +33,7 @@ func (c *Client) NatsPublish(falcopayload types.FalcoPayload) { nc, err := nats.Connect(c.EndpointURL.String()) if err != nil { c.setNatsErrorMetrics() - log.Printf("[ERROR] : NATS - %v\n", err) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return } defer nc.Flush() @@ -42,15 +41,15 @@ func (c *Client) NatsPublish(falcopayload types.FalcoPayload) { j, err := json.Marshal(falcopayload) if err != nil { - c.setStanErrorMetrics() - log.Printf("[ERROR] : STAN - %v\n", err.Error()) + c.setNatsErrorMetrics() + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return } err = nc.Publish(subject, j) if err != nil { c.setNatsErrorMetrics() - log.Printf("[ERROR] : NATS - %v\n", err) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return } @@ -58,7 +57,7 @@ func (c *Client) NatsPublish(falcopayload types.FalcoPayload) { c.Stats.Nats.Add(OK, 1) c.PromStats.Outputs.With(map[string]string{"destination": "nats", "status": OK}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "nats"), attribute.String("status", OK)).Inc() - log.Printf("[INFO] : NATS - Publish OK\n") + utils.Log(utils.InfoLvl, c.OutputType, "Publish OK") } // setNatsErrorMetrics set the error stats diff --git a/outputs/nodered.go b/outputs/nodered.go index 37bda45de..9fb723f67 100644 --- a/outputs/nodered.go +++ b/outputs/nodered.go @@ -3,10 +3,11 @@ package outputs import ( - "go.opentelemetry.io/otel/attribute" - "log" "net/http" + "go.opentelemetry.io/otel/attribute" + + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" "github.com/falcosecurity/falcosidekick/types" ) @@ -29,7 +30,7 @@ func (c *Client) NodeRedPost(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "nodered", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "nodered"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : NodeRed - %v\n", err.Error()) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return } diff --git a/outputs/openfaas.go b/outputs/openfaas.go index 0ac673df8..5608e9a1d 100644 --- a/outputs/openfaas.go +++ b/outputs/openfaas.go @@ -6,16 +6,16 @@ import ( "context" "encoding/json" "fmt" - "github.com/falcosecurity/falcosidekick/outputs/otlpmetrics" - "go.opentelemetry.io/otel/attribute" - "log" "strconv" "github.com/DataDog/datadog-go/statsd" "github.com/google/uuid" + "go.opentelemetry.io/otel/attribute" "k8s.io/client-go/kubernetes" "k8s.io/client-go/tools/clientcmd" + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" + "github.com/falcosecurity/falcosidekick/outputs/otlpmetrics" "github.com/falcosecurity/falcosidekick/types" ) @@ -75,10 +75,10 @@ func (c *Client) OpenfaasCall(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "openfaas", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "openfaas"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : %v - %v\n", Openfaas, err) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return } - log.Printf("[INFO] : %v - Function Response : %v\n", Openfaas, string(rawbody)) + utils.Log(utils.InfoLvl, c.OutputType, fmt.Sprintf("Function Response : %v", string(rawbody))) } else { err := c.Post(falcopayload) if err != nil { @@ -87,11 +87,11 @@ func (c *Client) OpenfaasCall(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "openfaas", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "openfaas"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : %v - %v\n", Openfaas, err) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return } } - log.Printf("[INFO] : %v - Call Function \"%v\" OK\n", Openfaas, c.Config.Openfaas.FunctionName+"."+c.Config.Openfaas.FunctionNamespace) + utils.Log(utils.InfoLvl, c.OutputType, fmt.Sprintf("Call Function \"%v\" OK", c.Config.Openfaas.FunctionName+"."+c.Config.Openfaas.FunctionNamespace)) go c.CountMetric(Outputs, 1, []string{"output:openfaas", "status:ok"}) c.Stats.Openfaas.Add(OK, 1) c.PromStats.Outputs.With(map[string]string{"destination": "openfaas", "status": OK}).Inc() diff --git a/outputs/openobserve.go b/outputs/openobserve.go index d64a5c2d0..88195fe37 100644 --- a/outputs/openobserve.go +++ b/outputs/openobserve.go @@ -3,10 +3,11 @@ package outputs import ( - "go.opentelemetry.io/otel/attribute" - "log" "net/http" + "go.opentelemetry.io/otel/attribute" + + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" "github.com/falcosecurity/falcosidekick/types" ) @@ -25,7 +26,7 @@ func (c *Client) OpenObservePost(falcopayload types.FalcoPayload) { }) if err != nil { c.setOpenObserveErrorMetrics() - log.Printf("[ERROR] : OpenObserve - %v\n", err) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return } diff --git a/outputs/opsgenie.go b/outputs/opsgenie.go index df83aedb0..e4b97a53d 100644 --- a/outputs/opsgenie.go +++ b/outputs/opsgenie.go @@ -3,11 +3,12 @@ package outputs import ( - "go.opentelemetry.io/otel/attribute" - "log" "net/http" "strings" + "go.opentelemetry.io/otel/attribute" + + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" "github.com/falcosecurity/falcosidekick/types" ) @@ -76,7 +77,7 @@ func (c *Client) OpsgeniePost(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "opsgenie", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "opsgenie"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : OpsGenie - %v\n", err) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return } diff --git a/outputs/otlp_traces.go b/outputs/otlp_traces.go index 1f133a95b..e1b26ebef 100644 --- a/outputs/otlp_traces.go +++ b/outputs/otlp_traces.go @@ -7,16 +7,17 @@ import ( "encoding/hex" "errors" "fmt" - "github.com/falcosecurity/falcosidekick/outputs/otlpmetrics" "hash/fnv" - "log" "time" "github.com/DataDog/datadog-go/statsd" - "github.com/falcosecurity/falcosidekick/types" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/trace" + + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" + "github.com/falcosecurity/falcosidekick/outputs/otlpmetrics" + "github.com/falcosecurity/falcosidekick/types" ) // Unit-testing helper @@ -38,10 +39,10 @@ func NewOtlpTracesClient(config *types.Configuration, stats *types.Statistics, p } shutDownFunc, err := otlpInit(config) if err != nil { - log.Printf("[ERROR] : OLTP Traces - Error client creation: %v\n", err) + utils.Log(utils.ErrorLvl, "OLTP Traces", fmt.Sprintf("Error client creation: %v", err)) return nil, err } - log.Printf("[INFO] : OTLP Traces - %+v\n", config.OTLP.Traces) + utils.Log(utils.InfoLvl, "OTLP Traces", "Client created") otlpClient.ShutDownFunc = shutDownFunc return otlpClient, nil } @@ -79,7 +80,7 @@ func (c *Client) newTrace(falcopayload types.FalcoPayload) (*trace.Span, error) span.End(trace.WithTimestamp(endTime)) if c.Config.Debug { - log.Printf("[DEBUG] : OTLP Traces - payload generated successfully for traceid=%s", span.SpanContext().TraceID()) + utils.Log(utils.DebugLvl, c.OutputType, fmt.Sprintf("Payload generated successfully for traceid=%s", span.SpanContext().TraceID())) } return &span, nil @@ -98,7 +99,7 @@ func (c *Client) OTLPTracesPost(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "otlptraces", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "otlptraces"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : OLTP Traces - Error generating trace: %v\n", err) + utils.Log(utils.ErrorLvl, c.OutputType, fmt.Sprintf("Error generating trace: %v", err)) return } // Setting the success status @@ -107,7 +108,7 @@ func (c *Client) OTLPTracesPost(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "otlptraces", "status": OK}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "otlptraces"), attribute.String("status", OK)).Inc() - log.Println("[INFO] : OLTP Traces - OK") + utils.Log(utils.InfoLvl, c.OutputType, "OK") } func generateTraceID(falcopayload types.FalcoPayload) (trace.TraceID, error) { diff --git a/outputs/otlp_traces_init.go b/outputs/otlp_traces_init.go index be1c16dbd..43a907488 100644 --- a/outputs/otlp_traces_init.go +++ b/outputs/otlp_traces_init.go @@ -5,17 +5,18 @@ package outputs import ( "context" "fmt" - "log" "os" "strings" - "github.com/falcosecurity/falcosidekick/types" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/exporters/otlp/otlptrace" "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp" otelresource "go.opentelemetry.io/otel/sdk/resource" sdktrace "go.opentelemetry.io/otel/sdk/trace" semconv "go.opentelemetry.io/otel/semconv/v1.23.1" + + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" + "github.com/falcosecurity/falcosidekick/types" ) const ( @@ -84,7 +85,7 @@ func otlpInit(config *types.Configuration) (func(), error) { } shutDownCallback := func() { if err := shutdown(ctx); err != nil { - log.Printf("[ERROR] : OLTP Traces - Error: %v\n", err) + utils.Log(utils.ErrorLvl, "OLTP", err.Error()) } } diff --git a/outputs/otlp_traces_test.go b/outputs/otlp_traces_test.go index 308905ef7..252af558e 100644 --- a/outputs/otlp_traces_test.go +++ b/outputs/otlp_traces_test.go @@ -6,10 +6,9 @@ import ( "testing" "github.com/stretchr/testify/require" + "go.opentelemetry.io/otel/trace" "github.com/falcosecurity/falcosidekick/types" - - "go.opentelemetry.io/otel/trace" ) func TestOtlpNewTrace(t *testing.T) { diff --git a/outputs/otlpmetrics/otlpmetrics.go b/outputs/otlpmetrics/otlpmetrics.go index f7cd2543b..113301df9 100644 --- a/outputs/otlpmetrics/otlpmetrics.go +++ b/outputs/otlpmetrics/otlpmetrics.go @@ -3,7 +3,6 @@ package otlpmetrics import ( "context" "fmt" - "log" "os" "strings" @@ -15,6 +14,8 @@ import ( sdkmetric "go.opentelemetry.io/otel/sdk/metric" sdkresource "go.opentelemetry.io/otel/sdk/resource" semconv "go.opentelemetry.io/otel/semconv/v1.23.1" + + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" ) const ( @@ -126,7 +127,7 @@ func setEnv(envVar, value string) (func(), error) { } return func() { if err := os.Setenv(envVar, ""); err != nil { - log.Printf("[ERROR] : OTLP Metrics - Error unsetting env variable %q: %v\n", envVar, err) + utils.Log(utils.ErrorLvl, "OTLP Metrics", fmt.Sprintf("Error unsetting env variable %q: %v", envVar, err)) } }, nil } @@ -173,7 +174,7 @@ func initMeterProvider(ctx context.Context, config *Config) (func(context.Contex func createGRPCExporter(ctx context.Context, config *Config) (sdkmetric.Exporter, error) { var options []otlpmetricgrpc.Option - if config.CheckCert == false { + if !config.CheckCert { options = append(options, otlpmetricgrpc.WithInsecure()) } @@ -182,7 +183,7 @@ func createGRPCExporter(ctx context.Context, config *Config) (sdkmetric.Exporter func createHTTPExporter(ctx context.Context, config *Config) (sdkmetric.Exporter, error) { var options []otlpmetrichttp.Option - if config.CheckCert == false { + if !config.CheckCert { options = append(options, otlpmetrichttp.WithInsecure()) } return otlpmetrichttp.New(ctx, options...) @@ -242,7 +243,7 @@ func (c *counterInstrument) Inc() { meter := otel.Meter(meterName) ruleCounter, err := meter.Int64Counter(c.name, metric.WithDescription(c.description)) if err != nil { - log.Printf("[ERROR] : OTLP Metrics - Error generating metric: %v\n", err) + utils.Log(utils.ErrorLvl, "OTLP Metrics", fmt.Sprintf("Error generating metric: %v", err)) return } diff --git a/outputs/pagerduty.go b/outputs/pagerduty.go index 135be3e4e..a01e4afc0 100644 --- a/outputs/pagerduty.go +++ b/outputs/pagerduty.go @@ -4,14 +4,14 @@ package outputs import ( "context" - "go.opentelemetry.io/otel/attribute" - "log" "sort" "strings" "time" "github.com/PagerDuty/go-pagerduty" + "go.opentelemetry.io/otel/attribute" + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" "github.com/falcosecurity/falcosidekick/types" ) @@ -38,7 +38,7 @@ func (c *Client) PagerdutyPost(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "pagerduty", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "pagerduty"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : PagerDuty - %v\n", err) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return } @@ -47,7 +47,7 @@ func (c *Client) PagerdutyPost(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "pagerduty", "status": OK}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "pagerduty"), attribute.String("status", OK)).Inc() - log.Printf("[INFO] : Pagerduty - Create Incident OK\n") + utils.Log(utils.InfoLvl, c.OutputType, "Create Incident OK") } func createPagerdutyEvent(falcopayload types.FalcoPayload, config types.PagerdutyConfig) pagerduty.V2Event { diff --git a/outputs/policyreport.go b/outputs/policyreport.go index 7246fd855..3b1859175 100644 --- a/outputs/policyreport.go +++ b/outputs/policyreport.go @@ -4,15 +4,11 @@ package outputs import ( "context" - "log" + "fmt" "os" - "github.com/falcosecurity/falcosidekick/outputs/otlpmetrics" - "go.opentelemetry.io/otel/attribute" - "github.com/DataDog/datadog-go/statsd" - "github.com/falcosecurity/falcosidekick/types" - + "go.opentelemetry.io/otel/attribute" corev1 "k8s.io/api/core/v1" errorsv1 "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -21,6 +17,10 @@ import ( "k8s.io/client-go/tools/clientcmd" wgpolicy "sigs.k8s.io/wg-policy-prototypes/policy-report/pkg/api/wgpolicyk8s.io/v1alpha2" crd "sigs.k8s.io/wg-policy-prototypes/policy-report/pkg/generated/v1alpha2/clientset/versioned" + + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" + "github.com/falcosecurity/falcosidekick/outputs/otlpmetrics" + "github.com/falcosecurity/falcosidekick/types" ) type resource struct { @@ -107,7 +107,7 @@ func NewPolicyReportClient(config *types.Configuration, stats *types.Statistics, if err != nil { clientConfig, err = clientcmd.BuildConfigFromFlags("", config.PolicyReport.Kubeconfig) if err != nil { - log.Printf("[ERROR] : PolicyReport - Unable to load kube config file: %v\n", err) + utils.Log(utils.ErrorLvl, "PolicyReport", fmt.Sprintf("Unable to load kube config file: %v", err)) return nil, err } } @@ -123,7 +123,7 @@ func NewPolicyReportClient(config *types.Configuration, stats *types.Statistics, if config.PolicyReport.FalcoNamespace == "" { dat, err := os.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace") if err != nil { - log.Printf("[ERROR] : PolicyReport - Unable to get the Falcosidekick's namespace, '%v' used instead\n", defaultNamespace) + utils.Log(utils.ErrorLvl, "PolicyReport", fmt.Sprintf("Unable to get the Falcosidekick's namespace, '%v' used instead", defaultNamespace)) } else { defaultNamespace = string(dat) } @@ -199,7 +199,7 @@ func (c *Client) createOrUpdatePolicyReport(result *wgpolicy.PolicyReportResult, _, err := c.KubernetesClient.CoreV1().Namespaces().Get(context.Background(), namespace, metav1.GetOptions{}) if err != nil { if errorsv1.IsNotFound(err) { - log.Printf("[INFO] : PolicyReport - Can't find the namespace '%v', fallback to '%v'\n", namespace, defaultNamespace) + utils.Log(utils.InfoLvl, c.OutputType, fmt.Sprintf("Can't find the namespace '%v', fallback to '%v'", namespace, defaultNamespace)) namespace = defaultNamespace result.Subjects[0].Namespace = defaultNamespace } @@ -231,28 +231,28 @@ func (c *Client) createOrUpdatePolicyReport(result *wgpolicy.PolicyReportResult, action = updateStr policyr, err = c.Crdclient.Wgpolicyk8sV1alpha2().PolicyReports(namespace).Get(context.Background(), policyReportName, metav1.GetOptions{}) if err != nil { - log.Printf("[ERROR] : PolicyReport - Error with with the Policy Report %v in namespace %v: %v\n", policyReportName, namespace, err) + utils.Log(utils.ErrorLvl, c.OutputType, fmt.Sprintf("Error with with the Policy Report %v in namespace %v: %v", policyReportName, namespace, err)) return err } _, err := c.Crdclient.Wgpolicyk8sV1alpha2().PolicyReports(namespace).Update(context.Background(), policyr, metav1.UpdateOptions{}) if err != nil { - log.Printf("[ERROR] : PolicyReport - Can't %v the Policy Report %v in namespace %v: %v\n", action, policyReportName, namespace, err) + utils.Log(utils.ErrorLvl, c.OutputType, fmt.Sprintf("Can't %v the Policy Report %v in namespace %v: %v", action, policyReportName, namespace, err)) return err } } else { - log.Printf("[ERROR] : PolicyReport - Can't %v the Policy Report %v in namespace %v: %v\n", action, policyReportName, namespace, err) + utils.Log(utils.ErrorLvl, c.OutputType, fmt.Sprintf("Can't %v the Policy Report %v in namespace %v: %v", action, policyReportName, namespace, err)) return err } } - log.Printf("[INFO] : PolicyReport - %v the Policy Report %v in namespace %v\n", action, policyReportName, namespace) + utils.Log(utils.InfoLvl, c.OutputType, fmt.Sprintf("%v the Policy Report %v in namespace %v", action, policyReportName, namespace)) return nil } else { _, err := c.Crdclient.Wgpolicyk8sV1alpha2().PolicyReports(namespace).Update(context.Background(), policyr, metav1.UpdateOptions{}) if err != nil { - log.Printf("[ERROR] : PolicyReport - Can't %v the Policy Report %v in namespace %v: %v\n", action, policyReportName, namespace, err) + utils.Log(utils.ErrorLvl, c.OutputType, fmt.Sprintf("Can't %v the Policy Report %v in namespace %v: %v", action, policyReportName, namespace, err)) return err } - log.Printf("[INFO] : PolicyReport - %v the Policy Report %v in namespace %v\n", action, policyReportName, namespace) + utils.Log(utils.InfoLvl, c.OutputType, fmt.Sprintf("%v the Policy Report %v in namespace %v", action, policyReportName, namespace)) return nil } } @@ -286,28 +286,28 @@ func (c *Client) createOrUpdateClusterPolicyReport(result *wgpolicy.PolicyReport action = updateStr cpolicyr, err = c.Crdclient.Wgpolicyk8sV1alpha2().ClusterPolicyReports().Get(context.Background(), policyReportName, metav1.GetOptions{}) if err != nil { - log.Printf("[ERROR] : PolicyReport - Error with with the Cluster Policy Report %v: %v\n", policyReportName, err) + utils.Log(utils.ErrorLvl, c.OutputType, fmt.Sprintf("Error with with the Cluster Policy Report %v: %v", policyReportName, err)) return err } _, err := c.Crdclient.Wgpolicyk8sV1alpha2().ClusterPolicyReports().Update(context.Background(), cpolicyr, metav1.UpdateOptions{}) if err != nil { - log.Printf("[ERROR] : PolicyReport - Can't %v the Cluster Policy Report %v: %v\n", action, policyReportName, err) + utils.Log(utils.ErrorLvl, c.OutputType, fmt.Sprintf("Can't %v the Cluster Policy Report %v: %v", action, policyReportName, err)) return err } } else { - log.Printf("[ERROR] : PolicyReport - Can't %v the Cluster Policy Report %v: %v\n", action, clusterPolicyReportName, err) + utils.Log(utils.ErrorLvl, c.OutputType, fmt.Sprintf("Can't %v the Cluster Policy Report %v: %v", action, clusterPolicyReportName, err)) return err } } - log.Printf("[INFO] : PolicyReport - %v Cluster the Policy Report %v\n", action, policyReportName) + utils.Log(utils.InfoLvl, c.OutputType, fmt.Sprintf("%v Cluster the Policy Report %v", action, policyReportName)) return nil } else { _, err := c.Crdclient.Wgpolicyk8sV1alpha2().ClusterPolicyReports().Update(context.Background(), cpolicyr, metav1.UpdateOptions{}) if err != nil { - log.Printf("[ERROR] : PolicyReport - Can't %v the Cluster Policy Report %v: %v\n", action, clusterPolicyReportName, err) + utils.Log(utils.ErrorLvl, c.OutputType, fmt.Sprintf("Can't %v the Cluster Policy Report %v: %v", action, clusterPolicyReportName, err)) return err } - log.Printf("[INFO] : PolicyReport - %v the ClusterPolicy Report %v\n", action, policyReportName) + utils.Log(utils.InfoLvl, c.OutputType, fmt.Sprintf("%v the ClusterPolicy Report %v", action, policyReportName)) return nil } } diff --git a/outputs/quickwit.go b/outputs/quickwit.go index d5edbf9ac..4d57100b7 100644 --- a/outputs/quickwit.go +++ b/outputs/quickwit.go @@ -4,10 +4,11 @@ package outputs import ( "fmt" - "go.opentelemetry.io/otel/attribute" - "log" "net/http" + "go.opentelemetry.io/otel/attribute" + + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" "github.com/falcosecurity/falcosidekick/types" ) @@ -144,7 +145,7 @@ func (c *Client) AutoCreateQuickwitIndex(args types.InitClientArgs) error { } if args.Config.Debug { - log.Printf("[DEBUG] : Quickwit - mapping: %#v\n", mapping) + utils.Log(utils.DebugLvl, c.OutputType, fmt.Sprintf("mapping: %v", mapping)) } err = quickwitInitClient.Post(mapping) @@ -161,7 +162,7 @@ func (c *Client) QuickwitPost(falcopayload types.FalcoPayload) { c.Stats.Quickwit.Add(Total, 1) if c.Config.Debug { - log.Printf("[DEBUG] : Quickwit - ingesting payload: %v\n", falcopayload) + utils.Log(utils.DebugLvl, c.OutputType, fmt.Sprintf("ingesting payload: %v", falcopayload)) } err := c.Post(falcopayload, func(req *http.Request) { @@ -176,7 +177,7 @@ func (c *Client) QuickwitPost(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "quickwit", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "quickwit"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : Quickwit - %v\n", err.Error()) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return } diff --git a/outputs/rabbitmq.go b/outputs/rabbitmq.go index 9e18069d6..bb1c31487 100644 --- a/outputs/rabbitmq.go +++ b/outputs/rabbitmq.go @@ -5,13 +5,15 @@ package outputs import ( "encoding/json" "errors" - "github.com/falcosecurity/falcosidekick/outputs/otlpmetrics" - "go.opentelemetry.io/otel/attribute" - "log" + "fmt" "github.com/DataDog/datadog-go/statsd" - "github.com/falcosecurity/falcosidekick/types" amqp "github.com/rabbitmq/amqp091-go" + "go.opentelemetry.io/otel/attribute" + + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" + "github.com/falcosecurity/falcosidekick/outputs/otlpmetrics" + "github.com/falcosecurity/falcosidekick/types" ) // NewRabbitmqClient returns a new output.Client for accessing the RabbitmMQ API. @@ -22,12 +24,12 @@ func NewRabbitmqClient(config *types.Configuration, stats *types.Statistics, pro if config.Rabbitmq.URL != "" && config.Rabbitmq.Queue != "" { conn, err := amqp.Dial(config.Rabbitmq.URL) if err != nil { - log.Printf("[ERROR] : Rabbitmq - %v\n", "Error while connecting rabbitmq") + utils.Log(utils.ErrorLvl, "Rabbitmq", "Error while connecting rabbitmq") return nil, errors.New("error while connecting Rabbitmq") } ch, err := conn.Channel() if err != nil { - log.Printf("[ERROR] : Rabbitmq Channel - %v\n", "Error while creating rabbitmq channel") + utils.Log(utils.ErrorLvl, "Rabbitmq", "Error while creating rabbitmq channel") return nil, errors.New("error while creating rabbitmq channel") } channel = ch @@ -57,7 +59,7 @@ func (c *Client) Publish(falcopayload types.FalcoPayload) { }) if err != nil { - log.Printf("[ERROR] : RabbitMQ - %v - %v\n", "Error while publishing message", err.Error()) + utils.Log(utils.ErrorLvl, c.OutputType, fmt.Sprintf("Error while publishing message: %v", err)) c.Stats.Rabbitmq.Add(Error, 1) go c.CountMetric("outputs", 1, []string{"output:rabbitmq", "status:error"}) c.PromStats.Outputs.With(map[string]string{"destination": "rabbitmq", "status": Error}).Inc() @@ -67,7 +69,7 @@ func (c *Client) Publish(falcopayload types.FalcoPayload) { return } - log.Printf("[INFO] : RabbitMQ - Send to message OK \n") + utils.Log(utils.InfoLvl, c.OutputType, "Message published OK") c.Stats.Rabbitmq.Add(OK, 1) go c.CountMetric("outputs", 1, []string{"output:rabbitmq", "status:ok"}) c.PromStats.Outputs.With(map[string]string{"destination": "rabbitmq", "status": OK}).Inc() diff --git a/outputs/redis.go b/outputs/redis.go index 24c67904f..dcda8e56c 100644 --- a/outputs/redis.go +++ b/outputs/redis.go @@ -5,14 +5,16 @@ package outputs import ( "context" "encoding/json" - "github.com/falcosecurity/falcosidekick/outputs/otlpmetrics" - "go.opentelemetry.io/otel/attribute" - "log" + "fmt" "strings" "github.com/DataDog/datadog-go/statsd" - "github.com/falcosecurity/falcosidekick/types" "github.com/redis/go-redis/v9" + "go.opentelemetry.io/otel/attribute" + + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" + "github.com/falcosecurity/falcosidekick/outputs/otlpmetrics" + "github.com/falcosecurity/falcosidekick/types" ) func (c *Client) ReportError(err error) { @@ -21,7 +23,7 @@ func (c *Client) ReportError(err error) { c.PromStats.Outputs.With(map[string]string{"destination": "redis", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "redis"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : Redis - %v\n", err) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) } func NewRedisClient(config *types.Configuration, stats *types.Statistics, promStats *types.PromStatistics, @@ -35,9 +37,9 @@ func NewRedisClient(config *types.Configuration, stats *types.Statistics, promSt // Ping the Redis server to check if it's running pong, err := rClient.Ping(context.Background()).Result() if err != nil { - log.Printf("[ERROR] : Redis - Misconfiguration, cannot connect to the server %v\n", err) + utils.Log(utils.ErrorLvl, "Redis", fmt.Sprintf("Misconfiguration, cannot connect to the server: %v", err)) } - log.Printf("[INFO] : Redis - Connected to redis server: %v\n", pong) + utils.Log(utils.InfoLvl, "Redis", fmt.Sprintf("Connected to redis server: %v", pong)) return &Client{ OutputType: "Redis", diff --git a/outputs/rocketchat.go b/outputs/rocketchat.go index 168bd3752..cae03d240 100644 --- a/outputs/rocketchat.go +++ b/outputs/rocketchat.go @@ -4,11 +4,13 @@ package outputs import ( "bytes" - "go.opentelemetry.io/otel/attribute" - "log" + "fmt" "sort" "strings" + "go.opentelemetry.io/otel/attribute" + + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" "github.com/falcosecurity/falcosidekick/types" ) @@ -74,7 +76,7 @@ func newRocketchatPayload(falcopayload types.FalcoPayload, config *types.Configu if config.Rocketchat.MessageFormatTemplate != nil { buf := &bytes.Buffer{} if err := config.Rocketchat.MessageFormatTemplate.Execute(buf, falcopayload); err != nil { - log.Printf("[ERROR] : RocketChat - Error expanding RocketChat message %v", err) + utils.Log(utils.ErrorLvl, "RocketChat", fmt.Sprintf("Error expanding RocketChat message: %v", err)) } else { messageText = buf.String() } @@ -130,7 +132,7 @@ func (c *Client) RocketchatPost(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "rocketchat", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "rocketchat"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : RocketChat - %v\n", err.Error()) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return } diff --git a/outputs/slack.go b/outputs/slack.go index bed8c49d7..b0cf49006 100644 --- a/outputs/slack.go +++ b/outputs/slack.go @@ -4,11 +4,13 @@ package outputs import ( "bytes" - "go.opentelemetry.io/otel/attribute" - "log" + "fmt" "sort" "strings" + "go.opentelemetry.io/otel/attribute" + + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" "github.com/falcosecurity/falcosidekick/types" ) @@ -104,7 +106,7 @@ func newSlackPayload(falcopayload types.FalcoPayload, config *types.Configuratio if config.Slack.MessageFormatTemplate != nil { buf := &bytes.Buffer{} if err := config.Slack.MessageFormatTemplate.Execute(buf, falcopayload); err != nil { - log.Printf("[ERROR] : Slack - Error expanding Slack message %v", err) + utils.Log(utils.ErrorLvl, "Slack", fmt.Sprintf("Error expanding Slack message: %v", err)) } else { messageText = buf.String() } @@ -157,7 +159,7 @@ func (c *Client) SlackPost(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "slack", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "slack"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : Slack - %v\n", err) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return } diff --git a/outputs/smtp.go b/outputs/smtp.go index 6a0600dc5..1678bb86b 100644 --- a/outputs/smtp.go +++ b/outputs/smtp.go @@ -5,9 +5,8 @@ package outputs import ( "bytes" "crypto/tls" - "github.com/falcosecurity/falcosidekick/outputs/otlpmetrics" + "fmt" htmlTemplate "html/template" - "log" "net" "regexp" "strconv" @@ -18,6 +17,8 @@ import ( sasl "github.com/emersion/go-sasl" smtp "github.com/emersion/go-smtp" + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" + "github.com/falcosecurity/falcosidekick/outputs/otlpmetrics" "github.com/falcosecurity/falcosidekick/types" ) @@ -36,7 +37,7 @@ func NewSMTPClient(config *types.Configuration, stats *types.Statistics, promSta otlpMetrics *otlpmetrics.OTLPMetrics, statsdClient, dogstatsdClient *statsd.Client) (*Client, error) { reg := regexp.MustCompile(`.*:[0-9]+`) if !reg.MatchString(config.SMTP.HostPort) { - log.Printf("[ERROR] : SMTP - Bad Host:Port\n") + utils.Log(utils.ErrorLvl, "SMTP", "Bad Host:Port") return nil, ErrClientCreation } @@ -74,7 +75,7 @@ func newSMTPPayload(falcopayload types.FalcoPayload, config *types.Configuration var outtext bytes.Buffer err := ttmpl.Execute(&outtext, falcopayload) if err != nil { - log.Printf("[ERROR] : SMTP - %v\n", err) + utils.Log(utils.ErrorLvl, "SMTP", err.Error()) return s } s.Body += outtext.String() @@ -90,7 +91,7 @@ func newSMTPPayload(falcopayload types.FalcoPayload, config *types.Configuration var outhtml bytes.Buffer err = htmpl.Execute(&outhtml, falcopayload) if err != nil { - log.Printf("[ERROR] : SMTP - %v\n", err) + utils.Log(utils.ErrorLvl, "SMTP", err.Error()) return s } s.Body += outhtml.String() @@ -101,7 +102,7 @@ func newSMTPPayload(falcopayload types.FalcoPayload, config *types.Configuration func (c *Client) ReportErr(message string, err error) { go c.CountMetric("outputs", 1, []string{"output:smtp", "status:error"}) c.Stats.SMTP.Add(Error, 1) - log.Printf("[ERROR] : SMTP - %s : %v\n", message, err) + utils.Log(utils.ErrorLvl, c.OutputType, fmt.Sprintf("%s : %v", message, err)) } func (c *Client) GetAuth() (sasl.Client, error) { @@ -163,11 +164,11 @@ func (c *Client) SendMail(falcopayload types.FalcoPayload) { body := sp.Subject + "\n" + sp.Body if c.Config.Debug { - log.Printf("[DEBUG] : SMTP payload : \nServer: %v\n%v\n%v\nSubject: %v\n", c.Config.SMTP.HostPort, sp.From, sp.To, sp.Subject) + utils.Log(utils.DebugLvl, c.OutputType, fmt.Sprintf("payload : \nServer: %v\n%v\n%v\nSubject: %v", c.Config.SMTP.HostPort, sp.From, sp.To, sp.Subject)) if c.Config.SMTP.AuthMechanism != "" { - log.Printf("[DEBUG] : SMTP - SASL Auth : \nMechanisms: %v\nUser: %v\nToken: %v\nIdentity: %v\nTrace: %v\n", c.Config.SMTP.AuthMechanism, c.Config.SMTP.User, c.Config.SMTP.Token, c.Config.SMTP.Identity, c.Config.SMTP.Trace) + utils.Log(utils.DebugLvl, c.OutputType, fmt.Sprintf("SASL Auth : \nMechanisms: %v\nUser: %v\nToken: %v\nIdentity: %v\nTrace: %v", c.Config.SMTP.AuthMechanism, c.Config.SMTP.User, c.Config.SMTP.Token, c.Config.SMTP.Identity, c.Config.SMTP.Trace)) } else { - log.Printf("[DEBUG] : SMTP - SASL Auth : Disabled\n") + utils.Log(utils.DebugLvl, c.OutputType, "SASL Auth : Disabled") } } @@ -178,7 +179,7 @@ func (c *Client) SendMail(falcopayload types.FalcoPayload) { return } - log.Printf("[INFO] : SMTP - Sent OK\n") + utils.Log(utils.InfoLvl, c.OutputType, " SMTP - Sent OK\n") go c.CountMetric("outputs", 1, []string{"output:smtp", "status:ok"}) c.Stats.SMTP.Add(OK, 1) } diff --git a/outputs/spyderbat.go b/outputs/spyderbat.go index 259813a58..3f8cf0e22 100644 --- a/outputs/spyderbat.go +++ b/outputs/spyderbat.go @@ -7,18 +7,19 @@ import ( "encoding/json" "errors" "fmt" - "github.com/falcosecurity/falcosidekick/outputs/otlpmetrics" - "go.opentelemetry.io/otel/attribute" "io" - "log" "net/http" "net/url" "strings" "time" "github.com/DataDog/datadog-go/statsd" - "github.com/falcosecurity/falcosidekick/types" "github.com/google/uuid" + "go.opentelemetry.io/otel/attribute" + + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" + "github.com/falcosecurity/falcosidekick/outputs/otlpmetrics" + "github.com/falcosecurity/falcosidekick/types" ) const Falcosidekick_ string = "falcosidekick_" @@ -190,13 +191,13 @@ func NewSpyderbatClient(config *types.Configuration, stats *types.Statistics, pr hasSource, err := isSourcePresent(config) if err != nil { - log.Printf("[ERROR] : Spyderbat - %v\n", err.Error()) + utils.Log(utils.ErrorLvl, "Spyderbat", err.Error()) return nil, ErrClientCreation } if !hasSource { if err := makeSource(config); err != nil { if hasSource, err2 := isSourcePresent(config); err2 != nil || !hasSource { - log.Printf("[ERROR] : Spyderbat - %v\n", err.Error()) + utils.Log(utils.ErrorLvl, "Spyderbat", err.Error()) return nil, ErrClientCreation } } @@ -205,12 +206,12 @@ func NewSpyderbatClient(config *types.Configuration, stats *types.Statistics, pr source := Falcosidekick_ + config.Spyderbat.OrgUID data_url, err := url.JoinPath(config.Spyderbat.APIUrl, APIv1Path+config.Spyderbat.OrgUID+SourcePath+source+"/data/sb-agent") if err != nil { - log.Printf("[ERROR] : Spyderbat - %v\n", err.Error()) + utils.Log(utils.ErrorLvl, "Spyderbat", err.Error()) return nil, ErrClientCreation } endpointURL, err := url.Parse(data_url) if err != nil { - log.Printf("[ERROR] : Spyderbat - %v\n", err.Error()) + utils.Log(utils.ErrorLvl, "Spyderbat", err.Error()) return nil, ErrClientCreation } return &Client{ @@ -243,7 +244,7 @@ func (c *Client) SpyderbatPost(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "spyderbat", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "spyderbat"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : Spyderbat - %v\n", err.Error()) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return } diff --git a/outputs/stan.go b/outputs/stan.go index b92710bc7..b6b2737d0 100644 --- a/outputs/stan.go +++ b/outputs/stan.go @@ -4,13 +4,12 @@ package outputs import ( "encoding/json" - "log" "strings" - "go.opentelemetry.io/otel/attribute" - stan "github.com/nats-io/stan.go" + "go.opentelemetry.io/otel/attribute" + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" "github.com/falcosecurity/falcosidekick/types" ) @@ -29,7 +28,7 @@ func (c *Client) StanPublish(falcopayload types.FalcoPayload) { nc, err := stan.Connect(c.Config.Stan.ClusterID, c.Config.Stan.ClientID, stan.NatsURL(c.EndpointURL.String())) if err != nil { c.setStanErrorMetrics() - log.Printf("[ERROR] : STAN - %v\n", err.Error()) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return } defer nc.Close() @@ -37,14 +36,14 @@ func (c *Client) StanPublish(falcopayload types.FalcoPayload) { j, err := json.Marshal(falcopayload) if err != nil { c.setStanErrorMetrics() - log.Printf("[ERROR] : STAN - %v\n", err.Error()) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return } err = nc.Publish(subject, j) if err != nil { c.setStanErrorMetrics() - log.Printf("[ERROR] : STAN - %v\n", err) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return } @@ -53,7 +52,7 @@ func (c *Client) StanPublish(falcopayload types.FalcoPayload) { c.Stats.Stan.Add(OK, 1) c.PromStats.Outputs.With(map[string]string{"destination": "stan", "status": OK}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "stan"), attribute.String("status", OK)).Inc() - log.Printf("[INFO] : STAN - Publish OK\n") + utils.Log(utils.InfoLvl, c.OutputType, "Publish OK") } // setStanErrorMetrics set the error stats diff --git a/outputs/statsd.go b/outputs/statsd.go index 703156b5e..f17a59222 100644 --- a/outputs/statsd.go +++ b/outputs/statsd.go @@ -3,12 +3,13 @@ package outputs import ( - "go.opentelemetry.io/otel/attribute" - "log" + "fmt" "strings" "github.com/DataDog/datadog-go/statsd" + "go.opentelemetry.io/otel/attribute" + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" "github.com/falcosecurity/falcosidekick/types" ) @@ -26,7 +27,7 @@ func NewStatsdClient(outputType string, config *types.Configuration, stats *type fwd = config.Dogstatsd.Forwarder } if err != nil { - log.Printf("[ERROR] : Can't configure %v client for %v - %v", outputType, fwd, err) + utils.Log(utils.ErrorLvl, outputType, fmt.Sprintf("Can't configure client for %v - %v", fwd, err)) return nil, err } @@ -50,7 +51,7 @@ func (c *Client) CountMetric(metric string, value int64, tags []string) { c.PromStats.Outputs.With(map[string]string{"destination": "statsd", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "statsd"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : StatsD - Unable to send metric (%v%v%v) : %v\n", c.Config.Statsd.Namespace, metric, t, err) + utils.Log(utils.ErrorLvl, c.OutputType, fmt.Sprintf("Unable to send metric (%v%v%v) : %v", c.Config.Statsd.Namespace, metric, t, err)) return } @@ -59,7 +60,7 @@ func (c *Client) CountMetric(metric string, value int64, tags []string) { c.PromStats.Outputs.With(map[string]string{"destination": "statsd", "status": OK}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "statsd"), attribute.String("status", OK)).Inc() - log.Printf("[INFO] : StatsD - Send Metric OK (%v%v%v)\n", c.Config.Statsd.Namespace, metric, t) + utils.Log(utils.InfoLvl, c.OutputType, fmt.Sprintf("Send Metric OK (%v%v%v)", c.Config.Statsd.Namespace, metric, t)) } if c.DogstatsdClient != nil { @@ -69,7 +70,7 @@ func (c *Client) CountMetric(metric string, value int64, tags []string) { c.PromStats.Outputs.With(map[string]string{"destination": "dogstatsd", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "dogstatsd"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : DogStatsD - Send Metric Error (%v%v%v) : %v\n", c.Config.Statsd.Namespace, metric, tags, err) + utils.Log(utils.ErrorLvl, c.OutputType, fmt.Sprintf("Send Metric Error (%v%v%v) : %v", c.Config.Statsd.Namespace, metric, tags, err)) return } @@ -78,6 +79,6 @@ func (c *Client) CountMetric(metric string, value int64, tags []string) { c.PromStats.Outputs.With(map[string]string{"destination": "dogstatsd", "status": OK}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "dogstatsd"), attribute.String("status", OK)).Inc() - log.Printf("[INFO] : DogStatsD - Send Metric OK (%v%v %v)\n", c.Config.Statsd.Namespace, metric, tags) + utils.Log(utils.InfoLvl, c.OutputType, fmt.Sprintf("Send Metric OK (%v%v %v)", c.Config.Statsd.Namespace, metric, tags)) } } diff --git a/outputs/sumologic.go b/outputs/sumologic.go index 8eee95ecc..66e2641fe 100644 --- a/outputs/sumologic.go +++ b/outputs/sumologic.go @@ -3,11 +3,12 @@ package outputs import ( - "go.opentelemetry.io/otel/attribute" - "log" "net/http" "net/url" + "go.opentelemetry.io/otel/attribute" + + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" "github.com/falcosecurity/falcosidekick/types" ) @@ -18,7 +19,7 @@ func (c *Client) SumoLogicPost(falcopayload types.FalcoPayload) { endpointURL, err := url.Parse(c.Config.SumoLogic.ReceiverURL) if err != nil { c.setSumoLogicErrorMetrics() - log.Printf("[ERROR] : %v - %v\n", c.OutputType, err.Error()) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return } @@ -43,7 +44,7 @@ func (c *Client) SumoLogicPost(falcopayload types.FalcoPayload) { if err != nil { c.setSumoLogicErrorMetrics() - log.Printf("[ERROR] : %x - %v\n", c.OutputType, err) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return } diff --git a/outputs/syslog.go b/outputs/syslog.go index 7a1b4a302..408456330 100644 --- a/outputs/syslog.go +++ b/outputs/syslog.go @@ -5,14 +5,15 @@ package outputs import ( "encoding/json" "fmt" - "github.com/falcosecurity/falcosidekick/outputs/otlpmetrics" - "go.opentelemetry.io/otel/attribute" - "log" "log/syslog" "strings" "time" "github.com/DataDog/datadog-go/statsd" + "go.opentelemetry.io/otel/attribute" + + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" + "github.com/falcosecurity/falcosidekick/outputs/otlpmetrics" "github.com/falcosecurity/falcosidekick/types" ) @@ -92,7 +93,7 @@ func (c *Client) SyslogPost(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "syslog", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "syslog"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : Syslog - %v\n", err) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return } @@ -130,7 +131,7 @@ func (c *Client) SyslogPost(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "syslog", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "syslog"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : Syslog - %v\n", err) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return } diff --git a/outputs/talon.go b/outputs/talon.go index 84d05dca8..de09d5d43 100644 --- a/outputs/talon.go +++ b/outputs/talon.go @@ -3,10 +3,9 @@ package outputs import ( - "log" - "go.opentelemetry.io/otel/attribute" + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" "github.com/falcosecurity/falcosidekick/types" ) @@ -21,7 +20,7 @@ func (c *Client) TalonPost(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "talon", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "talon"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : Talon - %v\n", err.Error()) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return } diff --git a/outputs/teams.go b/outputs/teams.go index 74d4dbea3..74f5f0e10 100644 --- a/outputs/teams.go +++ b/outputs/teams.go @@ -3,11 +3,12 @@ package outputs import ( - "go.opentelemetry.io/otel/attribute" - "log" "sort" "strings" + "go.opentelemetry.io/otel/attribute" + + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" "github.com/falcosecurity/falcosidekick/types" ) @@ -126,7 +127,7 @@ func (c *Client) TeamsPost(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "teams", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "teams"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : Teams - %v\n", err) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return } diff --git a/outputs/tekton.go b/outputs/tekton.go index e3afd7e2d..d166cdbdb 100644 --- a/outputs/tekton.go +++ b/outputs/tekton.go @@ -4,8 +4,8 @@ package outputs import ( "go.opentelemetry.io/otel/attribute" - "log" + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" "github.com/falcosecurity/falcosidekick/types" ) @@ -20,7 +20,7 @@ func (c *Client) TektonPost(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "tekton", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "tekton"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : Tekton - %v\n", err.Error()) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return } diff --git a/outputs/telegram.go b/outputs/telegram.go index 53edf8486..3672d087d 100644 --- a/outputs/telegram.go +++ b/outputs/telegram.go @@ -5,11 +5,12 @@ package outputs import ( "bytes" "fmt" - "go.opentelemetry.io/otel/attribute" - "log" "strings" textTemplate "text/template" + "go.opentelemetry.io/otel/attribute" + + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" "github.com/falcosecurity/falcosidekick/types" ) @@ -70,7 +71,7 @@ func newTelegramPayload(falcopayload types.FalcoPayload, config *types.Configura ttmpl, _ := textTemplate.New("telegram").Funcs(funcs).Parse(telegramMarkdownV2Tmpl) err := ttmpl.Execute(&textBuffer, falcopayload) if err != nil { - log.Printf("[ERROR] : Telegram - %v\n", err) + utils.Log(utils.ErrorLvl, "Telegram", err.Error()) return payload } payload.Text = textBuffer.String() @@ -89,7 +90,7 @@ func (c *Client) TelegramPost(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "telegram", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "telegram"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : Telegram - %v\n", err) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return } diff --git a/outputs/timescaledb.go b/outputs/timescaledb.go index ea3d69f35..c272c88ec 100644 --- a/outputs/timescaledb.go +++ b/outputs/timescaledb.go @@ -5,14 +5,15 @@ package outputs import ( "context" "fmt" - "github.com/falcosecurity/falcosidekick/outputs/otlpmetrics" - "go.opentelemetry.io/otel/attribute" - "log" "strings" "github.com/DataDog/datadog-go/statsd" - "github.com/falcosecurity/falcosidekick/types" "github.com/jackc/pgx/v5/pgxpool" + "go.opentelemetry.io/otel/attribute" + + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" + "github.com/falcosecurity/falcosidekick/outputs/otlpmetrics" + "github.com/falcosecurity/falcosidekick/types" ) type timescaledbPayload struct { @@ -34,7 +35,7 @@ func NewTimescaleDBClient(config *types.Configuration, stats *types.Statistics, ) connPool, err := pgxpool.New(ctx, connStr) if err != nil { - log.Printf("[ERROR] : TimescaleDB - %v\n", err) + utils.Log(utils.ErrorLvl, "TimescaleDB", err.Error()) return nil, ErrClientCreation } @@ -125,7 +126,7 @@ func (c *Client) TimescaleDBPost(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "timescaledb", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "timescaledb"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : TimescaleDB - %v\n", err) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return } @@ -136,6 +137,6 @@ func (c *Client) TimescaleDBPost(falcopayload types.FalcoPayload) { attribute.String("status", OK)).Inc() if c.Config.Debug { - log.Printf("[DEBUG] : TimescaleDB payload : %v\n", tsdbPayload) + utils.Log(utils.DebugLvl, c.OutputType, fmt.Sprintf("payload : %v", tsdbPayload)) } } diff --git a/outputs/wavefront.go b/outputs/wavefront.go index 36032d0b3..670051961 100644 --- a/outputs/wavefront.go +++ b/outputs/wavefront.go @@ -4,15 +4,15 @@ package outputs import ( "fmt" - "github.com/falcosecurity/falcosidekick/outputs/otlpmetrics" - "go.opentelemetry.io/otel/attribute" - "log" "strings" "github.com/DataDog/datadog-go/statsd" - "github.com/falcosecurity/falcosidekick/types" - wavefront "github.com/wavefronthq/wavefront-sdk-go/senders" + "go.opentelemetry.io/otel/attribute" + + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" + "github.com/falcosecurity/falcosidekick/outputs/otlpmetrics" + "github.com/falcosecurity/falcosidekick/types" ) // NewWavefrontClient returns a new output.Client for accessing the Wavefront API. @@ -101,7 +101,7 @@ func (c *Client) WavefrontPost(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "wavefront", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "wavefront"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : Wavefront - Unable to send event %s: %s\n", falcopayload.Rule, err) + utils.Log(utils.ErrorLvl, c.OutputType, fmt.Sprintf("Unable to send event %s: %s", falcopayload.Rule, err)) return } if err := sender.Flush(); err != nil { @@ -109,13 +109,13 @@ func (c *Client) WavefrontPost(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "wavefront", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "wavefront"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : Wavefront - Unable to flush event %s: %s\n", falcopayload.Rule, err) + utils.Log(utils.ErrorLvl, c.OutputType, fmt.Sprintf("Unable to flush event %s: %s", falcopayload.Rule, err)) return } c.Stats.Wavefront.Add(OK, 1) c.PromStats.Outputs.With(map[string]string{"destination": "wavefront", "status": OK}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "wavefront"), attribute.String("status", OK)).Inc() - log.Printf("[INFO] : Wavefront - Send Event OK %s\n", falcopayload.Rule) + utils.Log(utils.InfoLvl, c.OutputType, fmt.Sprintf("Send Event %v OK", falcopayload.Rule)) } } diff --git a/outputs/webex.go b/outputs/webex.go index 2694d4f27..ee04e7c85 100644 --- a/outputs/webex.go +++ b/outputs/webex.go @@ -4,10 +4,11 @@ package outputs import ( "bytes" - "go.opentelemetry.io/otel/attribute" - "log" "text/template" + "go.opentelemetry.io/otel/attribute" + + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" "github.com/falcosecurity/falcosidekick/types" ) @@ -41,7 +42,7 @@ func newWebexPayload(falcopayload types.FalcoPayload) webexPayload { var tpl bytes.Buffer if err := webexTmpl.Execute(&tpl, falcopayload); err != nil { - log.Printf("[ERROR] : Webex Template - %v\n", err) + utils.Log(utils.ErrorLvl, "Webex", err.Error()) } t := webexPayload{ @@ -63,7 +64,7 @@ func (c *Client) WebexPost(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "webex", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "webex"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : Webex - %v\n", err.Error()) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return } diff --git a/outputs/webhook.go b/outputs/webhook.go index ffbfbdfcf..3cfed0866 100644 --- a/outputs/webhook.go +++ b/outputs/webhook.go @@ -3,11 +3,12 @@ package outputs import ( - "go.opentelemetry.io/otel/attribute" - "log" "net/http" "strings" + "go.opentelemetry.io/otel/attribute" + + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" "github.com/falcosecurity/falcosidekick/types" ) @@ -33,7 +34,7 @@ func (c *Client) WebhookPost(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "webhook", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "webhook"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : WebHook - %v\n", err.Error()) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return } diff --git a/outputs/webui.go b/outputs/webui.go index 37d84093c..0e6df7170 100644 --- a/outputs/webui.go +++ b/outputs/webui.go @@ -4,8 +4,8 @@ package outputs import ( "go.opentelemetry.io/otel/attribute" - "log" + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" "github.com/falcosecurity/falcosidekick/types" ) @@ -32,7 +32,7 @@ func (c *Client) WebUIPost(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "webui", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "webui"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : WebUI - %v\n", err.Error()) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return } diff --git a/outputs/yandex.go b/outputs/yandex.go index 943a53a19..a354b847b 100644 --- a/outputs/yandex.go +++ b/outputs/yandex.go @@ -7,20 +7,20 @@ import ( "encoding/json" "errors" "fmt" - "github.com/falcosecurity/falcosidekick/outputs/otlpmetrics" - "go.opentelemetry.io/otel/attribute" - "log" "time" "github.com/DataDog/datadog-go/statsd" "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/credentials" "github.com/aws/aws-sdk-go/aws/endpoints" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/kinesis" "github.com/aws/aws-sdk-go/service/s3" "github.com/google/uuid" + "go.opentelemetry.io/otel/attribute" - "github.com/aws/aws-sdk-go/aws/credentials" + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" + "github.com/falcosecurity/falcosidekick/outputs/otlpmetrics" "github.com/falcosecurity/falcosidekick/types" ) @@ -50,10 +50,10 @@ func NewYandexClient(config *types.Configuration, stats *types.Statistics, promS EndpointResolver: endpoints.ResolverFunc(resolverFn), }) if err != nil { - log.Printf("[ERROR] : Yandex - %v\n", "Error while creating Yandex Session") + utils.Log(utils.ErrorLvl, "Yandex", "Error while creating Yandex Session") return nil, errors.New("error while creating Yandex Session") } - log.Printf("[INFO] : Yandex Session has been configured successfully") + utils.Log(utils.InfoLvl, "Yandex", "Session has been configured successfully") return &Client{ OutputType: "Yandex", @@ -86,11 +86,11 @@ func (c *Client) UploadYandexS3(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "yandexs3", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "yandexs3"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : %v S3 - %v\n", c.OutputType, err.Error()) + utils.Log(utils.ErrorLvl, c.OutputType+" S3", err.Error()) return } - log.Printf("[INFO] : %v S3 - Upload payload OK\n", c.OutputType) + utils.Log(utils.InfoLvl, c.OutputType+" S3", "Upload payload OK") go c.CountMetric("outputs", 1, []string{"output:yandexs3", "status:ok"}) c.PromStats.Outputs.With(map[string]string{"destination": "yandexs3", "status": "ok"}).Inc() @@ -115,11 +115,11 @@ func (c *Client) UploadYandexDataStreams(falcoPayLoad types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "yandexdatastreams", "status": Error}).Inc() c.OTLPMetrics.Outputs.With(attribute.String("destination", "yandexs3"), attribute.String("status", Error)).Inc() - log.Printf("[ERROR] : %v Data Streams - %v\n", c.OutputType, err.Error()) + utils.Log(utils.ErrorLvl, c.OutputType+" Data Streams", err.Error()) return } - log.Printf("[INFO] : %v Data Streams - Put Record OK (%v)\n", c.OutputType, resp.SequenceNumber) + utils.Log(utils.InfoLvl, c.OutputType+"Data Streams", fmt.Sprintf("Put Record OK (%v)", resp.SequenceNumber)) go c.CountMetric("outputs", 1, []string{"output:yandexdatastreams", "status:ok"}) c.Stats.YandexDataStreams.Add(OK, 1) c.PromStats.Outputs.With(map[string]string{"destination": "yandexdatastreams", "status": "ok"}).Inc() diff --git a/outputs/zincsearch.go b/outputs/zincsearch.go index f0d0a7b8c..2cddc0c1d 100644 --- a/outputs/zincsearch.go +++ b/outputs/zincsearch.go @@ -3,10 +3,11 @@ package outputs import ( - "go.opentelemetry.io/otel/attribute" - "log" "net/http" + "go.opentelemetry.io/otel/attribute" + + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" "github.com/falcosecurity/falcosidekick/types" ) @@ -21,7 +22,7 @@ func (c *Client) ZincsearchPost(falcopayload types.FalcoPayload) { }) if err != nil { c.setZincsearchErrorMetrics() - log.Printf("[ERROR] : Zincsearch - %v\n", err) + utils.Log(utils.ErrorLvl, c.OutputType, err.Error()) return } diff --git a/stats_prometheus.go b/stats_prometheus.go index ccea63b68..3ae874c2e 100644 --- a/stats_prometheus.go +++ b/stats_prometheus.go @@ -3,13 +3,14 @@ package main import ( - "log" + "fmt" "regexp" "strings" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" + "github.com/falcosecurity/falcosidekick/internal/pkg/utils" "github.com/falcosecurity/falcosidekick/types" ) @@ -55,14 +56,14 @@ func getFalcoNewCounterVec(config *types.Configuration) *prometheus.CounterVec { } for i := range config.Customfields { if !regPromLabels.MatchString(strings.ReplaceAll(i, ".", "_")) { - log.Printf("[ERROR] : Custom field '%v' is not a valid prometheus label", i) + utils.Log(utils.ErrorLvl, "", fmt.Sprintf("Custom field '%v' is not a valid prometheus label", i)) continue } labelnames = append(labelnames, strings.ReplaceAll(i, ".", "_")) } for _, i := range config.Prometheus.ExtraLabelsList { if !regPromLabels.MatchString(strings.ReplaceAll(i, ".", "_")) { - log.Printf("[ERROR] : Extra field '%v' is not a valid prometheus label", i) + utils.Log(utils.ErrorLvl, "", fmt.Sprintf("Extra field '%v' is not a valid prometheus label", i)) continue } labelnames = append(labelnames, strings.ReplaceAll(i, ".", "_")) diff --git a/types/types.go b/types/types.go index 2e74ea48b..c5007f9d2 100644 --- a/types/types.go +++ b/types/types.go @@ -9,11 +9,11 @@ import ( "text/template" "time" - "github.com/falcosecurity/falcosidekick/outputs/otlpmetrics" - "github.com/DataDog/datadog-go/statsd" "github.com/embano1/memlog" "github.com/prometheus/client_golang/prometheus" + + "github.com/falcosecurity/falcosidekick/outputs/otlpmetrics" ) // FalcoPayload is a struct to map falco event json