diff --git a/cmd/all-in-one/main.go b/cmd/all-in-one/main.go index 2b0b05167bc..b40e427cfa7 100644 --- a/cmd/all-in-one/main.go +++ b/cmd/all-in-one/main.go @@ -16,29 +16,18 @@ package main import ( - "context" "fmt" "io" "log" "os" - "time" "github.com/opentracing/opentracing-go" "github.com/spf13/cobra" "github.com/spf13/viper" jaegerClientConfig "github.com/uber/jaeger-client-go/config" jaegerClientZapLog "github.com/uber/jaeger-client-go/log/zap" - "go.opentelemetry.io/otel" - "go.opentelemetry.io/otel/attribute" - otelBridge "go.opentelemetry.io/otel/bridge/opentracing" - "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc" - "go.opentelemetry.io/otel/sdk/resource" - sdktrace "go.opentelemetry.io/otel/sdk/trace" - semconv "go.opentelemetry.io/otel/semconv/v1.17.0" _ "go.uber.org/automaxprocs" "go.uber.org/zap" - "google.golang.org/grpc" - "google.golang.org/grpc/credentials/insecure" agentApp "github.com/jaegertracing/jaeger/cmd/agent/app" agentRep "github.com/jaegertracing/jaeger/cmd/agent/app/reporter" @@ -285,28 +274,7 @@ func startQuery( ) *queryApp.Server { spanReader = storageMetrics.NewReadMetricsDecorator(spanReader, baseFactory.Namespace(metrics.NSOptions{Name: "query"})) qs := querysvc.NewQueryService(spanReader, depReader, *queryOpts) - // jtracer := jtracer.OT(opentracing.GlobalTracer()) - - ctx := context.Background() - exp, err := newExporter(ctx) - if err != nil { - log.Fatal(err) - } - - tp := sdktrace.NewTracerProvider( - sdktrace.WithBatcher(exp), - sdktrace.WithResource(newResource()), - ) - - otelTracer := tp.Tracer("mytracer_name") - // Use the bridgeTracer as your OpenTracing tracer. - bridgeTracer, wrapperTracerProvider := otelBridge.NewTracerPair(otelTracer) - // Set the wrapperTracerProvider as the global OpenTelemetry - // TracerProvider so instrumentation will use it by default. - otel.SetTracerProvider(wrapperTracerProvider) - - jtracer := jtracer.OT(bridgeTracer) - + jtracer := jtracer.OT(opentracing.GlobalTracer()) server, err := queryApp.NewServer(svc.Logger, qs, metricsQueryService, qOpts, tm, jtracer) if err != nil { svc.Logger.Fatal("Could not start jaeger-query service", zap.Error(err)) @@ -322,41 +290,6 @@ func startQuery( return server } -// newExporter returns a console exporter. -func newExporter(ctx context.Context) (sdktrace.SpanExporter, error) { - ctx, cancel := context.WithTimeout(ctx, time.Second) - defer cancel() - conn, err := grpc.DialContext(ctx, "otel_collector:4317", - // Note the use of insecure transport here. TLS is recommended in production. - grpc.WithTransportCredentials(insecure.NewCredentials()), - grpc.WithBlock(), - ) - if err != nil { - return nil, fmt.Errorf("failed to create gRPC connection to collector: %w", err) - } - - // Set up a trace exporter - traceExporter, err := otlptracegrpc.New(ctx, otlptracegrpc.WithGRPCConn(conn)) - if err != nil { - return nil, fmt.Errorf("failed to create trace exporter: %w", err) - } - return traceExporter, nil -} - -// newResource returns a resource describing this application. -func newResource() *resource.Resource { - r, _ := resource.Merge( - resource.Default(), - resource.NewWithAttributes( - semconv.SchemaURL, - semconv.ServiceName("query"), - semconv.ServiceVersion("v0.1.0"), - attribute.String("environment", "demo"), - ), - ) - return r -} - func initTracer(svc *flags.Service) io.Closer { logger := svc.Logger traceCfg := &jaegerClientConfig.Configuration{ diff --git a/examples/hotrod/cmd/frontend.go b/examples/hotrod/cmd/frontend.go index e1eedc65c14..d2be7cb77e7 100644 --- a/examples/hotrod/cmd/frontend.go +++ b/examples/hotrod/cmd/frontend.go @@ -45,7 +45,7 @@ var frontendCmd = &cobra.Command{ logger := log.NewFactory(zapLogger) server := frontend.NewServer( options, - tracing.Init("frontend", otelExporter, metricsFactory, logger), + tracing.InitOP("frontend", otelExporter, metricsFactory, logger), logger, ) return logError(zapLogger, server.Run()) diff --git a/examples/hotrod/cmd/route.go b/examples/hotrod/cmd/route.go index 191bd96c556..fd61f3a1ded 100644 --- a/examples/hotrod/cmd/route.go +++ b/examples/hotrod/cmd/route.go @@ -37,7 +37,7 @@ var routeCmd = &cobra.Command{ logger := log.NewFactory(zapLogger) server := route.NewServer( net.JoinHostPort("0.0.0.0", strconv.Itoa(routePort)), - tracing.Init("route", otelExporter, metricsFactory, logger), + tracing.InitOP("route", otelExporter, metricsFactory, logger), logger, ) return logError(zapLogger, server.Run()) diff --git a/examples/hotrod/pkg/tracing/init.go b/examples/hotrod/pkg/tracing/init.go index 5e32a44e0a8..42885b958b0 100644 --- a/examples/hotrod/pkg/tracing/init.go +++ b/examples/hotrod/pkg/tracing/init.go @@ -44,42 +44,28 @@ import ( var once sync.Once -// InitOTEL initializes OpenTelemetry SDK and uses OTel-OpenTracing Bridge -// to return an OpenTracing-compatible tracer. +// InitOTEL initializes OpenTelemetry SDK +// to return an Otel tracer. func InitOTEL(serviceName string, exporterType string, metricsFactory metrics.Factory, logger log.Factory) trace.TracerProvider { - once.Do(func() { - otel.SetTextMapPropagator( - propagation.NewCompositeTextMapPropagator( - propagation.TraceContext{}, - propagation.Baggage{}, - )) - }) + _, oteltp := InitBOTH(serviceName, exporterType, metricsFactory, logger) + otel.SetTracerProvider(oteltp) - exp, err := createOtelExporter(exporterType) - if err != nil { - logger.Bg().Fatal("cannot create exporter", zap.String("exporterType", exporterType), zap.Error(err)) - } - logger.Bg().Debug("using " + exporterType + " trace exporter") + logger.Bg().Debug("Added OTEL tracer", zap.String("service-name", serviceName)) + return oteltp +} - rpcmetricsObserver := rpcmetrics.NewObserver(metricsFactory, rpcmetrics.DefaultNameNormalizer) +// InitOP initializes OTel-OpenTracing Bridge +// to return an OpenTracing-compatible tracer. +func InitOP(serviceName string, exporterType string, metricsFactory metrics.Factory, logger log.Factory) opentracing.Tracer { + optracer, _ := InitBOTH(serviceName, exporterType, metricsFactory, logger) - tp := sdktrace.NewTracerProvider( - sdktrace.WithBatcher(exp), - sdktrace.WithSpanProcessor(rpcmetricsObserver), - sdktrace.WithResource(resource.NewWithAttributes( - semconv.SchemaURL, - semconv.ServiceNameKey.String(serviceName), - )), - ) - otel.SetTracerProvider(tp) - // _, otelTracerProvider := otbridge.NewTracerPair(tp.Tracer("")) logger.Bg().Debug("created OTEL->OT bridge", zap.String("service-name", serviceName)) - return tp + return optracer } // Init initializes OpenTelemetry SDK and uses OTel-OpenTracing Bridge -// to return an OpenTracing-compatible tracer. -func Init(serviceName string, exporterType string, metricsFactory metrics.Factory, logger log.Factory) opentracing.Tracer { +// to return an OpenTracing-compatible tracer and Otel tracer. +func InitBOTH(serviceName string, exporterType string, metricsFactory metrics.Factory, logger log.Factory) (opentracing.Tracer, trace.TracerProvider) { once.Do(func() { otel.SetTextMapPropagator( propagation.NewCompositeTextMapPropagator( @@ -105,8 +91,7 @@ func Init(serviceName string, exporterType string, metricsFactory metrics.Factor )), ) otTracer, _ := otbridge.NewTracerPair(tp.Tracer("")) - logger.Bg().Debug("created OTEL->OT bridge", zap.String("service-name", serviceName)) - return otTracer + return otTracer, tp } // withSecure instructs the client to use HTTPS scheme, instead of hotrod's desired default HTTP diff --git a/examples/hotrod/services/customer/server.go b/examples/hotrod/services/customer/server.go index 7cbe0e6da4c..b9fb17c6436 100644 --- a/examples/hotrod/services/customer/server.go +++ b/examples/hotrod/services/customer/server.go @@ -40,10 +40,10 @@ type Server struct { func NewServer(hostPort string, otelExporter string, metricsFactory metrics.Factory, logger log.Factory) *Server { return &Server{ hostPort: hostPort, - tracer: tracing.Init("customer", otelExporter, metricsFactory, logger), + tracer: tracing.InitOP("customer", otelExporter, metricsFactory, logger), logger: logger, database: newDatabase( - tracing.Init("mysql", otelExporter, metricsFactory, logger), + tracing.InitOP("mysql", otelExporter, metricsFactory, logger), logger.With(zap.String("component", "mysql")), ), } diff --git a/examples/hotrod/services/driver/server.go b/examples/hotrod/services/driver/server.go index e1d1aa3975b..fc5e4754256 100644 --- a/examples/hotrod/services/driver/server.go +++ b/examples/hotrod/services/driver/server.go @@ -42,7 +42,7 @@ var _ DriverServiceServer = (*Server)(nil) // NewServer creates a new driver.Server func NewServer(hostPort string, otelExporter string, metricsFactory metrics.Factory, logger log.Factory) *Server { - tracer := tracing.Init("driver", otelExporter, metricsFactory, logger) + tracer := tracing.InitOP("driver", otelExporter, metricsFactory, logger) server := grpc.NewServer( grpc.UnaryInterceptor(otgrpc.OpenTracingServerInterceptor(tracer)), grpc.StreamInterceptor(otgrpc.OpenTracingStreamServerInterceptor(tracer)),