From e1cbe2e9e2771e68eaac02a5a7b4bbd8fb25dac4 Mon Sep 17 00:00:00 2001 From: Blaize M Kaye Date: Mon, 20 Nov 2023 11:42:45 +1300 Subject: [PATCH] Futher logging --- internal/handler/processing.go | 49 ++++++++++++++++++++--------- internal/handler/trivyProcessing.go | 15 +++++++-- 2 files changed, 47 insertions(+), 17 deletions(-) diff --git a/internal/handler/processing.go b/internal/handler/processing.go index 16f5d1a..e22fe17 100644 --- a/internal/handler/processing.go +++ b/internal/handler/processing.go @@ -7,6 +7,7 @@ import ( "github.com/cheshir/go-mq" "github.com/uselagoon/lagoon/services/insights-handler/internal/lagoonclient" "log" + "log/slog" "net/http" "strconv" ) @@ -15,23 +16,26 @@ import ( func processFactsDirectly(message mq.Message, h *Messaging) string { var directFacts DirectFacts + json.Unmarshal(message.Body(), &directFacts) err := json.Unmarshal(message.Body(), &directFacts) if err != nil { - log.Println("Error unmarshaling JSON:", err.Error()) + slog.Error("Could not unmarshal data", "Error", err.Error()) return "exciting, unable to process direct facts" } - // since its useful to allow int and string json definitions, we need to convert strings here to ints. + // since it's useful to allow int and string json definitions, we need to convert strings here to ints. environmentId, err := strconv.Atoi(directFacts.EnvironmentId.String()) if err != nil { - log.Println("Error converting EnvironmentId to int:", err) + slog.Error("Error converting EnvironmentId to int", "Error", err) return "exciting, unable to process direct facts" } - if h.EnableDebug { - log.Print("[DEBUG] facts", directFacts) - } + //if h.EnableDebug { + // log.Print("[DEBUG] facts", directFacts) + //} + // + slog.Debug("Facts info", "data", directFacts) apiClient := graphql.NewClient(h.LagoonAPI.Endpoint, &http.Client{Transport: &authedTransport{wrapped: http.DefaultTransport, h: h}}) @@ -61,14 +65,26 @@ func processFactsDirectly(message mq.Message, h *Messaging) string { for _, s := range factSources { _, err = lagoonclient.DeleteFactsFromSource(context.TODO(), apiClient, environmentId, s) if err != nil { - log.Println(err) + + slog.Error("Error deleting facts from source", + "Project", directFacts.ProjectName, + "Environment", directFacts.EnvironmentName, + "Source", s, + "Error", err, + ) } - log.Printf("Deleted facts on '%v:%v' for source %v\n", directFacts.ProjectName, directFacts.EnvironmentName, s) + //log.Printf("Deleted facts on '%v:%v' for source %v\n", directFacts.ProjectName, directFacts.EnvironmentName, s) + slog.Info("Deleted facts", + "Project", directFacts.ProjectName, + "Environment", directFacts.EnvironmentName, + "Source", s, + ) } facts, err := lagoonclient.AddFacts(context.TODO(), apiClient, processedFacts) if err != nil { - log.Println(err) + //log.Println(err) + slog.Error("Issue adding facts", "Error", err.Error()) } return facts @@ -80,13 +96,12 @@ func processProblemsDirectly(message mq.Message, h *Messaging) ([]string, error) log.Println(directProblems) err := json.Unmarshal(message.Body(), &directProblems) if err != nil { - log.Println("Error unmarshaling JSON:", err) + //log.Println("Error unmarshaling JSON:", err) + slog.Error("Could not unmarshal JSON", "Error", err) return []string{}, err } - if h.EnableDebug { - log.Print("[DEBUG] problems", directProblems) - } + slog.Debug("Problems data", "data", directProblems) apiClient := graphql.NewClient(h.LagoonAPI.Endpoint, &http.Client{Transport: &authedTransport{wrapped: http.DefaultTransport, h: h}}) @@ -114,15 +129,19 @@ func processProblemsDirectly(message mq.Message, h *Messaging) ([]string, error) for _, s := range problemSources { _, err := lagoonclient.DeleteProblemsFromSource(context.TODO(), apiClient, directProblems.EnvironmentId, s.Service, s.Source) if err != nil { - log.Println(err) //This could potentially mess up the state if we've already deleted source info, might return []string{}, err } log.Printf("Deleted Problems on '%v:%v' for source %v\n", directProblems.ProjectName, directProblems.EnvironmentName, s) + slog.Info("Deleted problems", + "Project", directProblems.ProjectName, + "Environment", directProblems.EnvironmentName, + "Source", s, + ) } resptext, err := lagoonclient.AddProblems(context.TODO(), apiClient, directProblems.Problems) if err != nil { - log.Println(err) + return []string{}, err } return resptext, nil diff --git a/internal/handler/trivyProcessing.go b/internal/handler/trivyProcessing.go index 7aa8178..b8e3abd 100644 --- a/internal/handler/trivyProcessing.go +++ b/internal/handler/trivyProcessing.go @@ -11,6 +11,7 @@ import ( "github.com/aquasecurity/trivy/pkg/types" "github.com/uselagoon/lagoon/services/insights-handler/internal/lagoonclient" "io" + "log/slog" "net/http" "os" "strings" @@ -83,7 +84,12 @@ func writeProblemsArrayToApi(apiClient graphql.Client, environment int, source s if err != nil { return err } - fmt.Printf("Deleted problems from API for %v:%v - response: %v\n", service, source, ret) + //fmt.Printf("Deleted problems from API for %v:%v - response: %v\n", service, source, ret) + slog.Info("Deleted problems from API", + "Service", service, + "Source", source, + "Return Data", ret, + ) //now we write the problems themselves _, err = lagoonclient.AddProblems(context.TODO(), apiClient, problems) @@ -239,7 +245,12 @@ func trivyReportToProblems(environment int, source string, service string, repor ret = append(ret, p) } } - fmt.Printf("Found %v problems for environment %v\n", len(ret), environment) + //fmt.Printf("Found %v problems for environment %v\n", len(ret), environment) + slog.Info("Found problems", + "EnvironmentId", environment, + "Source", source, + "Number", len(ret), + ) return ret, nil }