diff --git a/examples/local/scripts/vtadmin-up.sh b/examples/local/scripts/vtadmin-up.sh index 496f3488b96..497f2326d65 100755 --- a/examples/local/scripts/vtadmin-up.sh +++ b/examples/local/scripts/vtadmin-up.sh @@ -9,6 +9,9 @@ vtadmin \ --addr ":${vtadmin_api_port}" \ --http-origin "http://localhost:3000" \ --http-tablet-url-tmpl "http://localhost:15{{ .Tablet.Alias.Uid }}" \ + --tracer "opentracing-jaeger" \ + --grpc-tracing \ + --http-tracing \ --logtostderr \ --alsologtostderr \ --cluster "id=local,name=local,discovery=staticfile,discovery-staticfile-path=./vtadmin/discovery.json,tablet-fqdn-tmpl=localhost:15{{ .Tablet.Alias.Uid }}" \ diff --git a/go/cmd/vtadmin/main.go b/go/cmd/vtadmin/main.go index 60a61eb23e9..35f8128ca99 100644 --- a/go/cmd/vtadmin/main.go +++ b/go/cmd/vtadmin/main.go @@ -17,12 +17,15 @@ limitations under the License. package main import ( + "context" "flag" + "io" "os" "time" "github.com/spf13/cobra" + "vitess.io/vitess/go/trace" "vitess.io/vitess/go/vt/log" "vitess.io/vitess/go/vt/vtadmin" "vitess.io/vitess/go/vt/vtadmin/cluster" @@ -37,6 +40,8 @@ var ( clusterFileConfig cluster.FileConfig defaultClusterConfig cluster.Config + traceCloser io.Closer = &noopCloser{} + rootCmd = &cobra.Command{ Use: "vtadmin", PreRun: func(cmd *cobra.Command, args []string) { @@ -44,32 +49,68 @@ var ( os.Args = os.Args[0:1] flag.Parse() os.Args = tmp - // (TODO:@amason) Check opts.EnableTracing and trace boot time. + + if opts.EnableTracing || httpOpts.EnableTracing { + startTracing(cmd) + } }, Run: run, + PostRun: func(cmd *cobra.Command, args []string) { + traceCloser.Close() + }, } ) +// fatal ensures the tracer is closed and final spans are sent before issuing +// a log.Fatal call with the given args. +func fatal(args ...interface{}) { + traceCloser.Close() + log.Fatal(args...) +} + +// startTracing checks the value of --tracer and then starts tracing, populating +// the private global traceCloser +func startTracing(cmd *cobra.Command) { + tracer, err := cmd.Flags().GetString("tracer") + if err != nil { + log.Warningf("not starting tracer; err: %s", err) + return + } + + if tracer == "" || tracer == "noop" { + log.Warningf("starting tracing with noop tracer") + } + + traceCloser = trace.StartTracing("vtadmin") +} + func run(cmd *cobra.Command, args []string) { + bootSpan, _ := trace.NewSpan(context.Background(), "vtadmin.boot") + defer bootSpan.Finish() + configs := clusterFileConfig.Combine(defaultClusterConfig, clusterConfigs) clusters := make([]*cluster.Cluster, len(configs)) if len(configs) == 0 { - log.Fatal("must specify at least one cluster") + bootSpan.Finish() + fatal("must specify at least one cluster") } for i, cfg := range configs { cluster, err := cfg.Cluster() if err != nil { - log.Fatal(err) + bootSpan.Finish() + fatal(err) } clusters[i] = cluster } s := vtadmin.NewAPI(clusters, opts, httpOpts) + bootSpan.Finish() + if err := s.ListenAndServe(); err != nil { - log.Fatal(err) + fatal(err) } } @@ -81,8 +122,10 @@ func main() { rootCmd.Flags().Var(&clusterFileConfig, "cluster-config", "path to a yaml cluster configuration. see clusters.example.yaml") // (TODO:@amason) provide example config. rootCmd.Flags().Var(&defaultClusterConfig, "cluster-defaults", "default options for all clusters") + rootCmd.Flags().AddGoFlag(flag.Lookup("tracer")) // defined in go/vt/trace rootCmd.Flags().BoolVar(&opts.EnableTracing, "grpc-tracing", false, "whether to enable tracing on the gRPC server") rootCmd.Flags().BoolVar(&httpOpts.EnableTracing, "http-tracing", false, "whether to enable tracing on the HTTP server") + rootCmd.Flags().BoolVar(&httpOpts.DisableCompression, "http-no-compress", false, "whether to disable compression of HTTP API responses") rootCmd.Flags().StringSliceVar(&httpOpts.CORSOrigins, "http-origin", []string{}, "repeated, comma-separated flag of allowed CORS origins. omit to disable CORS") rootCmd.Flags().StringVar(&httpOpts.ExperimentalOptions.TabletURLTmpl, @@ -106,3 +149,7 @@ func main() { log.Flush() } + +type noopCloser struct{} + +func (nc *noopCloser) Close() error { return nil }