Skip to content

Commit

Permalink
etcdmain/grpc-proxy: add 'metrics-addr' option
Browse files Browse the repository at this point in the history
Signed-off-by: Gyu-Ho Lee <gyuhox@gmail.com>
  • Loading branch information
gyuho committed Jul 12, 2017
1 parent 7fbe416 commit 4a4d627
Showing 1 changed file with 32 additions and 5 deletions.
37 changes: 32 additions & 5 deletions etcdmain/grpc_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"net"
"net/http"
"net/url"
"os"
"time"

Expand All @@ -40,6 +41,7 @@ import (

var (
grpcProxyListenAddr string
grpcProxyMetricsListenAddr string
grpcProxyEndpoints []string
grpcProxyDNSCluster string
grpcProxyInsecureDiscovery bool
Expand Down Expand Up @@ -79,7 +81,7 @@ func newGRPCProxyStartCommand() *cobra.Command {
}

cmd.Flags().StringVar(&grpcProxyListenAddr, "listen-addr", "127.0.0.1:23790", "listen address")
cmd.Flags().StringVar(&grpcProxyDNSCluster, "discovery-srv", "", "DNS domain used to bootstrap initial cluster")
cmd.Flags().StringVar(&grpcProxyMetricsListenAddr, "metrics-addr", "", "listen for /metrics requests on an additional interface")
cmd.Flags().BoolVar(&grpcProxyInsecureDiscovery, "insecure-discovery", false, "accept insecure SRV records")
cmd.Flags().StringSliceVar(&grpcProxyEndpoints, "endpoints", []string{"127.0.0.1:2379"}, "comma separated etcd cluster endpoints")
cmd.Flags().StringVar(&grpcProxyCert, "cert", "", "identify secure connections with etcd servers using this TLS certificate file")
Expand Down Expand Up @@ -129,7 +131,7 @@ func startGRPCProxy(cmd *cobra.Command, args []string) {
}()
m := cmux.New(l)

cfg, err := newClientCfg()
cfg, cfgtls, err := newClientCfg()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
Expand Down Expand Up @@ -202,14 +204,39 @@ func startGRPCProxy(cmd *cobra.Command, args []string) {

go func() { errc <- m.Serve() }()

if len(grpcProxyMetricsListenAddr) > 0 {
murl, err := url.Parse(grpcProxyMetricsListenAddr)
if err != nil {
fmt.Fprintf(os.Stderr, "cannot parse %q", grpcProxyMetricsListenAddr)
os.Exit(1)
}
if murl.Scheme == "https" {
murl.Scheme = "http"
plog.Warningf("grpc-proxy: serving non-secured '/metrics' for %s", murl.String())
}
ml, err := transport.NewListener(murl.Host, murl.Scheme, nil)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

mux := http.NewServeMux()
mux.Handle("/metrics", prometheus.Handler())

go func() {
plog.Info("grpc-proxy: listening for metrics on ", murl.String())
plog.Fatal(http.Serve(ml, mux))
}()
}

// grpc-proxy is initialized, ready to serve
notifySystemd()

fmt.Fprintln(os.Stderr, <-errc)
os.Exit(1)
}

func newClientCfg() (*clientv3.Config, error) {
func newClientCfg() (*clientv3.Config, *transport.TLSInfo, error) {
// set tls if any one tls option set
var cfgtls *transport.TLSInfo
tlsinfo := transport.TLSInfo{}
Expand All @@ -235,12 +262,12 @@ func newClientCfg() (*clientv3.Config, error) {
if cfgtls != nil {
clientTLS, err := cfgtls.ClientConfig()
if err != nil {
return nil, err
return nil, nil, err
}
cfg.TLS = clientTLS
}

// TODO: support insecure tls

return &cfg, nil
return &cfg, cfgtls, nil
}

0 comments on commit 4a4d627

Please sign in to comment.