@@ -8,11 +8,13 @@ import (
8
8
9
9
"github.com/prometheus/client_golang/prometheus"
10
10
"github.com/prometheus/client_golang/prometheus/promhttp"
11
+ "golang.org/x/net/http2"
12
+ "golang.org/x/net/http2/h2c"
11
13
)
12
14
13
15
var (
14
16
appVersion string
15
- version = prometheus .NewGauge (prometheus.GaugeOpts {
17
+ version = prometheus .NewGauge (prometheus.GaugeOpts {
16
18
Name : "version" ,
17
19
Help : "Version information about this binary" ,
18
20
ConstLabels : map [string ]string {
34
36
func main () {
35
37
version .Set (1 )
36
38
bind := ""
39
+ enableH2c := false
37
40
flagset := flag .NewFlagSet (os .Args [0 ], flag .ExitOnError )
38
41
flagset .StringVar (& bind , "bind" , ":8080" , "The socket to bind to." )
42
+ flagset .BoolVar (& enableH2c , "h2c" , false , "Enable h2c (http/2 over tcp) protocol." )
39
43
flagset .Parse (os .Args [1 :])
40
44
41
45
r := prometheus .NewRegistry ()
@@ -56,9 +60,17 @@ func main() {
56
60
promhttp .InstrumentHandlerCounter (httpRequestsTotal , foundHandler ),
57
61
)
58
62
59
- http .Handle ("/" , foundChain )
60
- http .Handle ("/err" , promhttp .InstrumentHandlerCounter (httpRequestsTotal , notfoundHandler ))
63
+ mux := http .NewServeMux ()
64
+ mux .Handle ("/" , foundChain )
65
+ mux .Handle ("/err" , promhttp .InstrumentHandlerCounter (httpRequestsTotal , notfoundHandler ))
66
+ mux .Handle ("/metrics" , promhttp .HandlerFor (r , promhttp.HandlerOpts {}))
61
67
62
- http .Handle ("/metrics" , promhttp .HandlerFor (r , promhttp.HandlerOpts {}))
63
- log .Fatal (http .ListenAndServe (bind , nil ))
68
+ var srv * http.Server
69
+ if enableH2c {
70
+ srv = & http.Server {Addr : bind , Handler : h2c .NewHandler (mux , & http2.Server {})}
71
+ } else {
72
+ srv = & http.Server {Addr : bind , Handler : mux }
73
+ }
74
+
75
+ log .Fatal (srv .ListenAndServe ())
64
76
}
0 commit comments