diff --git a/embed/etcd.go b/embed/etcd.go index b48caa8989ef..90179f462a72 100644 --- a/embed/etcd.go +++ b/embed/etcd.go @@ -35,7 +35,6 @@ import ( "github.com/coreos/etcd/pkg/types" "github.com/coreos/etcd/rafthttp" "github.com/coreos/pkg/capnslog" - "github.com/prometheus/client_golang/prometheus" ) var plog = capnslog.NewPackageLogger("github.com/coreos/etcd", "embed") @@ -405,12 +404,15 @@ func (e *Etcd) serve() (err error) { } if len(e.cfg.ListenMetricsUrls) > 0 { - // TODO: maybe etcdhttp.MetricsPath or get the path from the user-provided URL metricsMux := http.NewServeMux() - metricsMux.Handle("/metrics", prometheus.Handler()) + etcdhttp.HandleMetricsHealth(metricsMux, e.Server) for _, murl := range e.cfg.ListenMetricsUrls { - ml, err := transport.NewListener(murl.Host, murl.Scheme, &e.cfg.ClientTLSInfo) + tlsInfo := &e.cfg.ClientTLSInfo + if murl.Scheme == "http" { + tlsInfo = nil + } + ml, err := transport.NewListener(murl.Host, murl.Scheme, tlsInfo) if err != nil { return err } diff --git a/etcdmain/etcd.go b/etcdmain/etcd.go index e5d5bd5b897a..faff8378efc8 100644 --- a/etcdmain/etcd.go +++ b/etcdmain/etcd.go @@ -30,6 +30,7 @@ import ( "github.com/coreos/etcd/discovery" "github.com/coreos/etcd/embed" "github.com/coreos/etcd/etcdserver" + "github.com/coreos/etcd/etcdserver/api/etcdhttp" "github.com/coreos/etcd/pkg/cors" "github.com/coreos/etcd/pkg/fileutil" pkgioutil "github.com/coreos/etcd/pkg/ioutil" @@ -40,7 +41,6 @@ import ( "github.com/coreos/etcd/version" "github.com/coreos/pkg/capnslog" "github.com/grpc-ecosystem/go-grpc-prometheus" - "github.com/prometheus/client_golang/prometheus" "google.golang.org/grpc" ) @@ -344,7 +344,7 @@ func startProxy(cfg *config) error { go func() { plog.Info("proxy: listening for client requests on ", host) mux := http.NewServeMux() - mux.Handle("/metrics", prometheus.Handler()) // v2 proxy just uses the same port + etcdhttp.HandlePrometheus(mux) // v2 proxy just uses the same port mux.Handle("/", ph) plog.Fatal(http.Serve(l, mux)) }() diff --git a/etcdmain/grpc_proxy.go b/etcdmain/grpc_proxy.go index 0fdf69ef514b..4cc7910e1642 100644 --- a/etcdmain/grpc_proxy.go +++ b/etcdmain/grpc_proxy.go @@ -26,6 +26,7 @@ import ( "github.com/coreos/etcd/clientv3" "github.com/coreos/etcd/clientv3/namespace" + "github.com/coreos/etcd/etcdserver/api/etcdhttp" "github.com/coreos/etcd/etcdserver/api/v3election/v3electionpb" "github.com/coreos/etcd/etcdserver/api/v3lock/v3lockpb" pb "github.com/coreos/etcd/etcdserver/etcdserverpb" @@ -35,7 +36,6 @@ import ( "github.com/cockroachdb/cmux" grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus" - "github.com/prometheus/client_golang/prometheus" "github.com/spf13/cobra" "google.golang.org/grpc" ) @@ -157,7 +157,7 @@ func startGRPCProxy(cmd *cobra.Command, args []string) { mhttpl := mustMetricsListener(tlsinfo) go func() { mux := http.NewServeMux() - mux.Handle("/metrics", prometheus.Handler()) + etcdhttp.HandlePrometheus(mux) plog.Fatal(http.Serve(mhttpl, mux)) }() } @@ -293,7 +293,7 @@ func newGRPCProxyServer(client *clientv3.Client) *grpc.Server { func mustHTTPListener(m cmux.CMux, tlsinfo *transport.TLSInfo) (*http.Server, net.Listener) { httpmux := http.NewServeMux() httpmux.HandleFunc("/", http.NotFound) - httpmux.Handle("/metrics", prometheus.Handler()) + etcdhttp.HandlePrometheus(httpmux) if grpcProxyEnablePprof { for p, h := range debugutil.PProfHandlers() { httpmux.Handle(p, h) diff --git a/etcdserver/api/etcdhttp/base.go b/etcdserver/api/etcdhttp/base.go index 98891da242a2..e7dc144f6964 100644 --- a/etcdserver/api/etcdhttp/base.go +++ b/etcdserver/api/etcdhttp/base.go @@ -20,19 +20,14 @@ import ( "fmt" "net/http" "strings" - "time" etcdErr "github.com/coreos/etcd/error" "github.com/coreos/etcd/etcdserver" "github.com/coreos/etcd/etcdserver/api" "github.com/coreos/etcd/etcdserver/api/v2http/httptypes" - "github.com/coreos/etcd/etcdserver/etcdserverpb" "github.com/coreos/etcd/pkg/logutil" - "github.com/coreos/etcd/raft" "github.com/coreos/etcd/version" "github.com/coreos/pkg/capnslog" - "github.com/prometheus/client_golang/prometheus" - "golang.org/x/net/context" ) var ( @@ -42,8 +37,6 @@ var ( const ( configPath = "/config" - metricsPath = "/metrics" - healthPath = "/health" varsPath = "/debug/vars" versionPath = "/version" ) @@ -53,35 +46,10 @@ const ( func HandleBasic(mux *http.ServeMux, server *etcdserver.EtcdServer) { mux.HandleFunc(varsPath, serveVars) mux.HandleFunc(configPath+"/local/log", logHandleFunc) - mux.Handle(metricsPath, prometheus.Handler()) - mux.Handle(healthPath, healthHandler(server)) + HandleMetricsHealth(mux, server) mux.HandleFunc(versionPath, versionHandler(server.Cluster(), serveVersion)) } -func healthHandler(server *etcdserver.EtcdServer) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - if !allowMethod(w, r, "GET") { - return - } - if uint64(server.Leader()) == raft.None { - http.Error(w, `{"health": "false"}`, http.StatusServiceUnavailable) - return - } - if len(server.Alarms()) > 0 { - w.Write([]byte(`{"health": "false"}`)) - return - } - ctx, cancel := context.WithTimeout(context.Background(), time.Second) - defer cancel() - if _, err := server.Do(ctx, etcdserverpb.Request{Method: "QGET"}); err != nil { - http.Error(w, `{"health": "false"}`, http.StatusServiceUnavailable) - return - } - w.WriteHeader(http.StatusOK) - w.Write([]byte(`{"health": "true"}`)) - } -} - func versionHandler(c api.Cluster, fn func(http.ResponseWriter, *http.Request, string)) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { v := c.Version()