@@ -8,11 +8,13 @@ import (
88
99 "github.com/prometheus/client_golang/prometheus"
1010 "github.com/prometheus/client_golang/prometheus/promhttp"
11+ "golang.org/x/net/http2"
12+ "golang.org/x/net/http2/h2c"
1113)
1214
1315var (
1416 appVersion string
15- version = prometheus .NewGauge (prometheus.GaugeOpts {
17+ version = prometheus .NewGauge (prometheus.GaugeOpts {
1618 Name : "version" ,
1719 Help : "Version information about this binary" ,
1820 ConstLabels : map [string ]string {
3436func main () {
3537 version .Set (1 )
3638 bind := ""
39+ enableH2c := false
3740 flagset := flag .NewFlagSet (os .Args [0 ], flag .ExitOnError )
3841 flagset .StringVar (& bind , "bind" , ":8080" , "The socket to bind to." )
42+ flagset .BoolVar (& enableH2c , "h2c" , false , "Enable h2c (http/2 over tcp) protocol." )
3943 flagset .Parse (os .Args [1 :])
4044
4145 r := prometheus .NewRegistry ()
@@ -56,9 +60,17 @@ func main() {
5660 promhttp .InstrumentHandlerCounter (httpRequestsTotal , foundHandler ),
5761 )
5862
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 {}))
6167
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 ())
6476}
0 commit comments