Skip to content

Commit

Permalink
fix(traps): send trap to all destinations on failure (#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxwo authored Nov 5, 2023
1 parent c353851 commit e9ec862
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 205 deletions.
1 change: 1 addition & 0 deletions configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"text/template"

"github.com/go-kit/log"

"github.com/prometheus/common/promlog"
promlogflag "github.com/prometheus/common/promlog/flag"

Expand Down
32 changes: 24 additions & 8 deletions httpserver/http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"net/http"
"strconv"

"github.com/go-kit/log"
"github.com/go-kit/log/level"

"github.com/prometheus/exporter-toolkit/web"
Expand All @@ -31,7 +32,6 @@ import (

"github.com/prometheus/client_golang/prometheus/promhttp"

"github.com/go-kit/log"
"github.com/prometheus/common/version"
)

Expand All @@ -40,7 +40,8 @@ type HTTPServer struct {
configuration Configuration
alertParser alertparser.AlertParser
trapSender trapsender.TrapSender
logger log.Logger
logger *log.Logger
server *http.Server
}

// Configuration describes the configuration for serving HTTP requests
Expand All @@ -49,12 +50,12 @@ type Configuration struct {
}

// New creates an HTTPServer instance
func New(configuration Configuration, alertParser alertparser.AlertParser, trapSender trapsender.TrapSender, logger log.Logger) *HTTPServer {
return &HTTPServer{configuration, alertParser, trapSender, logger}
func New(configuration Configuration, alertParser alertparser.AlertParser, trapSender trapsender.TrapSender, logger *log.Logger) *HTTPServer {
return &HTTPServer{configuration, alertParser, trapSender, logger, nil}
}

// Configure creates and configures the HTTP server
func (httpServer HTTPServer) Configure() *http.Server {
func (httpServer HTTPServer) Start() error {
mux := http.NewServeMux()
server := &http.Server{
Handler: mux,
Expand All @@ -75,7 +76,7 @@ func (httpServer HTTPServer) Configure() *http.Server {
})

mux.HandleFunc("/alerts", func(w http.ResponseWriter, req *http.Request) {
level.Debug(httpServer.logger).Log("msg", "Handling /alerts webhook request")
level.Debug(*httpServer.logger).Log("msg", "Handling /alerts webhook request")

defer req.Body.Close()

Expand Down Expand Up @@ -104,7 +105,22 @@ func (httpServer HTTPServer) Configure() *http.Server {
mux.Handle("/metrics", promhttp.Handler())
mux.HandleFunc("/health", healthHandler)

return server
if err := web.ListenAndServe(server, &httpServer.configuration.ToolKitConfiguration, *httpServer.logger); err != nil {
level.Error(*httpServer.logger).Log("err", err)
return err
}

httpServer.server = server

return nil
}

func (httpServer HTTPServer) Stop() error {
if httpServer.server != nil {
level.Error(*httpServer.logger).Log("msg", "stopping server")
return httpServer.server.Close()
}
return nil
}

func healthHandler(w http.ResponseWriter, r *http.Request) {
Expand All @@ -128,6 +144,6 @@ func (httpServer HTTPServer) errorHandler(w http.ResponseWriter, status int, err
json := string(bytes[:])
fmt.Fprint(w, json)

level.Error(httpServer.logger).Log("status", status, "statustext", http.StatusText(status), "err", err, "data", data)
level.Error(*httpServer.logger).Log("status", status, "statustext", http.StatusText(status), "err", err, "data", data)
telemetry.RequestTotal.WithLabelValues(strconv.FormatInt(int64(status), 10)).Inc()
}
Loading

0 comments on commit e9ec862

Please sign in to comment.