From 0be7cc3cfc875684d5df1da471665c661bfdad50 Mon Sep 17 00:00:00 2001 From: Sebastien Blot Date: Fri, 19 Apr 2024 18:02:19 +0200 Subject: [PATCH] store more payload --- pkg/apiserver/controllers/v1/usagemetrics.go | 48 ++++++++++---------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/pkg/apiserver/controllers/v1/usagemetrics.go b/pkg/apiserver/controllers/v1/usagemetrics.go index f77239e56..d630943fd 100644 --- a/pkg/apiserver/controllers/v1/usagemetrics.go +++ b/pkg/apiserver/controllers/v1/usagemetrics.go @@ -3,19 +3,18 @@ package v1 import ( "encoding/json" "fmt" - "net/http" + "net/http" "time" - "github.com/gin-gonic/gin" - "github.com/go-openapi/strfmt" - log "github.com/sirupsen/logrus" + "github.com/gin-gonic/gin" + "github.com/go-openapi/strfmt" + log "github.com/sirupsen/logrus" - "github.com/crowdsecurity/crowdsec/pkg/models" - "github.com/crowdsecurity/crowdsec/pkg/database/ent" - "github.com/crowdsecurity/crowdsec/pkg/database/ent/metric" + "github.com/crowdsecurity/crowdsec/pkg/database/ent" + "github.com/crowdsecurity/crowdsec/pkg/database/ent/metric" + "github.com/crowdsecurity/crowdsec/pkg/models" ) - // updateBaseMetrics updates the base metrics for a machine or bouncer func (c *Controller) updateBaseMetrics(machineID string, bouncer *ent.Bouncer, baseMetrics *models.BaseMetrics, hubItems *models.HubItems) error { switch { @@ -30,24 +29,23 @@ func (c *Controller) updateBaseMetrics(machineID string, bouncer *ent.Bouncer, b return nil } - // UsageMetrics receives metrics from log processors and remediation components func (c *Controller) UsageMetrics(gctx *gin.Context) { - var input models.AllMetrics + var input models.AllMetrics // parse the payload - if err := gctx.ShouldBindJSON(&input); err != nil { - log.Errorf("Failed to bind json: %s", err) - gctx.JSON(http.StatusBadRequest, gin.H{"message": err.Error()}) - return - } + if err := gctx.ShouldBindJSON(&input); err != nil { + log.Errorf("Failed to bind json: %s", err) + gctx.JSON(http.StatusBadRequest, gin.H{"message": err.Error()}) + return + } - if err := input.Validate(strfmt.Default); err != nil { - log.Errorf("Failed to validate usage metrics: %s", err) - c.HandleDBErrors(gctx, err) - return - } + if err := input.Validate(strfmt.Default); err != nil { + log.Errorf("Failed to validate usage metrics: %s", err) + c.HandleDBErrors(gctx, err) + return + } // TODO: validate payload with the right type, depending on auth context @@ -74,9 +72,9 @@ func (c *Controller) UsageMetrics(gctx *gin.Context) { // TODO: if both or none are set, which error should we return? var ( - payload map[string]any + payload map[string]any baseMetrics models.BaseMetrics - hubItems models.HubItems + hubItems models.HubItems ) switch len(input.LogProcessors) { @@ -89,6 +87,8 @@ func (c *Controller) UsageMetrics(gctx *gin.Context) { payload = map[string]any{ "console_options": item0.ConsoleOptions, "datasources": item0.Datasources, + "metrics": item0.Metrics, + "meta": item0.Meta, } baseMetrics = item0.BaseMetrics hubItems = item0.HubItems @@ -105,7 +105,9 @@ func (c *Controller) UsageMetrics(gctx *gin.Context) { case 1: item0 := input.RemediationComponents[0][0] payload = map[string]any{ - "type": item0.Type, + "type": item0.Type, + "metrics": item0.Metrics, + "meta": item0.Meta, // TODO: RC stuff like traffic stats } baseMetrics = item0.BaseMetrics