From acfcb2be7ac5be14eb030bd2ffd8c9ae00047c31 Mon Sep 17 00:00:00 2001 From: Nikola Grcevski Date: Tue, 27 Aug 2024 16:54:06 -0400 Subject: [PATCH 1/3] end beyla if we can't open a configured prom port --- pkg/internal/connector/prommgr.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pkg/internal/connector/prommgr.go b/pkg/internal/connector/prommgr.go index 512fd6033..97665c38a 100644 --- a/pkg/internal/connector/prommgr.go +++ b/pkg/internal/connector/prommgr.go @@ -8,8 +8,10 @@ import ( "fmt" "log/slog" "net/http" + "os" "strconv" "sync/atomic" + "syscall" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" @@ -109,9 +111,10 @@ func (pm *PrometheusManager) listenAndServe(ctx context.Context, port int, handl go func() { err := server.ListenAndServe() if errors.Is(err, http.ErrServerClosed) { - log.Debug("HTTP server was closed", "error", err) + log.Debug("Prometheus endpoint server was closed", "error", err) } else { - log.Error("HTTP service ended unexpectedly", "error", err) + log.Error("Prometheus endpoint service ended unexpectedly", "error", err) + syscall.Kill(os.Getpid(), syscall.SIGINT) // interrupt for graceful shutdown, instead of os.Exit } }() go func() { From 2315251769c37682eee233d8c4e6b5978408611f Mon Sep 17 00:00:00 2001 From: Nikola Grcevski Date: Tue, 27 Aug 2024 16:54:12 -0400 Subject: [PATCH 2/3] add test --- pkg/export/prom/prom_test.go | 53 ++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/pkg/export/prom/prom_test.go b/pkg/export/prom/prom_test.go index 168402e56..5b400db85 100644 --- a/pkg/export/prom/prom_test.go +++ b/pkg/export/prom/prom_test.go @@ -5,13 +5,17 @@ import ( "fmt" "io" "net/http" + "os" + "os/signal" "regexp" "sync" + "syscall" "testing" "time" "github.com/mariomac/guara/pkg/test" "github.com/mariomac/pipes/pipe" + "github.com/prometheus/client_golang/prometheus" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -341,6 +345,55 @@ func TestSpanMetricsDiscarded(t *testing.T) { } } +func TestTerminatesOnBadPromPort(t *testing.T) { + now := syncedClock{now: time.Now()} + timeNow = now.Now + + ctx, cancelCtx := context.WithCancel(context.Background()) + defer cancelCtx() + openPort, err := test.FreeTCPPort() + require.NoError(t, err) + + // Grab the port we just allocated for something else + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, "Hello, %v, http: %v\n", r.URL.Path, r.TLS == nil) + }) + server := http.Server{Addr: fmt.Sprintf(":%d", openPort), Handler: handler} + serverUp := make(chan bool, 1) + + go func() { + go func() { + time.Sleep(5 * time.Second) + serverUp <- true + }() + server.ListenAndServe() + }() + + sigChan := make(chan os.Signal, 1) + signal.Notify(sigChan, syscall.SIGINT) + + pm := connector.PrometheusManager{} + + c := prometheus.NewGaugeVec(prometheus.GaugeOpts{ + Name: TracesTargetInfo, + Help: "target service information in trace span metric format", + }, []string{"a"}).MetricVec + + pm.Register(openPort, "/metrics", c) + go pm.StartHTTP(ctx) + + ok := false + select { + case sig := <-sigChan: + assert.Equal(t, sig, syscall.SIGINT) + ok = true + case <-time.After(5 * time.Second): + ok = false + } + + assert.True(t, ok) +} + var mmux = sync.Mutex{} func getMetrics(t require.TestingT, promURL string) string { From b5a83f561cf465a83f841d1c6efd910ed5311133 Mon Sep 17 00:00:00 2001 From: Nikola Grcevski Date: Tue, 27 Aug 2024 17:21:37 -0400 Subject: [PATCH 3/3] make liner happy --- pkg/export/prom/prom_test.go | 3 ++- pkg/internal/connector/prommgr.go | 5 ++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/pkg/export/prom/prom_test.go b/pkg/export/prom/prom_test.go index 5b400db85..b3c0c6398 100644 --- a/pkg/export/prom/prom_test.go +++ b/pkg/export/prom/prom_test.go @@ -366,7 +366,8 @@ func TestTerminatesOnBadPromPort(t *testing.T) { time.Sleep(5 * time.Second) serverUp <- true }() - server.ListenAndServe() + err := server.ListenAndServe() + fmt.Printf("Terminating server %v\n", err) }() sigChan := make(chan os.Signal, 1) diff --git a/pkg/internal/connector/prommgr.go b/pkg/internal/connector/prommgr.go index 97665c38a..a9a4cf00f 100644 --- a/pkg/internal/connector/prommgr.go +++ b/pkg/internal/connector/prommgr.go @@ -114,7 +114,10 @@ func (pm *PrometheusManager) listenAndServe(ctx context.Context, port int, handl log.Debug("Prometheus endpoint server was closed", "error", err) } else { log.Error("Prometheus endpoint service ended unexpectedly", "error", err) - syscall.Kill(os.Getpid(), syscall.SIGINT) // interrupt for graceful shutdown, instead of os.Exit + err = syscall.Kill(os.Getpid(), syscall.SIGINT) // interrupt for graceful shutdown, instead of os.Exit + if err != nil { + log.Error("unable to terminate Beyla", "error", err) + } } }() go func() {