From 995f7569c9a8274568168a153f799028f26b0ab2 Mon Sep 17 00:00:00 2001 From: Adam Reese Date: Tue, 11 Oct 2016 11:00:35 -0700 Subject: [PATCH] feat(tiller): add optional grpc tracing --- cmd/tiller/tiller.go | 23 +++++++++++------ cmd/tiller/trace.go | 60 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 8 deletions(-) create mode 100644 cmd/tiller/trace.go diff --git a/cmd/tiller/tiller.go b/cmd/tiller/tiller.go index 2c0e1dd9a..b00cddba0 100644 --- a/cmd/tiller/tiller.go +++ b/cmd/tiller/tiller.go @@ -46,9 +46,11 @@ var rootServer = grpc.NewServer() var env = environment.New() var ( - addr = ":44134" - probe = ":44135" - store = storageConfigMap + grpcAddr = ":44134" + probeAddr = ":44135" + traceAddr = ":44136" + enableTracing = false + store = storageConfigMap ) const globalUsage = `The Kubernetes Helm server. @@ -67,8 +69,9 @@ var rootCommand = &cobra.Command{ func main() { pf := rootCommand.PersistentFlags() - pf.StringVarP(&addr, "listen", "l", ":44134", "The address:port to listen on") + pf.StringVarP(&grpcAddr, "listen", "l", ":44134", "The address:port to listen on") pf.StringVar(&store, "storage", storageConfigMap, "The storage driver to use. One of 'configmap' or 'memory'") + pf.BoolVar(&enableTracing, "trace", false, "Enable rpc tracing") rootCommand.Execute() } @@ -84,16 +87,20 @@ func start(c *cobra.Command, args []string) { env.Releases = storage.Init(driver.NewConfigMaps(c.ConfigMaps(environment.TillerNamespace))) } - lstn, err := net.Listen("tcp", addr) + lstn, err := net.Listen("tcp", grpcAddr) if err != nil { fmt.Fprintf(os.Stderr, "Server died: %s\n", err) os.Exit(1) } - fmt.Printf("Tiller is running on %s\n", addr) - fmt.Printf("Tiller probes server is running on %s\n", probe) + fmt.Printf("Tiller is listening on %s\n", grpcAddr) + fmt.Printf("Probes server is listening on %s\n", probeAddr) fmt.Printf("Storage driver is %s\n", env.Releases.Name()) + if enableTracing { + startTracing(traceAddr) + } + srvErrCh := make(chan error) probeErrCh := make(chan error) go func() { @@ -104,7 +111,7 @@ func start(c *cobra.Command, args []string) { go func() { mux := newProbesMux() - if err := http.ListenAndServe(probe, mux); err != nil { + if err := http.ListenAndServe(probeAddr, mux); err != nil { probeErrCh <- err } }() diff --git a/cmd/tiller/trace.go b/cmd/tiller/trace.go new file mode 100644 index 000000000..b9e0583f2 --- /dev/null +++ b/cmd/tiller/trace.go @@ -0,0 +1,60 @@ +/* +Copyright 2016 The Kubernetes Authors All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package main // import "k8s.io/helm/cmd/tiller" + +import ( + "fmt" + "log" + "net/http" + + _ "net/http/pprof" + + "google.golang.org/grpc" +) + +func startTracing(addr string) { + fmt.Printf("Tracing server is listening on %s\n", addr) + grpc.EnableTracing = true + + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/" { + http.NotFound(w, r) + return + } + w.Header().Set("Content-Type", "text/html; charset=utf-8") + w.Write([]byte(traceIndexHTML)) + }) + + go func() { + if err := http.ListenAndServe(addr, nil); err != nil { + log.Printf("tracing error: %s", err) + } + }() +} + +const traceIndexHTML = ` + + + + + +`