Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --tracer flag to vtadmin and actually start tracing #8165

Merged
merged 1 commit into from
May 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/local/scripts/vtadmin-up.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}" \
Expand Down
55 changes: 51 additions & 4 deletions go/cmd/vtadmin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -37,39 +40,77 @@ var (
clusterFileConfig cluster.FileConfig
defaultClusterConfig cluster.Config

traceCloser io.Closer = &noopCloser{}

rootCmd = &cobra.Command{
Use: "vtadmin",
PreRun: func(cmd *cobra.Command, args []string) {
tmp := os.Args
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)
}
}

Expand All @@ -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,
Expand All @@ -106,3 +149,7 @@ func main() {

log.Flush()
}

type noopCloser struct{}

func (nc *noopCloser) Close() error { return nil }