diff --git a/examples/hotrod/cmd/frontend.go b/examples/hotrod/cmd/frontend.go index d2be7cb77e7..e1eedc65c14 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.InitOP("frontend", otelExporter, metricsFactory, logger), + tracing.Init("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 fd61f3a1ded..191bd96c556 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.InitOP("route", otelExporter, metricsFactory, logger), + tracing.Init("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 42885b958b0..94bc48139b0 100644 --- a/examples/hotrod/pkg/tracing/init.go +++ b/examples/hotrod/pkg/tracing/init.go @@ -46,18 +46,18 @@ var once sync.Once // InitOTEL initializes OpenTelemetry SDK // to return an Otel tracer. -func InitOTEL(serviceName string, exporterType string, metricsFactory metrics.Factory, logger log.Factory) trace.TracerProvider { - _, oteltp := InitBOTH(serviceName, exporterType, metricsFactory, logger) +func InitOTEL(serviceName string, exporterType string, metricsFactory metrics.Factory, logger log.Factory) trace.Tracer { + _, oteltp := initBOTH(serviceName, exporterType, metricsFactory, logger) otel.SetTracerProvider(oteltp) logger.Bg().Debug("Added OTEL tracer", zap.String("service-name", serviceName)) - return oteltp + return oteltp.Tracer(serviceName) } // 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) +func Init(serviceName string, exporterType string, metricsFactory metrics.Factory, logger log.Factory) opentracing.Tracer { + optracer, _ := initBOTH(serviceName, exporterType, metricsFactory, logger) logger.Bg().Debug("created OTEL->OT bridge", zap.String("service-name", serviceName)) return optracer @@ -65,7 +65,7 @@ func InitOP(serviceName string, exporterType string, metricsFactory metrics.Fact // Init initializes OpenTelemetry SDK and uses OTel-OpenTracing Bridge // 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) { +func initBOTH(serviceName string, exporterType string, metricsFactory metrics.Factory, logger log.Factory) (opentracing.Tracer, trace.TracerProvider) { once.Do(func() { otel.SetTextMapPropagator( propagation.NewCompositeTextMapPropagator( @@ -90,7 +90,7 @@ func InitBOTH(serviceName string, exporterType string, metricsFactory metrics.Fa semconv.ServiceNameKey.String(serviceName), )), ) - otTracer, _ := otbridge.NewTracerPair(tp.Tracer("")) + otTracer, _ := otbridge.NewTracerPair(tp.Tracer(serviceName)) return otTracer, tp } diff --git a/examples/hotrod/services/customer/server.go b/examples/hotrod/services/customer/server.go index b9fb17c6436..7cbe0e6da4c 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.InitOP("customer", otelExporter, metricsFactory, logger), + tracer: tracing.Init("customer", otelExporter, metricsFactory, logger), logger: logger, database: newDatabase( - tracing.InitOP("mysql", otelExporter, metricsFactory, logger), + tracing.Init("mysql", otelExporter, metricsFactory, logger), logger.With(zap.String("component", "mysql")), ), } diff --git a/examples/hotrod/services/driver/redis.go b/examples/hotrod/services/driver/redis.go index 7eae0b7bd69..3102c8d3edf 100644 --- a/examples/hotrod/services/driver/redis.go +++ b/examples/hotrod/services/driver/redis.go @@ -35,27 +35,25 @@ import ( // Redis is a simulator of remote Redis cache type Redis struct { - tracer trace.TracerProvider // simulate redis as a separate process + tracer trace.Tracer // simulate redis as a separate process logger log.Factory errorSimulator } func newRedis(otelExporter string, metricsFactory metrics.Factory, logger log.Factory) *Redis { return &Redis{ - tracer: tracing.InitOTEL("redis", otelExporter, metricsFactory, logger), + tracer: tracing.InitOTEL("redis-manual", otelExporter, metricsFactory, logger), logger: logger, } } // FindDriverIDs finds IDs of drivers who are near the location. func (r *Redis) FindDriverIDs(ctx context.Context, location string) []string { - tracer := r.tracer.Tracer("") - if _, span := tracer.Start(ctx, "FindDriverIDs", trace.WithSpanKind(trace.SpanKindClient) /*...set child of option*/); span != nil { - span.SetAttributes(attribute.Key("param.location").String(location)) + _, span := r.tracer.Start(ctx, "FindDriverIDs", trace.WithSpanKind(trace.SpanKindClient)) + span.SetAttributes(attribute.Key("param.driver.location").String(location)) + + defer span.End() - defer span.End() - ctx = trace.ContextWithSpan(ctx, span) - } /* if span := opentracing.SpanFromContext(ctx); span != nil { span := r.tracer.StartSpan("FindDriverIDs", opentracing.ChildOf(span.Context())) span.SetTag("param.location", location) @@ -77,13 +75,11 @@ func (r *Redis) FindDriverIDs(ctx context.Context, location string) []string { // GetDriver returns driver and the current car location func (r *Redis) GetDriver(ctx context.Context, driverID string) (Driver, error) { - tracer := r.tracer.Tracer("") - if _, span := tracer.Start(ctx, "GetDriver", trace.WithSpanKind(trace.SpanKindClient) /*...set child of option*/); span != nil { - span.SetAttributes(attribute.Key("param.driverID").String(driverID)) + _, span := r.tracer.Start(ctx, "GetDriver", trace.WithSpanKind(trace.SpanKindClient)) + span.SetAttributes(attribute.Key("param.driverID").String(driverID)) + + defer span.End() - defer span.End() - ctx = trace.ContextWithSpan(ctx, span) - } /* if span := opentracing.SpanFromContext(ctx); span != nil { span := r.tracer.StartSpan("GetDriver", opentracing.ChildOf(span.Context())) span.SetTag("param.driverID", driverID) diff --git a/examples/hotrod/services/driver/server.go b/examples/hotrod/services/driver/server.go index fc5e4754256..e1d1aa3975b 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.InitOP("driver", otelExporter, metricsFactory, logger) + tracer := tracing.Init("driver", otelExporter, metricsFactory, logger) server := grpc.NewServer( grpc.UnaryInterceptor(otgrpc.OpenTracingServerInterceptor(tracer)), grpc.StreamInterceptor(otgrpc.OpenTracingStreamServerInterceptor(tracer)),