Skip to content

Commit

Permalink
Futher logging
Browse files Browse the repository at this point in the history
  • Loading branch information
bomoko committed Nov 19, 2023
1 parent d0126c0 commit e1cbe2e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 17 deletions.
49 changes: 34 additions & 15 deletions internal/handler/processing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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}})

Expand Down Expand Up @@ -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
Expand All @@ -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}})

Expand Down Expand Up @@ -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
Expand Down
15 changes: 13 additions & 2 deletions internal/handler/trivyProcessing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

0 comments on commit e1cbe2e

Please sign in to comment.