Skip to content

Commit

Permalink
etcdmain/grpc-proxy: add 'metrics-addr' option
Browse files Browse the repository at this point in the history
  • Loading branch information
gyuho committed Jul 12, 2017
1 parent a49e5c4 commit 893a76a
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 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", "", "Additional '/metrics' listen address (secured via client TLS)")
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,34 @@ func startGRPCProxy(cmd *cobra.Command, args []string) {

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

if len(grpcProxyMetricsListenAddr) > 0 {
go func() {
murl, err := url.Parse(grpcProxyMetricsListenAddr)
if err != nil {
fmt.Fprintf(os.Stderr, "cannot parse %q", grpcProxyMetricsListenAddr)
os.Exit(1)
}
ml, err := transport.NewListener(murl.Host, murl.Scheme, cfgtls)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

plog.Info("grpc-proxy: listening for metrics on ", murl.String())
mux := http.NewServeMux()
mux.Handle("/metrics", prometheus.Handler())
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 +257,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 893a76a

Please sign in to comment.