1
1
package main
2
2
3
3
import (
4
+ "context"
4
5
"flag"
5
6
"fmt"
6
7
"log"
7
8
"net/http"
9
+ "os"
10
+ "os/signal"
11
+ "sync"
8
12
9
13
alertscollector "github.com/appuio/alerts_exporter/internal/alerts_collector"
10
14
"github.com/appuio/alerts_exporter/internal/healthcheck"
@@ -15,7 +19,7 @@ import (
15
19
"github.com/prometheus/client_golang/prometheus/promhttp"
16
20
)
17
21
18
- var listenAddr string
22
+ var listenAddr , healthListenAddr string
19
23
20
24
var host string
21
25
var withInhibited , withSilenced , withUnprocessed , withActive bool
@@ -29,6 +33,7 @@ var k8sBearerTokenAuth bool
29
33
30
34
func main () {
31
35
flag .StringVar (& listenAddr , "listen-addr" , ":8080" , "The addr to listen on" )
36
+ flag .StringVar (& healthListenAddr , "health-listen-addr" , ":8081" , "The addr to listen on for the health check endpoint." )
32
37
33
38
flag .StringVar (& host , "host" , "localhost:9093" , "The host of the Alertmanager" )
34
39
@@ -98,12 +103,48 @@ func main() {
98
103
Filters : filters ,
99
104
})
100
105
101
- // Expose metrics and custom registry via an HTTP server
102
- // using the HandleFor function. "/metrics" is the usual endpoint for that.
103
- http .Handle ("/metrics" , promhttp .HandlerFor (reg , promhttp.HandlerOpts {Registry : reg }))
104
- http .HandleFunc ("/healthz" , healthcheck.HealthCheck {GeneralService : ac .General }.HandleHealthz )
105
- log .Printf ("Listening on `%s`" , listenAddr )
106
- log .Fatal (http .ListenAndServe (listenAddr , nil ))
106
+ msm := http .NewServeMux ()
107
+ msm .Handle ("/metrics" , promhttp .HandlerFor (reg , promhttp.HandlerOpts {Registry : reg }))
108
+
109
+ hsm := http .NewServeMux ()
110
+ hsm .HandleFunc ("/healthz" , healthcheck.HealthCheck {GeneralService : ac .General }.HandleHealthz )
111
+
112
+ ms := & http.Server {
113
+ Addr : listenAddr ,
114
+ Handler : msm ,
115
+ }
116
+
117
+ hs := & http.Server {
118
+ Addr : healthListenAddr ,
119
+ Handler : hsm ,
120
+ }
121
+
122
+ ctx , cancel := signal .NotifyContext (context .Background (), os .Interrupt )
123
+ go func () {
124
+ defer cancel ()
125
+ log .Printf ("Metrics: Listening on `%s`" , listenAddr )
126
+ log .Println ("Metrics:" , ms .ListenAndServe ())
127
+ }()
128
+ go func () {
129
+ defer cancel ()
130
+ log .Printf ("Healthz: Listening on `%s`" , healthListenAddr )
131
+ log .Println ("Healthz:" , hs .ListenAndServe ())
132
+ }()
133
+
134
+ var waitShutdown sync.WaitGroup
135
+ waitShutdown .Add (2 )
136
+ go func () {
137
+ defer waitShutdown .Done ()
138
+ <- ctx .Done ()
139
+ ms .Shutdown (context .Background ())
140
+ }()
141
+ go func () {
142
+ defer waitShutdown .Done ()
143
+ <- ctx .Done ()
144
+ hs .Shutdown (context .Background ())
145
+ }()
146
+
147
+ waitShutdown .Wait ()
107
148
}
108
149
109
150
type stringSliceFlag []string
0 commit comments