Skip to content

Commit

Permalink
Further logging
Browse files Browse the repository at this point in the history
  • Loading branch information
bomoko committed Nov 19, 2023
1 parent e1cbe2e commit c844c62
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ gettrivy:

.PHONY: runlocal
runlocal:
go run main.go --problems-from-sbom=true --rabbitmq-username=guest --rabbitmq-password=guest --lagoon-api-host=http://localhost:8888/graphql --jwt-token-signing-key=secret --access-key-id=minio --secret-access-key=minio123 --disable-s3-upload=true
go run main.go --problems-from-sbom=true --rabbitmq-username=guest --rabbitmq-password=guest --lagoon-api-host=http://localhost:8888/graphql --jwt-token-signing-key=secret --access-key-id=minio --secret-access-key=minio123 --disable-s3-upload=true --debug=true


2 changes: 1 addition & 1 deletion internal/handler/insightsFactsParserFilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func processFactsInsightsData(h *Messaging, insights InsightsData, v string, api
//dec := base64.NewDecoder(base64.StdEncoding, r)
res, err := ioutil.ReadAll(r)
if err != nil {
fmt.Println("err: ", err)
slog.Error("Error reading insights data", "Error", err)
}

facts := processFactsFromJSON(logger, res, source)
Expand Down
24 changes: 11 additions & 13 deletions internal/handler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ func (h *Messaging) sendToLagoonS3(incoming *InsightsMessage, insights InsightsD
return err
}
} else {
log.Printf("Successfully created %s", h.S3Config.Bucket)
slog.Info(fmt.Sprintf("Successfully created %s", h.S3Config.Bucket))
}

if len(incoming.Payload) != 0 {
Expand All @@ -466,14 +466,14 @@ func (h *Messaging) sendToLagoonS3(incoming *InsightsMessage, insights InsightsD
return putObjErr
}

log.Printf("Successfully uploaded %s of size %d", objectName, info.Size)
slog.Info(fmt.Sprintf("Successfully uploaded %s of size %d", objectName, info.Size))
}

if len(incoming.BinaryPayload) != 0 {
for _, p := range incoming.BinaryPayload {
result, err := decodeGzipString(p)
if err != nil {
fmt.Errorf(err.Error())
return err
}
resultJson, _ := json.MarshalIndent(result, "", " ")

Expand All @@ -491,7 +491,7 @@ func (h *Messaging) sendToLagoonS3(incoming *InsightsMessage, insights InsightsD
if insights.OutputCompressed != true {
err = ioutil.WriteFile(tempFilePath, resultJson, 0644)
if err != nil {
fmt.Errorf(err.Error())
return err
}
} else {
var buf bytes.Buffer
Expand All @@ -500,7 +500,7 @@ func (h *Messaging) sendToLagoonS3(incoming *InsightsMessage, insights InsightsD
gz.Close()
err = ioutil.WriteFile(tempFilePath, buf.Bytes(), 0644)
if err != nil {
fmt.Errorf(err.Error())
return err
}
}

Expand All @@ -510,13 +510,13 @@ func (h *Messaging) sendToLagoonS3(incoming *InsightsMessage, insights InsightsD
ContentEncoding: contentEncoding,
})
if err != nil {
fmt.Errorf(err.Error())
return err
}
log.Printf("Successfully uploaded %s of size %d\n", s3FilePath, info.Size)
slog.Info(fmt.Sprintf("Successfully uploaded %s of size %d\n", s3FilePath, info.Size))

err = os.Remove(tempFilePath)
if err != nil {
fmt.Errorf(err.Error())
return err
}
}
}
Expand All @@ -533,10 +533,6 @@ func (h *Messaging) pushFactsToLagoonApi(facts []LagoonFact, resource ResourceDe
)
apiClient := graphql.NewClient(h.LagoonAPI.Endpoint, &http.Client{Transport: &authedTransport{wrapped: http.DefaultTransport, h: h}})

//if EnableDebug {
// log.Printf("[DEBUG] attempting to add %d fact(s)...", len(facts))
//}

slog.Debug("Attempting to add facts",
"Number", len(facts),
)
Expand Down Expand Up @@ -568,7 +564,9 @@ func (h *Messaging) pushFactsToLagoonApi(facts []LagoonFact, resource ResourceDe
}
}

log.Println(result)
logger.Debug("Response from API",
"result", result,
)
return nil
}

Expand Down
5 changes: 1 addition & 4 deletions internal/handler/processing.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/Khan/genqlient/graphql"
"github.com/cheshir/go-mq"
"github.com/uselagoon/lagoon/services/insights-handler/internal/lagoonclient"
"log"
"log/slog"
"net/http"
"strconv"
Expand Down Expand Up @@ -93,10 +92,8 @@ func processFactsDirectly(message mq.Message, h *Messaging) string {
func processProblemsDirectly(message mq.Message, h *Messaging) ([]string, error) {
var directProblems DirectProblems
json.Unmarshal(message.Body(), &directProblems)
log.Println(directProblems)
err := json.Unmarshal(message.Body(), &directProblems)
if err != nil {
//log.Println("Error unmarshaling JSON:", err)
slog.Error("Could not unmarshal JSON", "Error", err)
return []string{}, err
}
Expand Down Expand Up @@ -131,7 +128,7 @@ func processProblemsDirectly(message mq.Message, h *Messaging) ([]string, error)
if err != nil {
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,
Expand Down
2 changes: 1 addition & 1 deletion internal/handler/testassets/nginxcyclonedxpayload.json

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions internal/handler/trivyProcessing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package handler

import (
"encoding/json"
"fmt"
"github.com/CycloneDX/cyclonedx-go"
"github.com/aquasecurity/trivy/pkg/types"
"github.com/goccy/go-yaml"
Expand Down Expand Up @@ -43,7 +42,6 @@ func Test_convertBOMToProblemsArray(t *testing.T) {
var bom cyclonedx.BOM
json.Unmarshal(bomText, &bom)
got, err := convertBOMToProblemsArray(tt.args.environment, tt.args.source, tt.args.service, bom)
fmt.Print(len(got))
if (err != nil) != tt.wantErr {
t.Errorf("convertBOMToProblemsArray() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"github.com/cheshir/go-mq"
"github.com/uselagoon/lagoon/services/insights-handler/internal/handler"
"log"
"log/slog"
"os"
"strconv"
Expand Down Expand Up @@ -109,6 +108,7 @@ func main() {
if enableDebug {
debugLevel = slog.LevelDebug
}

slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: debugLevel,
})))
Expand Down Expand Up @@ -151,7 +151,8 @@ func main() {

err := handler.RegisterFiltersFromDisk(filterTransformerFile)
if err != nil {
log.Println(err)
// TODO: BETTER ERROR HANDLING
slog.Error("Unable to register filters from disk", "Error", err)
}

config := mq.Config{
Expand Down

0 comments on commit c844c62

Please sign in to comment.