From 43c1fbb59f2ba9206cd5fb26cb208333fa316c3e Mon Sep 17 00:00:00 2001 From: Andrew Mason Date: Mon, 7 Jun 2021 19:06:45 -0400 Subject: [PATCH 1/2] [trace] Add support for turning on logging in tracing plugins with the standard logging infrastructure Signed-off-by: Andrew Mason --- go/trace/logger.go | 32 ++++++++++++++++++++++++++++++++ go/trace/plugin_datadog.go | 12 +++++++++--- go/trace/plugin_jaeger.go | 9 ++++++++- go/trace/trace.go | 1 + 4 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 go/trace/logger.go diff --git a/go/trace/logger.go b/go/trace/logger.go new file mode 100644 index 00000000000..158fab3c8b8 --- /dev/null +++ b/go/trace/logger.go @@ -0,0 +1,32 @@ +/* +Copyright 2021 The Vitess Authors. + +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 trace + +import "vitess.io/vitess/go/vt/log" + +// traceLogger wraps the standard vitess log package to satisfy the datadog and +// jaeger logger interfaces. +type traceLogger struct{} + +// Log is part of the ddtrace.Logger interface. Datadog only ever logs errors. +func (*traceLogger) Log(msg string) { log.Errorf(msg) } + +// Error is part of the jaeger.Logger interface. +func (*traceLogger) Error(msg string) { log.Errorf(msg) } + +// Infof is part of the jaeger.Logger interface. +func (*traceLogger) Infof(msg string, args ...interface{}) { log.Infof(msg, args...) } diff --git a/go/trace/plugin_datadog.go b/go/trace/plugin_datadog.go index 45b743c408e..f222af1cbeb 100644 --- a/go/trace/plugin_datadog.go +++ b/go/trace/plugin_datadog.go @@ -20,12 +20,18 @@ func newDatadogTracer(serviceName string) (tracingService, io.Closer, error) { return nil, nil, fmt.Errorf("need host and port to datadog agent to use datadog tracing") } - t := opentracer.New( - ddtracer.WithAgentAddr(*dataDogHost+":"+*dataDogPort), + opts := []ddtracer.StartOption{ + ddtracer.WithAgentAddr(*dataDogHost + ":" + *dataDogPort), ddtracer.WithServiceName(serviceName), ddtracer.WithDebugMode(true), ddtracer.WithSampler(ddtracer.NewRateSampler(samplingRate.Get())), - ) + } + + if *enableLogging { + opts = append(opts, ddtracer.WithLogger(&traceLogger{})) + } + + t := opentracer.New(opts...) opentracing.SetGlobalTracer(t) diff --git a/go/trace/plugin_jaeger.go b/go/trace/plugin_jaeger.go index ba64e277bdf..38781ed8d5b 100644 --- a/go/trace/plugin_jaeger.go +++ b/go/trace/plugin_jaeger.go @@ -98,7 +98,14 @@ func newJagerTracerFromEnv(serviceName string) (tracingService, io.Closer, error log.Infof("Tracing sampler type %v (param: %v)", cfg.Sampler.Type, cfg.Sampler.Param) - tracer, closer, err := cfg.NewTracer() + var opts []config.Option + if *enableLogging { + opts = append(opts, config.Logger(&traceLogger{})) + } else if cfg.Reporter.LogSpans { + log.Warningf("JAEGER_REPORTER_LOG_SPANS was set, but -tracing-enable-logging was not; spans will not be logged") + } + + tracer, closer, err := cfg.NewTracer(opts...) if err != nil { return nil, &nilCloser{}, err diff --git a/go/trace/trace.go b/go/trace/trace.go index 0038051468a..181d3964e57 100644 --- a/go/trace/trace.go +++ b/go/trace/trace.go @@ -133,6 +133,7 @@ var currentTracer tracingService = noopTracingServer{} var ( tracingServer = flag.String("tracer", "noop", "tracing service to use") + enableLogging = flag.Bool("tracing-enable-logging", false, "whether to enable logging in the tracing service") ) // StartTracing enables tracing for a named service From ba65125b8044d6070780a604042a5ffc91d388b0 Mon Sep 17 00:00:00 2001 From: Andrew Mason Date: Mon, 7 Jun 2021 19:12:10 -0400 Subject: [PATCH 2/2] [vtadmin] expose trace logging flag in vtadmin entrypoint Signed-off-by: Andrew Mason --- go/cmd/vtadmin/main.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/go/cmd/vtadmin/main.go b/go/cmd/vtadmin/main.go index 706fefd04fa..d1ee284b59f 100644 --- a/go/cmd/vtadmin/main.go +++ b/go/cmd/vtadmin/main.go @@ -123,9 +123,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().AddGoFlag(flag.Lookup("tracing-sampling-type")) // defined in go/vt/trace - rootCmd.Flags().AddGoFlag(flag.Lookup("tracing-sampling-rate")) // defined in go/vt/trace + rootCmd.Flags().AddGoFlag(flag.Lookup("tracer")) // defined in go/vt/trace + rootCmd.Flags().AddGoFlag(flag.Lookup("tracing-enable-logging")) // defined in go/vt/trace + rootCmd.Flags().AddGoFlag(flag.Lookup("tracing-sampling-type")) // defined in go/vt/trace + rootCmd.Flags().AddGoFlag(flag.Lookup("tracing-sampling-rate")) // 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")