From 3010783fcb257f4be4c925d77b7f81d5b3e1eda2 Mon Sep 17 00:00:00 2001 From: Thomas Labarussias Date: Sat, 27 Apr 2024 17:03:38 +0200 Subject: [PATCH] add more checks for the upload on GCP Storage Signed-off-by: Thomas Labarussias --- outputs/gcp.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/outputs/gcp.go b/outputs/gcp.go index 14ee0cb42..48bb7e38e 100644 --- a/outputs/gcp.go +++ b/outputs/gcp.go @@ -190,8 +190,7 @@ func (c *Client) UploadGCS(falcopayload types.FalcoPayload) { key := fmt.Sprintf("%s/%s/%s.json", prefix, t.Format("2006-01-02"), t.Format(time.RFC3339Nano)) bucketWriter := c.GCSStorageClient.Bucket(c.Config.GCP.Storage.Bucket).Object(key).NewWriter(context.Background()) - defer bucketWriter.Close() - _, err := bucketWriter.Write(payload) + n, err := bucketWriter.Write(payload) if err != nil { log.Printf("[ERROR] : GCPStorage - %v - %v\n", "Error while Uploading message", err.Error()) c.Stats.GCPStorage.Add(Error, 1) @@ -199,6 +198,20 @@ func (c *Client) UploadGCS(falcopayload types.FalcoPayload) { c.PromStats.Outputs.With(map[string]string{"destination": "gcpstorage", "status": Error}).Inc() return } + if n == 0 { + log.Printf("[ERROR] : GCPStorage - %v\n", "Empty payload uploaded") + c.Stats.GCPStorage.Add(Error, 1) + go c.CountMetric("outputs", 1, []string{"output:gcpstorage", "status:error"}) + c.PromStats.Outputs.With(map[string]string{"destination": "gcpstorage", "status": Error}).Inc() + return + } + if err := bucketWriter.Close(); err != nil { + log.Printf("[ERROR] : GCPStorage - %v - %v\n", "Error while closing the writer", err.Error()) + c.Stats.GCPStorage.Add(Error, 1) + go c.CountMetric("outputs", 1, []string{"output:gcpstorage", "status:error"}) + c.PromStats.Outputs.With(map[string]string{"destination": "gcpstorage", "status": Error}).Inc() + return + } log.Printf("[INFO] : GCPStorage - Upload to bucket OK \n") c.Stats.GCPStorage.Add(OK, 1)