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

Do not panic when serving metrics fail #67

Merged
merged 1 commit into from
Apr 22, 2019
Merged
Changes from all 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
26 changes: 18 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/corvus-ch/rabbitmq-cli-consumer/consumer"
"github.com/corvus-ch/rabbitmq-cli-consumer/log"
"github.com/corvus-ch/rabbitmq-cli-consumer/processor"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/streadway/amqp"
Expand Down Expand Up @@ -145,17 +146,25 @@ func Action(c *cli.Context) error {
}
defer client.Close()

errs := make(chan error)

if c.Bool("metrics") {
ll.Infof("Registering metrics server at %v", c.String("web.listen-address"))
setupAndServeMetrics(c.String("web.listen-address"), c.String("web.telemetry-path"))
go func() {
errs <- setupAndServeMetrics(c.String("web.listen-address"), c.String("web.telemetry-path"))
}()
} else {
ll.Infof("Metrics disabled.")
}

return consume(client, l)
go func() {
errs <- consume(client, l)
}()

return <-errs
}

func setupAndServeMetrics(addr string, path string) {
func setupAndServeMetrics(addr string, path string) error {
srv := &http.Server{
Addr: addr,
// Good practice to set timeouts to avoid Slowloris attacks.
Expand All @@ -178,11 +187,12 @@ func setupAndServeMetrics(addr string, path string) {
</body>
</html>`))
})
go func() {
if err := srv.ListenAndServe(); err != nil {
panic(err)
}
}()

if err := srv.ListenAndServe(); err != nil {
return errors.Wrap(err, "failed to serve metrics")
}

return nil
}

func consume(client *consumer.Consumer, l logr.Logger) error {
Expand Down