Sfoglia il codice sorgente

*: use etcdhttp.Handle* for health, prometheus handlers

Signed-off-by: Gyu-Ho Lee <gyuhox@gmail.com>
Gyu-Ho Lee 8 anni fa
parent
commit
74c8050adc
4 ha cambiato i file con 12 aggiunte e 42 eliminazioni
  1. 6 4
      embed/etcd.go
  2. 2 2
      etcdmain/etcd.go
  3. 3 3
      etcdmain/grpc_proxy.go
  4. 1 33
      etcdserver/api/etcdhttp/base.go

+ 6 - 4
embed/etcd.go

@@ -35,7 +35,6 @@ import (
 	"github.com/coreos/etcd/pkg/types"
 	"github.com/coreos/etcd/pkg/types"
 	"github.com/coreos/etcd/rafthttp"
 	"github.com/coreos/etcd/rafthttp"
 	"github.com/coreos/pkg/capnslog"
 	"github.com/coreos/pkg/capnslog"
-	"github.com/prometheus/client_golang/prometheus"
 )
 )
 
 
 var plog = capnslog.NewPackageLogger("github.com/coreos/etcd", "embed")
 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 {
 	if len(e.cfg.ListenMetricsUrls) > 0 {
-		// TODO: maybe etcdhttp.MetricsPath or get the path from the user-provided URL
 		metricsMux := http.NewServeMux()
 		metricsMux := http.NewServeMux()
-		metricsMux.Handle("/metrics", prometheus.Handler())
+		etcdhttp.HandleMetricsHealth(metricsMux, e.Server)
 
 
 		for _, murl := range e.cfg.ListenMetricsUrls {
 		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 {
 			if err != nil {
 				return err
 				return err
 			}
 			}

+ 2 - 2
etcdmain/etcd.go

@@ -30,6 +30,7 @@ import (
 	"github.com/coreos/etcd/discovery"
 	"github.com/coreos/etcd/discovery"
 	"github.com/coreos/etcd/embed"
 	"github.com/coreos/etcd/embed"
 	"github.com/coreos/etcd/etcdserver"
 	"github.com/coreos/etcd/etcdserver"
+	"github.com/coreos/etcd/etcdserver/api/etcdhttp"
 	"github.com/coreos/etcd/pkg/cors"
 	"github.com/coreos/etcd/pkg/cors"
 	"github.com/coreos/etcd/pkg/fileutil"
 	"github.com/coreos/etcd/pkg/fileutil"
 	pkgioutil "github.com/coreos/etcd/pkg/ioutil"
 	pkgioutil "github.com/coreos/etcd/pkg/ioutil"
@@ -40,7 +41,6 @@ import (
 	"github.com/coreos/etcd/version"
 	"github.com/coreos/etcd/version"
 	"github.com/coreos/pkg/capnslog"
 	"github.com/coreos/pkg/capnslog"
 	"github.com/grpc-ecosystem/go-grpc-prometheus"
 	"github.com/grpc-ecosystem/go-grpc-prometheus"
-	"github.com/prometheus/client_golang/prometheus"
 	"google.golang.org/grpc"
 	"google.golang.org/grpc"
 )
 )
 
 
@@ -344,7 +344,7 @@ func startProxy(cfg *config) error {
 		go func() {
 		go func() {
 			plog.Info("proxy: listening for client requests on ", host)
 			plog.Info("proxy: listening for client requests on ", host)
 			mux := http.NewServeMux()
 			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)
 			mux.Handle("/", ph)
 			plog.Fatal(http.Serve(l, mux))
 			plog.Fatal(http.Serve(l, mux))
 		}()
 		}()

+ 3 - 3
etcdmain/grpc_proxy.go

@@ -26,6 +26,7 @@ import (
 
 
 	"github.com/coreos/etcd/clientv3"
 	"github.com/coreos/etcd/clientv3"
 	"github.com/coreos/etcd/clientv3/namespace"
 	"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/v3election/v3electionpb"
 	"github.com/coreos/etcd/etcdserver/api/v3lock/v3lockpb"
 	"github.com/coreos/etcd/etcdserver/api/v3lock/v3lockpb"
 	pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
 	pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
@@ -35,7 +36,6 @@ import (
 
 
 	"github.com/cockroachdb/cmux"
 	"github.com/cockroachdb/cmux"
 	grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
 	grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
-	"github.com/prometheus/client_golang/prometheus"
 	"github.com/spf13/cobra"
 	"github.com/spf13/cobra"
 	"google.golang.org/grpc"
 	"google.golang.org/grpc"
 )
 )
@@ -157,7 +157,7 @@ func startGRPCProxy(cmd *cobra.Command, args []string) {
 		mhttpl := mustMetricsListener(tlsinfo)
 		mhttpl := mustMetricsListener(tlsinfo)
 		go func() {
 		go func() {
 			mux := http.NewServeMux()
 			mux := http.NewServeMux()
-			mux.Handle("/metrics", prometheus.Handler())
+			etcdhttp.HandlePrometheus(mux)
 			plog.Fatal(http.Serve(mhttpl, 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) {
 func mustHTTPListener(m cmux.CMux, tlsinfo *transport.TLSInfo) (*http.Server, net.Listener) {
 	httpmux := http.NewServeMux()
 	httpmux := http.NewServeMux()
 	httpmux.HandleFunc("/", http.NotFound)
 	httpmux.HandleFunc("/", http.NotFound)
-	httpmux.Handle("/metrics", prometheus.Handler())
+	etcdhttp.HandlePrometheus(httpmux)
 	if grpcProxyEnablePprof {
 	if grpcProxyEnablePprof {
 		for p, h := range debugutil.PProfHandlers() {
 		for p, h := range debugutil.PProfHandlers() {
 			httpmux.Handle(p, h)
 			httpmux.Handle(p, h)

+ 1 - 33
etcdserver/api/etcdhttp/base.go

@@ -20,19 +20,14 @@ import (
 	"fmt"
 	"fmt"
 	"net/http"
 	"net/http"
 	"strings"
 	"strings"
-	"time"
 
 
 	etcdErr "github.com/coreos/etcd/error"
 	etcdErr "github.com/coreos/etcd/error"
 	"github.com/coreos/etcd/etcdserver"
 	"github.com/coreos/etcd/etcdserver"
 	"github.com/coreos/etcd/etcdserver/api"
 	"github.com/coreos/etcd/etcdserver/api"
 	"github.com/coreos/etcd/etcdserver/api/v2http/httptypes"
 	"github.com/coreos/etcd/etcdserver/api/v2http/httptypes"
-	"github.com/coreos/etcd/etcdserver/etcdserverpb"
 	"github.com/coreos/etcd/pkg/logutil"
 	"github.com/coreos/etcd/pkg/logutil"
-	"github.com/coreos/etcd/raft"
 	"github.com/coreos/etcd/version"
 	"github.com/coreos/etcd/version"
 	"github.com/coreos/pkg/capnslog"
 	"github.com/coreos/pkg/capnslog"
-	"github.com/prometheus/client_golang/prometheus"
-	"golang.org/x/net/context"
 )
 )
 
 
 var (
 var (
@@ -42,8 +37,6 @@ var (
 
 
 const (
 const (
 	configPath  = "/config"
 	configPath  = "/config"
-	metricsPath = "/metrics"
-	healthPath  = "/health"
 	varsPath    = "/debug/vars"
 	varsPath    = "/debug/vars"
 	versionPath = "/version"
 	versionPath = "/version"
 )
 )
@@ -53,35 +46,10 @@ const (
 func HandleBasic(mux *http.ServeMux, server *etcdserver.EtcdServer) {
 func HandleBasic(mux *http.ServeMux, server *etcdserver.EtcdServer) {
 	mux.HandleFunc(varsPath, serveVars)
 	mux.HandleFunc(varsPath, serveVars)
 	mux.HandleFunc(configPath+"/local/log", logHandleFunc)
 	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))
 	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 {
 func versionHandler(c api.Cluster, fn func(http.ResponseWriter, *http.Request, string)) http.HandlerFunc {
 	return func(w http.ResponseWriter, r *http.Request) {
 	return func(w http.ResponseWriter, r *http.Request) {
 		v := c.Version()
 		v := c.Version()