Skip to content

Commit

Permalink
introduce waitgroup
Browse files Browse the repository at this point in the history
  • Loading branch information
stefans-elastic committed Dec 26, 2024
1 parent eb7bbcb commit 9ae1258
Showing 1 changed file with 35 additions and 29 deletions.
64 changes: 35 additions & 29 deletions metricbeat/module/mongodb/collstats/collstats.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"context"
"errors"
"fmt"
"sync"

"github.com/elastic/beats/v7/metricbeat/mb"
"github.com/elastic/beats/v7/metricbeat/module/mongodb"
Expand Down Expand Up @@ -71,10 +72,6 @@ func (m *Metricset) Fetch(reporter mb.ReporterV2) error {
}
}()

if err != nil {
return fmt.Errorf("could not get a list of databases: %w", err)
}

// This info is only stored in 'admin' database
db := client.Database("admin")
res := db.RunCommand(context.Background(), bson.D{bson.E{Key: "top"}})
Expand All @@ -100,6 +97,8 @@ func (m *Metricset) Fetch(reporter mb.ReporterV2) error {
return fmt.Errorf("'top' command failed: %w", err)
}

wg := &sync.WaitGroup{}

for group, info := range totals {
if group == "note" {
continue
Expand All @@ -111,33 +110,40 @@ func (m *Metricset) Fetch(reporter mb.ReporterV2) error {
continue
}

names, err := splitKey(group)
if err != nil {
reporter.Error(fmt.Errorf("splitting a collection key failed: %w", err))
continue
}

collStats, err := fetchCollStats(client, names[0], names[1])
if err != nil {
reporter.Error(fmt.Errorf("fetching collStats failed: %w", err))
continue
}

for key, val := range collStats {
infoMap[key] = val
}

event, err := eventMapping(group, infoMap)
if err != nil {
reporter.Error(fmt.Errorf("mapping of the event data failed: %w", err))
continue
}

reporter.Event(mb.Event{
MetricSetFields: event,
})
wg.Add(1)
go func(eventReporter mb.ReporterV2, mongoClient *mongo.Client) {
defer wg.Done()

names, err := splitKey(group)
if err != nil {
eventReporter.Error(fmt.Errorf("splitting a collection key failed: %w", err))
return
}

collStats, err := fetchCollStats(mongoClient, names[0], names[1])
if err != nil {
eventReporter.Error(fmt.Errorf("fetching collStats failed: %w", err))
return
}

for key, val := range collStats {
infoMap[key] = val
}

event, err := eventMapping(group, infoMap)
if err != nil {
eventReporter.Error(fmt.Errorf("mapping of the event data failed: %w", err))
return
}

eventReporter.Event(mb.Event{
MetricSetFields: event,
})
}(reporter, client)
}

wg.Wait()

return nil
}

Expand Down

0 comments on commit 9ae1258

Please sign in to comment.