Skip to content

[Flaky Test] Fix TestMakeReporter and TestHTTP test #45415

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 18, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion libbeat/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"net/http"
"net/url"
"strconv"
"sync"

"github.com/gorilla/mux"

Expand All @@ -37,6 +38,7 @@
mux *mux.Router
l net.Listener
config Config
wg sync.WaitGroup
}

// New creates a new API Server with no routes attached.
Expand All @@ -63,16 +65,23 @@
// Start starts the HTTP server and accepting new connection.
func (s *Server) Start() {
s.log.Info("Starting stats endpoint")
s.wg.Add(1)
go func(l net.Listener) {
defer s.wg.Done()
s.log.Infof("Metrics endpoint listening on: %s (configured: %s)", l.Addr().String(), s.config.Host)
err := http.Serve(l, s.mux)

Check failure on line 72 in libbeat/api/server.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

G114: Use of net/http serve function that has no support for setting timeouts (gosec)
s.log.Infof("Stats endpoint (%s) finished: %v", l.Addr().String(), err)
}(s.l)
}

// Stop stops the API server and free any resource associated with the process like unix sockets.
func (s *Server) Stop() error {
return s.l.Close()
err := s.l.Close()
if err != nil {
return fmt.Errorf("error stopping monitoring server: %w", err)
}
s.wg.Wait()
return nil
}

// AttachHandler will attach a handler at the specified route. Routes are
Expand Down
9 changes: 8 additions & 1 deletion libbeat/monitoring/report/elasticsearch/elasticsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"io"
"math/rand/v2"
"strconv"
"sync"
"time"

"github.com/elastic/beats/v7/libbeat/beat"
Expand Down Expand Up @@ -55,6 +56,7 @@ type reporter struct {
client beat.Client

out []outputs.NetworkClient
wg sync.WaitGroup
}

const logSelector = "monitoring"
Expand Down Expand Up @@ -195,6 +197,7 @@ func makeReporter(beat beat.Info, mon beat.Monitoring, settings report.Settings,
client: pipeConn,
out: clients,
}
r.wg.Add(1)
go r.initLoop(config)
return r, nil
}
Expand All @@ -203,11 +206,15 @@ func (r *reporter) Stop() {
r.done.Stop()
r.client.Close()
r.pipeline.Close()
r.wg.Wait()
}

func (r *reporter) initLoop(c config) {
r.logger.Debug("Start monitoring endpoint init loop.")
defer r.logger.Debug("Finish monitoring endpoint init loop.")
defer func() {
r.logger.Debug("Finish monitoring endpoint init loop.")
r.wg.Done()
}()

log := r.logger

Expand Down
Loading