Skip to content

Commit 858d359

Browse files
author
Christian Häusler
authored
Do not panic when serving metrics fail (corvus-ch#67)
Delegate the error up the caller stack instead.
1 parent f79a9da commit 858d359

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

main.go

+18-8
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/corvus-ch/rabbitmq-cli-consumer/consumer"
2121
"github.com/corvus-ch/rabbitmq-cli-consumer/log"
2222
"github.com/corvus-ch/rabbitmq-cli-consumer/processor"
23+
"github.com/pkg/errors"
2324
"github.com/prometheus/client_golang/prometheus"
2425
"github.com/prometheus/client_golang/prometheus/promhttp"
2526
"github.com/streadway/amqp"
@@ -145,17 +146,25 @@ func Action(c *cli.Context) error {
145146
}
146147
defer client.Close()
147148

149+
errs := make(chan error)
150+
148151
if c.Bool("metrics") {
149152
ll.Infof("Registering metrics server at %v", c.String("web.listen-address"))
150-
setupAndServeMetrics(c.String("web.listen-address"), c.String("web.telemetry-path"))
153+
go func() {
154+
errs <- setupAndServeMetrics(c.String("web.listen-address"), c.String("web.telemetry-path"))
155+
}()
151156
} else {
152157
ll.Infof("Metrics disabled.")
153158
}
154159

155-
return consume(client, l)
160+
go func() {
161+
errs <- consume(client, l)
162+
}()
163+
164+
return <-errs
156165
}
157166

158-
func setupAndServeMetrics(addr string, path string) {
167+
func setupAndServeMetrics(addr string, path string) error {
159168
srv := &http.Server{
160169
Addr: addr,
161170
// Good practice to set timeouts to avoid Slowloris attacks.
@@ -178,11 +187,12 @@ func setupAndServeMetrics(addr string, path string) {
178187
</body>
179188
</html>`))
180189
})
181-
go func() {
182-
if err := srv.ListenAndServe(); err != nil {
183-
panic(err)
184-
}
185-
}()
190+
191+
if err := srv.ListenAndServe(); err != nil {
192+
return errors.Wrap(err, "failed to serve metrics")
193+
}
194+
195+
return nil
186196
}
187197

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

0 commit comments

Comments
 (0)