Skip to content

Commit

Permalink
compacting logs into single lines and moving verbosity to debug flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Clifford committed Jun 20, 2023
1 parent 5080dab commit 49f507e
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 29 deletions.
6 changes: 4 additions & 2 deletions internal/handler/imageInspectParserFilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func processImageInspectInsightsData(h *Messaging, insights InsightsData, v stri
if err != nil {
return nil, "", err
}
log.Printf("Successfully decoded image-inspect")
log.Printf("Successfully decoded image-inspect, for '$s:$s', from '%s'", resource.Project, resource.Environment, source)

facts, err = KeyFactsFilter(facts)
if err != nil {
Expand Down Expand Up @@ -95,7 +95,9 @@ func processFactsFromImageInspect(imageInspectData ImageData, id int, source str
KeyFact: false,
Type: FactTypeText,
}
fmt.Println("Processing fact name " + f.Key)
if EnableDebug {
log.Println("[DEBUG] processing fact name " + f.Key)
}
fact, _ = ProcessLagoonFactAgainstRegisteredFilters(fact, f)
factsInput = append(factsInput, fact)
}
Expand Down
7 changes: 4 additions & 3 deletions internal/handler/insightsFactsParserFilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ func processFactsInsightsData(h *Messaging, insights InsightsData, v string, api
return nil, "", fmt.Errorf("no facts to process")
}

log.Printf("Successfully processed facts")
log.Printf("- Facts found: %d\n", len(facts))
log.Printf("Successfully processed %d fact(s), for '%s:%s', from source '%s'", len(facts), resource.Project, resource.Environment, source)

return facts, source, nil
}
Expand Down Expand Up @@ -81,7 +80,9 @@ func processFactsFromJSON(facts []byte, source string) []LagoonFact {
KeyFact: f.KeyFact,
Type: FactTypeText,
}
fmt.Println("Processing fact name " + f.Name)
if EnableDebug {
log.Println("[DEBUG] processing fact name " + f.Name)
}
fact, _ = ProcessLagoonFactAgainstRegisteredFilters(fact, f)
factsInput = append(factsInput, fact)
}
Expand Down
9 changes: 4 additions & 5 deletions internal/handler/insightsParserFilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,7 @@ func processSbomInsightsData(h *Messaging, insights InsightsData, v string, apiC
return nil, "", fmt.Errorf("no facts to process")
}

log.Printf("Successfully decoded SBOM of image %s\n", bom.Metadata.Component.Name)
log.Printf("- Generated: %s with %s\n", bom.Metadata.Timestamp, (*bom.Metadata.Tools)[0].Name)
log.Printf("- Packages found: %d\n", len(*bom.Components))

log.Printf("Successfully decoded SBOM of image %s with %s, found %d for '%s:%s'", bom.Metadata.Component.Name, (*bom.Metadata.Tools)[0].Name, len(*bom.Components), resource.Project, resource.Environment)
return facts, source, nil
}

Expand Down Expand Up @@ -107,7 +104,9 @@ func processFactsFromSBOM(facts *[]cdx.Component, environmentId int, source stri
KeyFact: false,
Type: FactTypeText,
}
fmt.Println("Processing fact name " + f.Name)
if EnableDebug {
log.Println("[DEBUG] processing fact name " + f.Name)
}
fact, _ = ProcessLagoonFactAgainstRegisteredFilters(fact, f)
factsInput = append(factsInput, fact)
}
Expand Down
36 changes: 20 additions & 16 deletions internal/handler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import (
"github.com/uselagoon/lagoon/services/insights-handler/internal/lagoonclient/jwt"
)

var EnableDebug bool

// RabbitBroker .
type RabbitBroker struct {
Hostname string `json:"hostname"`
Expand Down Expand Up @@ -369,7 +371,9 @@ func processingIncomingMessageQueueFactory(h *Messaging) func(mq.Message) {

// Determine incoming payload type
if incoming.Payload == nil && incoming.BinaryPayload == nil {
log.Printf("no payload was found")
if h.EnableDebug {
log.Printf("[DEBUG] no payload was found")
}
err := message.Reject(false)
if err != nil {
fmt.Errorf("%s", err.Error())
Expand Down Expand Up @@ -439,7 +443,7 @@ func processItemsDirectly(message mq.Message, h *Messaging) string {
}

if h.EnableDebug {
log.Print("directFacts: ", directFacts)
log.Print("[DEBUG] facts", directFacts)
}

apiClient := graphql.NewClient(h.LagoonAPI.Endpoint, &http.Client{Transport: &authedTransport{wrapped: http.DefaultTransport, h: h}})
Expand All @@ -462,7 +466,7 @@ func processItemsDirectly(message mq.Message, h *Messaging) string {
if err != nil {
log.Println(err)
}
log.Printf("Deleted facts on environment %v for source %v", environmentId, directFacts.Source)
log.Printf("Deleted facts on '%v:%v' for source %v", directFacts.ProjectName, directFacts.EnvironmentName, directFacts.Source)

facts, err := lagoonclient.AddFacts(context.TODO(), apiClient, processedFacts)
if err != nil {
Expand Down Expand Up @@ -521,9 +525,13 @@ func (h *Messaging) sendToLagoonAPI(incoming *InsightsMessage, resource Resource
}

func (h *Messaging) sendFactsToLagoonAPI(facts []LagoonFact, apiClient graphql.Client, resource ResourceDestination, source string) error {

project, environment, apiErr := determineResourceFromLagoonAPI(apiClient, resource)
log.Printf("Matched %v number of facts for project:environment '%v:%v' from source '%v'", len(facts), project.Name, environment, source)
if apiErr != nil {
log.Println(apiErr)
}
if EnableDebug {
log.Printf("[DEBUG] matched %d number of fact(s) for '%v:%v', from source '%s'", len(facts), project.Name, environment, source)
}

// Even if we don't find any new facts, we need to delete the existing ones
// since these may be the end product of a filter process
Expand All @@ -549,9 +557,7 @@ func (h *Messaging) deleteExistingFactsBySource(apiClient graphql.Client, enviro
return err
}

log.Println("--------------------")
log.Printf("Previous facts deleted for '%s:%s' and source '%s'", project.Name, environment.Name, source)
log.Println("--------------------")
return nil
}

Expand Down Expand Up @@ -602,7 +608,7 @@ func (h *Messaging) sendToLagoonS3(incoming *InsightsMessage, insights InsightsD
return err
}
} else {
log.Printf("Successfully created %s\n", h.S3Config.Bucket)
log.Printf("Successfully created %s", h.S3Config.Bucket)
}

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

log.Println("--------------------")
log.Printf("Successfully uploaded %s of size %d\n", objectName, info.Size)
log.Println("--------------------")
log.Printf("Successfully uploaded %s of size %d", objectName, info.Size)
}

if len(incoming.BinaryPayload) != 0 {
Expand Down Expand Up @@ -685,9 +689,9 @@ func (h *Messaging) sendToLagoonS3(incoming *InsightsMessage, insights InsightsD
func (h *Messaging) pushFactsToLagoonApi(facts []LagoonFact, resource ResourceDestination) error {
apiClient := graphql.NewClient(h.LagoonAPI.Endpoint, &http.Client{Transport: &authedTransport{wrapped: http.DefaultTransport, h: h}})

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

processedFacts := make([]lagoonclient.AddFactInput, len(facts))
for i, fact := range facts {
Expand All @@ -711,7 +715,7 @@ func (h *Messaging) pushFactsToLagoonApi(facts []LagoonFact, resource ResourceDe

if h.EnableDebug {
for _, fact := range facts {
log.Println("[DEBUG]...", fact.Name, ":", fact.Value)
log.Println("[DEBUG]", fact.Name, ":", fact.Value)
}
}

Expand Down Expand Up @@ -770,7 +774,7 @@ func (h *Messaging) toLagoonInsights(messageQueue mq.MQ, message map[string]inte
msgBytes, err := json.Marshal(message)
if err != nil {
if h.EnableDebug {
log.Println(err, "Unable to encode message as JSON")
log.Println("[DEBUG]", err, "Unable to encode message as JSON")
}
}
producer, err := messageQueue.AsyncProducer("lagoon-insights")
Expand Down
2 changes: 1 addition & 1 deletion internal/handler/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func Test_processDirectFacts(t *testing.T) {
message: &MockMessage{},
h: &h,
},
want: "Added 2 facts",
want: "Added 2 fact(s)",
want1: "insights:facts:cli",
wantErr: false,
},
Expand Down
3 changes: 2 additions & 1 deletion internal/lagoonclient/facts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package lagoonclient
import (
"context"
"fmt"

"github.com/Khan/genqlient/graphql"
)

Expand Down Expand Up @@ -78,7 +79,7 @@ func AddFacts(ctx context.Context, client graphql.Client, facts []AddFactInput)
return "", err
}

return fmt.Sprintf("Added %d facts", len(resp.AddFacts)), nil
return fmt.Sprintf("Added %d fact(s)", len(resp.AddFacts)), nil
}

func DeleteFactsFromSource(ctx context.Context, client graphql.Client, environmentID int, source string) (string, error) {
Expand Down
2 changes: 1 addition & 1 deletion internal/lagoonclient/facts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestAddFacts(t *testing.T) {
return
}

if result != "Added 2 facts" {
if result != "Added 2 fact(s)" {
t.Errorf("Two facts should have been added: %v", err)
return
}
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ func main() {
flag.BoolVar(&enableDebug, "debug", false, "Enable debugging output")
flag.Parse()

handler.EnableDebug = enableDebug

// get overrides from environment variables
mqUser = getEnv("RABBITMQ_USERNAME", mqUser)
mqPass = getEnv("RABBITMQ_PASSWORD", mqPass)
Expand Down

0 comments on commit 49f507e

Please sign in to comment.