From eb213e7a94682c4560a7a76a5d3a552aee535c82 Mon Sep 17 00:00:00 2001 From: VladislavSenkevich Date: Wed, 16 Aug 2023 15:53:06 +0300 Subject: [PATCH] GROUNDWORK-2604: fix linter --- .../events-connector/eventsConnector.go | 48 ++++++++++--------- .../events-connector/helpers/builder.go | 2 +- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/connectors/events-connector/eventsConnector.go b/connectors/events-connector/eventsConnector.go index 4f03771a..1d8a95c8 100644 --- a/connectors/events-connector/eventsConnector.go +++ b/connectors/events-connector/eventsConnector.go @@ -34,45 +34,49 @@ func receiver(c *gin.Context) { } log.Debug().Interface("data", data).Msg("receive data") - host, group, mb, err := helpers.GetMetricBuildersFromPrometheusData(data, helpers.GetExtConfig()) + results, err := helpers.ParsePrometheusData(data, helpers.GetExtConfig()) if err != nil { log.Debug().Err(err). - Msg("could not process incomings") + Msg("could not parse prometheus data") c.JSON(http.StatusInternalServerError, err.Error()) return } - monitoredServices := make([]transit.MonitoredService, 0, len(mb)) - for _, m := range mb { - service, err := connectors.BuildServiceForMetric(host, m) + groups := make([]transit.ResourceGroup, 0) + hostToServiceMap := make(map[string][]*transit.MonitoredService) + hostToHostGroupMap := make(map[string]string) + + for _, r := range results { + service, err := connectors.BuildServiceForMetric(r.HostName, r.MetricBuilder) if err != nil { - log.Debug().Err(err). - Str("host", host). - Msg("could not build service") c.JSON(http.StatusInternalServerError, err.Error()) } service.Status = transit.ServiceWarning - service.LastPluginOutput = helpers.GetLastPluginOutput(m.Tags) - monitoredServices = append(monitoredServices, *service) + service.LastPluginOutput = helpers.GetLastPluginOutput(r.MetricBuilder.Tags) + + hostToServiceMap[r.HostName] = append(hostToServiceMap[r.HostName], service) + if r.HostGroupName != "" { + hostToHostGroupMap[r.HostName] = r.HostGroupName + } } - resource, err := connectors.CreateResource(host, monitoredServices) - if err != nil { - log.Debug().Err(err). - Str("host", host). - Msg("could not create resource") - c.JSON(http.StatusInternalServerError, err.Error()) - return + monitoredResources := make([]transit.MonitoredResource, 0, len(hostToServiceMap)) + for h, s := range hostToServiceMap { + resource, err := connectors.CreateResource(h, s) + if err != nil { + c.JSON(http.StatusInternalServerError, err.Error()) + return + } + monitoredResources = append(monitoredResources, *resource) } - groups := make([]transit.ResourceGroup, 0) - if group != "" { - resourceRef := connectors.CreateResourceRef(host, "", transit.ResourceTypeHost) - resourceGroup := connectors.CreateResourceGroup(group, group, transit.HostGroup, []transit.ResourceRef{resourceRef}) + for h, hg := range hostToHostGroupMap { + resourceRef := connectors.CreateResourceRef(h, "", transit.ResourceTypeHost) + resourceGroup := connectors.CreateResourceGroup(hg, hg, transit.HostGroup, []transit.ResourceRef{resourceRef}) groups = append(groups, resourceGroup) } - if err = connectors.SendMetrics(c.Request.Context(), []transit.MonitoredResource{*resource}, &groups); err != nil { + if err = connectors.SendMetrics(c.Request.Context(), monitoredResources, &groups); err != nil { log.Err(err). Msg("could not send metrics") c.JSON(http.StatusInternalServerError, err.Error()) diff --git a/connectors/events-connector/helpers/builder.go b/connectors/events-connector/helpers/builder.go index a9c2e283..d0c9610c 100644 --- a/connectors/events-connector/helpers/builder.go +++ b/connectors/events-connector/helpers/builder.go @@ -18,7 +18,7 @@ const ( ) func ParsePrometheusData(data template.Data, cfg *ExtConfig) ([]ParseResult, error) { - var results []ParseResult + results := make([]ParseResult, 0) for _, alert := range data.Alerts { hostName, err := cfg.HostMappings.Apply(alert.Labels)