diff --git a/internal/handler/imageInspectParserFilter.go b/internal/handler/imageInspectParserFilter.go index 62959b9..ab30b2e 100644 --- a/internal/handler/imageInspectParserFilter.go +++ b/internal/handler/imageInspectParserFilter.go @@ -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 { @@ -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) } diff --git a/internal/handler/insightsFactsParserFilter.go b/internal/handler/insightsFactsParserFilter.go index 99054dd..a1b8fed 100644 --- a/internal/handler/insightsFactsParserFilter.go +++ b/internal/handler/insightsFactsParserFilter.go @@ -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 } @@ -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) } diff --git a/internal/handler/insightsParserFilter.go b/internal/handler/insightsParserFilter.go index b0e358e..c3a1ff5 100644 --- a/internal/handler/insightsParserFilter.go +++ b/internal/handler/insightsParserFilter.go @@ -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 } @@ -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) } diff --git a/internal/handler/main.go b/internal/handler/main.go index f62638a..9b640de 100644 --- a/internal/handler/main.go +++ b/internal/handler/main.go @@ -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"` @@ -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()) @@ -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}}) @@ -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 { @@ -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 @@ -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 } @@ -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 { @@ -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 { @@ -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 { @@ -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) } } @@ -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") diff --git a/internal/handler/main_test.go b/internal/handler/main_test.go index 3040df5..a0f5b34 100644 --- a/internal/handler/main_test.go +++ b/internal/handler/main_test.go @@ -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, }, diff --git a/internal/lagoonclient/facts.go b/internal/lagoonclient/facts.go index f2330c0..68405fa 100644 --- a/internal/lagoonclient/facts.go +++ b/internal/lagoonclient/facts.go @@ -3,6 +3,7 @@ package lagoonclient import ( "context" "fmt" + "github.com/Khan/genqlient/graphql" ) @@ -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) { diff --git a/internal/lagoonclient/facts_test.go b/internal/lagoonclient/facts_test.go index 5ea66e5..7129ba2 100644 --- a/internal/lagoonclient/facts_test.go +++ b/internal/lagoonclient/facts_test.go @@ -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 } diff --git a/main.go b/main.go index db44f3d..fc742fd 100644 --- a/main.go +++ b/main.go @@ -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)