Skip to content
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

Terminate Beyla if we can't open a configured Prometheus port #1111

Merged
merged 3 commits into from
Aug 28, 2024
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
53 changes: 53 additions & 0 deletions pkg/export/prom/prom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
"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"

Expand Down Expand Up @@ -341,6 +345,55 @@
}
}

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()

Check failure on line 369 in pkg/export/prom/prom_test.go

View workflow job for this annotation

GitHub Actions / test (1.23)

Error return value of `server.ListenAndServe` is not checked (errcheck)
}()

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 {
Expand Down
7 changes: 5 additions & 2 deletions pkg/internal/connector/prommgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
"fmt"
"log/slog"
"net/http"
"os"
"strconv"
"sync/atomic"
"syscall"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
Expand Down Expand Up @@ -109,9 +111,10 @@
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

Check failure on line 117 in pkg/internal/connector/prommgr.go

View workflow job for this annotation

GitHub Actions / test (1.23)

Error return value of `syscall.Kill` is not checked (errcheck)
}
}()
go func() {
Expand Down
Loading